highcharts - Draw horizontal line outside the chart area - highcharts

I have a column chart as in the picture below. How do I draw a responsive horizontal line (and label) outside the chart area?
I know we could achieve something similar using plotLines and plotBands but the value has to be the one within the value range in the series, and the line won't be drawn outside the chart either.
In my case, I want to draw the line outside the chart area, specifically under the xAxis (see the line in red color).
Is there a way to do that?
$(function() {
Highcharts.chart('sd-median-annual-earnings-chart', {
credits: {
enabled: false
},
chart: {
type: 'column',
backgroundColor: null,
style: {
fontSize: 'inherit',
overflow: 'visible',
width: '100%'
}
},
title: {
text: 'Median Annual Earnings',
style: {
color: '#000',
fontFamily: 'inherit',
fontSize: '1.2em',
fontWeight: 700
}
},
xAxis: {
categories: [
'A',
'B',
'C',
'D'
],
labels: {
overflow: 'justify',
style: {
color: '#000'
}
},
tickWidth: 0
},
yAxis: {
min: 0,
max: 40000,
gridLineColor: '#d6d6d6',
title: {
text: 'Percent',
style: {
color: '#FFFFFF',
fontFamily: 'inherit',
fontSize: 'inherit'
}
},
labels: {
overflow: 'justify',
style: {
color: '#000'
}
},
tickPositioner: function(min, max) {
var each = Highcharts.each,
ticks = [];
for (var i = min; i <= max; i++) {
if (i % 5000 === 0)
ticks.push(i);
}
return ticks;
}
},
tooltip: {
enabled: false
},
legend: {
symbolRadius: 0,
itemStyle: {
color: '#000'
},
itemHoverStyle: {
color: '#86BC25'
},
itemMarginBottom: 5
},
plotOptions: {
column: {
pointPadding: 0,
borderColor: null,
dataLabels: {
enabled: true,
color: '#000',
format: "{y}",
style: {
textOutline: false
}
},
events: {
legendItemClick: function() {
return false;
}
}
},
series: {
groupPadding: 0.1
}
},
series: [{
name: 'A',
color: '#000',
data: [10000, 34000, 25000, 6000]
}, {
name: 'B',
color: 'blue',
data: [5000, 4000, 10000, 20000]
}, {
name: 'C',
color: '#046A38',
data: [6000, 15000, 5000, 1500]
}, {
name: 'D',
color: 'green',
data: [8700, 12000, 7000, 15000]
}]
});
});
html,
body {
padding: 0;
margin: 0px;
font-family: Helvetica, sans-serif;
font-size: 14px;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
background-color: #fff;
}
<script src="https://code.highcharts.com/highcharts.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="sd-median-annual-earnings-chart" style="height:450px;width:100%;"></div>
Here is my fiddle: https://jsfiddle.net/Brianing/wtoy7L15/

