Highcharts, displaying two sets of data on the same line chart - highcharts

I am working on a script that uses HighCharts. I have written the script to produce and output the result in JSON format, see below.
[{"name":"Month","data":["Jan","Feb","Mar","Apr","May"]},{"name":"Rooms","data":[140,77,76,1,1]},{"name":"Target","data":["155"]}]
The JSON has three (3) parts, the month, the total for the month and the target. What I am trying to do is display the data for each month as a line chart.
This I have working, but I would like to display the target data as a line on the same chart.
How can I get the data for the target to display.
I have also written the script that calls the JSON data, see below.
$(function () {
var categories=[];
var data2 =[];
var targetValue = null;
var chart;
$(document).ready(function() {
$.getJSON("../charts/imaint_audit_monthly_chart.php", function(json) {
$.each(json,function(i,el) {
if (el.name == "Month") {
categories = el.data;
} else if (el.name == "Target") {
targetValue = el.data[0];
} else {
data2.push(el);
}
});
$('#container1').highcharts({
chart: {
renderTo: 'container',
type: 'line',
marginTop: 40,
marginRight: 30,
marginBottom: 50,
plotBackgroundColor: '#FCFFC5'
},
title: {
text: 'Monthly audits <?php echo $year;?>',
x: -20, //center
style: {
fontFamily: 'Tahoma',
color: '#000000',
fontWeight: 'bold',
fontSize: '10px'
}
},
subtitle: {
text: '',
x: -20
},
xAxis: {
categories: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
},
yAxis: {
showFirstLabel: false,
lineColor:'#999',
lineWidth:1,
tickColor:'#666',
tickWidth:1,
tickLength:2,
tickInterval: 10,
gridLineColor:'#ddd',
title: {
text: '',
style: {
fontFamily: 'Tahoma',
color: '#000000',
fontWeight: 'bold',
fontSize: '10px'
}
},
plotLines: [{
color: '#FF0000',
value: targetValue,
label: {
text: 'Target',
style: {
color: '#FF0000'
}
}
}]
},
legend: {
enabled: false,
itemStyle: {
font: '11px Trebuchet MS, Verdana, sans-serif',
color: '#000000'
},
itemHoverStyle: {
color: '#000000'
},
itemHiddenStyle: {
color: '#444'
}
},
plotOptions: {
column: {
color:'#ff00ff'
},
series: {
dataLabels: {
enabled: true,
color: '#000000',
align: 'center',
y: 0,
style: {
fontSize: '10px',
fontFamily: 'Verdana, sans-serif'
}
}
}
},
credits: {
enabled: false
},
series: data2
});
});
});
});
Many thanks for your time.

