Highchart Persist bar widh for null data - highcharts

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/

Related

highcharts heatmap drilldown link on yasix

I created highcharts heatmap with drilldown. Drilldown has link on xaxis lable. Could I have link only on yaxis? And remove link on xaxis. Since my drilldown chart is associate to y not x.
Highcharts.chart('container', {
chart: {
type: 'heatmap'
},
colorAxis: {
min: 0,
stops: [
[0, '#7EAB55'],
[0.5, '#FFFE55'],
[1, '#B02418']
],
},
series: [{
data: [{
x: 0,
y: 0,
value: 0,
drilldown: 'animals'
}, {
x: 0,
y: 1,
value: 1
}, {
x: 1,
y: 0,
value: 2,
drilldown: 'animals'
}, {
x: 1,
y: 1,
value: 1
}],
dataLabels: {
enabled: true,
color: '#000000'
}
}],
drilldown: {
series: [{
id: 'animals',
data: [
[0, 3, 1],
[1, 1, 4],
[2, 4, 3]
]
}]
}
});
This is example in fiddle. Thanks.
https://jsfiddle.net/lundi/jdc4mpk2/
From Sebastian's suggestion, this is the answer.
You can use the chart.inverted feature to achieve the wanted result: jsfiddle.net/BlackLabel/82qapmoe

How to plot a gaussian curve over histogram in chart.js?

I create histogram with gaussian curve, but first data point of curve is skipped.
My example:
var ctx = document.getElementById("myChart");
var myChart = new Chart(ctx, {
type: 'horizontalBar',
data: {
labels: [0,1,2,3,4,5,6,7,8,
9,10,11,12,13,14,15],
datasets: [{
borderWidth: 1,
backgroundColor: 'rgba(255, 99, 132,0.5)',
borderColor: 'rgb(255, 99, 132)',
data: [3,1,0,0,0,2,0
,2,0,2,0,0,0,0,0,0]
},{
type: 'line',
fill: false,
borderWidth: 1,
data: [
{
"x": 0.4175611241375584,
"y": 0
},
{
...other points
}
],
borderColor: 'rgb(54, 162, 235)',
radius: 2
}]
},
options: {
scales: {
yAxes: [{
categoryPercentage : 1,
barPercentage: 1
}],
xAxes: [{
type: 'linear',
position: 'bottom',
ticks: {
stepSize: 1,
min: 0,
max: 4
}
}]
}
}
});
https://codepen.io/krna/pen/ejzqwg
When I only draw a Gaussian curve, all points are drawn correctly.
Inversion of histogram doesn't help.
I tried many strange tricks with data and chart axis configuration, but without success.
The problem is in 0 value on y-axis, it's probably interpreted as null instead of 0. You can make this work however, by making the labels and y values a string. Full answer You could find on https://github.com/chartjs/Chart.js/issues/5641.

highchart's line chart with constant series not plotting when using linear gradient colour

Observe the series one in the below example having a constant value and for that series points connecting lines are not plotted, but when we give a solid colour then the line will be plotted.
Highchart's line chart with constant series not plotting the line b/w points when we use line gradient colour, but if you change that to solid colour then line will be plotting.
highcharts.series[].data = [2, 2, 2, ...];
highcharts.color[0] = {
linearGradient: {...},
stop: {...}
};
Check this example:
https://jsfiddle.net/4vk7cdmz/
This is because of the attribute gradientUnits in linearGradient which the default value is objectBoundingBox.
Keyword objectBoundingBox should not be used when the geometry of the
applicable element has no width or no height, such as the case of a
horizontal or vertical line, even when the line has actual thickness
when viewed due to having a non-zero stroke width since stroke width
is ignored for bounding box calculations. When the geometry of the
applicable element has no width or height and objectBoundingBox is
specified, then the given effect (e.g., a gradient or a filter) will
be ignored.
W3C Recommendation
You need to use gradientUnits="userSpaceOnUse".
Highcharts.js has fixed this issue in version 2.2.
Instead of using linearGradient as an object
"linearGradient": {
"x1": 0,
"y1": 0,
"x2": 1,
"y2": 0
}
, using it as an array
"linearGradient": [x1, y1, x2, y2],
will set gradientUnits to userSpaceOnUse in highcharts.js
(This requires knowledge of the line width.)
Here's a demo:
var Chart = Highcharts.chart('container', {
chart: {
type: 'line'
},
title: {
text: 'a constant series line is not plotting when using linear gradient colour.'
},
legend: {
layout: 'vertical',
align: 'left',
verticalAlign: 'top',
x: 150,
y: 100,
floating: true,
borderWidth: 1,
backgroundColor: (Highcharts.theme && Highcharts.theme.legendBackgroundColor) || '#FFFFFF'
},
colors: [{
"linearGradient": [0, 0, 500, 0],
"stops": [
[0, "rgb(35,190,250)"],
[1, "rgb(51,223,188)"]
]
}, {
"linearGradient": [0, 0, 500, 0],
"stops": [
[0, "rgb(250,79,168)"],
[1, "rgb(156,120,229)"]
]
}],
xAxis: {
categories: [
'Monday',
'Tuesday',
'Wednesday',
'Thursday',
'Friday',
'Saturday',
'Sunday'
],
plotBands: [{ // visualize the weekend
from: 4.5,
to: 6.5,
color: 'rgba(68, 170, 213, .2)'
}]
},
yAxis: {
title: {
text: 'Fruit units'
}
},
tooltip: {
shared: true,
valueSuffix: ' units'
},
credits: {
enabled: false
},
plotOptions: {
areaspline: {
fillOpacity: 0.5
}
},
series: [{
name: 'John',
data: [2, 2, 2, 2, 2, 2, 2]
},
{
name: 'Jane',
data: [1, 1, 1, 1, 1, 1, 2]
}
]
});
#container {
width: 100%;
height: 400px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/highcharts/6.0.7/highcharts.src.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highcharts/6.0.7/css/highcharts.css" />
<div id="container"></div>

