Chart Title overlapping on chart - highcharts

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
}

Related

On Mouse over on the high chart data the tool tip value should be displayed next to legend label

I am using high chart in my project. Currently when user mouse over on the data points the tool tip will be displayed top to the data points. Some times the data are big and the tool tip will look very big, hence user wants to display the tool tip value next to the legend title during mouse over of the data points.
Example Legends should look like below:
Female % 23 Male % 22
Here 23 and 22 are the data points value which should be printed next to legend label when we mouse over on the data points.
The 23 and 22 value should be changed as per the mouse over on the data points.
enter code here
primaryChart = $('#container').highcharts('StockChart', {
chart: {
// my code
},
yAxis: [{
// my code
}],
xAxis: {
// my code
},
plotOptions: {
// my code
},
tooltip: {
//enabled: false,
followPointer: false,
useHTML: true,
formatter: function () {
//debugger;
var precision;
var s = '<b>' + Highcharts.dateFormat('%A, %b %e %Y, %H:%M:%S', new Date(this.x)) + '</b>';
$.each(this.points, function () {
precision = GetTooltipData(this.series.options.paramID);
s += '<br/ ><span style="color:' + this.series.options.color + '">' + this.series.name + '</span> : ' + parseFloat(this.y.toFixed(precision)) + ' ' + this.series.yAxis.axisTitle.textStr;
// s += "<div class='comment_filter'><a class='comments_buble' href='#' data-series='" + this.point.index + "'>Comment</a></div>";
});
return s;
},
shared: true
},
series: chartData,
rangeSelector: {
selected: 4,
inputEnabled: false,
buttonTheme: {
visibility: 'hidden'
},
labelStyle: {
visibility: 'hidden'
}
},
legend: {
enabled: true,
layout: 'horizontal',
align: 'left',
borderColor: '#909090',
verticalAlign: 'top'
},
credits: {
enabled: false
}
});
You can use positioner function for tooltip to place it next to a legend:
tooltip: {
positioner: function() {
var legend = this.chart.legend,
x = legend.group.translateX + legend.legendWidth,
y = legend.group.translateY;
return {
x: x,
y: y
};
},
...
}
Live demo: https://jsfiddle.net/BlackLabel/3rsp4bdy/
API Reference: https://api.highcharts.com/highcharts/tooltip.positioner
enter code here
tooltip: {
followPointer: true,
useHTML: true,
split: false,
formatter: function () {
var s = '<span>' + this.series.name +'</span>';
this.series.legendItem.element.innerHTML = s;
return false;
},
shared: true,
},

Highchart, Plot bands for each bar

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

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.

Highcharts - Fix the Width of the xAxis Categories (and Wrap them)