Use SVGRenderer to add line and text inside charts
chart: {
type: 'column',
backgroundColor: null,
style: {
fontSize: 'inherit',
overflow: 'visible',
width: '100%'
},
events: {
load: function() {
var ren = this.renderer,
stline = ['M', 100, 0, 'L', 340, 0];
ren.label('Label', this.chartWidth - 155, this.chartHeight - 50)
.css({
fontSize: '10px',
color: '#FF0000'
})
.add();
ren.path(stline)
.attr({
'stroke-width': 2,
stroke: '#FF0000'
})
.translate(this.chartWidth - 360, this.chartHeight - 50)
.add();
}
}
},
Note works best on fixed sized containers
$(function() {
Highcharts.chart('sd-median-annual-earnings-chart', {
credits: {
enabled: false
},
chart: {
type: 'column',
backgroundColor: null,
style: {
fontSize: 'inherit',
overflow: 'visible',
width: '100%'
},
events: {
load: function() {
var ren = this.renderer,
stline = ['M', 100, 0, 'L', 340, 0];
ren.label('Label', this.chartWidth - 155, this.chartHeight - 50)
.css({
fontSize: '10px',
color: '#FF0000'
})
.add();
ren.path(stline)
.attr({
'stroke-width': 2,
stroke: '#FF0000'
})
.translate(this.chartWidth - 360, this.chartHeight - 50)
.add();
}
}
},
title: {
text: 'Median Annual Earnings',
style: {
color: '#000',
fontFamily: 'inherit',
fontSize: '1.2em',
fontWeight: 700
}
},
xAxis: {
categories: [
'A',
'B',
'C',
'D'
],
labels: {
overflow: 'justify',
style: {
color: '#000'
}
},
tickWidth: 0
},
yAxis: {
min: 0,
max: 40000,
gridLineColor: '#d6d6d6',
title: {
text: 'Percent',
style: {
color: '#FFFFFF',
fontFamily: 'inherit',
fontSize: 'inherit'
}
},
labels: {
overflow: 'justify',
style: {
color: '#000'
}
},
tickPositioner: function(min, max) {
var each = Highcharts.each,
ticks = [];
for (var i = min; i <= max; i++) {
if (i % 5000 === 0)
ticks.push(i);
}
return ticks;
}
},
tooltip: {
enabled: false
},
legend: {
symbolRadius: 0,
itemStyle: {
color: '#000'
},
itemHoverStyle: {
color: '#86BC25'
},
itemMarginBottom: 5
},
plotOptions: {
column: {
pointPadding: 0,
borderColor: null,
dataLabels: {
enabled: true,
color: '#000',
format: "{y}",
style: {
textOutline: false
}
},
events: {
legendItemClick: function() {
return false;
}
}
},
series: {
groupPadding: 0.1
}
},
series: [{
name: 'A',
color: '#000',
data: [10000, 34000, 25000, 6000]
}, {
name: 'B',
color: 'blue',
data: [5000, 4000, 10000, 20000]
}, {
name: 'C',
color: '#046A38',
data: [6000, 15000, 5000, 1500]
}, {
name: 'D',
color: 'green',
data: [8700, 12000, 7000, 15000]
}]
});
});
html,
body {
padding: 0;
margin: 0px;
font-family: Helvetica, sans-serif;
font-size: 14px;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
background-color: #fff;
}
<script src="https://code.highcharts.com/highcharts.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="sd-median-annual-earnings-chart" style="width: 600px; height: 250px; margin: 0 auto"></div>

Related

highcharts x axis categories not updating dynamically vuejs

Below is the highchart application iam working.In mounted i will call axios post that will fetch x axis lables as array.But the x axis categories of highchart is not updating with the given array
Below is the code
<template>
<div class="container">
<div class="row">
<div class="col-md-12">
<div id="container" style="min-width: 100%; max-width:100%; height: 600px; margin: 0 auto"></div>
</div>
</div>
</div>
</template>
<script>
var $ = require('../../node_modules/jquery/dist/jquery.js')
var Highcharts = require('highcharts')
// Load module after Highcharts is loaded
require('highcharts/modules/exporting')(Highcharts)
// Create the chart
$(function () {
Highcharts.chart('container', {
chart: {
marginTop: 120,
marginBottom: 100,
type: 'bar'
},
credits: {enabled: false},
exporting: {enabled: false},
legend: {
enabled: true,
layout: 'vertical',
backgroundColor: '#FFFFFF',
floating: true,
align: 'right',
verticalAlign: 'bottom',
margin: 50,
reversed: true
},
title: {text: null},
tooltip: {},
plotOptions: {
series: {
stacking: 'normal'
}
},
series: [{
name: 'viewers',
color: '#006666',
pointWidth: 40,
data: [8, 16]
}, {
name: 'calls',
color: '#00FF00',
pointWidth: 40,
data: [7, 14]
}, {
name: 'chats',
color: '#FF8C00',
pointWidth: 40,
data: [8, 16]
}],
xAxis: {
categories: [],
labels: {
overflow: 'right',
display: 'block',
align: 'left',
x: 5,
style: {
fontSize: '1em',
color: '#000',
width: '500px'
}
}
},
yAxis: {
min: 0,
allowDecimals: false,
title: {
text: ''
}
}
})
})
export default {
data () {
return {
}
},
mounted () {
//will fetch labels from db
var arr = ['computers', 'games']
this.catList = arr
}
}
</script>
<style scoped>
.container{
margin: 0 auto;
}
#container{
margin: 0 auto;
min-width: 100%;
padding: 0;
max-width:100%;
height: 400px;
margin: 0 auto;
}
</style>
Please give solution to above problem.
To make categories reactive, you should put Highchart code inside component
Note that in xAxis config, we need to put categories: this.categories
new Vue({
el: '#app',
data() {
return {
chart: null,
categories: []
}
},
mounted() {
this.drawChart()
// example after pull data
var arr = ['computers', 'games']
const vm = this;
setTimeout(function() {
vm.categories = arr
vm.drawChart()
}, 2000)
},
methods: {
drawChart() {
if (this.chart) {
this.chart.destroy();
}
this.chart = Highcharts.chart('container', {
chart: {
marginTop: 120,
marginBottom: 100,
type: 'bar'
},
credits: {
enabled: false
},
exporting: {
enabled: false
},
legend: {
enabled: true,
layout: 'vertical',
backgroundColor: '#FFFFFF',
floating: true,
align: 'right',
verticalAlign: 'bottom',
margin: 50,
reversed: true
},
title: {
text: null
},
tooltip: {},
plotOptions: {
series: {
stacking: 'normal'
}
},
series: [{
name: 'viewers',
color: '#006666',
pointWidth: 40,
data: [8, 16]
}, {
name: 'calls',
color: '#00FF00',
pointWidth: 40,
data: [7, 14]
}, {
name: 'chats',
color: '#FF8C00',
pointWidth: 40,
data: [8, 16]
}],
xAxis: {
categories: this.categories,
labels: {
overflow: 'right',
display: 'block',
align: 'left',
x: 5,
style: {
fontSize: '1em',
color: '#000',
width: '500px'
}
}
},
yAxis: {
min: 0,
allowDecimals: false,
title: {
text: ''
}
}
})
}
}
})
Working example: https://jsfiddle.net/a9eqd8th/

