Dates instead of values on Highchart labels in graph with multiple axis - highcharts
I've created a line and a bar graph that share the xAxis. But for some reason it chooses to display the value instead of date, even though I am supplying it the standard way.
What am I missing here?
Fiddle: http://jsfiddle.net/ZfP84/
$(function () {
var options = {
chart: {
renderTo: 'container',
},
navigator:{
enabled:true
},
scrollbar: {
enabled:true
},
rangeSelector: {
enabled: true,
//selected: 1
},
xAxis: {
gridLineWidth: 1,
labels: {
rotation: -45,
align: 'right',
style: {
fontSize: '13px',
fontFamily: 'Verdana, sans-serif'
}
}
},
yAxis: [
{
height: 150,
lineWidth: 2,
offset: 0,
title: {
text: 'Price',
}
},
{
top: 225,
//lineWidth: 0,
//min: 0,
//max: 5,
gridLineWidth: 0,
offset: 0,
height: 100,
title: {
text: 'Volume',
}
},
],
series: [
{
yAxis: 0,
name: 'Price by time',
stack: 0,
//data: [1, 12, 32, 43],
data: [[1147651200000,67.79],[1147737600000,64.98],[1147824000000,65.26],[1147910400000,63.18],[1147996800000,64.51],[1148256000000,63.38],[1148342400000,63.15],[1148428800000,63.34],[1148515200000,64.33],[1148601600000,63.55],[1148947200000,61.22],[1149033600000,59.77],[1149120000000,62.17],[1149206400000,61.66],[1149465600000,60.00],[1149552000000,59.72],[1149638400000,58.56],[1149724800000,60.76],[1149811200000,59.24],[1150070400000,57.00],[1150156800000,58.33],[1150243200000,57.61],[1150329600000,59.38],[1150416000000,57.56],[1150675200000,57.20],[1150761600000,57.47],[1150848000000,57.86],[1150934400000,59.58],[1151020800000,58.83],[1151280000000,58.99],[1151366400000,57.43],[1151452800000,56.02],[1151539200000,58.97],[1151625600000,57.27],[1151884800000,57.95],[1152057600000,57.00],[1152144000000,55.77],[1152230400000,55.40],[1152489600000,55.00],[1152576000000,55.65],[1152662400000,52.96],[1152748800000,52.25],[1152835200000,50.67],[1153094400000,52.37],[1153180800000,52.90],[1153267200000,54.10],[1153353600000,60.50],[1153440000000,60.72],[1153699200000,61.42],[1153785600000,61.93],[1153872000000,63.87],[1153958400000,63.40],[1154044800000,65.59],[1154304000000,67.96],[1154390400000,67.18],[1154476800000,68.16],[1154563200000,69.59],[1154649600000,68.30],[1154908800000,67.21],[1154995200000,64.78],[1155081600000,63.59],[1155168000000,64.07],[1155254400000,63.65],[1155513600000,63.94],[1155600000000,66.45],[1155686400000,67.98],[1155772800000,67.59],[1155859200000,67.91],[1156118400000,66.56],[1156204800000,67.62],[1156291200000,67.31],[1156377600000,67.81],[1156464000000,68.75],[1156723200000,66.98],[1156809600000,66.48],[1156896000000,66.96],[1156982400000,67.85],],
tooltip: {
valueDecimals: 2
},
},
{
name: 'Volume by time',
yAxis: 1,
stack: 0,
data: [[1147651200000,67.79],[1147737600000,64.98],[1147824000000,65.26],[1147910400000,63.18],[1147996800000,64.51],[1148256000000,63.38],[1148342400000,63.15],[1148428800000,63.34],[1148515200000,64.33],[1148601600000,63.55],[1148947200000,61.22],[1149033600000,59.77],[1149120000000,62.17],[1149206400000,61.66],[1149465600000,60.00],[1149552000000,59.72],[1149638400000,58.56],[1149724800000,60.76],[1149811200000,59.24],[1150070400000,57.00],[1150156800000,58.33],[1150243200000,57.61],[1150329600000,59.38],[1150416000000,57.56],[1150675200000,57.20],[1150761600000,57.47],[1150848000000,57.86],[1150934400000,59.58],[1151020800000,58.83],[1151280000000,58.99],[1151366400000,57.43],[1151452800000,56.02],[1151539200000,58.97],[1151625600000,57.27],[1151884800000,57.95],[1152057600000,57.00],[1152144000000,55.77],[1152230400000,55.40],[1152489600000,55.00],[1152576000000,55.65],[1152662400000,52.96],[1152748800000,52.25],[1152835200000,50.67],[1153094400000,52.37],[1153180800000,52.90],[1153267200000,54.10],[1153353600000,60.50],[1153440000000,60.72],[1153699200000,61.42],[1153785600000,61.93],[1153872000000,63.87],[1153958400000,63.40],[1154044800000,65.59],[1154304000000,67.96],[1154390400000,67.18],[1154476800000,68.16],[1154563200000,69.59],[1154649600000,68.30],[1154908800000,67.21],[1154995200000,64.78],[1155081600000,63.59],[1155168000000,64.07],[1155254400000,63.65],[1155513600000,63.94],[1155600000000,66.45],[1155686400000,67.98],[1155772800000,67.59],[1155859200000,67.91],[1156118400000,66.56],[1156204800000,67.62],[1156291200000,67.31],[1156377600000,67.81],[1156464000000,68.75],[1156723200000,66.98],[1156809600000,66.48],[1156896000000,66.96],[1156982400000,67.85],],
tooltip: {
valueDecimals: 2
},
lineWidth: 3,
marker: {
enabled: false
},
type: 'column',
},
]
};
var chart = new Highcharts.Chart(options);
});
If you added the the "type" to the xAxis definition then the x values will be interpreted as dates and times rather than decimal values
xAxis: {
type : "datetime", //add this line
gridLineWidth: 1,
labels: {
rotation: -45,
align: 'right',
style: {
fontSize: '13px',
fontFamily: 'Verdana, sans-serif'
}
}
},
you may have to fiddle with the start times and intervals to get HighChart to correctly interpret your x-values.
see this demo on the highchart website as an example. http://jsfiddle.net/gh/get/jquery/1.9.1/highslide-software/highcharts.com/tree/master/samples/highcharts/demo/line-time-series/
The reason is that you are not actually creating a Stock chart. Your code looks like:
var chart = new Highcharts.Chart(options);
If you want it to be a Stock chart do:
var chart = new Highcharts.StockChart(options);
A Chart by default is a category chart. StockChart is time-based.
You can use dateFormat to replace time in miliseconds with date.
http://jsfiddle.net/3bE4X/
formatter:function(){
return Highcharts.dateFormat('%d / %m / %Y',this.value);
}
http://api.highcharts.com/highcharts#Highcharts.dateFormat()
Related
Spline line is very curved, how to smooth it?
A smoother line is needed (the line should rise smoothly and evenly), but the values should not change. Is it possible to build a more straight line without changing the values? Screen with explanations: http://prntscr.com/qudf96 And my code: <script> function reDrawCalc(gdata, gcurr, nums = 2) { Highcharts.chart('container', { chart: { height: 400, type: 'area', margin: [20, 0, 20, 0] }, title: { text: null }, subtitle: { text: null }, exporting: { enabled: false }, xAxis: { gridLineWidth: 1, categories: ['One', 'Two', 'Three', 'Four', 'Five'] }, yAxis: { gridLineWidth: 0, }, tooltip: { enabled: false, crosshairs: true }, plotOptions: { series: { dataLabels: { enabled: true, useHTML: true, inside: false, style: { fontFamily: 'Rubik, sans-serif', textTransform: 'uppercase', fontSize: 'none', fontWeight: 'normal', textShadow: 'none' }, formatter: function() { return '<div class="chitem-time">'+ this.x+'</div>' +'<div class="chitem-val">'+ Highcharts.numberFormat(this.y,nums)+'<sup>'+gcurr+'</sup></div>'; } } } }, series: [{ type: 'areaspline', data: gdata, lineWidth: 5, color: '#f0c997', fillColor:'transparent' }], responsive: { rules: [{ condition: { maxWidth: 500 }, chartOptions: { chart: { height: 350, type: 'area', margin: [20, 0, 20, 0] }, series: [{ type: 'areaspline', data: gdata, lineWidth: 3, color: '#f0c997', fillColor:'transparent' }], } }] } }); } </script> Is it possible to do it? And how to modify my code for it? Thank you very much in advance!
As I mentioned in the comment current line shape is at it is because of the points positions on the chart. If you don't need to keep points actual positions the solution which you can use is to use the dummy data for the points and use the correct one in the dataLabels. See: Demo: https://jsfiddle.net/BlackLabel/fshx3n50/ data: [{ y: 5, z: 0.20 }, { y: 10, z: 1.40 }, { y: 18, z: 6.20 }, { y: 30, z: 18.00 }, { y: 50, z: 73.00 }], I also changed the formatter function to display the z value in dataLabel: formatter: function() { return '<div class="chitem-time">' + this.x + '</div>' + '<div class="chitem-val">' + Highcharts.numberFormat(this.point.z) + '<sup>REM</sup></div>'; }
Highcharts mixed column/spline, wrong xaxis labels
I am trying to finalize a graph where I report the daily production within a month and a couple of other daily based series. The graph is always set at full scale, meaning that I always show all the days within a month, even if I do not have values for them, whichever is the reason why. On the first day of each month, I display the graph of the previous month, as a report. This is the jfiddle I've set up so that you can have an idea. The problem I've been fighting against is that, whatever I try to do, Highcharts simply ignores me and displays wrong labels on x axis. So the days start from 2, go throughout the whole month to the last and they end with 1. This is the code for my chart: $(function() { var chart = new Highcharts.Chart({ chart: { renderTo: 'tab-month-graph', zoomType: 'xy' }, title: { text: 'Daily - August 2015' }, xAxis: { type: 'datetime', tickInterval: 24 * 3600 * 1000, labels: { align: 'center', text: 'Giorni', style: { fontSize: '10px', fontFamily: 'Verdana, sans-serif' } }, dateTimeLabelFormats: { day: '%e' } }, yAxis: [{ // Primary yAxis title: { text: 'Produzione giornaliera', style: { color: Highcharts.getOptions().colors[1] } } }, { // Secondary yAxis title: { text: 'PR giornaliero', style: { color: Highcharts.getOptions().colors[0] } }, min: 0, startOnTick: false, opposite: true }], legend: { enabled: false }, series: [{ name: 'Prod.', type: 'column', data: [[1438466230000, 4603.36],[1438552630000, 4264.92],[1438639030000, 3108.05],[1438725430000, 2047.03],[1438811830000, 2270.39],[1438898230000, 2258.2],[1438984630000, 3971.95],[1439071030000, 3784.45],[1439157430000, 3674.53],[1439243830000, 3131.95],[1439330230000, 1963.13],[1439416630000, 3333.52],[1439503030000, 4161.8],[1439589430000, 4408.59],[1439675830000, 2968.83],[1439762230000, 2808.05],[1439848630000, 4439.77],[1439935020000, 3746.48],[1440021430000, 4980],[1440107830000, 3683.91],[1440194230000, 4480.78],[1440280630000, 4406.48],[1440367030000, 4518.98],[1440453430000, 4492.73],[1440539830000, 3924.14],[1440626230000, 4062.89],[1440712630000, 3225.47],[1440799030000, 3213.75],[1440885430000, 4631.95],[1440971830000, 4471.17],[1441058230000, 4223.91]], color: '#89CE7F', dataLabels: { enabled: true, rotation: -90, color: '#000000', align: 'right', x: 4, y: 10, style: { fontSize: '11px', fontFamily: 'Verdana, sans-serif', textShadow: '0 0 3px black' } } }, { name: 'PR', type: 'spline', yAxis: 1, lineWidth: 0, marker: { radius: 6 }, data: [[1438466230000, 73.36],[1438552630000, 75.08],[1438639030000, 57.54],[1438725430000, 38.85],[1438811830000, 39.14],[1438898230000, 63.78],[1438984630000, 76.03],[1439071030000, 75.78],[1439157430000, 77.61],[1439243830000, 79.74],[1439330230000, 77.35],[1439416630000, 73.98],[1439503030000, 75.79]], color: '#AA0000', tooltip: { valueSuffix: ' %' } }, { name: 'PR Cont.', type: 'spline', yAxis: 1, lineWidth: 0, marker: { radius: 5, symbol: 'circle' }, data: [[1438466230000, 78.84],[1438552630000, 79.85],[1438639030000, 75.16],[1438725430000, 59.15],[1440971830000, 78.5],[1441058230000, 78.45]], color: '#BBBB00', tooltip: { valueSuffix: ' %' } } ], tooltip: { xDateFormat: '%A, %d %B %Y', valueSuffix: ' kW', shared: true // LEGENDA CONDIVISA }, plotOptions: { series: { pointStart: Date.UTC(2015, 7, 1), pointRange: 24 * 3600 * 1000 } } }); }); I've read tons of posts on Stack Overflow, tried tons of suggestions, I've even tried this post tinyurl.com/psbzxeh on HighCharts official forum which is referred to a similar problem, but it did not solve my issue. Could someone point me out to the right solution?
Just to clarify things: columns look like translated by one day, because of that time (21:57:10 UTC) - which is almost another day (without UTC it's 23:57:10 for GMT+2:00). In general, if you have daily data, then it's better to use 00:00:00 as this is when exactly date starts. Manipulating JSON shouldn't be that hard, see: function modify(data) { var time; data.map(function(e) { time = new Date(e[0]); time.setUTCHours(0); time.setUTCMinutes(0); time.setUTCSeconds(0); e[0] = time.getTime(); return e; }); return data; } And: series: [{ name: 'Prod.', type: 'column', data: modify([ [1438466230000, 4603.36], ... ]) }] Demo: http://jsfiddle.net/p7vnqmt7/3/ Note: to remove extra tick at the last place on the axis, you can simply set xAxis.max. And to answer question in the comment: no, pointInterval shouldn't be used when you have data as x-y pair.
try below function above your highchart function and set xAxis min and max. Use very first timestamp(date) as min and last timestamp(date) as max value. $( function () { Highcharts.setOptions( { lang : { rangeSelectorZoom : ' ' }, global : { useUTC : false } } ); } );
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/
how to set the interval of points on Y - Axis highcharts
I am using highcharts for the first time, and I am trying to figure out how to set the Y axis points static. I have used min=0 and max=140 , and the points on y axis come up as 0,25,50,75,100,125 and 150. Wherein I want it as 0,20,40,60,80,100,140. Can someone let me know how could I achieve this. Below is the highchart optins : var chart1 = new Highcharts.Chart({ chart: { renderTo: 'Div1', width: 600, height: 400 }, yAxis:{ min: 0, max: 140, lineColor: '#FF0000', lineWidth: 1, title: { text: 'Values' }, plotLines: [{ value: 0, width: 10, color: '#808080' }] }, series: [{ name: 'Value', data: YaxisValuesArray }] }); });
You can set the tickInterval (http://api.highcharts.com/highstock#yAxis.tickInterval) on the axis http://jsfiddle.net/blaird/KdHME/ $(function () { var chart1 = new Highcharts.Chart({ chart: { renderTo: 'Div1', width: 600, height: 400 }, credits: { enabled: false }, title: { text: 'Productivity Report', x: -20 //center }, xAxis: { lineColor: '#FF0000', categories: [1, 2, 3] }, yAxis: { min: 0, max: 140, tickInterval: 20, lineColor: '#FF0000', lineWidth: 1, title: { text: 'Values' }, plotLines: [{ value: 0, width: 10, color: '#808080' }] }, tooltip: { valueSuffix: '' }, legend: { layout: 'vertical', align: 'right', verticalAlign: 'middle', borderWidth: 0 }, series: [{ name: 'Value', data: [ [1, 10], [2, 20], [3, 30] ] }] }); });
To do this using HighChart in a StockChart mode, I just need to set the property tickPixelInterval. yAxis: { ... tickPixelInterval: 35 ... }
grouping date in Highcharts if the date range is too big
I am using Highcharts to show some statistic for my customer but I have problem when the customer select long data range here is the first image for my highchart in the default view and if I select too long date range here is the result here is my code $(function () { var chart; $(document).ready(function() { chart = new Highcharts.Chart({ chart: { renderTo: 'container', type: 'line', marginRight: 50, marginBottom: 80, dataGrouping: { enabled: true } }, title: { text: 'Visits Statistics', x: -20 //center }, credits: { text: '', href: '' }, subtitle: { text: '', x: -20 }, xAxis: { categories: [<?php print $categories; ?>], labels: { rotation: -45, align: 'right', style: { fontSize: '10px', fontFamily: 'Verdana, sans-serif' } } }, yAxis: { title: { text: 'Visits' }, min: 0, plotLines: [{ value: 0, width: 1, color: '#808080' }] }, tooltip: { formatter: function() { return '<b>'+ this.x +' '+'</b><br/>'+ this.y +'Hit'; } }, legend: { layout: 'vertical', align: 'right', verticalAlign: 'top', x: -10, y: 10, borderWidth: 0 }, series: [{name:'from 2011-09-1',data: [<?php print $visits; ?>]}] }); }); });
Highcharts can automatically manage time values in the x-Axis, provided that your chart is configured correctly. The problem in your case is that you've told Highcharts to use your categories, and it shows all of the categories. To set up your chart to avoid this, you'll need to do two things: Set the x-Axis type to datetime Make sure that your data is formatted correctly Or, use pointStart and pointInterval if you can't mess around with the data. Using your example: // ... xAxis: { //remove categories and set type as 'datetime' type: 'datetime', labels: { rotation: -45, align: 'right', style: { fontSize: '10px', fontFamily: 'Verdana, sans-serif' } } }, // ... series: [{ name:'from 2011-09-1', // since you probably don't want to change your data, we leave it alone... data: [<?php print $visits; ?>], // ... and instead, set `pointStart` and `pointInterval` pointStart: Date.UTC(2011, 8, 1), // September 1, 2011 pointInterval: 24 * 3600 * 1000 // each point is 1 day (measured in milliseconds) }]