How to create a single series bar graph with Highcharts - highcharts

I'm trying to create a simple Highcharts bar graph with a single series and multiple labels in the legend. How is this done?
Here is an example:
$('#container').highcharts({
chart: {
type: 'bar',
},
legend: {
enabled: true,
layout: 'vertical',
align: 'right',
verticalAlign: 'middle',
labelFormatter: function() {
return this.name + " - <span class='total'>"+this.y+"</span>"
}
},
title: {
text: 'Simple Bar Graph'
},
xAxis: {
categories: ['First', 'Second', 'Third', 'Fourth', , 'Fifth'],
allowDecimals: false
},
yAxis: {
allowDecimals: false
},
series: [
{
data: [
{y: 6, name: 'First', color: 'blue'},
{y: 7, name: 'Second', color: 'green'},
{y: 9, name: 'Third', color: 'yellow'},
{y: 1, name: 'Fourth', color: 'orange'},
{y: 1, name: 'Fifth', color: 'red'}
]
}
],
});

Luckily, Highcharts is pretty flexible.
We can do some tricks (maybe hacks?) to achieve this kind of "non-convential" tasks.
What you could do in your case is create "fake" serieses, and use custom event handlers:
series: [
{
pointWidth:20,
color: colors[0],
showInLegend:false,
data: [
{y: 6, name: 'First', color: colors[0]},
{y: 7, name: 'Second', color: colors[1]},
{y: 9, name: 'Third', color: colors[2]},
{y: 1, name: 'Fourth', color: colors[3]},
{y: 1, name: 'Fifth', color: colors[4]}
]
},
{color: 'blue'},
{color: 'green'},
{color: 'yellow'},
{color: 'orange'},
{color: 'red'}
],
In order to format the legend labels we can use labelFormatter for the legend:
legend: {
labelFormatter: function(){
return names[this.index-1];
}
},
this will set the legend label to the name of the corresponding point.
And finally we need to handle the legend click, to imitate the "normal" behaviour:
plotOptions: {
series: {
events: {
legendItemClick: function (x) {
var i = this.index - 1;
var series = this.chart.series[0];
var point = series.points[i];
if(point.oldY == undefined)
point.oldY = point.y;
point.update({y: point.y != null ? null : point.oldY});
}
}
}
},
These are just examples, you can obviously improve this and adjust to your own need.
Good Luck!
http://jsfiddle.net/otm0oq2c/3/

A simpler way than the accepted answer is by setting the categories key as an array of 'labels' and the series.data as an array of 'values'.
Here is an example that uses a collection (Array of objects) or a flat object to populate a bar chart in Highcharts (jsfiddle):
The example assumes a an array of objects with keys name/value, or a simple object of key/value:
var chart = Highcharts.chart('container', {
title: {
text: 'Simple Bar'
},
chart: {type: 'bar'},
xAxis: {
// categories: Object.keys(obj)
// or
categories: collection.map(i => i.name)
},
series: [{
name: 'Usage',
colorByPoint: true,
// data: Object.values(obj)
// or:
data: collection.map(i => i.value)
}],
legend: false
});

Related

HighCharts - Single legend for two columns in a Stack Column chart