Format y-axis values using numeral JSHighCharts

I want to format my values on the y-axis using HighCharts. Suppose I pass 14000, it should print "14K" on the y-axis. How can I do that? I've benn trying using numeral.js, but to no avail.
<!DOCTYPE html>
<html>
<head>
<title>Charts demo </title>
<script
src="https://code.jquery.com/jquery-3.2.1.slim.min.js"
integrity="sha256-k2WSCIexGzOj3Euiig+TlR8gA0EmPjuc79OEeY5L45g="
crossorigin="anonymous"></script>
<script src="https://code.highcharts.com/highcharts.js"></script>
<script src="http://code.highcharts.com/highcharts-more.js"></script>
<script src="http://code.highcharts.com/modules/solid-gauge.src.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/numeral.js/2.0.6/numeral.min.js"></script>
<script >
$(function () {
$('#container').highcharts({
colors: ['#8dafb7', '#f19962', '#e97381', '#f8c465', '#6cd299', '#87cfe2'],
chart: {
type: 'column',
marginBottom : 50,
marginTop : 150,
height : 700
},
title: {
text: ''
},
xAxis: {
categories: ['Q2,16', 'Q3,16', 'Q4,16', 'Q1,17'],
width: 700,
tickmarkPlacement: 'on',
labels: {
y : 40,
style: {
color: '#333333',
fontSize:'25',
fontFamily: 'ProximaNova-Light',
opacity : '.6'
},
}
},
yAxis: {
gridLineWidth: 0,
min: 0,
tickInterval: 20,
title: {
text: ''
},
labels: {
style: {
color: '#333333',
fontSize:'25',
fontFamily: 'ProximaNova-Light',
opacity : '.5'
},
formatter: function() {
return "$"+ this.value ;
}
},
stackLabels: {
style: {
color: '#555555',
fontSize:'25',
fontFamily: 'ProximaNova-Regular'
},
enabled: true,
formatter: function() {
return "$"+ this.total ;
}
}
},
legend:{
enabled:false,
width: 600,
floating: false,
x:50,
align: 'left',
style: {
color: '#555555',
fontSize:'25',
fontFamily: 'ProximaNova-Regular',
opacity : '.8'
},
borderWidth: 0
},
tooltip: {
enabled: false
},
credits : {
enabled : false
},
plotOptions: {
column: {
stacking: 'normal',
dataLabels: {
enabled: false,
color: (Highcharts.theme && Highcharts.theme.dataLabelsColor) || 'white'
}
}
},
series: [{
name: 'FTE-OpEx ',
data: [5000, 30000, 40000, 700000],
pointWidth: 30,
}, {
name: 'FTE-CapEx',
data: [20000, 20000, 30000, 20000],
pointWidth: 30
}, {
name: 'AWF-OpEx',
data: [30000, 40000, 4000, 2000],
pointWidth: 30
}, {
name: 'AWF-CapEx',
data: [3000, 4000, 4000, 2000],
pointWidth: 30
}, {
name: 'HW/SW',
data: [3000, 4000, 4000, 20000],
pointWidth: 30
}, {
name: 'Other',
data: [3000, 4000, 4000, 2000],
pointWidth: 30
}]
});
});
</script>
</head>
<body id="body">
<div id="container" style="height: 100%; width: 100%; position: center; margin-left: 5%; "></div>
</body>
</html>
}
It's problem with your formatter which returns default value, see API. If you want to keep numeric symbols, call defaultLabelFormatter.
Demo: http://jsfiddle.net/BlackLabel/uL2jx0t9/1/
yAxis: {
labels: {
formatter: function() {
return '$' + this.axis.defaultLabelFormatter.call(this);
}
}
}

