i have a simple line highchart with single "null" values.
I know, that I can connect the line with connectNulls: true.
Question:
Is it possible to mark the connection in a separate color or with a dotted line?
FIDDLE:
http://jsfiddle.net/SebastianH4/xpjqou95/1/
Yes, it is possible.
See here: http://jsfiddle.net/mtg6bd0o/3/
You can obviously use the trick described in brightmatrix's answer, but here's another solution:
Highcharts provides us a pretty simple to use Renderer API which lets us do some cool stuff such as drawing shapes, texts and paths (In general, svg elements).
Then using some chart manipulations together with the renderer, we can achieve this kind of goal.
var chart = $('#container').highcharts();
var plotLeft = chart.plotLeft;
var plotTop = chart.plotTop;
for (var i = 0; i < data.length; i++) {
var path = [];
if (data[i] == null) {
var p1 = chart.series[0].points[i - 1],
p2 = chart.series[0].points[i + 1];
p2 = p2 == undefined ? p1 : p2;
path.push('M');
path.push(p1.plotX + plotLeft, p1.plotY + plotTop);
path.push('L');
path.push(p2.plotX + plotLeft, p2.plotY + plotTop);
}
var line = chart.renderer.path(path)
.attr({
'stroke-width': 2,
stroke: Highcharts.getOptions().colors[0],
dashstyle: 'ShortDash'
})
.add();
}
I'll explain:
At first, we use 2 variables plotleft and plotTop which hold the values for the charts actual position relatively to the top and left coordinate.
Then I iterate the data, such that in each iteration I create a path array which has the initial point (where the line starts, comes after 'M'), and the end point coordinate (comes after 'L').
This is a standard way to create paths using SVG.
What actually happens is that for each null point, I take the previous and the next points's coordinates and push them to path.
Here I iterate some dummy data, it could obviously be retrived from the series itself.
Then, after I have the required coordinates, I can draw the line itself using the Renderer API (Reference: http://api.highcharts.com/highcharts#Renderer)
Working fiddle: http://jsfiddle.net/mtg6bd0o/3/
I found another question here on Stack Overflow that discusses a similar situation where a user wanted to color different segments of the same line (Highcharts: change line options mid-way through a line) as well as a Highcharts module that allows for multicolor series (http://blacklabel.github.io/multicolor_series/).
However, the crux of this is that those points need to have a value; they cannot be null. I tried adding a color to your null points, but enabling connectNulls ignored that setting and overrode it with the series default.
If this is a stand-alone chart that would not be fed random or updated data, you could plot a second series that sits behind the first and only has points where the first has nulls. Here's an example I worked up:
$(function () {
$('#container').highcharts({
title: {
text: 'LineWithNulls'
},
subtitle: {
text: 'How can I connect the the line in a different color or with a dotted line'
},
xAxis: {
categories: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
},
series: [{
data: [4, 5, 1, { y: null, color: 'red'}, 3, 4, 2, null, 5, 6],
connectNulls: false, zIndex: 2
}, {
/* differently formatted series for "null" values in first series
prevent from showing up in legend and prevent series from showing in tooltip on mouse over */
data: [null, null, 1, 2, 3, null, 2, 3.5, 5, null],
connectNulls: false, color: 'red', marker: {enabled: false}, dashStyle: 'dash', zIndex: 1, showInLegend: false, enableMouseTracking: false
}]
});
});`
You can also see the fiddle here: http://jsfiddle.net/brightmatrix/xpjqou95/3/
Please let me know whether this is helpful!
Related
Am using high charts, when am calculating the average time and plotting the pin position slightly changed. Here am attaching the screen shot, please have a look on it and help me out from this. Thank you.
Here is my series,
var data = [
{
name: 'No. of potholes',
data: seriesArray,
stack: 'North',
color: 'lightblue'
},
{
type: 'line',
name: 'Average',
data: seriesAvg,
color: 'orange',
marker: {
lineWidth: 2,
lineColor: 'orange',
fillColor: 'white'
}
},
{
name: 'KPI',
color: 'grey'
},
]
The plotting circle position has been changed:
This is caused by the grouping functionality (https://api.highcharts.com/highcharts/series.column.grouping):
When it is enabled each column series has a reserved space in every group. There're two column series in your chart. The second column series occupies half of the space in each group even though it has no data.
Set grouping to false to prevent this unwanted behavior.
i am quite new to Highcharts and i am a little bit stucked.
I am trying to add a average line (spline) to each bar (column) in a combination chart. The average line shall not be displayed in the middle, it sall be displayed over the related bar, as i tried to show in this picture.
picture to explain
Thanks a lot!
You can add new xAxis that you can use to add your spline series in right places.
xAxis: [{
min: 0,
max: 3,
categories: ['Jan', 'Feb', 'Mar', 'Apr'],
tickPixelInterval: 0
}, {
categories: true,
min: 0,
max: 19,
visible: false
}],
In this case I am making the second xAxis much more dense. Here you can see how both of my axes looks here:
http://jsfiddle.net/feqq1eog/1/
To hide second axis you can use visible: false parameter.
Here you can find an example how it can work:
http://jsfiddle.net/feqq1eog/4/
When data labels overlap in Highcharts, only one data label is displayed. This seems to be handled randomly. See fiddle:
http://jsfiddle.net/lamarant/rmxLd1d4/
$(function () {
$('#container').highcharts({
title: {
text: 'Label Test'
},
series: [{
type: 'line',
data: [ 10, 10, 10, 10, 10, 10, 50],
dataLabels: {
enabled: true,
color: 'blue',
zIndex: 10
},
zIndex: 10
},
{
type: 'line',
data: [ 11, 11, 11, 11, 11, 11, 49],
dataLabels: {
enabled: true,
color: 'red',
zIndex: 10
},
zIndex: 20
}]
});
});
Notice that the first data label displayed in the chart is from the second series while the rest are from the first series.
I tried setting the priority of the label display using zIndex in both series and series.dataLabel with no luck.
Is there any way to set it so that a designated series always takes priority when Highcharts determines which label to display?
One possible fix is supplying a labelrank for each point, which is used to evaluate their sorting order. If you supply this rank for each point in a series, that series should be considered "above" series without such a rank (or with a lower rank integer value).
In code, manually for each point:
series: [{
data: [
{ y: 11, labelrank: 1 },
{ y:11, labelrank: 1 }
// ...
],
// ...
}]
In code, using the callback function (JSFiddle):
$('#container').highcharts({
// Options ...
}, function() {
var mySeriesIndex = 1;
// For each point in the preferred series
for(i = 0; i < this.series[mySeriesIndex].points.length; i++)
// Set a labelrank
this.series[mySeriesIndex].points[i].labelrank = 1;
});
My current take on the issue itself is this:
With the current implementation of the overlap logic it seems to me that this is a bit of an unintended behavior. The source code uses Array.sort in a way that shifts the positions of the point labels, since Array.sort is not guaranteed to be stable (same-value items wont retain their original order).
If you check your example in Firefox it should work as intended (their implementation of Array.sort is different), while in Chrome it doesn't (not stable). You could hope that this little feature is fixed.
If I have a Highcharts bar or column chart that has a data series with an array of all zeroes, the y axis tick label displays in the center of the y axis, along with an extra y axis. I've experimented with the tick placement and min values but these dont seem to work in the case of this peculiar set of data. see http://jsfiddle.net/davidpmcintyre/mepd17tn/
Here's the highcharts code snippet:
$(function () {
$('#container').highcharts({
chart: {
type: 'bar'
},
xAxis: {
categories: ['Jan', 'Feb', 'Mar', 'Apr' ]
},
series: [{
data: [0, 0, 0, 0]
}]
});
});
Assuming (given requirements of your chart):
All data will be positive
You always want to start from 0 on the y-axis
The following code will allow you to left-align your y-axis, and also specify how much of the y-axis you want to show in minimal cases (JSFiddle):
yAxis: {
min: 0,
minRange: 1
}
This will still allow you to have larger ranges on the y-axis as it only kicks in when the value range is less than one. This in turn means you do not have to check your data before setting any values.
I have two lines of date data. One line plots a point sequentially every day. The other line plots non-sequental points that fall on one of the days of the previous line.
The problem is that the crosshair functionality behaves weirdly when I do this. See the example linked below. You will notice that although the tooltip and point highlighting shows for the proper point, the crosshair gets stuck on the non-sequential point along the x-axis, even though this is not the current position of my mouse on the chart. The exception to this is when you hover your mouse directly across the line. The crosshair follows the mouse properly then.
See example here: http://jsfiddle.net/2H9m3/1/
$(function () {
$('#container').highcharts({
tooltip: {
shared: true,
crosshairs: true
},
xAxis: {
type: "datetime", maxPadding: 0, minPadding: 0
},
yAxis: {
min: 0
},
series: [{
name: 'Line',
data: GenerateData(10, 50)
}, {
name: "Events",
color: "#EF4B68",
lineWidth: 0,
marker: {
enabled: true,
symbol: "triangle",
radius: 6
},
data: [
[5 * 86400000, 0],
[21 * 86400000, 0]
]
}]
});
});
function GenerateData(min, max) {
var data = [];
for (i = 0; i < (30 * 86400000); i = i + 86400000) {
data.push([i, GetRandomInt(min, max)]);
}
return data;
}
function GetRandomInt(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
I am not sure if this is a bug or something that I am doing wrong.
The issue here is subtle. It is actually caused by the fact that the tooltip is linked over the top-most series. Since you are creating your linear series first then your non-sequential series the top-most series is the 2 points. So, in order to recapture the 'tooltip` and your crosshair you have to hover closer to the series that is behind the non-sequential series.
Fixes:
Make the non-sequential series the first one you add.
Give an index value to whichever series you want on top that is larger than the least-full series.
I went with option 2:
series: [{
name: 'Line',
index: 1,
data: GenerateData(10, 50)
}, {
name: "Events",
index: 0,
color: "#EF4B68",
lineWidth: 0,
marker: {
enabled: true,
symbol: "triangle",
radius: 6
},
data: [
[5 * 86400000, 0],
[21 * 86400000, 0]
]
}]
Example here.
Looks like a bug in new crosshairs core.
Reported to devs: https://github.com/highslide-software/highcharts.com/issues/2816
Thanks!