Add a Specific Y-Axis Label with Highcharts - highcharts

I have a graph with a labelled Y-Axis with points 1 through 100. In addition to the regularly spaced labels (0, 10, 20, etc), I want to add a label for an arbitrary point, say 47. Is this possible with highcharts?

You can add a specific line to the yAxis with the plotLines option.
yAxis: {
plotLines: [{
value: 47,
color: 'rgb(216, 216, 216)',
width: 1,
}]
},
http://jsfiddle.net/nicholasduffy/wa6ukyyp/1/
EDIT:
This seems a bit of a hack, but you could fake the label.
yAxis: {
plotLines: [{
value: 47,
color: 'rgb(216, 216, 216)',
width: 1,
label : {
text: '47',
x: -30,
y: 2
}
}]
},
http://jsfiddle.net/nicholasduffy/wa6ukyyp/2/

Based on your comment, you can add a custom tick with tickPositioner function, with a code like this
$(function () {
$('#container').highcharts({
xAxis: {
categories: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
},
yAxis: {
tickInterval: 20, //sets the interval ticks
tickPositioner: function(){
var ticks = this.tickPositions; // gets the tick positions
ticks.push(47); // adds the custom tick
ticks.sort(function(a, b) {
return a - b; // sorts numerically the ticks
});
return ticks; // returns the new ticks
}
},
series: [{
data: [29.9, 71.5, 86.4, 29.2, 44.0, 76.0, 93.5, 98.5, 16.4, 94.1, 95.6, 54.4]
}]
});
});
JSFiddle demo

I think the solution I found that comes closest is to create a line of zero thickness and label the line:
plotLines: [{
value: cutoff,
color: 'rgb(216, 216, 216)',
width: 0,
label: {
text: 'label_name',
style: {
color: 'grey',
fontweight: 'bold'
}
}
}],
The label_name goes pretty close to the Y-Axis, and it gets the point across without putting a tooltip onto the graph

Related

Highcharts series label legend in one line

I need to show highcharts api series name in one line but not able to get it correctly.
When viewport width is larger then it's not an issue but on narrow width the third legend item drops to new line. Due to SVG rendering, I am not able to apply CSS into group element.
In any case active users just wraps to second line and I am not able to set width of parent group element. So far I have tried all legend default options but it didn't work.
So any help would be great.
https://jsfiddle.net/h28jv3zL/
Highcharts.chart('container', {
chart: {
type: 'column'
},
xAxis: {
categories: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
},
legend: {
align: "left",
x: 0,
y: 0,
itemWidth: 100,
// symbolWidth: 0,
// symbolHeight: 0,
// symbolRadius: 0,
verticalAlign: "top",
layout: "horizontal",
floating: false,
backgroundColor: "white",
shadow: false,
itemStyle: {
"fontWeight": "normal",
"color": "#101010",
"textOverflow": "normal",
"width": "auto",
"white-space": "nowrap",
'padding-right': "10px"
}
},
plotOptions: {
series: {
fillOpacity: 0.1
}
},
series: [{
name: "Inactive Users",
data: [10,11,12],
color: "#D8D8D8",
//showInLegend: false
},
{
name: "New Users",
data: [10,11,12],
color: "#696969",
//showInLegend: false
},
{
name: "Active Users",
data: [10,11,12],
color: "#202020",
//showInLegend: false
},]
});
<script src="https://code.highcharts.com/highcharts.js"></script>
<div id="container" style="height: 400px"></div>
You can add some logic to check the chart.plotWidth and base on that substract series name or hide it inside the labelFormatter callback.
Demo: https://jsfiddle.net/BlackLabel/souphrg9/

Properly align columns in highcharts

I tried to create a column chart in highcharts to display monthly data on a datetime axis. The problem is that the spacing between the bars is different. For example after the month february there is only a small spacing whereas after other months the spacing is bigger. Maybe this is happening because the month feburary only has 28 days.
Anyway here is my code:
Highcharts.chart('container', {
chart: {
type: 'column'
},
xAxis: [{
type: 'datetime'
}, {
type: 'datetime',
visible: false
}],
plotOptions: {
column: {
grouping: false,
groupPadding: 0,
pointPadding: 0,
pointPlacement: 'between'
}
},
series: [{
name: 'Year',
xAxis: 1,
data: [25],
zIndex: 4,
color: '#222',
pointStart: Date.UTC(2017, 0, 1),
pointInterval: 1,
pointIntervalUnit: 'year'
}, {
name: 'Month',
data: [50, 100, 130, 160, 170, 200, 170, 165, 250, 200, 230, 160],
pointStart: Date.UTC(2017, 0, 1),
pointInterval: 1,
pointIntervalUnit: 'month',
zIndex: 2,
color: 'red',
}],
});
http://jsfiddle.net/bosngxk1/2/
I also added another bar for the whole year. You notice the already mentioned problem with the spacing and that the year area is a bit too wide. Do you have any ideas how to fix this?
If you do not mind, you can use Highstock. There is an axis option called ordinal. With that set to true (by default), all points are equally spaced. Take a look at the example posted below.
API Reference:
https://api.highcharts.com/highstock/xAxis.ordinal
Example:
http://jsfiddle.net/k0k2n83p/
Well, I guess there is no other option than using categorical x axis. I don't like this solution since I somehow have to hardcode datetime values instead of using datetime formatters and so on.
Anyway by defining the xAxis as follows I get the desired chart layout.
xAxis: [{
categories: [
'Jan',
'Feb',
'Mar',
'Apr',
'May',
'Jun',
'Jul',
'Aug',
'Sep',
'Oct',
'Nov',
'Dec'
],
tickmarkPlacement: 'on'
},
{
categories: [
'2017'
],
visible: false
}]
Fiddle: http://jsfiddle.net/hho4zrso/

