We are not limited to displaying a single series in a chart, and likewise, we are not limited to displaying a single type of chart within the same chart. In some circumstances, we may want to display the same data using different types of charts.
Earlier, we saw that a series
object can have data associated with it, such as name
. Similarly, a series
object can also have a type, which changes how the data is displayed in the rendered chart.
To get started, follow the ensuing instructions:
Define our chart options as we did in the previous recipe, as shown in the following code:
var options = { chart: { type: 'bar' }, title: { text: 'Displaying multiple charts in one graph' }, series: [{ name: 'Bar #1', data: [1, 2, 3, 4] }, { name: 'Bar #2', data: [4, 3, 2, 1] }]
Add a new series to our chart with the type
pie
using the following code:var options = { chart: { type: 'bar' }, title: { text: 'Displaying multiple charts in one graph' }, series: [{ name: 'Bar #1', data: [1, 2, 3, 4] }, { name: 'Bar #2', data: [4, 3, 2, 1] }, { // add new series type: 'pie', data: [1,2,3,4], center: [0,0] }] }
By default, Highcharts will use the chart.type
string to determine how the different series should be displayed. However, if a series has its own type provided, it will use that type when it is rendered in the chart.
Just changing the type
string of the series will probably result in something ugly or otherwise undesired, especially in the case of a pie chart where it will render on top of the existing chart. Fortunately, it is possible to adjust the positioning and style of a pie series by providing a center
option.
If we wanted to enable the labels on the pie chart, we could set dataLabels.enabled
to true
, as shown in the following code:
var options = { // ... series: [{ type: 'pie', name: 'Bar #1', data: [1,2,3,4], dataLabels: { enabled: true } }] };
In fact, a series
object can have any options that you would normally set inside plotOptions.<chartType>
. For more details, visit http://api.highcharts.com/highcharts#plotOptions.