How to customize a legend in highcharts as the image and while clicking on the leg end appears lines inside linechart

Highcharts.chart('myChart', {
chart:{
backgroundColor: 'transparent',
//polar: true,
height: 615,
type: 'line',
//marginTop: 27 ,
margin: [90, 10, 90, 10]
},
title: {
style: {
display: 'none'
}
},
credits: {
enabled: false
},
exporting: {
enabled: false
},
yAxis: {
tickPixelInterval: 50,
offset: -19,
floor: 0,
ceiling: 100,
gridLineDashStyle: 'dash',
gridLineColor: '#676767',
title: {
text: null
},
labels: {
style: {
color: 'red'
},
y: -8,
x: 0,
format: '{value} %'
},
},
xAxis: {
tickmarkPlacement:'on',
type: 'category',
categories: ['sun', 'mon', 'tue', 'wed', 'thu', 'fri', 'sat'],
max: 11,
gridLineColor: '#686868',
gridLineWidth: 1,
//offset: 50,
tickLength: 0,
lineColor: 'transparent',
labels: {
style: {
color: 'red'
},
x: 5,
//offset: -50
}
},
legend: {
enabled: false
},
plotOptions: {
series: {
lineWidth: 3,
marker: {
enabled: false
},
label: {
enabled: false
},
states: {
// hover: {
// enabled: true,
// lineWidth: 10
// },
},
events:{
click: function (event) {
lineWidth: 5
//event.point.series.chart.tooltip.refresh(e.point, e)
}
},
}
},
series: [{
name: 'PR',
data: [10,67,88],
color: '#35FEDB',
marker: {
states: {
hover: {
enabled: false
}
}
},
}, {
name: 'PREV',
data: [1, 9,27, 40, 57, 69, 93],
color: '#7EFF75',
marker: {
states: {
hover: {
enabled: false
}
}
},
}, {
name: 'GO',
data: [0, 28, 40, 42, 65, 69, 82],
color: '#EF4556',
marker: {
states: {
hover: {
enabled: false
}
}
},
}, {
name: 'Demo',
data: [0, 30,45, 45, 68, 88, 92],
color: 'white',
marker: {
states: {
hover: {
enabled: false
}
}
},
}],
tooltip: {
shared: false,
useHTML: true,
borderWidth: 0,
backgroundColor:'transparent',
style: {
fontWeight: 'bold',
fontSize: '13px',
padding: 0,
},
formatter: function() {
if (this.series.name == 'PR') {
$('.cyan-tooltip').text('PR '+this.y+'%');
return '<div class="pr-tooltip-head"style="color: black; background: #35FEDB; height: 80px; text-align: center; width: 190px; left: -7px; position: relative; padding: 5px;">' + this.series.name +
'<br/>'+ this.y +'%'+'</div>'
;
}
else if (this.series.name == 'PREV') {
$('.green-tooltip').html('PREVENDAS '+this.y+'%');
return '<div class="prevtooltip-head"style="color: black; background: #7EFF75; height: 80px; text-align: center; width: 190px; left: -7px; position: relative; padding: 5px;">' + this.series.name +
'<br/>'+ this.y +'%'+'</div>'
;
}
else if (this.series.name == 'GOB') {
$('.red-tooltip').html('GO '+this.y+'%');
return '<div class="go-tooltip-head" style="color: $secondary; background: #EF4556; height: 200px; text-align: left; width: 190px; left: -7px; position: relative; padding: 5px;">' + this.series.name +
'<br/>'+'Objetivo\'17'+'<br/>'+ '4.250.000.00'+ '<br/>'+'Objetivo Nov\'17'+'<br/>' + '<span class="seperator"></span><div style="text-align:center">'+ this.y +'%'+'</div>'+'</div>'
;
}
else if (this.series.name == 'Demo') {
return '<div class="demo-tooltip-head"style="color: black; background: $secondary; height: 80px; text-align: center; width: 190px; left: -7px; position: relative; padding: 5px;">' + this.series.name +
'<br/>'+ this.y +'%'+'</div>'
;
}
else return false;
},
},
responsive: {
rules: [{
condition: {
Width: 200
},
}]
}
});
(function (H) {
H.wrap(H.Tooltip.prototype, 'refresh', function (proceed, point, e) {
if (e && e.type !== 'mousemove') {
proceed.call(this, point, e);
}
});
H.addEvent(H.Point.prototype, 'click', function (e) {
e.point.series.chart.tooltip.refresh(e.point, e);
//e.point.series.chart.lineWidth.refresh(10);
});
}(Highcharts));
<div id="myChart"></div>
How to customize a legend in highcharts as given in image.
And place it right to the given line chart
Now I created a line chart using highchart. And it has 3 lines. While loading the chart all lines are visible. And now I want to make:
Invisible each line while clicking on corresponding colors in required legend.That is while clicking on red spot make invisible the red line.
And make visible by clicking again on the red spot.Similarly for rest of lines in the chart.
When red spot is clicked make other spot to see as disabled but not
completely
chart must not be shown empty, minimum accepted 1 line chart
Inside the green region y-axis value to be displayed while hovering green spot after clicking on the green line.Until clicking the line disable the hovering property.This is similar to all other colors
Refer to this live demo: http://jsfiddle.net/kkulig/zp7rjzx3/
I create and configure custom legend in chart.events.load:
chart: {
events: {
load: function() {
// legend box
var div = document.createElement('div');
div.setAttribute('class', 'legend-box');
document.getElementsByClassName('highcharts-container')[0].appendChild(div);
// items
var ul = document.createElement('ul');
ul.setAttribute('class', 'item-list');
div.appendChild(ul);
this.series.forEach(function(s) {
var li = document.createElement('li');
li.setAttribute('class', 'menu-item');
li.style.color = s.color;
ul.appendChild(li);
li.addEventListener('click', function() {
s.setVisible();
});
});
}
}
}
Associated CSS:
#container {
background-color: pink;
width: 600px;
}
.legend-box {
width: 50px;
height: 200px;
background: #455;
position: absolute;
top: 100px;
left: 500px;
border-radius: 25px;
font-size: 30px;
}
.item-list {
margin: 20px 0 0 5px;
}
.menu-item {
margin: 0 0 10px 0;
cursor: pointer
}
setVisible method serves for toggling visibility of a series.
You can easily implement other functionalities using this demo as starting point.
API reference: https://api.highcharts.com/class-reference/Highcharts.Series#setVisible