I have four stack in a Stacked Column chart: "One Completed", "One Pending", "Two Completed", "Two Pending".
By default, I have four legends for these stacks.
What I want is to have two legends: "One" and "Two". Is it possible using Highcharts?
Edit:
Added code. Some of text/logic are removed. Most of the styling is what I found on Internet somewhere.
Highcharts.chart('', {
chart: {
type: 'column',
},
title: {
text: '',
},
xAxis: {
categories: c,
labels: {
y: 40
}
},
yAxis: {
min: 0,
title: {
text: ''
},
stackLabels: {
enabled: true,
verticalAlign: 'bottom',
crop: false,
overflow: 'none',
y: 20,
formatter: function () {
return this.stack
},
style: {
fontSize: '9px',
color: (
Highcharts.defaultOptions.title.style &&
Highcharts.defaultOptions.title.style.color
) || 'gray'
}
}
},
credits: {
enabled: false
},
legend: {
align: 'right',
x: -30,
verticalAlign: 'top',
y: 25,
floating: true,
backgroundColor: Highcharts.defaultOptions.legend.backgroundColor || 'white',
borderColor: '#CCC',
borderWidth: 1,
shadow: false
},
tooltip: {
formatter: function () {
//custom logic
}
},
plotOptions: {
column: {
stacking: 'normal',
dataLabels: {
enabled: true
}
},
series: {
cursor: 'pointer',
events: {
click: function (event) {
//custom click to filter logic
}
}
}
},
series: [{
name: 'One Pending',
data: one_series_pending,
color: '#7cb5ec',
stack: 'Pending'
}, {
name: 'One Completed',
data: one_series_completed,
color: '#7cb5ec',
stack: 'Completed'
}, {
name: 'Two Pending',
data: two_series_pending,
color: '#434348',
stack: 'Pending'
}, {
name: 'Two Completed',
data: two_series_completed,
color: '#434348',
stack: 'Completed'
}]
});
Sample chart:
I want single legend for same color(two stacks)
You can link one series with another and have one legend item for them by using linkedTo property.
series: [{
name: 'One Pending',
data: [9, 5],
color: '#7cb5ec',
stack: 'Pending'
}, {
name: 'One Completed',
data: [6, 2],
color: '#7cb5ec',
stack: 'Completed',
linkedTo: 0
}, {
name: 'Two Pending',
data: [35, 5],
color: '#434348',
stack: 'Pending'
}, {
name: 'Two Completed',
data: [3, 0],
color: '#434348',
stack: 'Completed',
linkedTo: 2
}]
Live demo: http://jsfiddle.net/BlackLabel/eh5m10q9/
API Reference: https://api.highcharts.com/highcharts/series.column.linkedTo

highcharts x-axis not showing with series of just one value

I use highcharts for a chart. I made series with "linkedTo" because I need to group the columns by region and generate a legend displaying these regions. Until then, the result is the desired one. But I can not display the name of my categories on the X axis. Under each column there should be "A", "B", ... As in the picture. See the jsfiddle for the code. Thank you for your help.
http://jsfiddle.net/yucca/hpkLy6t0/24/
// Create the chart
Highcharts.chart('container', {
chart: {
type: 'column'
},
title: {
text: 'TEST'
},
subtitle: {
text: 'TEST'
},
xAxis: {
type: 'category',
categories: ['A', 'B', 'C', 'D']
},
legend: {
enabled: true,
labelFormatter: function() {
return this.userOptions.id
}
},
plotOptions: {
series: {
borderWidth: 0,
dataLabels: {
enabled: false,
}
}
},
tooltip: {
headerFormat: '<span style="font-size:11px">{series.name}</span><br>',
pointFormat: '<span style="color:{point.color}">{point.name}</span>: <b>{point.y:.2f}%</b> of total<br/>'
},
series: [
{
id:'China',
color: '#004f9e',
data: [1100]
},
{
id:'International',
color: '#e73357',
data: [10]
},
{
linkedTo: 'International',
color: '#e73357',
data: [1000]
},
{
linkedTo: 'China',
color: '#004f9e',
data: [686]
}
]
});
graph
You need to make a slight alteration to your series in order for this to work.
series: [{
data: [{
id:'China',
color: '#004f9e',
y: 1100
},{
id:'International',
color: '#e73357',
y: 10
},{
linkedTo: 'International',
color: '#e73357',
y: 1000
},{
linkedTo: 'China',
color: '#004f9e',
y: 686
}],
}]
What I did is the following:
I changed the data[] so it holds an array of your values.
I changed the values stored as data: [686] to y: 686
Here you can find a working JSFiddle

Highcharts multiple series and axes types overlapping

