Introduction to Charting

The Chart package consists of a hierarchy of classes that provide data visualization functionality. These include series, axes, interactions, and mechanisms for implementing markers and legends. This guide provides an overview on how these classes are tied together and what functionality is included in each class. The following sections cover the specific axes, series, and interactions.

Installing Sencha Charts

Sencha Charts are not included in the Ext JS library by default. In order to include the charts package, simply add “sencha-charts” to the requires block in your application’s app.json file. Adding a package name to the requires block directs Cmd to make the package available to your application.

After making the inclusion, ensure that your application has been rebuilt. Applications may be rebuilt by manually issuing the following command:

sencha app build

You may also activate “app watch”:

sencha app watch

Sencha app watch monitors your application’s assets and rebuilds when it detects change.

The Chart Component Class

All types of chart components are extended from the AbstractChart class, which extends Ext.draw.Container . A chart component is the container for managing series and axes. It is responsible for laying out the series and axes every time the size is changed. An implementation of the abstract class must override the performLayout method. For example, a chart needs to respond to the changes of the thickness of the axes and recalculate the series area.

There are three types of charts:

  • cartesian/chart - Creates a chart for series implementations that plot values using cartesian coordinates. Examples of this include Bar, Area, Scatter, and Line charts.
  • polar - Creates a chartfor series implementations that plot values using polar coordinates. Examples of this include Pie and Radial charts.
  • spacefilling - Creates a chart that fills the entire area of the chart.

ext-charts tried to automatically determine the chart type by evaluating the series type. This allowed users to simply set the xtype to ‘chart’. However, sencha-charts needs a designated xtype in some circumstances. Cartesian charts are the most commonly used and are mapped to the alias of ‘chart’. Setting an xtype of ‘chart’ will makes the assumption that you want a Bar, Line, Scatter, or Area chart. Other types of charts will require an xtype designation of ‘polar’ or ‘spacefilling’.

Chart Component Management

A Chart component manages the following items:

  • Axes - These are accessed through Ext.chart.axis.Axis and represent all the axes being defined and drawn for this visualization. This is a mixed collection.
  • Series - These are accessed through Ext.chart.series.Series and represent all the series being drawn for the chart. This could be line, bar, scatter, and so on. This is also a mixed collection.
  • Interactions - These are controllers that directly manipulate the series and axes when certain events are recognized.
  • Legend Store - This represents the legend information collected from the series. Normally you can attach a data list to this store and get automatically updated on the legend information changes.

Axes

We provide Ext.chart.axis.Axis to represent all kinds of axes. The axis object works differently according to the its docking position.

  • left/right - The axis is vertical.
  • top/bottom - The axis is horizontal.

Axes help series to map data values into coordinates. They are bound to the type of data that needs to be represented.

  • numeric - the data attached to this axis is numeric and continuous.
  • time - the data attached to this axis is (or gets converted into) a date/time value; it is continuous.
  • category - the data attached to this axis belongs to a finite set. The data points are evenly placed along the axis.

The behavior of an axis can be easily changed by setting different types of axis layout and axis segmenter to the axis.

Series

Series is an abstract class extended by concrete visualizations, such as Line Series, Bar Series, or Scatter Series.

The Series class itself contains code that is common to all of these series, such as event handling, highlighting, markers, labels and so on.

The following sections describe available series types. It also shows a complete series configuration example, including the Chart, Axis, and Series options. For more specific information, see the Series documentation.

Area

The Area series creates a stacked area chart, which is typically used to display multiple aggregated layers of information. Similar to other series, the Area series must be added into the series config of the chart.

You can specify multiple y fields on a stacked series such as Area series and Bar series. A typical configuration object for the Area series looks as in the following code sample:

Ext.create('Ext.Container', {
    renderTo: Ext.getBody(),
    width:500,
    height:500,
    layout: 'fit',
    items: [
        {
            xtype: 'chart',
            store: {
                fields: ['name', 'g1', 'g2'],
                data: [
                    {"name": "Item-0", "g1": 18.34,"g2": 0.04},
                    {"name": "Item-1", "g1": 2.67, "g2": 14.87},
                    {"name": "Item-2", "g1": 1.90, "g2": 5.72},
                    {"name": "Item-3", "g1": 21.37,"g2": 2.13},
                    {"name": "Item-4", "g1": 2.67, "g2": 8.53},
                    {"name": "Item-5", "g1": 18.22,"g2": 4.62},
                    {"name": "Item-6", "g1": 28.51, "g2": 12.43},
                    {"name": "Item-7", "g1": 34.43, "g2": 4.40},
                    {"name": "Item-8", "g1": 21.65, "g2": 13.87},
                    {"name": "Item-9", "g1": 12.98, "g2": 35.44},
                    {"name": "Item-10", "g1": 22.96, "g2": 38.70},
                    {"name": "Item-11", "g1": 0.49, "g2": 51.90},
                    {"name": "Item-12", "g1": 20.87, "g2": 62.07},
                    {"name": "Item-13", "g1": 25.10, "g2": 78.46},
                    {"name": "Item-14", "g1": 16.87, "g2": 56.80}
                ]
            },

            interactions: {
                type: 'panzoom'
            },

            legend: {
                docked: 'right'
            },

            axes: [
                {
                    type: 'numeric',
                    position: 'left',
                    grid: true
                },
                {
                    type: 'category',
                    position: 'bottom',
                    visibleRange: [0, 0.4]
                }
            ],

            series: [
                {
                    type: 'area',
                    xField: 'name',
                    yField: ['g1', 'g2'],
                    title: ['G1', 'G2'],
                    style: {
                        stroke: '#666666',
                        fillOpacity: 0.8
                    }
                }
            ]
        }
    ]
});

Bar

The Bar series creates a stackable or group-able bar chart, which is typically used to display categorized numeric data that shows progression or regression. Note that opposed to the old chart package, a Bar series refers to a vertical bar series, or a column series. You can obtain the horizontal one by setting flipXY to the chart.

A Bar series can either be stacked or grouped. You can set the stacked config on the series to false in order to switch the series to the grouped mode.

A sample Bar chart is shown in the following example:

Ext.create('Ext.Container', {
    renderTo: Ext.getBody(),
    width:500,
    height:500,
    layout: 'fit',
    items: [
        {
            xtype: 'chart',
            flipXY: true,
            store: {
                fields: ['name', 'g1', 'g2'],
                data: [
                    {"name": "Item-0", "g1": 18.34,"g2": 0.04},
                    {"name": "Item-1", "g1": 2.67, "g2": 14.87},
                    {"name": "Item-2", "g1": 1.90, "g2": 5.72},
                    {"name": "Item-3", "g1": 21.37,"g2": 2.13},
                    {"name": "Item-4", "g1": 2.67, "g2": 8.53},
                    {"name": "Item-5", "g1": 18.22,"g2": 4.62},
                    {"name": "Item-6", "g1": 28.51, "g2": 12.43},
                    {"name": "Item-7", "g1": 34.43, "g2": 4.40},
                    {"name": "Item-8", "g1": 21.65, "g2": 13.87},
                    {"name": "Item-9", "g1": 12.98, "g2": 35.44},
                    {"name": "Item-10", "g1": 22.96, "g2": 38.70},
                    {"name": "Item-11", "g1": 0.49, "g2": 51.90},
                    {"name": "Item-12", "g1": 20.87, "g2": 62.07},
                    {"name": "Item-13", "g1": 25.10, "g2": 78.46},
                    {"name": "Item-14", "g1": 16.87, "g2": 56.80}
                ]
            },  

            //set legend configuration
            legend: {
                docked: 'right'
            },

            //define the x and y-axis configuration.
            axes: [
                {
                    type: 'numeric',
                    position: 'bottom',
                    grid: true,
                    minimum: 0
                },
                {
                    type: 'category',
                    position: 'left'
                }
            ],

            //define the actual bar series.
            series: [
                {
                    type: 'bar',
                    xField: 'name',
                    yField: ['g1', 'g2'],
                    axis: 'bottom',
                    // Cycles the green and blue fill mode over 2008 and 2009
                    // subStyle parameters also override style parameters
                    subStyle: {
                        fill: ["#115fa6", "#94ae0a"]
                    }
                }
            ]
        }
    ]
});

Line

The Line series creates a line chart, an alternative to a bar chart, for showing categorized numeric data that shows progression or regression. The following example shows a typical configuration object for the Line series:

Ext.create('Ext.Container', {
    renderTo: Ext.getBody(),
    width:500,
    height:500,
    layout: 'fit',
    items: [
        {
            xtype: 'chart',
            store: {
                fields: ['name', 'g1', 'g2'],
                data: [
                    {"name": "Item-0", "g1": 18.34,"g2": 0.04},
                    {"name": "Item-1", "g1": 2.67, "g2": 14.87},
                    {"name": "Item-2", "g1": 1.90, "g2": 5.72},
                    {"name": "Item-3", "g1": 21.37,"g2": 2.13},
                    {"name": "Item-4", "g1": 2.67, "g2": 8.53},
                    {"name": "Item-5", "g1": 18.22,"g2": 4.62},
                    {"name": "Item-6", "g1": 28.51, "g2": 12.43},
                    {"name": "Item-7", "g1": 34.43, "g2": 4.40},
                    {"name": "Item-8", "g1": 21.65, "g2": 13.87},
                    {"name": "Item-9", "g1": 12.98, "g2": 35.44},
                    {"name": "Item-10", "g1": 22.96, "g2": 38.70},
                    {"name": "Item-11", "g1": 0.49, "g2": 51.90},
                    {"name": "Item-12", "g1": 20.87, "g2": 62.07},
                    {"name": "Item-13", "g1": 25.10, "g2": 78.46},
                    {"name": "Item-14", "g1": 16.87, "g2": 56.80}
                ]
            },
            interactions: ['panzoom'],

            legend: {
                docked: 'bottom'
            },

            //define x and y axis.
            axes: [
                {
                    type: 'numeric',
                    position: 'left'
                },
                {
                    type: 'category',
                    visibleRange: [0, 1],
                    position: 'bottom'
                }
            ],

            //define the actual series
            series: [{
                type: 'line',
                xField: 'name',
                yField: 'g1',
                title: 'Normal',
                style: {
                    fill: "#115fa6",
                    stroke: "#115fa6",
                    fillOpacity: 0.6,
                    miterLimit: 3,
                    lineCap: 'miter',
                    lineWidth: 2
                }
            }, {
                type: 'line',
                xField: 'name',
                yField: 'g2',
                title: 'Smooth',
                style: {
                    smooth: true,
                    stroke: "#94ae0a",
                    fillOpacity: 0.6,
                    miterLimit: 3,
                    lineCap: 'miter',
                    lineWidth: 2
                }
            }]
        }
    ]
});

Pie

The Pie series creates a pie chart, a great way to display quantitative information for a number of categories that also have a meaning as a whole, for example, all 12 months of a given year.

A Pie chart example is given in the following code sample:

Ext.create('Ext.Container', {
    renderTo: Ext.getBody(),
    width: 500,
    height: 500,
    layout: 'fit',
    items: [
        {
            xtype: 'polar',
            store: {
                fields: ['name', 'g1', 'g2'],
                data: [
                    {"name": "Item-0", "g1": 18.34,"g2": 0.04},
                    {"name": "Item-1", "g1": 2.67, "g2": 14.87},
                    {"name": "Item-2", "g1": 1.90, "g2": 5.72},
                    {"name": "Item-3", "g1": 21.37,"g2": 2.13},
                    {"name": "Item-4", "g1": 2.67, "g2": 8.53},
                    {"name": "Item-5", "g1": 18.22,"g2": 4.62}
                ]
            },

            interactions: ['rotate'],

            //configure the legend.
            legend: {
                docked: 'bottom'
            },

            //describe the actual pie series.
            series: [
                {
                    type: 'pie',
                    xField: 'g1',
                    label: {
                        field: 'name',
                        display: 'rotate'
                    },
                    donut: 25,
                    style: {
                        miterLimit: 10,
                        lineCap: 'miter',
                        lineWidth: 2
                    }
                }
            ]
        }
    ]
});

Radar

The Radar series creates a radar chart, a great way to display a comparison of different quantitative values for a constrained number of categories. A Radar series example is illustrated by the following code:

Ext.create('Ext.Container', {
    renderTo: Ext.getBody(),
    width:500,
    height:500,
    layout: 'fit',
    items: [
        {
            xtype: 'polar',
            store: {
                fields: ['name', 'g1', 'g2'],
                data: [
                    {"name": "0", "g1": 18.34},
                    {"name": "1", "g1": 2.67},
                    {"name": "2", "g1": 1.90},
                    {"name": "3", "g1": 21.37},
                    {"name": "4", "g1": 2.67},
                    {"name": "5", "g1": 18.22},
                    {"name": "6", "g1": 28.51},
                    {"name": "7", "g1": 34.43},
                    {"name": "8", "g1": 21.65},
                    {"name": "9", "g1": 12.98},
                    {"name": "10", "g1": 22.96},
                    {"name": "11", "g1": 0.49},
                    {"name": "12", "g1": 20.87},
                    {"name": "13", "g1": 25.10},
                    {"name": "14", "g1": 16.87}
                ]
            },

            // Set rotation
            interactions: ['rotate'],

            // Define radial and angular axis for the radar chart.
            axes: [
                {
                    type: 'numeric',
                    position: 'radial',
                    fields: 'g1',
                    grid: true,
                    label: {
                        fill: 'black',
                        y: -8
                    }
                },
                {
                    type: 'category',
                    position: 'angular',
                    fields: 'name',
                    grid: true,
                    label: {
                        fill: 'black'
                    }
                }
            ],

            series: [
                {
                    type: 'radar',
                    xField: 'name',
                    yField: 'g1'
                }
            ]
        }
    ]
});

