Highcharts x series group - highcharts

Help me guys. I have much data with 6 months interval. How I can group it in X with moth interval?
$(function () {
$('#container').highcharts({
xAxis: {
type: 'datetime'
},
plotOptions: {
spline: {
marker:{
enabled: false
}
}
},
series: [{
type: 'spline',
data: [
[Date.UTC(2010, 0, 1), 29.9],
[Date.UTC(2010, 0, 3), 29.9],
[Date.UTC(2010, 0, 10), 29.9],
[Date.UTC(2010, 1, 1), 71.5],
[Date.UTC(2010, 1, 5), 29.9],
[Date.UTC(2010, 2, 1), 106.4]
]
}]
});
});
https://jsfiddle.net/L2g2uzz3/3/
For example, I want to receive January, February and March labels in X axe

I understand you would like to display in your xAxis the name of the month for each data.
Since you want to display only the month and you have many data on the same month, you have to play with 2 attributes of the Highcharts API: formatter & tickPositioner.
formatter: use this function when you want to format data. Here you want to display name instead of timeValue more info here
tickPositioner: use it in order to set the tick (label position on the axis). In our case, we may have multiple ticks and same labels so we have to filter them. see the Highcharts doc for more info
In the label property of xAxis, add:
labels:{
formatter: function(){
return monthNames[new Date(this.value).getMonth()];
}
},
tickPositioner: function () {
var positions = [];
var month = [];
for(var i = 0; i < this.series[0].xData.length; i++){
if(month.indexOf(new Date(this.series[0].xData[i]).getMonth()) == -1){
positions.push(this.series[0].xData[i]);
month.push(new Date(this.series[0].xData[i]).getMonth());
}
}
return positions;
}
},
Also don't forget to init the monthNames array
var monthNames = ["January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December"
];
You can see your update FIDDLE
Let me know if it was helpful

Related

Use max yAxis when plotting boolean values

I'm trying to figure out a way to plot boolean values in Highcharts using the max yAxis range. I'd like the false value is aligned with the bottom of the chart and the true values is aligned with the top. Highcharts seems to be auto padding the ticks at ~30% and ~70% of the yAxis and I can't seem to find a way to adjust these to 0 and 100%.
Here's a simplified code example I've been experimenting with:
Highcharts.chart("container", {
xAxis: {
categories: ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"],
},
yAxis: [
{
title: {
text: "Valve Position",
},
categories: ["on", "off"],
},
],
series: [
{
data: [0, 1, 0, 1, 0, 1],
},
],
});
<script src="https://code.highcharts.com/highcharts.js"></script>
<div id="container" style="height: 300px"></div>
Use the linear axis type (default) insted of category:
yAxis: [{
...,
tickPositions: [0, 1],
labels: {
formatter: function() {
return this.isFirst ? 'on' : 'off';
}
}
}],
series: [{
data: [0, 1, 0, 1, 0, 1]
}]
Live demo: http://jsfiddle.net/BlackLabel/7v04d3r5/
API Reference: https://api.highcharts.com/highcharts/yAxis

highcharts how to make x and y axis starting on the same zero using categories on yAxis