I have a problem with a highcharts graph, regarding overlapping of multiple axes.
There are 2 types of series, one type column and one type line. I want the line type to be always above the others and never intersect them.
You may see my problem here:
http://jsfiddle.net/99tC7/
Anticipated thank you for any help provided.
$('#container').highcharts({
title: {
text: 'Title',
x: -20 //center
},
xAxis: {
categories: [ "5.11.13", "6.11.13", "7.11.13", "8.11.13", "9.11.13"],
labels: {
rotation: 270
}
},
yAxis: [{
plotLines: [{
value: 0,
width: 1,
color: '#808080'
}],
title:{
text: "Number 1"
},
},
{
plotLines: [{
value: 0,
width: 1,
color: '#808080'
}],
title:{
text: "Number 2"
},
opposite: true,
max:50,
minPadding:5,
}],
legend: {
layout: 'horizontal',
align: 'center',
verticalAlign: 'bottom',
borderWidth: 0
},
tooltip: {
enabled: false
},
plotOptions: {
line: {
dataLabels: {
enabled: true,
formatter: function() {
return this.y +'%';
}
}
},
column: {
dataLabels: {
enabled: true,
formatter: function() {
return this.y;
}
}
}
},
series: [{
name: 'L1',
type: 'column',
color: '#FF0000',
yAxis: 0,
data: [1, 5, 9, 10, 12]
},
{
name: 'L2',
type: 'column',
color: '#00FF00',
yAxis: 0,
data: [16, 16, 106, 12, 11]
},
{
name: 'L3',
yAxis: 1,
data: [38, 24, 37, 29, 37]
}
]
});
You can try to set correct min/max values in both yAxis like here http://jsfiddle.net/99tC7/1/

Two different thresholds in HighCharts 3.0

With HighCharts 3.0, it is now possible to indicate to colors above and below one threshold. Like this example :
http://jsfiddle.net/highcharts/YWVHx/
Following code :
$(function () {
$.getJSON('http://www.highcharts.com/samples/data/jsonp.php?filename=range.json&callback=?', function(data) {
$('#container').highcharts({
chart: {
type: 'arearange'
},
title: {
text: 'Temperature variation by day'
},
xAxis: {
type: 'datetime'
},
yAxis: {
title: {
text: null
}
},
tooltip: {
crosshairs: true,
shared: true,
valueSuffix: '°C'
},
legend: {
enabled: false
},
series: [{
name: 'Temperatures',
data: data,
color: '#FF0000',
negativeColor: '#0088FF'
}]
});
});
});
Is it possible to have another threshold with a third color, like this for example :
Thanks in advance for your help.
It actually is possible if you don't mind plotting the data twice.
$('#container').highcharts({
chart: {
type: 'arearange'
},
title: {
text: 'Temperature variation by day'
},
xAxis: {
type: 'datetime'
},
yAxis: {
title: {
text: null
}
},
tooltip: {
crosshairs: true,
shared: true,
valueSuffix: '°C'
},
legend: {
enabled: false
},
series: [{
name: 'Temperatures',
threshold : 0,
data: data,
color: 'orange',
negativeColor: 'blue'
},
{
name: 'Temperatures',
threshold : 10,
data: data,
color: 'red',
negativeColor: 'transparent'
}]
});
});
http://jsfiddle.net/YWVHx/97/
A feature to solve this without "hacks" was added in Highcharts 4.1.0 (February 2015), called zones (API). The given problem can be solved like this, using zones:
plotOptions: {
series: {
zones: [{
value: 0, // Values up to 0 (not including) ...
color: 'blue' // ... have the color blue
},{
value: 10, // Values up to 10 (not including) ...
color: 'orange' // ... have the color orange
},{
color: 'red' // Values from 10 (including) and up have the color red
}]
}
}
See this JSFiddle demonstration of how it looks.
Unfortunately this option is not possible, but you can request your suggestion in http://highcharts.uservoice.com and vote for it.
By the way, I can try use a plotLine, like this:
yAxis: {
title: {
text: 'My Chart'
},
plotLines: [{
id: 'limit-min',
dashStyle: 'ShortDash',
width: 2,
value: 80,
zIndex: 0,
label : {
text : '80% limit'
}
}, {
id: 'limit-max',
color: '#008000',
dashStyle: 'ShortDash',
width: 2,
value: 90,
zIndex: 0,
label : {
text : '90% limit'
}
}]
},