I have a bar chart with some longer xAxis categories (URLs). I would like to set the width of the display area for these categories and have them wrap accordingly. As it currently stands, the categories are forcing my chart area too far to the right.
Here is my code:
$(function () {
$('#container').highcharts({
chart:{
type:'bar',
height:1500
},
title:{text:false},
legend:{enabled:false},
xAxis:{
title:{text:null},
categories:[
'javascript:%3B',
'http:\/\/www.feathersflights.com\/',
'http:\/\/www.feathersflights.com\/search\/label\/sewing101',
'http:\/\/www.feathersflights.com\/search\/label\/You%20Flew%3A%20Sewing%20Linkup',
'http:\/\/www.feathersflights.com\/p\/tutorials-and-past-projects.html',
'https:\/\/www.facebook.com\/feathersflights',
'https:\/\/twitter.com\/feathersflights',
'http:\/\/pinterest.com\/feathersflights\/',
'http:\/\/instagram.com\/feathersflights#',
'https:\/\/plus.google.com\/u\/0\/101032706297651559107',
'http:\/\/www.bloglovin.com\/blog\/2866677\/feathers-flights-a-creative-sewing',
'mailto:feathersflights%40gmail.com',
'http:\/\/feeds.feedburner.com\/FeathersFlights',
'http:\/\/www.blogger.com\/rearrange?blogID%3D7070182830087816119%26widgetType%3DHTML%26widgetId%3DHTML6%26action%3DeditWidget%26sectionId%3Dheader-right',
'http:\/\/www.feathersflights.com\/p\/about.html',
'http:\/\/www.feathersflights.com\/p\/cant-live-without.html',
'http:\/\/www.feathersflights.com\/p\/sponsor.html',
'http:\/\/feathersflights.bigcartel.com\/',
'http:\/\/www.blogger.com\/rearrange?blogID%3D7070182830087816119%26widgetType%3DHTML%26widgetId%3DHTML7%26action%3DeditWidget%26sectionId%3Dcrosscol',
'http:\/\/www.blogger.com\/rearrange?blogID%3D7070182830087816119%26widgetType%3DHTML%26widgetId%3DHTML4%26action%3DeditWidget%26sectionId%3Dsidebar-right-1',
'http:\/\/www.blogger.com\/rearrange?blogID%3D7070182830087816119%26widgetType%3DHTML%26widgetId%3DHTML15%26action%3DeditWidget%26sectionId%3Dsidebar-right-1',
'http:\/\/www.blogger.com\/rearrange?blogID%3D7070182830087816119%26widgetType%3DText%26widgetId%3DText2%26action%3DeditWidget%26sectionId%3Dsidebar-right-1',
'http:\/\/www.blogger.com\/rearrange?blogID%3D7070182830087816119%26widgetType%3DCustomSearch%26widgetId%3DCustomSearch1%26action%3DeditWidget%26sectionId%3Dsidebar-right-1',
'http:\/\/www.blogger.com\/rearrange?blogID%3D7070182830087816119%26widgetType%3DText%26widgetId%3DText3%26action%3DeditWidget%26sectionId%3Dsidebar-right-1',
'http:\/\/www.blogger.com\/rearrange?blogID%3D7070182830087816119%26widgetType%3DFollowers%26widgetId%3DFollowers1%26action%3DeditWidget%26sectionId%3Dsidebar-right-1'
]
},
yAxis:{
min:0,
title:{text:"Number of Links"},
labels:{style:{width:'1000px'}
}
},
tooltip:{
formatter:function() {
return '' + this.series.name +': '+ this.y;
}
},
plotOptions:{
bar:{
dataLabels:{
enabled:1
}
}
},
credits:{enabled:false},
series:[
{
name:'Number of Links',
data:[86,61,35,34,32,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30],
color:'#F79646'
}
]
});
});
Link to the Chart on JSFiddle:
http://jsfiddle.net/vCrc7/1/
Any suggestions?
You could forcefully break up the long string. For instance this inserts a <br/> every 25 chars:
labels:{
formatter: function(){
var rV = '';
var someCat = this.value;
while (someCat .length > 0) {
rV += someCat.substring(0, 25) + '<br/>';
someCat = someCat.substring(25);
}
return rV ;
}
},
Updated fiddle.

Highcharts - remove points from series

Is it possible to remove certain points from a series?
I'm looking for some way to draw a chart with a fixed back period,
something like: the last 1 hour.
I know how to add points using dynamic update:
http://www.highcharts.com/demo/dynamic-update
But in my case the time interval between points is not constant,
so I can't just use the shift option of addPoint.
Thanks,
Omer
If you want to have the same, you can use the same logic as in addPoint, see: http://jsfiddle.net/JKCLx/1/
However, why can't you just use shift argument in addPoint()?
I think I found a partial answer to my own question:
I can iterate over the series datapoints like this:
http://api.highcharts.com/highcharts#Series.data
and then call point.Remove.
The problem with this solution is that it does not draw monitor style like in the example,
but rather it redrwas the entire chart on each change.
http://jsfiddle.net/JKCLx/
$(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, false);
// series.data[0].remove(false);
series.data[0].remove(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: [{
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;
})()
}]
});
});
});

Resources