Remove Highchart line when no value

I have a line chart like this:
http://jsfiddle.net/vbLdsodu/
How do i remove or hide the line after july? I want the date axis to continue but don't show the line where the values is 0.
$(function () {
$('#container').highcharts({
title: {
text: 'Monthly Average Temperature',
x: -20 //center
},
subtitle: {
text: 'Source: WorldClimate.com',
x: -20
},
xAxis: {
categories: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
},
yAxis: {
title: {
text: 'Temperature (°C)'
},
plotLines: [{
value: 0,
width: 1,
color: '#808080'
}]
},
tooltip: {
valueSuffix: '°C'
},
legend: {
layout: 'vertical',
align: 'right',
verticalAlign: 'middle',
borderWidth: 0
},
series: [{
name: 'Tokyo',
data: [7.0, 6.9, 9.5, 14.5, 18.2, 21.5, 25.2, 0, 0, 0, 0, 0]
}]
});
});
You can replace 0 with null in the data which will then keep the axis listing the months, but stop the line from drawing.
It's also possible to do this part way through the graph and the line will draw from the next data point.

Highcharts: Logarithmic chart with Y Axis [100, 99.9, 99, 90, 0]

I am trying to use Highcharts to generate our website's availability information.
The Y axis is availability in percentage, the expected labels are:
[100, 99.9, 99, 90, 0]
code:
http://jsfiddle.net/2E9vF/1/
$(function () {
$('#container').highcharts({
chart: {
},
xAxis: {
categories: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
},
yAxis: {
title: {
text: "AVAILABILITY %"
},
labels: {
formatter: function() {
return 100-this.value;
}
},
type: 'logarithmic',
reversed:true,
min:0.01,
plotLines: [{
color: '#CCCCCC',
width: 2,
value: 0.1,
dashStyle: 'ShortDot'
},{
color: 'red',
width: 2,
value: 1,
dashStyle: 'ShortDot'
},{
color: '#CCCCCCC',
width: 2,
value: 10,
dashStyle: 'ShortDot'
}],
},
tooltip: {
formatter: function() {
return 'x:'+ this.x +'<br>'+
'y:'+ (100-this.y);
}
},
series: [{
// real data:
// [90, 98, 99.7, 100, 100, 99.9, 99.7, 90, 91, 98, 96, 97.3]
data: [10, 2, 0.3, 0, 0, 0.1, 0.3, 10, 9, 2, 4, 2.7]
}]
});
});
However, I have two issues that I need help with:
I cannot draw 100 in the y axis
Orginal data with value 100 cannot be shown in the chart
Here is the expected chart:
http://www.shareimage.ro/images/0j1o0bnccavkqds8icuy.jpg
In general negative numbers and zero-based are not supported, but you can workaround this: http://jsfiddle.net/2E9vF/2/
In steps:
Assume that value 0 will be represented as other one, for example 0.001 (or lower)
Set min for yAxis to that value
Update formatters for tooltip and yAxis.labels accordingly:
tooltip: {
formatter: function() {
if(this.y === 0.001) {
return 'x:'+ this.x +'<br>'+ 'y:100';
} else {
return 'x:'+ this.x +'<br>'+ 'y:'+ (100-this.y);
}
}
},
And:
yAxis: {
labels: {
formatter: function() {
if(this.isFirst) {
return 100;
}
return 100-this.value;
}
}
}

Highcharts - How to force line chart to be visible above X Axis

When plotting a line chart where there are values equal to the yAxis.min value, the line is hidden underneath the X Axis.
I have the Y Axis minimum set to 0 and the data set contains zeros
Here is an example:
$(function () {
$('#container').highcharts({
xAxis: {
categories: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
},
yAxis: {
min: 0,
max: 100
},
series: [{
name: 'Tokyo',
data: [7.0, 6.9, 0, 0, 0, 21.5, 25.2, 26.5, 23.3, 18.3, 13.9, 9.6]
}]
});
});
http://jsfiddle.net/egQH8/1/
If I increase the values to say 0.2, you can just see the line beginning to appear: http://jsfiddle.net/2RCdV/1/
Is there anything I can do to get round this?
Remove the min: 0 and add startOnTick: false.
That will make Highcharts compute the min value automatically and since it's no longer starting on the tick it'll draw the 0 yAxis line just a few pixels above the xAxis.
See it running here.
we fixed this like this for 4px lines:
// This fixes the "thin lines at top & bottom of chart" bug
Highcharts.wrap(Highcharts.Chart.prototype, 'setChartSize', function (proceed) {
proceed.apply(this, [].slice.call(arguments, 1));
if (includes(['line','spline'], get(this, 'options.chart.type'))) {
this.clipBox.height += 6;
this.clipBox.y -= 3;
}
});

Resources