Sort series per category

I have a chart that looks like this:
http://jsfiddle.net/rtGFm/37/
I would like to have a "sort" button that sorts high to low each category, putting the columns in a different order for each category. Is this possible with HighCharts?
var chart;
$(document).ready(function() {
chart = new Highcharts.Chart({
chart: {
renderTo: 'container',
type: 'column'
},
title: {
text: ''
},
subtitle: {
text: ''
},
xAxis: {
categories: [
'foreclosures',
'nuisances',
'distance from city center'
]
},
yAxis: {
min: 0,
title: {
text: ''
}
},
tooltip: {
formatter: function() {
return ''+
this.x +': '+ this.y +' mm';
}
},
plotOptions: {
column: {
pointPadding: 0.2,
borderWidth: 0
}
},
series: [{
name: 'Alger Heights',
data: [49.9, 71.5, 50]
}, {
name: 'Shawmut Hills',
data: [83.6, 78.8, 67]
}, {
name: 'Baxter',
data: [48.9, 38.8, 100]
}, {
name: 'Midtown',
data: [42.4, 33.2, 80]
}, {
type: 'scatter',
data: [55,60,70],
marker: {
symbol: 'square',
lineColor: '#FFFFFF',
lineWidth: 1,
radius: 8
},
name: 'Average'
}]
});
});
I had the same problem :) highcharts still does not provide any solution for this problem, but it is flexibel enough so that you can sort anyway - just by javascript.
Put your data in a separate value:
var dataMain = [{name:"test1",data:5}, {name:"test1",data:1},{name:"test1",data:2}]
In series you can just add a function which sorts your data:
series: (function(){
for(var i = 0;i<dataMain.length;i++){
dataMain[i].data = dataMain[i].data.sort(function(a,b){
return a[0] - b[0] ;
})
return dataMain;
}
Hope I got everything wright.
Got it to work, I believe. Trick is to add each column in correct order as a new serie with same type (column), reuse colors and hide legend...
Very hackish, the JS code to sort like 8 categories independently will be ugly but the result looks fine.
Edit: Updated fiddle, I see the spacing between the categories grows with series, doesn't look supernice.
My jsfiddle is here
$(function () {
$('#container').highcharts({
chart: {
inverted: true
},
title: {
text: 'Series sorted in within categories'
},
xAxis: {
categories: ['January']
},
series: [{
type: 'column',
name: 'Stockholm',
data: [{x:0, y:95, color: Highcharts.getOptions().colors[0]}]
}, {
type: 'column',
name: 'Göteborg',
data: [{x:0, y:80, color: Highcharts.getOptions().colors[1]}]
}, {
type: 'column',
name: 'Göteborg',
data: [{x: 1, name: 'February', y: 98, color: Highcharts.getOptions().colors[1] // VGR's color
}],
showInLegend: false,
dataLabels: {
enabled: false
}
}, {
type: 'column',
name: 'Stockholm',
data: [{x: 1, name: 'February', y: 80, color: Highcharts.getOptions().colors[0] // VGR's color
}],
showInLegend: false,
dataLabels: {
enabled: false
}
}]
});
});
br,
Jens
I think this can be done,
this may look like a little work around.
since we have a limited number of columns i mean 4 in the given example.
if we have arrays with sorting done with respect to each series then we can handle them.
on button click we can update the chart with new set of data as well as category array.
May be there is no solution from the APi.
According to me this is a possible approach.
Thanks,

Resources