Scatter

The scatter series creates a scatter chart, which enables an application to display more than two variables in the same visualization. These variables can be mapped onto x, y coordinates and also to an element’s properties, such as radius, size, color, and so on.

Ext.create('Ext.Container', {
    renderTo: Ext.getBody(),
    width:500,
    height:500,
    layout: 'fit',
    items: [
        {
            xtype: 'cartesian',
            store: {
                fields: ['name', 'g1', 'g2'],
                data: [
                    {"name": "Item-0", "g1": 18.34,"g2": 0.04},
                    {"name": "Item-1", "g1": 2.67, "g2": 14.87},
                    {"name": "Item-2", "g1": 1.90, "g2": 5.72},
                    {"name": "Item-3", "g1": 21.37,"g2": 2.13},
                    {"name": "Item-4", "g1": 2.67, "g2": 8.53},
                    {"name": "Item-5", "g1": 18.22,"g2": 4.62},
                    {"name": "Item-6", "g1": 28.51, "g2": 12.43},
                    {"name": "Item-7", "g1": 34.43, "g2": 4.40},
                    {"name": "Item-8", "g1": 21.65, "g2": 13.87},
                    {"name": "Item-9", "g1": 12.98, "g2": 35.44},
                    {"name": "Item-10", "g1": 22.96, "g2": 38.70},
                    {"name": "Item-11", "g1": 0.49, "g2": 51.90},
                    {"name": "Item-12", "g1": 20.87, "g2": 62.07},
                    {"name": "Item-13", "g1": 25.10, "g2": 78.46},
                    {"name": "Item-14", "g1": 16.87, "g2": 56.80}
                ]
            },

            axes: [{
                type: 'numeric',
                position: 'bottom'
            },{
                type: 'numeric',
                position: 'left'
            }],
            series: [
                {
                    type: 'scatter',
                    xField: 'g1',
                    yField: 'g2',
                    marker: {
                        radius: 4
                    },
                    highlight: {
                        fillStyle: 'yellow',
                        radius: 7,
                        lineWidth: 2
                    },
                }
            ]
        }
    ]
});

Gauge

The Gauge series creates a gauge chart, typically used to show progress in a certain variable. In addition to using stores, you can also set a value of the gauge series to show the value.

An example is given in the following code sample:

Ext.create('Ext.Container', {
    renderTo: Ext.getBody(),
    width: 500,
    height: 500,
    layout: 'fit',
    items: [
        {
            xtype: 'polar',
            series: [{
                type: 'gauge',
                minimum: 100,
                maximum: 800,
                value: 400,
                donut: 30,
                colors: ["#115fa6", "lightgrey"]
            }]
        }
    ]
});

Interactions

This section introduces the interaction features in the Sencha Charts package. By taking advantage of these features, you can create highly interactive charts that enable users to visualize and navigate complex data sets.

Adding Interactions

To add interactions to a chart, set the interactions config of the chart to an array of interaction config objects. Each object must be a string or must contain a type property matching one of the interaction types, described in the subsequent sections. Each object may also contain additional config options that are appropriate for a particular interaction.

You can define your own interaction by extending the Ext.chart.interactions.Abstract class and registering the name using an alias with the interaction prefix. For example, if you want to create an interaction called remove, create an alias of the class named “interaction.remove”, then set the type: ‘remove’ in the interaction config.

Here are a couple of examples of basic interactions.

Item Highlighting

The itemhighlight interaction enables you to highlight individual data point items on the chart.

This interaction is implemented by the Ext.chart.interactions.ItemHighlight class. See the class documentation for detailed configuration options.

Rotate

The rotate interaction enables users to rotate a pie or a radar chart, by default the rotation being performed using a drag gesture. The following code snippet is an example of how simple it is to add the rotate interaction to your pie or radar charts:

interactions: ['rotate']

This interaction is implemented by the Ext.chart.interactions.Rotate class. See the class documentation for detailed configuration options.

Legend Store

The chart exposes a store to represent legend information collected from series. Technically, you can do anything with this store. Again, this architecture can help you decouple the legend information from the legend component, so you can use any technology and customization for showing the legend at any desired place.

For you convenience, we also provide a default legend component that already implements some common and basic functionality for displaying legends. The Chart configuration object accepts a legend section to enable the default legend component and insert it in the parent of the chart. The default legend component contains a docked config and it is docked on that side.

Last updated