I suggest that you should use the 'plotLine'-Feature to display the target value. This adds a line to any of your axes and it seems that your requirement is the best example for the use of a plot line. First you have to update your code to process the JSON returned from your web service:
$(function () {
var categories=[];
var data2 =[];
var targetValue = null;
var chart;
$(document).ready(function() {
$.getJSON("../charts/imaint_audit_monthly_chart.php", function(json) {
$.each(json,function(i,el) {
if (el.name == "Month") {
categories = el.data;
} else if (el.name == "Target") {
targetValue = el.data[0];
} else {
data2.push(el);
}
});
[...]
now we have the target value in the variable targetValue. This will now be inserted in the highcharts configuration to create a new plotLine for the yAxis:
yAxis: {
plotLines: [{
color: '#FF0000',
value: targetValue,
label: {
text: 'Target',
style: {
color: '#FF0000'
}
}
}],
minRange: targetValue, // to ensure the plotLine is always visible
[...]
},
This will add a red line at the specified targetValue that has a red label 'Target' attached to its left like in this example:
https://jsfiddle.net/doc_snyder/2spmxtwa/1/

Related

Highcharts display column category names on the xaxis

I have an issue displaying the column categories on the xaxis. I can get them to display in the "legend" and the "tooltips".
I have searched for a solution with no result. Can anyone see where I am going wrong.
My code:
$(document).ready(function() {
var options = {
chart: {
renderTo: 'container',
type: 'column',
marginRight: 130,
marginBottom: 25
},
title: {
text: '',
x: -20 //center
},
subtitle: {
text: '',
x: -20
},
xAxis: {
categories: ['Detractors','Passives','Promoters','Not used']
},
yAxis: {
endOnTick: false,
max:101,
showFirstLabel: false,
lineColor:'#999',
lineWidth:1,
tickColor:'#666',
tickWidth:1,
tickLength:2,
tickInterval: 30,
gridLineColor:'#ddd',
title: {
text: 'Percentage %',
style: {
fontFamily: 'Tahoma',
color: '#000000',
fontWeight: 'bold',
fontSize: '8px'
}
},
},
tooltip: {
formatter: function() {
return 'Guest responses:<br/> '+ this.y +'%<br/>'+ this.series.name +'<br/>';
}
},
legend: {
enabled: true,
layout: 'vertical',
align: 'right',
verticalAlign: 'top',
x: -10,
y: 100,
borderWidth: 0
},
colors: ['#FF0000','#FF6600','#009900','#333333'],
plotOptions: {
column: {
colorByPoint: false
},
series: {
cursor: 'pointer',
pointWidth: 20,
point: {
events: {
//click: function() {
}
},
legendIndex:0,
dataLabels: {
enabled: true,
color: '#000000',
align: 'center',
cursor: 'pointer',
//borderRadius: 5,
//backgroundColor: 'rgba(252, 255, 255, 255)',
//borderWidth: 1,
//borderColor: '#AAA',
y: -6,
format: '{y:.1f} %', // one decimal
y: -20, // 10 pixels down from the top
style: {
textShadow: false,
fontSize: '8px',
fontFamily: 'Verdana, sans-serif'
},
formatter: function() {
return '<b>Guest responses: '+ this.y +'<br/>'+ this.series.name +'%</b>';
}
}
}
},
credits: {
enabled: false
},
lang: {
noData: "No data"
},
noData: {
style: {
fontWeight: 'normal',
fontSize: '12px',
color: '#303030'
}
},
navigation: {
buttonOptions: {
enabled: false
}
},
series: []
}
$.getJSON("../charts/1-2-4-reports_chart.php?TAG=<?php echo $_SESSION['SectionVar'];?>&From=<?php echo $_SESSION['StartDate'];?>&To=<?php echo $_SESSION['EndDate'];?>", function(json) {
options.xAxis.categories = json[0];
options.series[0] = json[0];
options.series[1] = json[1];
options.series[2] = json[2];
options.series[3] = json[3];
chart = new Highcharts.Chart(options);
});
});
Many thanks in advance for your time.

Highcharts setting the colour of columns in each month period

I have one last issue to solve with my chart. I have a variable number of colums for each month of the year. What I am trying to do is to assign each column with a set colour. I have read and tried so many ways without success.
My code.
$(function () {
var colors = ['#999999', '#009900', '#FF6600', '#CC0000', '#FF0000'],
colorIterator = 0;
var chart;
$(document).ready(function() {
$.getJSON("../charts/1-2-4-overall_year_chart.php", function(json) {
chart = new Highcharts.Chart({
chart: {
renderTo: 'container1',
type: 'column',
marginRight: 130,
marginBottom: 25,
events:{
load:function() {
var chart = this,
series = chart.series,
each = Highcharts.each;
each(series, function(serie, i) {
each(serie.data, function(data, j) {
data.update({
color: colors[colorIterator]
},false);
colorIterator++;
});
});
chart.redraw();
}
}
},
title: {
text: 'Monthly trend',
x: -20 //center
},
subtitle: {
text: '',
x: -20
},
xAxis: {
categories: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
},
yAxis: {
endOnTick: false,
max:11,
showFirstLabel: false,
lineColor:'#999',
lineWidth:1,
tickColor:'#666',
tickWidth:1,
tickLength:2,
tickInterval: 10,
gridLineColor:'#ddd',
title: {
text: 'Count',
style: {
fontFamily: 'Tahoma',
color: '#000000',
fontWeight: 'bold',
fontSize: '8px'
}
},
zIndex: 10,
label: {
text: 'Maximum %',
align: 'center',
x: -10,
y: -10,
style: {
color: '#FF0000'
}
}
},
credits: {
enabled: false
},
tooltip: {
formatter: function() {
return '<b>'+ this.series.name +'</b><br/>'+
this.x +': '+ this.y;
}
},
legend: {
layout: 'vertical',
align: 'right',
verticalAlign: 'top',
x: -10,
y: 100,
borderWidth: 1
},
plotOptions: {
column: {
colorByPoint: true
},
series: {
cursor: 'pointer',
pointWidth: 6,
point: {
events: {
//click: function() {
//location.href = "feedback-items_detractors_iframe.php?FromDate1=<?php echo $StartDate;?>&ToDate1=<?php echo $EndDate;?> target='iframe2'";
//}
}
},
legendIndex:0,
dataLabels: {
enabled: true,
color: '#000000',
align: 'center',
cursor: 'pointer',
borderRadius: 5,
backgroundColor: 'rgba(252, 255, 255, 255)',
borderWidth: 1,
borderColor: '#AAA',
y: -6,
format: '{y:.1f}', // one decimal
y: -20, // 10 pixels down from the top
style: {
textShadow: false,
fontSize: '8px',
fontFamily: 'Verdana, sans-serif'
}
}
}
},
series: json,
});
});
});
$('#resizer').resizable({
// On resize, set the chart size to that of the
// resizer minus padding. If your chart has a lot of data or other
// content, the redrawing might be slow. In that case, we recommend
// that you use the 'stop' event instead of 'resize'.
resize: function () {
chart.setSize(
this.offsetWidth - 20,
this.offsetHeight - 20,
false
);
}
});
});
This is what I am trying to do
If anyone has any idea how this is done it would be very helpful.
Thank you in advance for your time.
Configure colorByPoint option true and set the color sequence that you want.
options = {
chart: {...},
plotOptions: {
column: {
colorByPoint: true
}
},
colors: [
// set colors
]
}
Example fiddle: http://jsfiddle.net/gh/get/library/pure/highcharts/highcharts/tree/master/samples/highcharts/plotoptions/column-colorbypoint-true/
just solved the issue by using colors: [ '#333333','#009900','#FF6600','#CC0000','#FF0000' ], plotOptions: { column: { colorByPoint: false },

Highcharts exporting className

How do you set a classname for an exported graph in Highcharts? I tried this but it did not work:
exporting: {
chartOptions: {
chart: {
className: 'negPos'
}
}
}
negativeColor add to plot options
Highcharts.chart(container, {
chart: {
//className: 'negPos',
type: 'column',
spacingBottom: 20,
style: {
fontFamily: 'Helvetica Neue,Helvetica,Arial,sans-serif',
fontSize: '11px',
fontWeight: 'lighter'
}
},
title: {
text: 'Highcharts',
align: 'center',
style: {
fontSize: '15px',
fontFamily: 'Helvetica Neue,Helvetica,Arial,sans-serif',
fontWeight: 'lighter'
}
},
xAxis: {
categories: [1, 2, 3, 5, 6]
},
yAxis: {
title: {
text: '%'
}
},
tooltip: {
borderWidth: 0,
style: {
fontFamily: 'Helvetica Neue,Helvetica,Arial,sans-serif',
fontSize: '11px',
align: 'center'
},
formatter: function() {
return '<b>' + this.x + ': </b><br>' + Highcharts.numberFormat(this.y, 1) + '%'
}
},
plotOptions: {
column: {
negativeColor: '#d9534f',
threshold: 0,
}
},
/* plotOptions: {
bar: {
dataLabels: {
enabled: false
}
}
},*/
legend: {
layout: 'vertical',
verticalAlign: 'bottom',
borderWidth: 1,
backgroundColor: ((Highcharts.theme && Highcharts.theme.legendBackgroundColor) || '#FFFFFF'),
shadow: true
},
series: [{
showInLegend: false,
color: '#5cb85c',
data: [54, 21, -3, 18, 5, -11]
}]
});
<script src="https://code.highcharts.com/highcharts.js"></script>
<script src="https://code.highcharts.com/modules/exporting.js"></script>
<div id="container"></div>

Highcharts bad rendering with slightly big numbers

When I use multiaxes and big numbers, Highcharts renders double the height needed to show the data. How can I achieve fitting the vertical axis to the data correctly?
Here the example code:
$(function () {
$('#container').highcharts({
chart: {
zoomType: 'xy'
},
title: {
text: 'Average Monthly Weather Data for Tokyo'
},
subtitle: {
text: 'Source: WorldClimate.com'
},
xAxis: [{
categories: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'],
crosshair: true
}],
yAxis: [{ // Primary yAxis
labels: {
format: '{value}°C',
style: {
color: Highcharts.getOptions().colors[2]
}
},
title: {
text: 'Temperature',
style: {
color: Highcharts.getOptions().colors[2]
}
},
opposite: true
}, { // Secondary yAxis
gridLineWidth: 0,
title: {
text: 'Rainfall',
style: {
color: Highcharts.getOptions().colors[0]
}
},
labels: {
format: '{value} mm',
style: {
color: Highcharts.getOptions().colors[0]
}
}
}, { // Tertiary yAxis
gridLineWidth: 0,
title: {
text: 'Sea-Level Pressure',
style: {
color: Highcharts.getOptions().colors[1]
}
},
labels: {
format: '{value} mb',
style: {
color: Highcharts.getOptions().colors[1]
}
},
opposite: true
}],
tooltip: {
shared: true
},
series: [{
name: 'Rainfall',
type: 'column',
yAxis: 1,
data: [067416, 056716, 1554.9, 15546.5, 10412.3, 1009.5, 991009.6, 1010.2, 14563.1, 1016.9, 1018.2, 1016.7],
tooltip: {
valueSuffix: ' mm'
}
}, {
name: 'Sea-Level Pressure',
type: 'spline',
yAxis: 2,
data: [967416, 056716, 1554.9, 15546.5, 10412.3, 1009.5, 1009.6, 1010.2, 14563.1, 991016.9, 1018.2, 1016.7],
marker: {
enabled: false
},
dashStyle: 'shortdot',
tooltip: {
valueSuffix: ' mb'
}
}, {
name: 'Temperature',
type: 'spline',
data: [067416, 056716, 1554.9, 15546.5, 10412.3, 1009.5, 1009.6, 991010.2, 14563.1, 1016.9, 1018.2, 1016.7],
tooltip: {
valueSuffix: ' °C'
}
}]
});
});
Fiddle here: http://jsfiddle.net/voqxkb3m/1/
You can set alignTicks: false to your chart properties.
Updated fiddle:
http://jsfiddle.net/jlbriggs/voqxkb3m/2/
You can adjust settings like maxPadding and endOnTick to further reduce the yAxis spacing.
http://jsfiddle.net/jlbriggs/voqxkb3m/3/

Highcharts Stacked Bar: How to remove spacing between bars

I'm working with the highcharts stacked bar and want to remove some space between the bars so I can make room for some text I am rendering.
I've tried pointPadding and groupPadding but those are not working. I've tried minPadding/maxPadding on the xAxis and that did not do anything as well.
Seems the only way get rid of that space is to change the width of the whole chart which is what I really don't want.
Here is my fiddle:
http://jsfiddle.net/nick1572/dfcysj39/
$("#profit-chart").highcharts({
lang: {
thousandsSep: ","
},
chart: {
type: "column",
style: {
fontFamily: "Open Sans, sans-serif"
}
},
legend: {
enabled: false
},
title: {
text: ""
},
xAxis: {
//minPadding: 20, Not working here
//maxPadding:1, Not working here either
categories: [ "other business", "somekind of business profit" ],
labels: {
style: {
color: "#333333",
fontSize: "15px"
}
}
},
yAxis: {
gridLineDashStyle: "longdash",
title: {
text: "Dollars"
},
labels: {
enabled: true,
formatter: function() {
return "$" + Highcharts.numberFormat(this.value, 0, "", ",");
}
}
},
tooltip: {
enabled: false
},
plotOptions: {
column: {
stacking: "normal",
dataLabels: {
enabled: true,
color: "white",
inside: true,
useHTML: true,
style: {
fontSize: "18px",
fontWeight: "600"
}
}
},
series: {
//pointPadding: 0,
//groupPadding: 0, this does not work
animation: {
complete: function() {
}
},
pointWidth: 145
}
},
series: [ {
color: "#327631",
data: [ 0, 850 ],
stack: "female",
dataLabels: {
enabled: true,
formatter: function() {
if (0 != this.y) return "$" + Highcharts.numberFormat(this.y, 0);
else return null;
}
}
}, {
color: "#ADC9AD ",
data: [ 10000, 10000 ],
stack: "female",
dataLabels: {
enabled: true,
formatter: function() {
return "$" + Highcharts.numberFormat(this.y, 0);
}
}
}]
},
function (chart) { // on complete
chart.renderer.text('<span class="bracketed">}</span> <em>Profit</em>', 870, 85)
.css({
color: 'green',
fontSize: '24px',
x: 200
})
.add();
});//End HighCharts Call
Thanks in advance!
it seems that the point width setting is conflicting with the groupPadding and pointPadding .. if you remove the pointWidth: 145 in the series and add groupPadding and pointPadding to the column you will get the gap removed. on the flipside however the bars will take up the entire available width of your chart.
column: {
stacking: "normal",
groupPadding: 0,//add here
pointPadding: 0,//add here
...
}
http://jsfiddle.net/dfcysj39/9/
Ok so I think this may solve my problem at this point. Not sure if its the right way but it works. The pointPadding and groupPadding partially worked for this particular problem. I thank you Birgit for taking the time to respond!!!
What I did to solve this is explicitly put a spacingRight on the chart to give the plot area more space w/o making the bars wider. I did go back and add some pointPadding to make the cols a little wider for aesthetics. This allowed for the rendered text to show.
http://jsfiddle.net/nick1572/dfcysj39/
$("#profit-chart").highcharts({
lang: {
thousandsSep: ","
},
chart: {
spacingRight: 220,
type: "column",
width: 1200,
style: {
fontFamily: "Open Sans, sans-serif"
}
},
legend: {
enabled: false
},
title: {
text: ""
},
xAxis: {
//minPadding: 20, Not working here
//maxPadding:1, Not working here either
categories: [ "other business", "somekind of business profit" ],
labels: {
style: {
color: "#333333",
fontSize: "15px"
}
}
},
yAxis: {
gridLineDashStyle: "longdash",
title: {
text: "Dollars"
},
labels: {
enabled: true,
formatter: function() {
return "$" + Highcharts.numberFormat(this.value, 0, "", ",");
}
}
},
tooltip: {
enabled: false
},
plotOptions: {
column: {
stacking: "normal",
dataLabels: {
enabled: true,
color: "white",
inside: true,
useHTML: true,
style: {
fontSize: "18px",
fontWeight: "600"
}
}
},
series: {
pointPadding: 0.05,
//groupPadding: 0, this does not work
animation: {
complete: function() {
}
}//,
//pointWidth: 145
}
},
series: [ {
color: "#327631",
data: [ 0, 850 ],
stack: "female",
dataLabels: {
enabled: true,
formatter: function() {
if (0 != this.y) return "$" + Highcharts.numberFormat(this.y, 0);
else return null;
}
}
}, {
color: "#ADC9AD ",
data: [ 10000, 10000 ],
stack: "female",
dataLabels: {
enabled: true,
formatter: function() {
return "$" + Highcharts.numberFormat(this.y, 0);
}
}
}]
},
function (chart) { // on complete
chart.renderer.text('<span class="bracketed">}</span> <em>Profit</em>', 900, 84)
.css({
color: 'green',
fontSize: '24px',
x: 200
})
.add();
});//End HighCharts Call

Resources