I am trying to color Y axes on a Highstock chart, I tried changing the plotBackgroundColor but it changes the whole plot area.
2 images are more than 2 thousand words:
On the left I have my current result, it changes the whole plot area background color, so is not easy to see where a chart ends and the next one starts, On the right is the expected result, each axis is separated by the background color and a gray border.
This is my code:
var p0 = [
[1540166400000,122.85],
[1540170000000,122.33],
[1540173600000,120.96],
[1540177200000,120.77],
[1540180800000,120.84],
[1540184400000,120.61],
[1540188000000,120.83],
[1540191600000,121.04],
[1540195200000,120.67],
[1540198800000,121.05],
[1540202400000,122.29],
[1540206000000,122.45],
[1540209600000,123.13],
[1540213200000,123.74],
[1540216800000,124.07],
[1540224000000,123.28],
[1540227600000,122.59],
[1540231200000,122.98],
[1540234800000,123.51],
[1540238400000,123.38],
[1540242000000,123.45],
[1540245600000,123.15],
[1540249200000,123.72],
[1540252800000,123.65],
[1540256400000,123.65],
[1540260000000,123.28],
[1540263600000,122.56],
[1540267200000,122.41],
[1540270800000,122.23],
[1540274400000,122.22],
[1540278000000,122.4],
[1540281600000,121.57],
[1540285200000,121.69],
[1540288800000,121.98]];
var p1 = [
[1540166400000,127.27],
[1540170000000,126.74],
[1540173600000,125.46],
[1540177200000,125.49],
[1540180800000,125.71],
[1540184400000,125.49],
[1540188000000,125.57],
[1540191600000,126.02],
[1540195200000,125.62],
[1540198800000,125.88],
[1540202400000,126.49],
[1540206000000,127.11],
[1540209600000,127.97],
[1540213200000,128.84],
[1540216800000,128.66],
[1540224000000,127.63],
[1540227600000,126.98],
[1540231200000,127.63],
[1540234800000,127.77],
[1540238400000,127.21],
[1540242000000,127.24],
[1540245600000,126.87],
[1540249200000,126.99],
[1540252800000,127.04],
[1540256400000,127.47],
[1540260000000,126.91],
[1540263600000,126.3],
[1540267200000,126.36],
[1540270800000,126.07],
[1540274400000,126.09],
[1540278000000,126.18],
[1540281600000,125.54],
[1540285200000,125.7],
[1540288800000,126.06]];
chart = new Highcharts.StockChart({
chart: {
renderTo: "container",
backgroundColor:'#EBEBEB',
plotBackgroundColor:'#FFFFFF',
plotBorderColor: '#999999',
plotBorderWidth: 2
},
title: {
text: "Coloring axis",
},
legend: {
enabled: false
},
xAxis: {
ordinal: false
},
yAxis: [
{
minorTicks: 1,
height: "46%",
top: "4%"
},
{
minorTicks: 1,
height: "46%",
top: "54%"
}
],
series: [
{
name: "Val1 1",
data: p0,
yAxis: 0,
},
{
name: "Presión 2",
data: p1,
yAxis: 1,
},
]
});
<script src="http://code.highcharts.com/stock/highstock.js"></script>
<div id="container" style="width: 600px; height: 600px;"></div>
A workaround to achieve what you are after can be done by using a third yAxis, like this:
{
height: "4%",
top: "50%",
labels: {
enabled: false
},
min: 0,
max: 1,
minorGridLineWidth: 0,
gridLineWidth: 0,
plotBands: [{
color: '#EBEBEB',
borderColor:'#999999',
borderWidth: 2,
from: 0.05,
to: 0.95,
}
}
var p0 = [
[1540166400000,122.85],
[1540170000000,122.33],
[1540173600000,120.96],
[1540177200000,120.77],
[1540180800000,120.84],
[1540184400000,120.61],
[1540188000000,120.83],
[1540191600000,121.04],
[1540195200000,120.67],
[1540198800000,121.05],
[1540202400000,122.29],
[1540206000000,122.45],
[1540209600000,123.13],
[1540213200000,123.74],
[1540216800000,124.07],
[1540224000000,123.28],
[1540227600000,122.59],
[1540231200000,122.98],
[1540234800000,123.51],
[1540238400000,123.38],
[1540242000000,123.45],
[1540245600000,123.15],
[1540249200000,123.72],
[1540252800000,123.65],
[1540256400000,123.65],
[1540260000000,123.28],
[1540263600000,122.56],
[1540267200000,122.41],
[1540270800000,122.23],
[1540274400000,122.22],
[1540278000000,122.4],
[1540281600000,121.57],
[1540285200000,121.69],
[1540288800000,121.98]];
var p1 = [
[1540166400000,127.27],
[1540170000000,126.74],
[1540173600000,125.46],
[1540177200000,125.49],
[1540180800000,125.71],
[1540184400000,125.49],
[1540188000000,125.57],
[1540191600000,126.02],
[1540195200000,125.62],
[1540198800000,125.88],
[1540202400000,126.49],
[1540206000000,127.11],
[1540209600000,127.97],
[1540213200000,128.84],
[1540216800000,128.66],
[1540224000000,127.63],
[1540227600000,126.98],
[1540231200000,127.63],
[1540234800000,127.77],
[1540238400000,127.21],
[1540242000000,127.24],
[1540245600000,126.87],
[1540249200000,126.99],
[1540252800000,127.04],
[1540256400000,127.47],
[1540260000000,126.91],
[1540263600000,126.3],
[1540267200000,126.36],
[1540270800000,126.07],
[1540274400000,126.09],
[1540278000000,126.18],
[1540281600000,125.54],
[1540285200000,125.7],
[1540288800000,126.06]];
chart = new Highcharts.StockChart({
chart: {
renderTo: "container",
backgroundColor:'#EBEBEB',
plotBackgroundColor:'#FFFFFF',
plotBorderColor: '#999999',
plotBorderWidth: 2
},
title: {
text: "Coloring axis",
},
legend: {
enabled: false
},
xAxis: {
ordinal: false
},
yAxis: [
{
minorTicks: 1,
height: "46%",
top: "4%",
plotLines: [
]
},
{
minorTicks: 1,
height: "46%",
top: "54%",
plotLines: [
]
},
{
height: "4%",
top: "50%",
labels: {
enabled: false
},
min: 0,
max: 1,
minorGridLineWidth: 0,
gridLineWidth: 0,
plotBands: [{
color: '#EBEBEB',
borderColor:'#999999',
borderWidth: 2,
from: 0.05,
to: 0.95,
}]
}
],
series: [
{
name: "Val1 1",
data: p0,
yAxis: 0,
},
{
name: "Presión 2",
data: p1,
yAxis: 1,
},
]
});
<script src="https://code.highcharts.com/stock/highstock.js"></script>
<div id="container" style="width: 600px; height: 600px;"></div>
JSFiddle example: https://jsfiddle.net/ewolden/q826ze9v/8/
The "correct" way to achieve what you are after would be to follow highcharts' syncronized chart example: https://www.highcharts.com/demo/synchronized-charts. This is more complicated, but allows you much more customization, and would probably prove easier to work with long term.
I finally did it using highcharts SVGRenderer, drawing white rectangles for each axis at z-index:-1 to draw it on the background.
I hope this helps someone!
var p0 = [
[1540166400000,122.85],
[1540170000000,122.33],
[1540173600000,120.96],
[1540177200000,120.77],
[1540180800000,120.84],
[1540184400000,120.61],
[1540188000000,120.83],
[1540191600000,121.04],
[1540195200000,120.67],
[1540198800000,121.05],
[1540202400000,122.29],
[1540206000000,122.45],
[1540209600000,123.13],
[1540213200000,123.74],
[1540216800000,124.07],
[1540224000000,123.28],
[1540227600000,122.59],
[1540231200000,122.98],
[1540234800000,123.51],
[1540238400000,123.38],
[1540242000000,123.45],
[1540245600000,123.15],
[1540249200000,123.72],
[1540252800000,123.65],
[1540256400000,123.65],
[1540260000000,123.28],
[1540263600000,122.56],
[1540267200000,122.41],
[1540270800000,122.23],
[1540274400000,122.22],
[1540278000000,122.4],
[1540281600000,121.57],
[1540285200000,121.69],
[1540288800000,121.98]];
var p1 = [
[1540166400000,127.27],
[1540170000000,126.74],
[1540173600000,125.46],
[1540177200000,125.49],
[1540180800000,125.71],
[1540184400000,125.49],
[1540188000000,125.57],
[1540191600000,126.02],
[1540195200000,125.62],
[1540198800000,125.88],
[1540202400000,126.49],
[1540206000000,127.11],
[1540209600000,127.97],
[1540213200000,128.84],
[1540216800000,128.66],
[1540224000000,127.63],
[1540227600000,126.98],
[1540231200000,127.63],
[1540234800000,127.77],
[1540238400000,127.21],
[1540242000000,127.24],
[1540245600000,126.87],
[1540249200000,126.99],
[1540252800000,127.04],
[1540256400000,127.47],
[1540260000000,126.91],
[1540263600000,126.3],
[1540267200000,126.36],
[1540270800000,126.07],
[1540274400000,126.09],
[1540278000000,126.18],
[1540281600000,125.54],
[1540285200000,125.7],
[1540288800000,126.06]];
chart = new Highcharts.StockChart({
chart: {
renderTo: "container",
backgroundColor:'#EBEBEB'
},
title: {
text: "Coloring axis",
},
legend: {
enabled: false
},
xAxis: {
ordinal: false
},
yAxis: [
{
minorTicks: 1,
height: "46%",
top: "4%"
},
{
minorTicks: 1,
height: "46%",
top: "54%"
}
],
series: [
{
name: "Val1 1",
data: p0,
yAxis: 0,
},
{
name: "Presión 2",
data: p1,
yAxis: 1,
},
]
}, function (chart) { // on complete
var yaxisColl = chart.yAxis;
for(f=0;f<yaxisColl.length;f++){
axis = yaxisColl[f];
chart.renderer.rect(10, axis.top, axis.width, axis.height, 0, 2)
.attr({
'stroke-width': 2,
stroke: '#999999',
fill: '#FFFFFF',
zIndex: -1
})
.add();
}
});
<script src="http://code.highcharts.com/stock/highstock.js"></script>
<div id="container" style="width: 600px; height: 600px;"></div>
You could also do it using plotBands on each yAxis:
yAxis: [
{
minorTicks: 1,
height: "46%",
top: "4%",
plotBands: [{
from:-1000,
to:1000,
color:'rgb(255,255,255)'
}]
},
{
minorTicks: 1,
height: "46%",
top: "54%",
plotBands: [{
from:-1000,
to:1000,
color:'rgb(255,255,255)'
}]
}
]
Example: http://jsfiddle.net/jlbriggs/k2syL51r/
Im trying to create plotbands but does not work correctly.
Here is my fiddle and code.
https://jsfiddle.net/z0h85fku/
Javascript
$(function() {
categories = ["09/07/2016 00:00", "09/07/2016 00:01", "09/07/2016 00:02", "09/07/2016 00:03", "09/07/2016 00:04"]
rate_1 = [0.8, 0.54, 0.6, 0.3, 0.4]
rate_2 = [0.33, 0.16, 0.33, 0.3, 0.38]
rate_3 = [0.03, 0.04, 0.05, 0.03, 0.01]
var addCallout = function(chart) {
$('.callout').remove();
var xAxis = chart.xAxis[0],
yAxis = chart.yAxis[0],
series = chart.series[0],
point = series.data[0];
console.log('xAxis == ', xAxis)
console.log('yAxis == ', yAxis.toPixels)
console.log('series == ', series)
console.log('point == ', point)
console.log(point.plotY)
console.log(point.plotX)
var a = chart.renderer.label('<div class="callout top">This is the callout text!</div>', point.plotX + chart.plotLeft - 20,
point.plotY + chart.plotTop - 70, 'callout', null, null, true).add();
};
$('#container').highcharts({
chart: {
// type: 'bar',
events: {
load: function() {
addCallout(this);
},
redraw: function() {
addCallout(this);
},
}
},
title: {
text: 'Spikes Graph',
x: -20 //center
},
subtitle: {
text: '',
x: -20
},
events: {
load: function() {
// addCallout(this);
},
redraw: function() {
addCallout(this);
},
},
series: [{
turboThreshold: 2000 // to accept point object configuration
}],
xAxis: {
categories: categories,
<!-- tickInterval: 60,-->
plotBands: [{
color: 'orange', // Color value
// from: Date.UTC(09/07/2016 02:00), // Start of the plot band
// Date.UTC(year,month,day,hours,minutes,seconds,millisec)
from: Date.UTC(2016,9,7,0,0),
to: Date.UTC(2016,9,7,4,0)
// to: '09/07/2016 05:00' // End of the plot band
}],
type:'datetime'
},
yAxis: {
title: {
text: 'Error Rate'
},
min: 0,
max: 5,
plotLines: [{
value: 0,
width: 1,
color: '#808080'
}],
labels: {
format: '{value} %'
}
},
tooltip: {
valueSuffix: '°C'
},
legend: {
layout: 'vertical',
align: 'right',
verticalAlign: 'middle',
borderWidth: 0
},
series: [
// {turboThreshold: 2000 },
{
name: 'Rate-1',
data: rate_1,
turboThreshold: 2000,
lineWidth: 1,
dataLabels: {
enabled: true,
useHTML: true,
style: {
fontWeight: 'bold'
},
},
}, {
name: 'Rate-2',
data: rate_2,
turboThreshold: 2000,
lineWidth: 1
}, {
name: 'Rate-3',
data: rate_3,
turboThreshold: 2000,
lineWidth: 1
}
]
});
});
Below is the fiddle for a Pareto chart. I want to draw a horizontal line parallel to the x-axis which spans from left to right and ends at the 80% mark on the y-axis. I want to do this always while plotting a Pareto chart.
Can you let me know if there's a way to do this dynamically?
var chart = new Highcharts.Chart({
chart: {
renderTo: 'container',
defaultSeriesType: 'column',
borderWidth:1,
borderColor:'#ccc',
marginLeft:110,
marginRight:50,
//backgroundColor:'#eee',
//plotBackgroundColor:'#fff',
},
title:{
text:'Pareto Test 1'
},
legend:{
},
tooltip:{
formatter:function(){
if(this.series.name == 'Line'){
var pcnt = Highcharts.numberFormat((this.y / 415 * 100),0,'.');
return pcnt + '%';
}
return this.y;
}
},
plotOptions: {
series: {
shadow:false,
}
},
xAxis:{
categories:['A','B','C','D','E','F','G','H'],
lineColor:'#999',
lineWidth:1,
tickColor:'#666',
tickLength:3,
title:{
text:'X Axis Title',
style:{
color:'#000'
}
}
},
yAxis:[{
min:0,
//endOnTick:false,
//lineColor:'#999',
lineWidth:1,
//tickColor:'#666',
//tickWidth:1,
//tickLength:3,
//gridLineColor:'#ddd',
/* title:{
text:'Y Axis Title',
rotation:0,
margin:50,
style:{
color:'#000'
}
}*/
},{
title:{text:''},
//alignTicks:false,
gridLineWidth:0,
lineColor:'#999',
lineWidth:1,
tickColor:'#666',
tickWidth:1,
tickLength:3,
tickInterval:415 / 20,
endOnTick:false,
opposite:true,
linkedTo:0,
labels:{
formatter:function(){
var pcnt = Highcharts.numberFormat((this.value / 415 * 100),0,'.');
return pcnt + '%';
}
}
}],
series: [{
//yAxis:0,
data: [115,75,60,55,45,30,20,15],
},{
type:'line',
name:'Line',
//yAxis:0,
data: [115,190,250,305,350,380,400,415],
}]
});
<script type="text/javascript" src="http://code.highcharts.com/highcharts.src.js"></script>
<div id="container" style="height: 400px"></div>
You can set this up the same way you calculated what 80% is. See the Fiddle below. Here is some sample code to add plotLines:
plotLines: [{
color: '#FF0000',
width: 2,
value: .80 * 415 // Need to set this probably as a var.
}]
var chart = new Highcharts.Chart({
chart: {
renderTo: 'container',
defaultSeriesType: 'column',
borderWidth: 1,
borderColor: '#ccc',
marginLeft: 110,
marginRight: 50,
//backgroundColor:'#eee',
//plotBackgroundColor:'#fff',
},
title: {
text: 'Pareto Test 1'
},
legend: {
},
tooltip: {
formatter: function() {
if (this.series.name == 'Line') {
var pcnt = Highcharts.numberFormat((this.y / 415 * 100), 0, '.');
return pcnt + '%';
}
return this.y;
}
},
plotOptions: {
series: {
shadow: false,
}
},
xAxis: {
categories: ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H'],
lineColor: '#999',
lineWidth: 1,
tickColor: '#666',
tickLength: 3,
title: {
text: 'X Axis Title',
style: {
color: '#000'
}
}
},
yAxis: [{
min: 0,
//endOnTick:false,
//lineColor:'#999',
lineWidth: 1
//tickColor:'#666',
//tickWidth:1,
//tickLength:3,
//gridLineColor:'#ddd',
/* title:{
text:'Y Axis Title',
rotation:0,
margin:50,
style:{
color:'#000'
}
}*/
}, {
title: {
text: ''
},
//alignTicks:false,
gridLineWidth: 0,
lineColor: '#999',
lineWidth: 1,
tickColor: '#666',
tickWidth: 1,
tickLength: 3,
tickInterval: 415 / 20,
endOnTick: false,
opposite: true,
linkedTo: 0,
labels: {
formatter: function() {
var pcnt = Highcharts.numberFormat((this.value / 415 * 100), 0, '.');
return pcnt + '%';
}
},
plotLines: [{
color: '#FF0000',
width: 2,
value: .80 * 415 // Need to set this probably as a var.
}]
}],
series: [{
//yAxis:0,
data: [115, 75, 60, 55, 45, 30, 20, 15]
}, {
type: 'line',
name: 'Line',
//yAxis:0,
data: [115, 190, 250, 305, 350, 380, 400, 415]
}]
});
<script type="text/javascript" src="http://code.highcharts.com/highcharts.src.js"></script>
<div id="container" style="height: 400px"></div>
What you want to look at is a plot line:
http://api.highcharts.com/highcharts#yAxis.plotLines
{{EDIT:
Also, look here for slightly improved way of getting the data sum dynamically:
Highcharts percentage of total for simple bar chart
I have the charts of lib HighCharts and I want to remove the gridline of yAxis on charts
I write gridLineWidth: 0
but gridlines are not removing
All code:
<script type="text/javascript">
(function($){ // encapsulate jQuery
$(function() {
Highcharts.setOptions({
lang: {
rangeSelectorZoom: 'Маcштаб',
rangeSelectorFrom: 'От',
rangeSelectorTo: 'До',
thousandsSep: ' '
},
global: {
useUTC: false
}
});
$.getJSON('http://www.highcharts.com/samples/data/jsonp.php?filename=aapl-c.json&callback=?', function(data) {
// Create the chart
window.chart = new Highcharts.StockChart({
chart : {
borderColor: 'white',
renderTo : <?php echo "cont".$i; ?>,
backgroundColor: '#f9f9f9' // Сделаем слегка серый фон
},
rangeSelector: {
buttons: [
{
type: 'week',
count: 1,
text: 'Неделя',
},
{
type: 'month',
count: 1,
text: 'Месяц',
},
{
type: 'year',
count: 1,
text: 'Год'
},
{
type: 'all',
text: 'Всё'
}],
inputDateFormat: '%d.%m.%Y', // Меняем на привычный для нас формат даты в интервалах
inputEditDateFormat: '%d.%m.%Y',
buttonTheme: {
width: 43 // Увеличим ширину кнопки
},
selected: 1 // Какая кнопка выбрана по умолчанию
},
yAxis: [{
gridLineWidth: 0,
plotBands: [{
color: 'rgba(1, 143, 189, 1)',
from: -2,
to: 11
},
{
color: 'rgba(157, 200, 5, 1)',
from: 11,
to: 21
},
{
color: 'rgba(202, 1, 94, 1)',
from: 21,
to: 50
}],
title: {
text: 'Позиции'
},
startOnTick: false,
// min: 1,
showFirstLabel: true,
showLastLabel: true,
reversed: true,
tickPositioner: function(min, max) {
// specify an interval for ticks or use max and min to get the interval
var interval = Math.round((max-min)/5);
// push the min value at beginning of array
var dataMin=this.dataMin;
var dataMax=this.dataMax;
var positions = [dataMin];
var defaultPositions = this.getLinearTickPositions(interval, dataMin, max);
//push all other values that fall between min and max
for (var i = 0; i < defaultPositions.length; i++) {
if (defaultPositions[i] > dataMin && defaultPositions[i] < dataMax) {
positions.push(defaultPositions[i]);
}
}
// push the max value at the end of the array
positions.push(dataMax);
return positions;
},
//changed min valuereversed: true
}],
navigator: {
enabled: false,
maskFill: 'rgba(255, 255, 255, 0.45)',
//margin: 20,
series: {
type: 'areaspline',
color: 'rgba(255, 255, 255, 0.00)',
fillOpacity: 0.4,
dataGrouping: {
smoothed: false
},
lineWidth: 2,
lineColor: '#e9cc00',
marker: {
enabled: false
},
shadow: true
},
yAxis: {
reversed: true
}
},
xAxis : {
gridLineWidth: 0,
type: 'datetime',
title : {
text : ' '
},
},
title : {
//text : 'Позиции сайта'
},
legend: {
enabled: true,
align: 'center',
itemWidth: 234, // указал ширину, чтобы выводились сайты в 4 колонки
verticalAlign: 'top'
},
series : [{
lineColor: 'white',
name : 'Позиция в яндексе',
id : 'dataseries',
data : <?php echo $d ?>,
tooltip: {
backgroundColor: 'rgba(250, 250, 250, .85)', // Фон немного темнее
borderColor: 'rgba(100, 100, 100, .90)', // Цвет границы (по умолчанию меняется автоматом)
xDateFormat: '%d.%m.%Y %H:%M', // Наш формат даты
// Тут немного увеличиваем размер даты
headerFormat: '<span style="font-size: 12px">{point.key}</span><br/>',
// Формат надписей в подсказке, названия цветом графика, а значения жирным
pointFormat: '<span style="color:{series.color}">{series.name}</span>: <b>{point.y}</b><br/>',
//valueDecimals: 2
}
}]
});
});
});
})(jQuery);
</script>
The problem is not the gridLineWidth. You've set that correctly.
In addition you need to set the minorGridLineWidth that you have to set to 0
Working demo
If you doesn't want to touch the config object, you just hide the grid by css:
.chart-container .highcharts-grid {
display: none;
}
you just need to gridLineWidth set to 0
yAxis: {
min:0,
categories: ["","Low","Medium","High"],
tickWidth: 0,
crosshair: false,
lineWidth: 0,
gridLineWidth:0,//Set this to zero
title: '',
labels: {
formatter: function () {
return this.value;labels
}
},
showEmpty: false
}
None of the mentioned solutions worked for me, so this one finally worked (taken from Sparklines examples: https://www.highcharts.com/demo/sparkline):
yAxis: {
startOnTick: false,
endOnTick: false,
tickPositions: [],
}
for all other lines here is what worked for me, in some cases using line transparency as the color was the only solution I could find.
$(function() {
$('#container').highcharts({
colors: ['#00f900', '#ffff3c', '#ff2600'],
credits: {
enabled: false
},
exporting: {
enabled: false
},
legend: {
itemDistance: 60
},
lineColor: 'red',
chart: {
type: 'column',
backgroundColor: 'transparent'
},
title: {
text: ''
},
legend: {
itemStyle: {
color: 'white',
fontWeight: 'normal',
fontFamily: 'helvetica',
fontSize: '12px'
}
}
// ....rest in js fiddle
http://jsfiddle.net/fv50sLkj/23/