Create Custom Charts using Sencha Charts
Guest Blog Post
Overview
With the launch of Ext JS 5, Sencha introduced a new and more powerful Sencha Charts package, which has built-in support for touch events. This means you can use touch gestures to interact with the charts. Sencha Charts was built with performance in mind and will render charts as SVG or HTML5 Canvas, where appropriate, (or VML in IE8). You can refer to Ext JS 5 Charts Kitchen Sink to test drive and learn more about Sencha Charts.
In this article, you will learn how to create custom charts using the new Sencha Charts package. We’ll demonstrate how to create a new financial chart with William %R indicator.
This article assumes that you have a prior understanding of Sencha Charts architecture. You can refer to the following articles to understand the architecture and also get a walkthrough of an existing chart implementation:
Creating a New Financial Chart
In this section, you will see how to create a custom stock chart – William %R – that is a momentum oscillator measuring the level of the close relative to the high-low range over a given period of time. This is also called “look-back period”.
What’s the Chart All About?
Based on the data, the following data needs to be calculated based on the look-back period:
- Highest-high for the period
- Lowest-low for the period
Once the above values have been calculated, the %R value will be calculated for a record by applying the following formula:
%R = (Highest High - Close)/(Highest High - Lowest Low) * -100
After the %R values have been calculated, we need to draw a chart between %R value and the time, as shown below in a sample William %R chart:
Credit: StockCharts.com
Technically, to create a William %R chart, we would consider the following requirements as the scope for this article:
- It takes the following parameters:
- oversoldLevel – this is typically set to -80. However, it needs to be configurable
- overboughtLevel – this is typically set to -20. However, it needs to be configurable
- lookbackPeriod – this is typically set to 14 days. However, it needs to be configurable
- It draws a numeric axis with -100 as minimum and 0 as maximum values
- The numeric axis indicates the following marks:
- -80 – oversoldLevel
- -20 – overboughtLevel
- -50 – middle
- Horizontal lines are drawn at the following levels:
- -80 – oversoldLevel – continuous line
- -20 – overboughtLevel – continuous line
- -50 – middle – dotted line
- The area above the overboughtLevel and below oversoldLevel need to be filled
- The chart will be created based on Sencha Charts package architecture.
For brevity, I have excluded axis, legend, marker, tooltip, and theme customization.
Let’s Structure It
The diagram below shows the specific classes that we need to implement to create William %R chart and how they are connected with Sencha Charts classes:
Time to Get Our Hands Dirty — Implementation Steps and Code
With the high-level requirements and design in place, let’s start the implementation.
CustomSenchaCharts.sprite.WilliamPctR
This class implements the William %R sprite logic to draw the lines at -80, -50, and -20 levels. Also, it takes care of filling the areas above -20 and below -80 levels. These levels are configurable, and they are passed as part of the series configuration.
In this class, we have three important methods:
- drawOverBought – draws and fills the area above overboughtLevel, i.e. -20
- drawOverSold – draws and fills the area above oversoldLevel, i.e. -80
- renderAggregates – main method that draws the lines at -80, -50, -20 levels, also calls drawOverBought and drawOverSold methods to draw and fill the overbought and oversold areas, and, finally, draws the series line with the passed stroke style
drawOverBought
This method detects the overbought area, draws it and fills it with the specified stroke style. When the beginning of overbought area is detected, we use the method below to find out the coordinate of the point where y=-20 line intersects the line segment drawn between the points on either side of it:
Similarly, logic is followed to detect the end of the area and calculate the coordinate of the point that lies on y=-20.
Following, is the complete implementation of the method:
drawOverSold
This methods, logically, works similarly to the drawOverBought method where it draws and fills the oversold area with the specified stroke style.
renderAggregates
This method is called during the rendering of the series, and it takes care of drawing all the sprites for a series. We need to override this method, so we can:
- draw horizontal lines at -80, -50, and -20 levels
- draw line series
- draw and fill overbought and oversold areas
To render the sprites for William %R, first, we transform the overboughtLevel and oversoldLevel to the co-ordinate system that the framework uses:
ctx contains the reference to the underlying rendering engine — Canvas or SVG.
After the coordinates have been transformed, we draw the horizontal lines — solid for -80 and -20 and dashed for -50:
Then, we call the drawOverSold and drawOverbought methods to draw and fill the areas by passing the transformed values to them:
Finally, we draw the path with a stroke by calling drawStroke, which is an existing method on Ext.chart.series.sprite.Line class:
drawYLine
A private method that draws a horizontal line — dashed or solid — for the given y.
Now, with the sprite class in place, let’s move on to our next class — CustomSenchaCharts.series.WilliamPctR — where we will implement the series related logic.
CustomSenchaCharts.series.WilliamPctR
The series class accepts the store and following specific parameters:
- overboughtLevel
- oversoldLevel
- lookBackPeriod
The store contains the low, high and close fields that this series class uses to calculate the %R value based on the lookBackPeriod — defaults to 14 days.
The following constructor code calculates %R and sets it on the records on the passed store:
Now that we have updated the records with their %R value, there is one more method that we need to implement — actually, override — which is called at the time of rendering the WilliamPctR sprite to get the configuration for the sprite. We need to implement it, so we can pass the overboughtLevel, oversoldLevel, and lookBackPeriod parameters to the sprite, which it uses to draw and fill different sprites:
Now, we are left with one last class — CustomSenchaCharts.chart.WilliamPctR — the chart that lets us create a William %R chart in an application.
CustomSenchaCharts.chart.WilliamPctR
The chart class extends the CartesianChart, where it expects a numeric type axis to be specified by the caller. It uses the specified numeric axis to draw the %R values. The class sets the following William %R related additional axis configs in its iinitConfig method, which is called at the time on the class initialization:
- fields – the field that would be rendered on the numeric axis; it is set to ‘pctr’ – %R
- minimum – minimum value to be shown on the axis; it is set to -100
- maximum – maximum value to be shown on the axis; it is set to 0 as %R oscillates between 0 and -100
We’re almost done except for one final step where we will use the above chart in an application and see it in action.
See it in Action
In the previous section, we defined a WilliamPctR chart with ‘williampctrchart’ xtype. Let’s see how we can use it in our sample application.
To use the above chart in an application, we can either instantiate it using Ext.create call or use the xtype. For example, here is the code showing the configuration to add this chart to an Ext JS container as one of its items:
The following shows the William %R output:
You may download the complete code from GitHub. Note: this code was written using the current version of Ext JS (5.0.1).
Summary
In this article, we discussed how to create a custom stock chart based on the Cartesian coordinate system by implementing a custom chart, series and a sprite. We then saw how to use the custom stock chart in an Ext JS 5 application. We hope this article has helped you understand the overall structure of the Sencha Charts package, important classes, their responsibilities, interactions and how you can create a custom chart.
References
We’re excited to announce the official release of Rapid Ext JS 1.0, a revolutionary low-code…
The Sencha team is pleased to announce the availability of Sencha Architect version 4.3.6. Building…
Sencha, a leader in JavaScript developer tools for building cross-platform and enterprise web applications, is…