Good evening,
I'm using highcharts and this is my actual situation
`
<script src="http://code.highcharts.com/highcharts.js"></script>
<div id="container" style="height: 400px"></div>
$(function () {
$('#container').highcharts({
chart: {
type: 'column'
},
title: {
text: "One might think points get grouped by name"
},
"series": [{
"data": [{
x: 1,
"name": "May",
"y": 1
}],
"name": "Alpha"
}, {
"data": [{
x: 0,
"name": "Apr",
"y": 1
}, {
x: 1,
"name": "May",
"y": 2
}, {
x: 2,
"name": "Jun",
"y": 3
}],
"name": "Beta"
}],
xAxis: {
type: 'category',
//categories : ["Jun", "Apr", "May"]
},
yAxis: {
type: 'category',
categories : ["First", "Second", "Third", "fourth","fifth"]
}
});
});
` .As you can see from the fiddle, the y axis is detached from the x axis but if I comment rows from 37 to 41 I get what I want. Is there any way to get x and y axis starting from the same point and keep using categories ?
You don't have to categorize yAxis - you can display proper yAxis labels using yAxis.labels.formatter function:
var yAxisCategories = ['first', 'second', 'third', 'fourth', 'fifth', 'sixth', 'seventh', 'eighth', 'ninth']
yAxis: {
labels: {
formatter() {
return yAxisCategories[this.pos]
}
}
},
jsFiddle with formatter
If you'd like to stick with categorized yAxis, you can manually move xAxis closer to they yAxis 0 value using xAxis.offset property together with yAxis.tickmarkPlacement 'on' property. In this example, I have put -34px rigidly, but you can calculate the proper value in chart.load event and then update calculated offset.
xAxis: {
type: 'category',
offset: -34
},
yAxis: {
type: 'category',
categories: ["First", "Second", "Third", "fourth", "fifth"],
tickmarkPlacement: 'on'
},
jsFiddle with offset
If you want to try a different approach, let me know - I will edit my answer.
API References:
https://api.highcharts.com/highcharts/yAxis.labels.formatter
https://api.highcharts.com/highcharts/yAxis.tickmarkPlacement
https://api.highcharts.com/highcharts/xAxis.offset

Highstock dataGrouping option

