Highcharts: how to separate stacked columns on datetime axis - highcharts

Screenshot of issue
I have multiple series shown on a datetime axis.
The problem is that the same series can have multiple entries for the same date.
Is there a way to separate these stacks based on another ID variable that I'm passing to highcharts?
This example is close to what I need: https://jsfiddle.net/gh/get/library/pure/highcharts/highcharts/tree/master/samples/highcharts/demo/column-stacked-and-grouped/
series: [{
name: 'John',
data: [5, 3, 4, 7, 2],
stack: 'male'
}, {
name: 'Joe',
data: [3, 4, 4, 2, 5],
stack: 'male'
}, {
name: 'Jane',
data: [2, 5, 6, 2, 1],
stack: 'female'
}, {
name: 'Janet',
data: [3, 0, 4, 4, 3],
stack: 'female'
}]
But instead of defining stack by series, I need it defined by individual data points.

But instead of defining stack by series, I need it defined by
individual data points.
Unfortunately, it will require a lot of custom code. However, there is a better solution with the use of linkedTo and id series properties. When you have the same series which have multiple entries for the same date split them as a separate series with different stack. Set for example id = '1' in the first series and linkedTo = '1' in the second one. When two series are linked, only the first one appears in the legend.
Example code:
series: [{
name: 'John',
data: [5, 3, 4, 7, 2],
stack: 'male',
id: 'john'
}, {
name: 'John',
data: [2, 5, 6, 2, 1],
stack: 'female',
linkedTo: 'john'
}, {
name: 'Joe',
data: [3, 4, 4, 2, 5],
stack: 'male'
}, {
name: 'Janet',
data: [3, 0, 4, 4, 3],
stack: 'female'
}]
Demo:
https://jsfiddle.net/BlackLabel/ure08d4k/6/

Related

cannot use strings in data in highcharts on doughnut charts

