Highchart, Plot bands for each bar - highcharts
I need to show the design in highchart somewhat like the following code I found:
//-------------------------------------------------------
Highcharts.Renderer.prototype.symbols.line = function(x, y, width, height) {
return ['M',x ,y + width / 2,'L',x+height,y + width / 2];
};
//-------------------------------------------------------
Highcharts.setOptions({
chart:{
type:'column',
inverted:false,
},
credits:{enabled:false},
exporting:{enabled:false},
legend:{enabled:false},
title:{text:''},
xAxis:{
tickLength:0,
lineColor:'#999',
lineWidth:1,
labels:{style:{fontWeight:'bold'}}
},
yAxis:{
min:0,
minPadding:0,
maxPadding:0,
tickColor:'#ccc',
tickWidth:1,
tickLength:3,
gridLineWidth:0,
endOnTick:true,
title:{text: ''},
labels:{
y:10,
style:{
fontSize:'8px'
},
formatter:function(){
if (this.isLast){
return this.value + ' %';
}
else{
return this.value;
}
}
}
},
tooltip:{
enabled:true,
backgroundColor:'rgba(255, 255, 255, .85)',
borderWidth:0,
shadow:true,
style:{fontSize:'10px',padding:2},
formatter:function() {
return this.series.name + ": <strong>" + Highcharts.numberFormat(this.y,2) + "</strong>";
}
},
plotOptions:{
column:{
color:'#000',
shadow:false,
borderWidth:0,
},
scatter:{
marker:{
symbol:'line',
lineWidth:3,
radius:8,
lineColor:'#000'
}
}
}
});
//-------------------------------------------------------
var chart1 = new Highcharts.Chart({
chart:{renderTo:'container1'},
xAxis:{categories:['Title 1']},
yAxis:{
max:100,
labels:{y:10,style:{fontSize:'8px'}},
plotBands:[{from:0,to:70,color: 'rgba(103,103,103,.35)'},
{from:70,to:85,color: 'rgba(153,153,153,.35)'},
{from:85,to:100,color: 'rgba(204,204,204,.35)'}]
},
series:[{name:'Measure',pointWidth:10,data:[80]},
{name:'Target',type: 'scatter',data:[90],}]
});
//-------------------------------------------------------
var chart2 = new Highcharts.Chart({
chart:{renderTo:'container2'},
xAxis:{categories:['Title 2']},
yAxis:{
max:100,
labels:{y:10,style:{fontSize:'8px'}},
plotBands:[{from:0,to:75,color: 'rgba(103,103,103,.35)'},
{from:75,to:90,color: 'rgba(153,153,153,.35)'},
{from:90,to:100,color: 'rgba(204,204,204,.35)'}]
},
series:[{name:'Measure',pointWidth:10,data:[92]},
{name:'Target',type: 'scatter',data:[95],}]
});
//-------------------------------------------------------
var chart3 = new Highcharts.Chart({
chart:{renderTo:'container3'},
xAxis:{categories:['Title 3']},
yAxis:{
max:100,
labels:{y:10,style:{fontSize:'8px'}},
plotBands:[{from:0,to:50,color: 'rgba(103,103,103,.35)'},
{from:50,to:75,color: 'rgba(153,153,153,.35)'},
{from:75,to:100,color: 'rgba(204,204,204,.35)'}]
},
series:[{name:'Measure',pointWidth:10,data:[64]},
{name:'Target',type: 'scatter',data:[75],}]
});
//-------------------------------------------------------
var chart4 = new Highcharts.Chart({
chart:{renderTo:'container4'},
xAxis:{categories:['Title 4']},
yAxis:{
max:1000,
labels:{y:10,style:{fontSize:'8px'},formatter:function(){return this.value;}},
plotBands:[{from:0,to:600,color: 'rgba(103,103,103,.35)'},
{from:600,to:800,color: 'rgba(153,153,153,.35)'},
{from:800,to:1000,color: 'rgba(204,204,204,.35)'}]
},
series:[{name:'Measure',pointWidth:10,data:[970]},
{name:'Target',type: 'scatter',data:[850],}]
});
//-------------------------------------------------------
var chart5 = new Highcharts.Chart({
chart:{renderTo:'container5'},
xAxis:{categories:['Title 5']},
yAxis:{
max:500,tickInterval:100,
labels:{y:10,style:{fontSize:'8px'},formatter:function(){return this.value;}},
plotBands:[{from:0,to:300,color: 'rgba(103,103,103,.35)'},
{from:300,to:400,color: 'rgba(153,153,153,.35)'},
{from:400,to:500,color: 'rgba(204,204,204,.35)'}]
},
series:[{name:'Measure',pointWidth:10,data:[475]},
{name:'Target',type: 'scatter',data:[450],}]
});
//-------------------------------------------------------
body {
font-family:helvetica, sans-serif;
font-size:.7em;
}
p {
margin:.5em 1em;
}
#container1, #container2, #container3, #container4, #container5{display:inline-block;}
<script src="http://code.highcharts.com/highcharts.js"></script>
<script src="http://code.highcharts.com/highcharts-more.js"></script>
<script src="http://code.highcharts.com/modules/exporting.js"></script>
<div id="container1" style="height:auto;width:100px;"></div>
<div id="container2" style="height:auto;width:100px;"></div>
<div id="container3" style="height:auto;width:100px;"></div>
<div id="container4" style="height:auto;width:100px;"></div>
<div id="container5" style="height:auto;width:100px;"></div>
See the same in this fiddle
In the above code, 5 charts are used separately, with bands for each chart.
But I need to implement the bands for each bar instead of each chart. Is there an option for this in highchart?
You can apply specific gradient on plot bands to create tricolor effect.
DOCS Reference:
https://www.highcharts.com/docs/chart-design-and-style/colors
Example:
http://jsfiddle.net/vr1sz8yo/
You can add band to each of the columns on load event of highcharts if chart is dynamic or else hard code it for static data(use different id,color etc)
xAxis.plotBands reference
Here is dynamic example
chart: {
alignTicks: false,
events: {
load: function() {
console.log(this.xAxis[0])
var j = 0;
var ticks = this.xAxis[0].paddedTicks;
for (var i = 0; i < ticks.length; i++) {
if (i == 9) {
j = 0;
}
this.xAxis[0].addPlotBand({
from: ticks[i] - .5,
to: ticks[i] + .5,
color: Highcharts.getOptions().colors[j],
id: 'plot-band-1' + i
});
j++;
}
}
}
},
Fiddle demo
Related
Chart Title overlapping on chart
I am trying to put title on the chart but when I add title its overlapping chart itself. Here is jsfiddle code jsfiddle code .Do you guys know what property I need to set to make it work? //------------------------------------------------------- Highcharts.Renderer.prototype.symbols.line = function(x, y, width, height) { return ['M',x ,y + width / 2,'L',x+height,y + width / 2]; }; //------------------------------------------------------- Highcharts.setOptions({ chart:{ defaultSeriesType:'bar', marginTop:5, marginRight:15, marginBottom:10, marginLeft:100, borderWidth:0 }, credits:{enabled:false}, exporting:{enabled:false}, title:{text:'Title'}, xAxis:{ tickLength:0, lineColor:'#999', lineWidth:1, labels:{style:{fontWeight:'bold'}} }, yAxis:{ min:0, minPadding:0, maxPadding:0, tickColor:'#ccc', tickWidth:1, tickLength:3, gridLineWidth:0, endOnTick:true, title:{text: ''}, labels:{ y:10, style:{ fontSize:'8px' }, formatter:function(){ if (this.isLast){ return this.value + ' %'; } else{ return this.value; } }, enabled: false } }, legend:{enabled:false}, tooltip:{ enabled:true, backgroundColor:'rgba(255, 255, 255, .85)', borderWidth:0, shadow:true, style:{fontSize:'10px',padding:2}, formatter:function() { return this.series.name + ": <strong>" + Highcharts.numberFormat(this.y,2) + "</strong>"; } }, plotOptions:{ bar:{ color:'#000', shadow:false, borderWidth:0, }, scatter:{ marker:{ symbol:'line', lineWidth:3, radius:8, lineColor:'#f00' } } } }); //------------------------------------------------------- var chart1 = new Highcharts.Chart({ chart:{renderTo:'container1'}, xAxis:{categories:['Title 1']}, yAxis:{ max:100, labels:{y:10,style:{fontSize:'8px'}}, plotBands:[{from:0,to:70,color: 'rgba(103,103,103,.35)'}, {from:70,to:85,color: 'rgba(153,153,153,.35)'}, {from:85,to:100,color: 'rgba(204,204,204,.35)'}] }, series:[{name:'Measure',pointWidth:10,data:[80]}, {name:'Target',type: 'scatter',data:[90],}] });
Assuming you want to leave the height of you chart identical to what it is now, increse the height of the div by 30px and the chart.marginTop by 30px. <div id="container1" style="height:66px;width:350px;"></div> Highcharts.setOptions({ chart:{ defaultSeriesType:'bar', marginTop:35, marginRight:15, marginBottom:10, marginLeft:100, borderWidth:0 }, http://jsfiddle.net/blaird/e96yX/89/ Your issue is that there is no room for your title above your chart, so highcharts is putting it where it can. The chart margins set fixed spacing between the edges and the plot area, so if you want to use them you need to allow space for the items outside the plot area. http://api.highcharts.com/highcharts#chart.marginTop
Try using y attribute in legend which sets vertical position legend: { y:18 }
highcharts stockchart 1m and 3m not enabled
Please refer to http://jsfiddle.net/a6h6cLnt/. The '3m' and '1m' buttons are not working. Earlier with a smaller dataset, none of the zoom buttons were working. The url to the smaller dataset is http://jsfiddle.net/a6h6cLnt/1/ $(function () { var seriesOptions = [], seriesCounter = 0, names = ['MSFT'], // create the chart when all data is loaded createChart = function () { $('#container').highcharts('StockChart', { rangeSelector: { selected: 4 }, yAxis: { labels: { formatter: function () { return (this.value > 0 ? ' + ' : '') + this.value + '%'; } }, plotLines: [{ value: 0, width: 2, color: 'silver' }] }, plotOptions: { series: { compare: 'percent' } }, tooltip: { pointFormat: '<span style="color:{series.color}">{series.name}</span>: <b>{point.y}</b> ({point.change}%)<br/>', valueDecimals: 2 }, series: seriesOptions }); }; $.each(names, function (i, name) { $.getJSON('http://www.highcharts.com/samples/data/jsonp.php?filename=' + name.toLowerCase() + '-c.json&callback=?', function (data) { seriesOptions[i] = { name: name, data: [[1346371200000,12],[1348963200000,13],[1354233600000,4],[1377907200000,12],[1380499200000,13],[1385769600000,4],[1406764800000,1],[1409443200000,15],[1412035200000,20],[1414713600000,2],[1417305600000,4]] }; // As we're loading the data asynchronously, we don't know what order it will arrive. So // we keep a counter and create the chart when all the data is loaded. seriesCounter += 1; if (seriesCounter === names.length) { createChart(); } }); }); }); <script src="http://code.highcharts.com/stock/highstock.js"></script> <script src="http://code.highcharts.com/stock/modules/exporting.js"></script> <div id="container" style="height: 400px; min-width: 310px"></div> The '1m' and '3m' zoom buttons/links are not working. Please let me know how to enable them or make them work Regards Joseph
You need to set minRange parameter to define minimum zoom. Example: http://jsfiddle.net/a6h6cLnt/2/
Function to create Highcharts where the series has the correct prototype
I am attempting to write a function to add a highchart to a page and a function that can update the data for that chart based on a streaming API. I added a setInterval to simulate the streaming api. The issue occurs on line 80. I believe it is because I have not set the series array with the chart object properly. When I need to add new data via 'addPoint', the prototype is not there. What am I missing in my AddChart function that wires the series up to highcharts? FIDDLE: http://jsfiddle.net/puto3Lg0/2/ $(function () { $(document).ready(function () { var metrics = []; Highcharts.setOptions({ global: { useUTC: false } }); function AddChart(metric) { $("#divMain").append('<div id="' + metric.key + '" style="min-width: 310px; height: 200px; margin: 0 auto"></div>'); $('#' + metric.key).highcharts({ chart: { type: 'spline', animation: Highcharts.svg, // don't animate in old IE marginRight: 10, }, title: { text: metric.Title }, xAxis: { type: 'datetime', tickPixelInterval: 150 }, yAxis: { title: { text: 'Messages' }, plotLines: [ { value: 0, width: 1, color: '#808080' } ] }, tooltip: { formatter: function() { return '<b>' + this.series.name + '</b><br/>' + Highcharts.dateFormat('%Y-%m-%d %H:%M:%S', this.x) + '<br/>' + Highcharts.numberFormat(this.y, 2); } }, legend: { enabled: false }, exporting: { enabled: false }, series: metric.series }); }; function ParseData(message) { var jsonObj = JSON.parse(message); $.each(jsonObj.Metrics, function(index, value) { var metricName = value.Metric.Name.replace(' ', ''); if (metrics[metricName] == undefined) { metrics[metricName] = { "title": value.Metric.Name, "key": metricName, "series": [ { name: value.Metric.Name, data: [] } ], } AddChart(metrics[metricName]); } metrics[metricName].series.addPoint([new Date().getTime(), parseInt(value.Metric.CurrentValue)], true, false); }); }; setInterval(function () { var m = "{\"Metrics\": [{\"Metric\":{\"Name\":\"Queue 01\",\"CurrentValue\":\"0\",\"TimeStamp\":\"\\\x2FDate(1415826323291)\\\x2F\"}},{\"Metric\":{\"Name\":\"Queue 02\",\"CurrentValue\":\"3\",\"TimeStamp\":\"\\\x2FDate(1415826323344)\\\x2F\"}},{\"Metric\":{\"Name\":\"Queue 03\",\"CurrentValue\":\"9\",\"TimeStamp\":\"\\\x2FDate(1415826323405)\\\x2F\"}}]}"; ParseData(m); }, 1000); }); });
First, you have metrics declared as an array. Should be an empty object: var metrics = {}; Second, the data structure you've created, metrics[metricName].series is not a Highcharts series object. It's an object you created and used to supply Highcharts data. To get the real series object, you'll have to get it back from the chart. // getting the chart from the DOM, then the first series... $("#"+metricName).highcharts().series[0].addPoint([new Date().getTime(), parseInt(value.Metric.CurrentValue)], true, false); Updated fiddle.
highchart threshold color with live updating series
I'm trying to apply the new threshold/negativeColor features of Highcharts v3.0.2 to essentially the 'spline updating each second' example. During the animation of the chart updating, I'm seeing weird artifacts in the series line - it looks like it's animating to a different set of control points.. and then it snaps back to the correct configuration when the animation (of the new data point coming in) is done. Commenting out the threshold/negativeColor features makes this visual artifact go away. Am I seeing a bug? UPDATE: I'm posting the following code as an example, which is the stock highcharts demo (my local jquery is v1.10.2) with the threshold/color/negativeColor lines (first lines under series) added by me. This code seemingly misbehaves. <html> <head> <script src="js/jquery.min.js"></script> <script src="http://code.highcharts.com/highcharts.js"></script> <script src="http://code.highcharts.com/modules/exporting.js"></script> <script> $(function () { $(document).ready(function() { Highcharts.setOptions({ global: { useUTC: false } }); var chart; $('#container').highcharts({ chart: { type: 'spline', animation: Highcharts.svg, // don't animate in old IE marginRight: 10, events: { load: function() { // set up the updating of the chart each second var series = this.series[0]; setInterval(function() { var x = (new Date()).getTime(), // current time y = Math.random(); series.addPoint([x, y], true, true); }, 1000); } } }, title: { text: 'Live random data' }, xAxis: { type: 'datetime', tickPixelInterval: 150 }, yAxis: { title: { text: 'Value' }, plotLines: [{ value: 0, width: 1, color: '#808080' }] }, tooltip: { formatter: function() { return '<b>'+ this.series.name +'</b><br/>'+ Highcharts.dateFormat('%Y-%m-%d %H:%M:%S', this.x) +'<br/>'+ Highcharts.numberFormat(this.y, 2); } }, legend: { enabled: false }, exporting: { enabled: false }, series: [{ threshold: 0.5, color: '#FF0000', negativeColor: '#00FF00', name: 'Random data', data: (function() { // generate an array of random data var data = [], time = (new Date()).getTime(), i; for (i = -19; i <= 0; i++) { data.push({ x: time + i * 1000, y: Math.random() }); } return data; })() }] }); }); }); </script> </head> <body> <div id="container" style="min-width: 400px; height: 400px; margin: 0 auto"></div> </body> </html>
Indeed it looks like a bug, reported here. At this moment you can only disable animations.
Row names are overlapping
I have created scatter plot http://jsfiddle.net/ashwinp/4aDQ2/1/ Here row names are are overlapping on each other?and point start drawing from middle of the row name.Why cant we start with the beginning of the row name? Can i know how to solve this? Here is the code var InfluenceCnt=0; var QResiduals=[]; var XLables=[]; var HottelingT2=[]; var EllipseChartData; var EllipseShift; QResiduals.push('0.5356899'); QResiduals.push('0.3356899'); QResiduals.push('0.6356899'); QResiduals.push('0.2356899'); QResiduals.push('0.8356899'); QResiduals.push('0.2356899'); QResiduals.push('0.4356899'); QResiduals.push('0.4356899'); QResiduals.push('0.4356899'); QResiduals.push('0.2356899'); QResiduals.push('0.2356899'); QResiduals.push('0.5356899'); QResiduals.push('0.8356899'); QResiduals.push('0.9356899'); QResiduals.push('0.5356899'); QResiduals.push('0.7356899'); QResiduals.push('0.2356899'); QResiduals.push('0.1356899'); QResiduals.push('0.0356899'); QResiduals.push('0.5356899'); QResiduals.push('0.8356899'); QResiduals.push('0.7356899'); HottelingT2.push('0.1') HottelingT2.push('0.2'); HottelingT2.push('0.3'); HottelingT2.push('0.4'); HottelingT2.push('0.5'); HottelingT2.push('0.6'); HottelingT2.push('0.4') HottelingT2.push('0.5'); HottelingT2.push('0.3'); HottelingT2.push('0.2'); HottelingT2.push('0.6'); HottelingT2.push('0.7'); HottelingT2.push('0.8') HottelingT2.push('0.9'); HottelingT2.push('0.2'); HottelingT2.push('0.3'); HottelingT2.push('0.5'); HottelingT2.push('0.5'); HottelingT2.push('0.7') HottelingT2.push('0.8'); HottelingT2.push('0.9'); HottelingT2.push('0.4'); HottelingT2.push('0.5'); HottelingT2.push('0.6'); XLables.push('abc') XLables.push('bdef'); XLables.push('ceff'); XLables.push('ddds'); XLables.push('edf'); XLables.push('fdf'); XLables.push('abc') XLables.push('bdef'); XLables.push('ceff'); XLables.push('dddsert'); XLables.push('edf'); XLables.push('fdf'); XLables.push('abcert') XLables.push('bdefert'); XLables.push('ceffert'); XLables.push('retret'); XLables.push('edfert'); XLables.push('fdfret'); XLables.push('bdefert'); XLables.push('ceff'); XLables.push('ddds'); XLables.push('edf'); XLables.push('fdf'); $(function () { $(document).ready(function () { Highcharts.setOptions({ global: { useUTC: false } }); // Ellipse Plot EllipseChartData = new Highcharts.Chart({ chart: { renderTo: 'EllipseContainer', type: 'scatter', marginRight: 10, zoomType: 'xy', events: { load: function () { // set up the updating of the chart each second EllipseSeries = this.series[0]; setInterval(function () { EllipseShift = EllipseSeries.data.length > 20; if (!isNaN(QResiduals[InfluenceCnt]) && $.isNumeric(QResiduals[InfluenceCnt]) && typeof (QResiduals[InfluenceCnt]) != "undefined") { //alert(QResiduals[InfluenceCnt]); var x = HottelingT2[InfluenceCnt], // current time y = parseFloat(QResiduals[InfluenceCnt]); InfluenceCnt++; EllipseSeries.addPoint([x,y], true, EllipseShift); } }, 1000); } } }, title: { text: 'Ellipse Plot' }, xAxis: { title: { text: 'Sample' }, categories:XLables, plotLines: [{ value:2.5, color: 'red', dashStyle: 'shortdash', width: 2, label: { text: '' } }] }, yAxis: { title: { text: '' }, plotLines: [{ value: 0.4, color: 'red', dashStyle: 'shortdash', width: 2, label: { text: '' } }] }, tooltip: { formatter: function () { return '<b>' + this.series.name + '</b><br/>X: ' + this.x + '<br/> Y: ' + Highcharts.numberFormat(this.y, 2); } }, legend: { enabled: false }, exporting: { enabled: true }, series: [{ name: 'Ellipse Plot', data: [] }] }); }); }); Thanks in advance.
You have limited width, so labels overlaps because "have no space" to contain "margin/padding". You can try to useHTML as true: http://api.highcharts.com/highcharts#xAxis.labels.useHTML and then use formatter() http://api.highcharts.com/highcharts#xAxis.labels.formatter with i.e return '<div class="ownlabel">'+this.value+'</div>'; Then use your own css styles for defined "ownlabel".
The problem is that you are using categories, so Highcharts won't prevent any overlapping. I advice to use standard xAxis with formatter, take a look: http://jsfiddle.net/TEDBG/ xAxis: { allowDecimals: false, labels: { formatter: function() { return XLables[this.value]; } } }