How to display months in x axis labels in highcharts - highcharts
PROBLEM
I have data coming for x axis as xAxisLabel ['Jul 16', 'Oct 16', 'Jan 17', 'May 17', 'Jul 17', 'Oct 17', 'Jan 18', 'Apr 18','Aug 20','Sep 20'] whose length may vary. I have to display only 8 labels on the x-axis whatever the length of data may be i.e skip interval will be decided by dividing length of data by 8.
Solutions TRIED
I tried using tickPositioner to pass only 8 dates. But somehow, it is not getting displayed.
xAxis: {
tickPositioner: function() {
let dataDisplay=[]
let xAxisLabel=['Jul 16', 'Oct 16', 'Jan 17', 'May 17', 'Jul 17', 'Oct 17', 'Jan 18', 'Apr
18','Aug 20','Sep 20'];
let steps = xAxisLabel.length/8
for(let i=0,k=0;k<8;i+=steps,k++){
dataDisplay[k] = xAxisLabel[i]
}
return dataDisplay;
},
labels:{
enabled:true,
formatter: function(){
return this.value;
}
},
},
Can anyone help in achieving the desired result?
Here is the fiddle
https://jsfiddle.net/harshita_97/sd3trebz/20/
I have a full example here (any questions let me know).
https://jsfiddle.net/alexander_L/6p1nk73y/14/
Also, here in a Stack Overflow Snippet:
let xAxisLabel=['Jul 16', 'Oct 16', 'Jan 17', 'May 17', 'Jul 17', 'Oct 17', 'Jan 18', 'Apr 18','Aug 20','Sep 20', 'Oct 20', 'Dec 20'];
let yAxisData = [29.9, 71.5, 106.4, 129.2, 144.0, 176.0, 135.6, 148.5, 216.4, 194.1, 95.6, 54.4];
//const newDate = new Date('Jul 1 16'); //test it parses data correctly
const myData = xAxisLabel.map((child,index) => {
const modString = child.replace(/ /, " 1 ");
const newDate = new Date(modString);
const month = newDate.getMonth();
const year = newDate.getFullYear();
return [Date.UTC(year, month, 1),yAxisData[index]]
});
//console.log(myData)
Highcharts.chart('container', {
chart: {
type: 'spline'
},
title: {
text: 'Example data'
},
subtitle: {
text: 'Irregular time data in Highcharts JS'
},
xAxis: {
type: 'datetime',
dateTimeLabelFormats: { // do display the year
month: '%b %y',
year: '%Y'
},
title: {
text: 'Date'
},
/*tickInterval: 1*/
tickPixelInterval: 100
},
yAxis: {
title: {
text: 'Some Example Values'
},
min: 0
},
series: [{
name: "my Example Data",
data: myData
}],
plotOptions: {
series: {
marker: {
enabled: true
}
}
},
});
<script src="https://code.highcharts.com/highcharts.js"></script>
<div id="container" style="height: 400px; width: 500px"></div>
Output:
Important is to parse your strings to real dates and then add them in the data as a 2D array.
this is done with the .map(), essentially zipping the two arrays together:
let xAxisLabel=['Jul 16', 'Oct 16', 'Jan 17', 'May 17', 'Jul 17', 'Oct 17', 'Jan 18', 'Apr 18','Aug 20','Sep 20', 'Oct 20', 'Dec 20'];
let yAxisData = [29.9, 71.5, 106.4, 129.2, 144.0, 176.0, 135.6, 148.5, 216.4, 194.1, 95.6, 54.4];
//const newDate = new Date('Jul 1 16'); //test it parses data correctly
const myData = xAxisLabel.map((child,index) => {
const modString = child.replace(/ /, " 1 ");
const newDate = new Date(modString);
const month = newDate.getMonth();
const year = newDate.getFullYear();
return [Date.UTC(year, month, 1),yAxisData[index]]
});
Essentially giving this data structure:
[
[1467331200000, 29.9]
[1475280000000, 71.5]
[1483228800000, 106.4]
[1493596800000, 129.2]
[1498867200000, 144]
[1506816000000, 176]
[1514764800000, 135.6]
[1522540800000, 148.5]
[1596240000000, 216.4]
[1598918400000, 194.1]
[1601510400000, 95.6]
[1606780800000, 54.4]
]
Also to modify the ticks:
And to modify the ticks, you can modify like this (with tickPixelInterval: 60 etc.):
xAxis: {
type: 'datetime',
dateTimeLabelFormats: { // don't display the dummy year
month: '%b',
year: '%Y'
},
title: {
text: 'Date'
},
/* tickInterval: 1 */
tickPixelInterval: 60
},
Then we get:
Or with tickPixelInterval: 20 we get:
To modify the ticks further you can try this:
xAxis: {
type: 'datetime',
dateTimeLabelFormats: { // do display the year
month: '%b %y',
year: '%Y'
},
title: {
text: 'Date'
},
/*tickInterval: 1*/
tickPixelInterval: 10
}
Then the output also includes the year in the month view like so:
Example:
let xAxisLabel=['Jul 16', 'Oct 16', 'Jan 17', 'May 17', 'Jul 17', 'Oct 17', 'Jan 18', 'Apr 18','Aug 20','Sep 20', 'Oct 20', 'Dec 20'];
let yAxisData = [29.9, 71.5, 106.4, 129.2, 144.0, 176.0, 135.6, 148.5, 216.4, 194.1, 95.6, 54.4];
//const newDate = new Date('Jul 1 16'); //test it parses data correctly
const myData = xAxisLabel.map((child,index) => {
const modString = child.replace(/ /, " 1 ");
const newDate = new Date(modString);
const month = newDate.getMonth();
const year = newDate.getFullYear();
return [Date.UTC(year, month, 1),yAxisData[index]]
});
//console.log(myData)
Highcharts.chart('container', {
chart: {
type: 'spline'
},
title: {
text: 'Example data'
},
subtitle: {
text: 'Irregular time data in Highcharts JS'
},
xAxis: {
type: 'datetime',
dateTimeLabelFormats: { // do display the year
month: '%b %y',
year: '%Y'
},
title: {
text: 'Date'
},
/*tickInterval: 1*/
tickPixelInterval: 10
},
yAxis: {
title: {
text: 'Some Example Values'
},
min: 0
},
series: [{
name: "my Example Data",
data: myData
}],
plotOptions: {
series: {
marker: {
enabled: true
}
}
},
});
<script src="https://code.highcharts.com/highcharts.js"></script>
<div id="container" style="height: 400px; width: 500px"></div>
or demo in jsfiddle: https://jsfiddle.net/alexander_L/6p1nk73y/20/
Related
How to decrease the chart opacity in highcharts
I am trying to decrease the opacity of charts instead of hiding when click the legend. Example: When i click the Tokyo legend series blue color column hiding instead of hiding I want to decrease the opacity to 0.34 of blue color column. same for other legend when clicking. So, How to do it? Highcharts.chart('container', { chart: { type: 'column' }, title: { text: 'Monthly Average Rainfall' }, subtitle: { text: 'Source: WorldClimate.com' }, xAxis: { categories: [ 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec' ], crosshair: true }, yAxis: { min: 0, title: { text: 'Rainfall (mm)' } }, tooltip: { headerFormat: '<span style="font-size:10px">{point.key}</span><table>', pointFormat: '<tr><td style="color:{series.color};padding:0">{series.name}: </td>' + '<td style="padding:0"><b>{point.y:.1f} mm</b></td></tr>', footerFormat: '</table>', shared: true, useHTML: true }, plotOptions: { column: { pointPadding: 0.2, borderWidth: 0 }, series: { events: { legendItemClick: function(event) { var visibility = this.visible ? '0.35' : ''; alert(event); } } }, }, series: [{ name: 'Tokyo', data: [49.9, 71.5, 106.4, 129.2, 144.0, 176.0, 135.6, 148.5, 216.4, 194.1, 95.6, 54.4] }, { name: 'New York', data: [83.6, 78.8, 98.5, 93.4, 106.0, 84.5, 105.0, 104.3, 91.2, 83.5, 106.6, 92.3] }, { name: 'London', data: [48.9, 38.8, 39.3, 41.4, 47.0, 48.3, 59.0, 59.6, 52.4, 65.2, 59.3, 51.2] }, { name: 'Berlin', data: [42.4, 33.2, 34.5, 39.7, 52.6, 75.5, 57.4, 60.4, 47.6, 39.1, 46.8, 51.1] }] }); https://www.highcharts.com/demo/column-basic Demo: https://jsfiddle.net/4t6arfg8/
You used the correct event, the remaining thing is to change opacity of the proper SVG element and colorize the legend item. series: { events: { legendItemClick: function(e) { const series = this; const chart = series.chart; const hasChangedOpacity = series.hasChangedOpacity; series.group.attr({ opacity: hasChangedOpacity ? 1 : 0.2 }); chart.legend.colorizeItem(series, hasChangedOpacity); series.hasChangedOpacity = !hasChangedOpacity; // prevent default behaviour return false; } } } Also, to keep the default series states behaviour, you need to add the below small plugin: (function(H) { H.wrap(H.Series.prototype, 'setState', function(proceed) { if (!this.hasChangedOpacity) { proceed.apply(this, Array.prototype.slice.call(arguments, 1)); } }); }(Highcharts)); Live demo: https://jsfiddle.net/BlackLabel/p1x6937f/ API Reference: https://api.highcharts.com/highcharts/series.column.events.legendItemClick Docs: https://www.highcharts.com/docs/extending-highcharts/extending-highcharts
What methods can I use to achieve this shaping of the x-axis?
I would like to make a graph like this: example image Highcharts.chart('container', { chart: { type: 'column', width: 780 }, legend: { enabled: false }, tooltip: { headerFormat: '<span style="font-size:10px">{point.key}</span><table>', pointFormat: '<tr><td style="color:{series.color};padding:0">{series.name}: </td>' + '<td style="padding:0"><b>{point.y:.1f} mm</b></td></tr>', footerFormat: '</table>', shared: true, useHTML: true }, plotOptions: { series: { pointPadding: 0.1, groupPadding: 0.1, pointWodth: 20 }, column: { borderRadius: '2px' } }, series: [{ color: 'orange', name: 'Приход', data: [49.9, 71.5, 106.4, 129.2, 144.0, 176.0, 135.6, 148.5, 216.4, 194.1, 95.6, 54.4] }, { color: 'blue', name: 'Расход', data: [83.6, 78.8, 98.5, 93.4, 106.0, 84.5, 105.0, 104.3, 91.2, 83.5, 106.6, 92.3] }] }); But I don't know how I can plot the X-axis like in the screenshot, I would appreciate your help, thanks)
Step 1. You can set the categories in the x-axis. If you put a comma after the last category description, it automatically creates the pattern. Step 2. Set the tickwidth to 1 in the x-axis. xAxis: { categories: ["0", "1", "2",], tickWidth: 1 },
For the second requirement (https://jsfiddle.net/r1d6tozq/29/), add pointPlacement: 'between' to the plotOptions as follows: plotOptions: { column: { shadow: false, pointPlacement: 'between' } }
how can Click on small figures in highcharts
I has series like this series: [{ data: [29.9, 71.5, 106.4, 129.2, 144.0, 176.0, 135.6, 148.5, 216.4, 19552.1, 95.6, 54.4] }] you can see that I has small points (29.9, 71.5) in this series and other (19552.1) has larger than small point that make I cant click in small point how can I deal with this problem you can see this jsfiddle to know what I mean
Or You could set a minimum length for the columns using the ´minPointLength´ option http://api.highcharts.com/highcharts#plotOptions.column.events.click minPointLength: 0, jsfiddle
how about to use logarithmic yAxis http://jsfiddle.net/xLcmojzr/2/ $(function () { $('#container').highcharts({ title: { text: 'Logarithmic axis demo' }, xAxis: { tickInterval: 1 }, yAxis: { type: 'logarithmic', minorTickInterval: 0.1 }, tooltip: { headerFormat: '<b>{series.name}</b><br />', pointFormat: 'x = {point.x}, y = {point.y}' }, series: [{ data: [1, 2, 4, 8, 16, 32, 64, 128, 256, 512], pointStart: 1 }] }); });
Is it possible to center the labels and data points between the ticks in a datetime xAxis for Highcharts?
I have a fiddle http://jsfiddle.net/adaykin/QKw77/ which uses the datetime type for the xAxis. As you can see the data points in the line and the labels are on the tick marks and not between them. How can I change both the data points and the labels so that they are centered within a tick mark? var chart = new Highcharts.Chart({ chart: { renderTo: 'container' }, xAxis: { type: 'datetime', labels: { format: "{value:%Y-%m-%d}", }, tickmarkPlacement: 'between' }, yAxis: { }, series: [ { data: [29.9, 71.5, 106.4, 129.2, 144.0, 176.0, 135.6], pointStart: Date.UTC(2014, 1, 3), pointInterval: 24 * 3600 * 1000 * 7 // one day }, { data: [50.0, 50.0, 50.0, 50.0, 50.0, 50.0, 50.0], pointStart: Date.UTC(2014, 1, 3), pointInterval: 24 * 3600 * 1000 * 7 } ] }); The data points and labels are centered between the tick marks when the xAxis is not using the datetime type.
You can use startOfWeek xAxis: { startOfWeek: 5, type: 'datetime', labels: { format: "{value:%Y-%m-%d}", }, }, http://jsfiddle.net/QKw77/2/
Dual x-axis on same line with Highcharts. Possible?
Can we create multiple x-axis chart like this using Highcharts? If yes, can someone please provide some pointers? There are three year data displayed. i.e. 2010,2011, 2012 https://www.adr.com/DRDetails/CorporateActions?cusip=055622104
The older answers didn't run for me in JSFiddle. Here is a working example, if it helps anyone: http://jsfiddle.net/kPqKW/ $(function () { $('#container').highcharts({ chart: { renderTo: 'container' }, xAxis: [{ type: 'datetime' }, { type: 'datetime', opposite: true }], series: [{ data: [29.9, 71.5, 106.4, 129.2, 144.0, 176.0, 135.6, 148.5, 216.4, 194.1, 95.6, 54.4], pointStart: Date.UTC(2010, 0, 1), pointInterval: 24 * 3600 * 1000 // one day }, { data: [176.0, 135.6, 148.5, 216.4, 194.1, 95.6, 54.4, 29.9, 71.5, 106.4, 129.2, 144.0], pointStart: Date.UTC(2010, 0, 10), pointInterval: 24 * 3600 * 1000, // one day xAxis: 1 }] }); });
Using highstocks (highcharts lesser known sibling), you can do something like what you're looking for. Check out this fiddle $(function() { $.getJSON('http://www.highcharts.com/samples/data/jsonp.php?filename=aapl-ohlcv.json&callback=?', function(data) { // split the data set into ohlc and volume var ohlc = [], volume = [], dataLength = data.length; for (i = 0; i < dataLength; i++) { ohlc.push([ data[i][0], // the date data[i][1], // open data[i][2], // high data[i][3], // low data[i][4] // close ]); volume.push([ data[i][0], // the date data[i][5] // the volume ]) } // set the allowed units for data grouping var groupingUnits = [[ 'week', // unit name [1] // allowed multiples ], [ 'month', [1, 2, 3, 4, 6] ]]; // create the chart $('#container').highcharts('StockChart', { rangeSelector: { selected: 1 }, title: { text: 'AAPL Historical' }, yAxis: [{ title: { text: 'OHLC' }, height: 200, lineWidth: 2 }, { title: { text: 'Volume' }, top: 300, height: 100, offset: 0, lineWidth: 2 }], series: [{ type: 'area', name: 'AAPL', data: ohlc, dataGrouping: { units: groupingUnits } }, { type: 'area', name: 'Volume', data: volume, yAxis: 1, dataGrouping: { units: groupingUnits } }] }); }); });
You can simply use Highcharts grouped categories plugin, take a look: https://github.com/blacklabel/grouped_categories
It seems possible, but in different view. one axis at the top and other one is at the bottom. You've set opposite to true as we do for y - axis. Have a look at this question in Highcharts forum <div id="container" style="height: 400px; width: 500px"></div> <script type="text/javascript"> var chart = new Highcharts.Chart({ chart: { renderTo: 'container' }, xAxis: [{ type: 'datetime', },{ type: 'datetime', opposite: true }], series: [{ data: [29.9, 71.5, 106.4, 129.2, 144.0, 176.0, 135.6, 148.5, 216.4, 194.1, 95.6, 54.4], pointStart: Date.UTC(2010, 0, 1), pointInterval: 24 * 3600 * 1000 // one day },{ data: [176.0, 135.6, 148.5, 216.4, 194.1, 95.6, 54.4, 29.9, 71.5, 106.4, 129.2, 144.0], pointStart: Date.UTC(2010, 0, 10), pointInterval: 24 * 3600 * 1000, // one day xAxis: 1 }] }); </script> for working example, look at this jsfilddle
you may go through this fiddle https://jsfiddle.net/ajaytripathi10/z8mrwhxz/. Hope this will help. $(function () { $('#container').highcharts({ title: { text: 'Shared tooltip chart' }, xAxis: [{ type: 'datetime', id: 'x1' }, { type: 'datetime', id: 'x2', opposite: true }], yAxis: { min: 0 }, tooltip: { shared: true, useHTML: true, formatter: function () { var tooltip = ''; var i, len; tooltip += '<small>Apple</small>'; tooltip += '<table>'; for (i = 0, len = 4; i < len; i++) { if(this.points[i] != undefined){ if(this.points[i].series.name.indexOf('Apple') >= 0){ tooltip += '<tr>'; tooltip += '<td style="color: ' + this.points[i].series.color + '">\u25CF</td>'; tooltip += '<td>' + Highcharts.dateFormat('%Y-%m-%d', this.points[i].x) + ':</td>'; tooltip += '<td style="text-align: right"><b>' + this.points[i].y + '</b></td>'; tooltip += '</tr>'; } } } tooltip += '</table>'; tooltip += '<small>Mango</small>'; tooltip += '<table>'; for (i = 0, len = 4; i < len; i++) { if(this.points[i] != undefined){ if(this.points[i].series.name.indexOf('Mango') >= 0){ tooltip += '<tr>'; tooltip += '<td style="color: ' + this.points[i].series.color + '">\u25CF</td>'; tooltip += '<td>' + Highcharts.dateFormat('%Y-%m-%d', this.points[i].x) + ':</td>'; tooltip += '<td style="text-align: right"><b>' + this.points[i].y + '</b></td>'; tooltip += '</tr>'; } } } tooltip += '</table>'; return tooltip; } }, series: [{ name: 'Apple', id: 'series1', xAxis: 'x1', color: 'rgba(255, 0, 0, 1.0)', data: [5, 3, 4, 7, 6, 1, 0], pointInterval: 1000 * 60 * 60 * 24, pointStart: Date.UTC(2015, 2, 10) }, { name: 'Apple New', id: 'series2', //linkedTo: 'series1', xAxis: 'x2', color: 'rgba(255, 180, 180, 1.0)', data: [4, 5, 5, 6, 1, 3, 4], pointInterval: 1000 * 60 * 60 * 24, pointStart: Date.UTC(2015, 2, 17) },{ name: 'Mango', id: 'series3', xAxis: 'x1', color: 'rgba(255, 0, 0, 1.0)', data: [15, 13, 14, 17, 16, 11, 10], pointInterval: 1000 * 60 * 60 * 24, pointStart: Date.UTC(2015, 2, 10) }, { name: 'Mango New', id: 'series4', //linkedTo: 'series1', xAxis: 'x2', color: 'rgba(255, 180, 180, 1.0)', data: [14, 15, 15, 16, 11, 13, 14], pointInterval: 1000 * 60 * 60 * 24, pointStart: Date.UTC(2015, 2, 17) }] }); });
Complete code for making horizontal and vertical line in column,piechart highcharts <!DOCTYPE html> <html> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> <script src="https://code.highcharts.com/highcharts.js"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script> <script src="https://code.highcharts.com/modules/exporting.js"></script> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"> </head> <body> <div id="container" class="col-md-4" style="min-width: 310px; height: 400px; margin: 0 auto"></div> <div class="col-md-8"> <div id="container2" class="col-md-6" style="min-width: 310px; height: 400px; margin: 0 auto" class="col-md-6"></div><div id="container3" class="col-md-6" style="min-width: 310px; height: 400px; margin: 0 auto" class="col-md-6" class="col-md-6">dddd</div> </div> <script> Highcharts.chart('container', { chart: { type: 'column', }, title: { text: 'sample charts for both horizontal and vertical line' }, subtitle: { text: 'Source: WorldClimate.com' }, xAxis: { gridLineWidth: 1, minPadding: 2, maxPadding: 2, maxZoom: 6 , categories: ['Jan', 'Feb', 'Apr', 'May', 'Jun', 'Dec'] }, yAxis: { title: { text: 'Temperature' }, labels: { formatter: function () { return '$'+this.value; } }, }, tooltip: { crosshairs: true, shared: true }, plotOptions: { spline: { marker: { radius: 4, lineColor: '#666666', lineWidth: 1 } } }, series: [{ // name: '', data: [7.0, 6.9, 9.5, 14.5, 18.2, 21.5] }] }); Highcharts.chart('container2', { chart: { plotBackgroundColor: null, plotBorderWidth: null, plotShadow: false, type: 'pie' }, title: { text: 'Browser market shares in January, 2018' }, tooltip: { pointFormat: '{series.name}: <b>{point.percentage:.1f}%</b>' }, plotOptions: { pie: { allowPointSelect: true, cursor: 'pointer', dataLabels: { enabled: true, format: '<b>{point.name}</b>: {point.percentage:.1f} %', style: { color: (Highcharts.theme && Highcharts.theme.contrastTextColor) || 'black' } } } }, series: [{ name: 'Brands', colorByPoint: true, data: [{ name: 'Chrome', y: 61.41, sliced: true, selected: true }, { name: 'Internet Explorer', y: 11.84 }, { name: 'Firefox', y: 10.85 }, { name: 'Edge', y: 4.67 }, { name: 'Safari', y: 4.18 }, { name: 'Sogou Explorer', y: 1.64 }, { name: 'Opera', y: 1.6 }, { name: 'QQ', y: 1.2 }, { name: 'Other', y: 2.61 }] }] }); Highcharts.chart('container3', { chart: { plotBackgroundColor: null, plotBorderWidth: null, plotShadow: false, type: 'pie' }, title: { text: 'Browser market shares in January, 2018' }, tooltip: { pointFormat: '{series.name}: <b>{point.percentage:.1f}%</b>' }, plotOptions: { pie: { allowPointSelect: true, cursor: 'pointer', dataLabels: { enabled: true, format: '<b>{point.name}</b>: {point.percentage:.1f} %', style: { color: (Highcharts.theme && Highcharts.theme.contrastTextColor) || 'black' } } } }, series: [{ name: 'Brands', colorByPoint: true, data: [{ name: 'Chrome', y: 61.41, sliced: true, selected: true }, { name: 'Internet Explorer', y: 11.84 }, { name: 'Firefox', y: 10.85 }, { name: 'Edge', y: 4.67 }, { name: 'Safari', y: 4.18 }, { name: 'Sogou Explorer', y: 1.64 }, { name: 'Opera', y: 1.6 }, { name: 'QQ', y: 1.2 }, { name: 'Other', y: 2.61 }] }] }); </script> </body> </html>