Why can't I use strings in data on a highchart doughnut chart? You can use it on Bar charts but not doughnut for some reason?
Example of strings in data for bar chart (no errors):
Fiddle: http://jsfiddle.net/8rsywuc7/
},
series: [{
name: 'John',
data: [5, 3, 4, 7, 2]
}, {
name: 'Jane',
data: ['','','','','']
}, {
name: 'Joe',
data: [3, 4, 4, 2, 5]
Example of strings in data for doughnut chart (see error):
Fiddle: http://jsfiddle.net/troqjcnz/
"data": [38, 44, 14, 20, 25, 75],
"name": "Male",
},
{
"data": ['','','','','',''],
"name": "Female",
},
{
"data": [38, 44, 14, 20, 25, 75],
"name": "No reply",
}
Notice that there is a big difference between those two series configs. In the first one, the data is an array of empty string, meanwhile in the second data becomes an array of objects where y is set to empty string. If you will do the same in the column, the same error will occur - see: http://jsfiddle.net/8rsywuc7/
series: [{
name: 'John',
data: [5, 3, 4, 7, 2]
}, {
name: 'Jane',
data: [{
name: 'test',
y: ''
}]
}, {
name: 'Joe',
data: [3, 4, 4, 2, 5]
}]
What can I suggest is:
use null rather than empty strings (I am aware that it is not the same, but the output should be similar), demo: http://jsfiddle.net/BlackLabel/jokbgryz/
change the logic responsible for creating data for something like is done here: http://jsfiddle.net/BlackLabel/9bov21gf/

Grouped and stacked columns with shared series

It's almost like this: https://www.highcharts.com/demo/column-stacked-and-grouped
Except that I want to have John (or any other person) in both stacks. In my case, each person is not either Male or Female, they are part of both.
Here are my categories and series:
categories: [
'Apples', 'Oranges',
],
series: [
{
name: 'John',
data: [1, 9],
stack: 'Type A',
},
{
name: 'John',
data: [2, 10],
stack: 'Type B',
},
{
name: 'Joe',
data: [3, 11],
stack: 'Type A',
},
{
name: 'Joe',
data: [4, 11],
stack: 'Type B',
},
{
name: 'Jane',
data: [5, 12],
stack: 'Type A',
},
{
name: 'Jane',
data: [6, 13],
stack: 'Type B',
},
{
name: 'Janet',
data: [7, 14],
stack: 'Type A',
},
{
name: 'Janet',
data: [8, 15],
stack: 'Type B',
},
],
But with this, I currently have all names (Janet, Joe, ...) duplicated in the legend.
You can use showInLegend: false to prevent duplicates in legend and make sure that corresponding series have the same color:
series: [{
name: 'John',
color: 'orange',
data: [1, 9],
stack: 'Type A',
}, {
name: 'John',
color: 'orange',
data: [2, 10],
stack: 'Type B',
showInLegend: false
}
]
This piece of code causes that legend performs the same action (show/hide) for all series with the common name:
events: {
legendItemClick: function(event) {
var series = this,
chart = this.chart;
var isVisible = series.visible;
chart.series.forEach(function(s) {
if (s.name === series.name) {
if (isVisible) {
s.hide();
} else {
s.show();
}
}
});
event.preventDefault();
}
}
Live working example: http://jsfiddle.net/kkulig/cgu0g7vm/
API references:
https://api.highcharts.com/highcharts/plotOptions.series.showInLegend
https://api.highcharts.com/highcharts/plotOptions.series.events.legendItemClick
Got this answer from the highcharts forum:
To remove duplicated names in the legend you can set series.linkedTo to ":previous" in the second series with the same name. Then you can change the series colors to be the same.
series: [{
name: 'John',
data: [1, 9],
stack: 'Type A',
}, {
name: 'John',
data: [2, 10],
stack: 'Type B',
linkedTo: ':previous',
color: Highcharts.getOptions().colors[0]
},
Live demo: http://jsfiddle.net/ppotaczek/psguhp3r/

Hide linked series on legend hover in Highcharts

According to http://api.highcharts.com/highcharts/series.line, by linking two series, "Toggling the visibility of this also toggles the linked series".
When linking two series, I expect them to both be activated when hovering a legend item. This does not work with this setup
Highcharts.chart('container', {
title: { text: 'Linked Series'},
series: [{
id: 2,
data: [3, 1, 3, 2]
}, {
id: 0,
data: [2, 3, 2, 1]
}, {
id: 1,
linkedTo: 2,
data: [1, 2, 1, 3]
}, ]
});
How can I make sure, that all linked series are hovered at the same time.
https://fiddle.jshell.net/papa_bravo/oxbm6mke/
It seems to be a small bug in Highcharts; if ids are numbers then turning off the visibility by clicking the legend does not work properly. If you convert ids to stings it works OK
Highcharts.chart('container', {
title: { text: 'Linked Series'},
series: [{
id: '2',
data: [3, 1, 3, 2]
}, {
id: '0',
data: [2, 3, 2, 1]
}, {
id: '1',
linkedTo: '2',
data: [1, 2, 1, 3]
}, ]
});
https://fiddle.jshell.net/oxbm6mke/6/
EDIT: Actually it's not a bug. According to the documentation linkedTo must be a string.

multiple spline charts with multiple nested series highcharts

I am using highcharts and trying to populate multiple spline charts and toggle them on one charts.
i need to toggle series starting with chart2 on one series called chart2 and toggle series starting with chart1 on another series called chart1.
(please check out this fiddle to understand my point).
series: [{
type: 'spline',
name: 'chart1-1',
data: [3, 2, 1, 3, 4]
}, {
type: 'spline',
name: 'chart1-2',
data: [2, 3, 5, 7, 6]
}, {
type: 'spline',
name: 'chart1-2',
data: [4, 3, 3, 9, 0]
}, {
type: 'spline',
name: 'chart2-1',
data: [4, 3, 5, 3, 2]
}, {
type: 'spline',
name: 'chart2-2',
data: [4, 3, 7, 2, 4]
}, {
type: 'spline',
name: 'chart2-3',
data: [3, 4, 7, 9, 6]
}],
how can i achieve this type of nested series toggle.
also check out this fiddle by using id and linkedTo property, but yet not fills requirement, because i need to combine both fiddles
series: [
{
id: 'Adwords',
type: 'spline',
name: 'Adwords',
},{
linkedTo: 'Adwords',
type: 'spline',
name: 'click',
data: [3, 2, 1, 3, 4]
},
{
linkedTo: 'Adwords',
type: 'spline',
name: 'Conver',
data: [2, 3, 5, 7, 6]
}, {
linkedTo: 'Adwords',
type: 'spline',
name: 'Impr',
data: [4, 3, 3, 9, 0]
},{
id: 'Bing',
type: 'spline',
name: 'Bing',
},{
linkedTo: 'Bing',
type: 'spline',
name: 'click',
data: [4, 3, 7, 2, 4]
},{
linkedTo: 'Bing',
type: 'spline',
name: 'Conver',
data: [4, 3, 5, 3, 2]
}, {
linkedTo: 'Bing',
type: 'spline',
name: 'Impr',
data: [3, 4, 7, 9, 6]
}]
thanks,

Multiple columns on same series. Highchart

I would like to have a chart that represent data like this.
the series code would be something like this.
series: [{
name: 'Car 1',
data: [[1, 3], [4, 6], [7, 9]]
}, {
name: 'Car 2',
data: [[2, 3], [8, 10], [12, 18]]
}]
The first number on each array would be the starting point and the second the finishing point, and all arrays inside the main array would be on the same line.
An inverted columnrange chart should work. Your data isn't formatted in a way that would work with what you want, but transforming it isn't difficult. If you aren't stuck with the data format you showed, you just need a point object with x, low, and high.
For example:
{
x: 1,
low: 0,
high: 4
}
The following massages your current structure into the correct format:
$(function () {
var series = [{
name: 'Car 1',
data: [
[1, 3],
[4, 6],
[7, 9]
]
}, {
name: 'Car 2',
data: [
[2, 3],
[8, 10],
[12, 18]
]
}, {
name: 'Car 3',
data: [
[5, 9],
[1, 2]
]
}];
// massage the data
var data = [];
for(var i=0;i<series.length;i++) {
for(var j=0;j<series[i].data.length;j++) {
data.push({
x: j,
low: series[i].data[j][0],
high: series[i].data[j][1],
name: series[i].name
});
}
}
$('#container').highcharts({
chart: {
type: 'columnrange',
inverted: true
},
plotOptions: {
columnrange: {
dataLabels: {
enabled: false
}
}
},
legend: {
enabled: false
},
series: [{
name: 'Cars',
data: data
}]
});
});
http://jsfiddle.net/hqwrx4uy/

Resources