Center tooltip for highcharts area chart

Since highcharts does not have a column chart where I can set height and width of each column, I have to use an area chart to accomplish this. So basically I am using area charts to draw rectangles. The problem with area charts is that the tooltip is not centered but is always shown above one edge of the rectangle. Plus, I am drawing multiple rectangles on top of each other causing to show the tooltip of the wrong rectangle.
$('#container').highcharts({
chart: {
type: 'area',
zoomType: 'x'
},
xAxis: {
type: 'datetime'
},
yAxis: {
title: {
enabled: false
}
},
plotOptions: {
area: {
marker: {
enabled: false,
states: {
hover: {
enabled: false
}
}
},
fillOpacity: 1
}
},
tooltip: {
enabled: true
},
series: [{
name: 'Year',
color: '#0d233a',
data: [
[Date.UTC(2017, 0, 1), 0],
[Date.UTC(2017, 0, 1), 4],
[Date.UTC(2017, 11, 31), 4],
[Date.UTC(2017, 11, 31), 0]
],
zIndex: 4,
fillColor: '#0d233a',
lineColor: '#fff'
}, {
name: 'Half year',
id: 'Half',
color: '#2f7ed8',
data: [
[Date.UTC(2017, 0, 1), 0],
[Date.UTC(2017, 0, 1), 10],
[Date.UTC(2017, 5, 31), 10],
[Date.UTC(2017, 5, 31), 0]
],
zIndex: 3,
fillColor: '#2f7ed8',
lineColor: '#fff'
}, {
name: 'Half year',
linkedTo: 'Half',
data: [
[Date.UTC(2017, 6, 1), 0],
[Date.UTC(2017, 6, 1), 6],
[Date.UTC(2017, 11, 31), 6],
[Date.UTC(2017, 11, 31), 0]
],
zIndex: 3,
fillColor: '#2f7ed8',
lineColor: '#fff'
}]
});
For an example see this fiddle.
You can see that the tooltip is not centered and that a wrong tooltip is shown when hovering the dark blue rectangle. Does anyone know how to fix this?
If all the columns within one series are the same width, you can try to do this with a normal column chart, using tightly packed columns and multiple xAxis:
...
xAxis: [{
type: 'datetime',
}, {
type: 'datetime',
}],
plotOptions: {
column: {
grouping: false,
groupPadding: 0,
pointPadding: 0,
pointPlacement: 'between',
},
},
series: [{
name: 'Year',
data: [
[Date.UTC(2016, 0, 1), 3],
[Date.UTC(2017, 0, 1), 4],
],
zIndex: 4,
color: '#222'
}, {
name: 'Half year',
xAxis: 1,
data: [
[Date.UTC(2016, 0, 1), 8],
[Date.UTC(2016, 6, 1), 11],
[Date.UTC(2017, 0, 1), 10],
[Date.UTC(2017, 6, 1), 6],
],
zIndex: 3,
color: 'royalblue',
}],
http://jsfiddle.net/j1fnkt01/3/
Note: Zooming won't work properly until the x-axes can be properly linked (linkedTo), and this issue prevents us from doing that right now.
You can use tooltip.followPointer to get desired result
Whether the tooltip should follow the mouse as it moves across columns, pie slices and other point types with an extent. By default it behaves this way for scatter, bubble and pie series by override in the plotOptions for those series types.
For touch moves to behave the same way, followTouchMove must be true also.
Defaults to false.
Tooltip will be
tooltip: {
enabled: true,
followPointer: true
},
Fiddle demo
Update
Use plotOptions.area.trackByArea to show tooltip properly
So plotOptions will be
plotOptions: {
area: {
marker: {
enabled: false,
states: {
hover: {
enabled: false
}
}
},
fillOpacity: 1
},
series: {
trackByArea: true,
stickyTracking: false,
}
},
updated fiddle

Round off the area points in Highcharts Polar

Is there is a way to round off the area points and make them less pointed when used in the polar chart?
I have tried the 'areaspline', but with polar chart it's not quite right, as you can see here:
http://jsfiddle.net/qwbu6r3t/2/
Highcharts.chart('container', {
chart: {
polar: true
},
title: {
text: 'Highcharts Polar Chart'
},
pane: {
startAngle: 0,
endAngle: 360
},
xAxis: {
tickInterval: 45,
min: 0,
max: 360,
labels: {
formatter: function () {
return this.value + '°';
}
}
},
yAxis: {
min: 0
},
plotOptions: {
series: {
pointStart: 0,
pointInterval: 45
}
},
series: [{
type: 'areaspline',
name: 'Area spline',
data: [1, 2, 3, 4, 5, 6, 7, 8]
}, {
type: 'area',
name: 'Area',
data: [1, 8, 2, 7, 3, 6, 4, 5]
}]
});
EDIT
What I need
You can override highcharts Highcharts.seriesTypes.areaSpline.prototype.getPointSpline with same method but with changed smoothing parameter. (Default 1.5)
...
var smoothing = 3, // 1 means control points midway between points, 2 means 1/3 from the point, 3 is 1/4 etc
...
Live example:
http://jsfiddle.net/q3h6xwog/
just change area to areaspline would do what you want i think so

Resources