Highcharts display column category names on the xaxis

I have an issue displaying the column categories on the xaxis. I can get them to display in the "legend" and the "tooltips".
I have searched for a solution with no result. Can anyone see where I am going wrong.
My code:
$(document).ready(function() {
var options = {
chart: {
renderTo: 'container',
type: 'column',
marginRight: 130,
marginBottom: 25
},
title: {
text: '',
x: -20 //center
},
subtitle: {
text: '',
x: -20
},
xAxis: {
categories: ['Detractors','Passives','Promoters','Not used']
},
yAxis: {
endOnTick: false,
max:101,
showFirstLabel: false,
lineColor:'#999',
lineWidth:1,
tickColor:'#666',
tickWidth:1,
tickLength:2,
tickInterval: 30,
gridLineColor:'#ddd',
title: {
text: 'Percentage %',
style: {
fontFamily: 'Tahoma',
color: '#000000',
fontWeight: 'bold',
fontSize: '8px'
}
},
},
tooltip: {
formatter: function() {
return 'Guest responses:<br/> '+ this.y +'%<br/>'+ this.series.name +'<br/>';
}
},
legend: {
enabled: true,
layout: 'vertical',
align: 'right',
verticalAlign: 'top',
x: -10,
y: 100,
borderWidth: 0
},
colors: ['#FF0000','#FF6600','#009900','#333333'],
plotOptions: {
column: {
colorByPoint: false
},
series: {
cursor: 'pointer',
pointWidth: 20,
point: {
events: {
//click: function() {
}
},
legendIndex:0,
dataLabels: {
enabled: true,
color: '#000000',
align: 'center',
cursor: 'pointer',
//borderRadius: 5,
//backgroundColor: 'rgba(252, 255, 255, 255)',
//borderWidth: 1,
//borderColor: '#AAA',
y: -6,
format: '{y:.1f} %', // one decimal
y: -20, // 10 pixels down from the top
style: {
textShadow: false,
fontSize: '8px',
fontFamily: 'Verdana, sans-serif'
},
formatter: function() {
return '<b>Guest responses: '+ this.y +'<br/>'+ this.series.name +'%</b>';
}
}
}
},
credits: {
enabled: false
},
lang: {
noData: "No data"
},
noData: {
style: {
fontWeight: 'normal',
fontSize: '12px',
color: '#303030'
}
},
navigation: {
buttonOptions: {
enabled: false
}
},
series: []
}
$.getJSON("../charts/1-2-4-reports_chart.php?TAG=<?php echo $_SESSION['SectionVar'];?>&From=<?php echo $_SESSION['StartDate'];?>&To=<?php echo $_SESSION['EndDate'];?>", function(json) {
options.xAxis.categories = json[0];
options.series[0] = json[0];
options.series[1] = json[1];
options.series[2] = json[2];
options.series[3] = json[3];
chart = new Highcharts.Chart(options);
});
});
Many thanks in advance for your time.