I have gone through the highstock datagrouping documenation(https://api.highcharts.com/highstock/series.column.dataGrouping), But little unclear about this data grouping option, Things I have noticed is, Data grouping is enabled by default in highstock, I n which we can force the datagrouping by giving custom datagrouping option through units option in datagrouping, The doubt I got is which is the default data grouping(default unit option) in highstock, another one is do we need to tell the datagrouping units for each time interval by which we are gonna show in chart(eg, if the datarange chosen is one month the grouping should by 1 day, If the daterange chosen is one day means the datagrouping units should be by hour),
units: [[
'millisecond', // unit name
[1, 2, 5, 10, 20, 25, 50, 100, 200, 500] // allowed multiples
], [
'second',
[1, 2, 5, 10, 15, 30]
], [
'minute',
[1, 2, 5, 10, 15, 30]
], [
'hour',
[1, 2, 3, 4, 6, 8, 12]
], [
'day',
[1]
], [
'week',
[1]
], [
'month',
[1, 3, 6]
], [
'year',
null
]]
Do we need to specify each unit or do we need to change the units dynamically based on the daterange chosen?
This is what I have done, The based on the daterange the user chosen the grouping units should be changed, Do we need to specify the units based on the daterange chosen and what the units array specifies (Is Giving only one unit as option to the grouping is the functionality, Or the units array options can be chosen based on the timestamps)
Highcharts.stockChart('chart', {
chart: {
zoomType: false
},
legend: {
enabled: true
align: 'right',
verticalAlign: 'top',
x: 0,
y: -20
},
navigator: {
enabled: false
},
rangeSelector: {
enabled: false
},
scrollbar: {
enabled: false
},
xAxis: {
type: 'datetime',
dateTimeLabelFormats: {
day:"%b %e"
}
},
yAxis: {
opposite : false
},
series: [{
data: data ,
marker: {
enabled: true
},
dataGrouping: {
forced: true,
approximation: "sum"
}
}]
EDIT
While forced option is set to false
which is the default data grouping(default unit option) in highstock
There's a little flaw in the API (https://api.highcharts.com/highstock/series.column.dataGrouping.units) in units section. First it says that it defaults to:
units: [[
'millisecond', // unit name
[1, 2, 5, 10, 20, 25, 50, 100, 200, 500] // allowed multiples
], [
'second',
[1, 2, 5, 10, 15, 30]
], [
'minute',
[1, 2, 5, 10, 15, 30]
], [
'hour',
[1, 2, 3, 4, 6, 8, 12]
], [
'day',
[1]
], [
'week',
[1]
], [
'month',
[1, 3, 6]
], [
'year',
null
]]
and after that "Defaults to undefined." line appears. The first one is correct.
Highcharts automatically finds the best grouping unit from units array. So, in other words: units specifies which groupings can be applied if Highcharts decides that applying data grouping is a good idea.
Enable dataGrouping.forced and use only one key-value pair in units array if you want to make sure that this grouping will be used no matter what e.g.:
units: [[
'hour',
[3]
] // Data will always be grouped into 3 hours periods
These're just general rules - they should be enough to use data grouping successfully.

Highchart Persist bar widh for null data

I have a simple HighStock chart with two series as shown in below image:
So functionality wise, HighChart decreases the width of the bar as soon as new series is added.
But I don't want to decrease the width of the bar, when there is data for only one series at specific point. (here date, 17. May)
Here is the Fiddle I have created. Try hiding one of the series and see width of the bar. The same width should be applied for the bar on "17. May" when both series are visible as there is data for only one series for that date. As shown below:
I have used below code:
Highcharts.stockChart('container', {
chart: {
alignTicks: false
},
legend: {
enabled: true
},
rangeSelector: {
selected: 1
},
title: {
text: 'AAPL Stock Volume'
},
"series":[
{
"name":"Key Rate 319",
"type":"column",
"data":[{x: 1147651200000, y: 1}, {x: 1147737600000, y: 7}, {x: 1147824000000, y: 5}, {x: 1147910400000, y: 4}],
"marker":{"enabled":false},
"shadow":false,
"color":"blue"
},
{
"name":"Key Rate 321",
"type":"column",
"data":[{x: 1147651200000, y: 4}, {x: 1147737600000, y: 2}, {x: 1147824000000, y: null}, {x: 1147910400000, y: 1}],
"color":"green"
}]
});
How can I achieve this ?
Series has two properties for controlling the range and placement of points: pointRange & pointPlacement. They work for all series' points and unfortunately can't be applied to individual columns.
Workaround
Disable grouping and reset pointPadding & groupPadding:
plotOptions: {
series: {
grouping: false,
pointPadding: 0,
groupPadding: 0
}
},
Create a separate series for every point and apply appropriate pointRange & pointPadding to them. Then link them using linkedTo property to mimic the original series structure:
series: [{
name: 'First series',
color: '#bada55',
data: [
[0, 2]
],
pointRange: 0.4,
pointPlacement: -0.5
}, {
data: [
[1, 7]
],
linkedTo: ':previous',
color: '#bada55',
pointRange: 0.4,
pointPlacement: -0.5
}, {
data: [
[2, 5]
],
linkedTo: ':previous',
color: '#bada55',
//pointRange: 1, // by default
//pointPlacement: 0 // by default
}, {
data: [
[3, 4]
],
linkedTo: ':previous',
color: '#bada55',
pointRange: 0.4,
pointPlacement: -0.5
}, {
name: 'Second series',
data: [
[0, 1]
],
color: '#c0ffee',
pointRange: 0.4,
pointPlacement: 0.5
}, {
data: [
[1, 2]
],
linkedTo: ':previous',
color: '#c0ffee',
pointRange: 0.4,
pointPlacement: 0.5
}, {
data: [
[3, 2]
],
linkedTo: ':previous',
color: '#c0ffee',
pointRange: 0.4,
pointPlacement: 0.5
}]
Live demo: http://jsfiddle.net/BlackLabel/q7j4m8eb/
Disadvantage of this approach are that you must define color for each point (series) explicitly and the data structure look a bit complex.
All options mentioned above can be found in the API: https://api.highcharts.com/highcharts/

How to create two plot areas with highcharts

I want to plot several values across the same time series. However, I'd like to split them up into separate plot areas. Ideally, it would be a single Y axis and multiple X axis. I don't just want a secondary axis as shown here.
I realize I could just create two charts, but then the time series might not line up perfectly well.
You can use one x-axis versus multiple y-axes. I think the example is self explanatory: http://jsfiddle.net/unqmP/
Revision: Adding the JS code:
$(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
chart = new Highcharts.StockChart({
chart: {
renderTo: 'container',
alignTicks: false
},
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: 'spline',
name: 'AAPL',
data: ohlc,
dataGrouping: {
units: groupingUnits
}
}, {
type: 'spline',
name: 'Volume',
data: volume,
yAxis: 1,
dataGrouping: {
units: groupingUnits
}
}]
});
});
});

Resources