Highcharts exporting className

How do you set a classname for an exported graph in Highcharts? I tried this but it did not work:
exporting: {
chartOptions: {
chart: {
className: 'negPos'
}
}
}
negativeColor add to plot options
Highcharts.chart(container, {
chart: {
//className: 'negPos',
type: 'column',
spacingBottom: 20,
style: {
fontFamily: 'Helvetica Neue,Helvetica,Arial,sans-serif',
fontSize: '11px',
fontWeight: 'lighter'
}
},
title: {
text: 'Highcharts',
align: 'center',
style: {
fontSize: '15px',
fontFamily: 'Helvetica Neue,Helvetica,Arial,sans-serif',
fontWeight: 'lighter'
}
},
xAxis: {
categories: [1, 2, 3, 5, 6]
},
yAxis: {
title: {
text: '%'
}
},
tooltip: {
borderWidth: 0,
style: {
fontFamily: 'Helvetica Neue,Helvetica,Arial,sans-serif',
fontSize: '11px',
align: 'center'
},
formatter: function() {
return '<b>' + this.x + ': </b><br>' + Highcharts.numberFormat(this.y, 1) + '%'
}
},
plotOptions: {
column: {
negativeColor: '#d9534f',
threshold: 0,
}
},
/* plotOptions: {
bar: {
dataLabels: {
enabled: false
}
}
},*/
legend: {
layout: 'vertical',
verticalAlign: 'bottom',
borderWidth: 1,
backgroundColor: ((Highcharts.theme && Highcharts.theme.legendBackgroundColor) || '#FFFFFF'),
shadow: true
},
series: [{
showInLegend: false,
color: '#5cb85c',
data: [54, 21, -3, 18, 5, -11]
}]
});
<script src="https://code.highcharts.com/highcharts.js"></script>
<script src="https://code.highcharts.com/modules/exporting.js"></script>
<div id="container"></div>

Resources