Drawing horizontal lines parallel to x axis using highcharts.js - highcharts

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

Related

Highchartjs SubPlots not correctly stacked

I whish two plots stacked vertically:
chart = new Highcharts.chart({
chart: {
renderTo: 'prod',
defaultSeriesType: 'spline'
},
title: {
text: 'Sums'
},
xAxis: {
type: 'datetime',
labels: {
formatter: function() {
return Highcharts.dateFormat('%A %e - %b - %Y %k:%M ', this.value);
}
}
},
yAxis: [{
title: {
text: 'Plot1'
},
height: '25%',
offset: 0,
lineWidth: 1,
tickInterval: 50,
labels: {
formatter: function () {
var ret = this.value;
if(ret <= 50)
return ret;
}
}
},{
title: {
text: 'Plot2'
},
top: '25%',
height: '25%',
offset: 0,
lineWidth: 1,
tickInterval: 50,
}],
legend:false,
series: [
{
name:'plot1',
data:data1,
yAxis:0
},
{
name:'plot2',
data:data2,
yAxis:1
}
]
});
But what I get is this:
How can I remove the blanck space between the last plot and the xAxis?
You need to correctly set height and top for y-axis:
yAxis: [{
height: '50%',
...
}, {
top: '50%',
height: '50%',
...
}]
Live demo: http://jsfiddle.net/BlackLabel/j5hkcr8y/
API Reference:
https://api.highcharts.com/highcharts/yAxis.top
https://api.highcharts.com/highcharts/yAxis.height

Spline line is very curved, how to smooth it?

A smoother line is needed (the line should rise smoothly and evenly), but the values ​​should not change. Is it possible to build a more straight line without changing the values?
Screen with explanations: http://prntscr.com/qudf96
And my code:
<script>
function reDrawCalc(gdata, gcurr, nums = 2) {
Highcharts.chart('container', {
chart: {
height: 400,
type: 'area',
margin: [20, 0, 20, 0]
},
title: {
text: null
},
subtitle: {
text: null
},
exporting: {
enabled: false
},
xAxis: {
gridLineWidth: 1,
categories: ['One', 'Two', 'Three', 'Four', 'Five']
},
yAxis: {
gridLineWidth: 0,
},
tooltip: {
enabled: false,
crosshairs: true
},
plotOptions: {
series: {
dataLabels: {
enabled: true,
useHTML: true,
inside: false,
style: {
fontFamily: 'Rubik, sans-serif',
textTransform: 'uppercase',
fontSize: 'none',
fontWeight: 'normal',
textShadow: 'none'
},
formatter: function() {
return '<div class="chitem-time">'+ this.x+'</div>'
+'<div class="chitem-val">'+ Highcharts.numberFormat(this.y,nums)+'<sup>'+gcurr+'</sup></div>';
}
}
}
},
series: [{
type: 'areaspline',
data: gdata,
lineWidth: 5,
color: '#f0c997',
fillColor:'transparent'
}],
responsive: {
rules: [{
condition: {
maxWidth: 500
},
chartOptions: {
chart: {
height: 350,
type: 'area',
margin: [20, 0, 20, 0]
},
series: [{
type: 'areaspline',
data: gdata,
lineWidth: 3,
color: '#f0c997',
fillColor:'transparent'
}],
}
}]
}
});
}
</script>
Is it possible to do it? And how to modify my code for it?
Thank you very much in advance!
As I mentioned in the comment current line shape is at it is because of the points positions on the chart. If you don't need to keep points actual positions the solution which you can use is to use the dummy data for the points and use the correct one in the dataLabels. See:
Demo: https://jsfiddle.net/BlackLabel/fshx3n50/
data: [{
y: 5,
z: 0.20
}, {
y: 10,
z: 1.40
}, {
y: 18,
z: 6.20
}, {
y: 30,
z: 18.00
}, {
y: 50,
z: 73.00
}],
I also changed the formatter function to display the z value in dataLabel:
formatter: function() {
return '<div class="chitem-time">' + this.x + '</div>' +
'<div class="chitem-val">' + Highcharts.numberFormat(this.point.z) + '<sup>REM</sup></div>';
}

how to set the interval of points on Y - Axis highcharts

I am using highcharts for the first time, and I am trying to figure out how to set the Y axis points static.
I have used min=0 and max=140 , and the points on y axis come up as 0,25,50,75,100,125 and 150. Wherein I want it as 0,20,40,60,80,100,140.
Can someone let me know how could I achieve this.
Below is the highchart optins :
var chart1 = new Highcharts.Chart({
chart: {
renderTo: 'Div1',
width: 600,
height: 400
},
yAxis:{
min: 0, max: 140,
lineColor: '#FF0000',
lineWidth: 1,
title: {
text: 'Values'
},
plotLines: [{
value: 0,
width: 10,
color: '#808080'
}]
},
series: [{
name: 'Value',
data: YaxisValuesArray
}]
});
});
You can set the tickInterval (http://api.highcharts.com/highstock#yAxis.tickInterval) on the axis
http://jsfiddle.net/blaird/KdHME/
$(function () {
var chart1 = new Highcharts.Chart({
chart: {
renderTo: 'Div1',
width: 600,
height: 400
},
credits: {
enabled: false
},
title: {
text: 'Productivity Report',
x: -20 //center
},
xAxis: {
lineColor: '#FF0000',
categories: [1, 2, 3]
},
yAxis: {
min: 0,
max: 140,
tickInterval: 20,
lineColor: '#FF0000',
lineWidth: 1,
title: {
text: 'Values'
},
plotLines: [{
value: 0,
width: 10,
color: '#808080'
}]
},
tooltip: {
valueSuffix: ''
},
legend: {
layout: 'vertical',
align: 'right',
verticalAlign: 'middle',
borderWidth: 0
},
series: [{
name: 'Value',
data: [
[1, 10],
[2, 20],
[3, 30]
]
}]
});
});
To do this using HighChart in a StockChart mode, I just need to set the property tickPixelInterval.
yAxis: {
...
tickPixelInterval: 35
...
}

Markers to appear between the ticks

I want the markers to appear between the ticks. I have searched but found nothing on google or the highcharts API etc.
My clients want the dots to appear between the numbers - not against them.
Here is the code,
jQuery('.graph').highcharts({
chart: {
type: 'line',
marginRight: 10,
marginBottom: 20,
height: 117,
plotBorderColor: '#E6DB41',
plotBorderWidth: 2,
borderColor: '#0793D1',
borderRadius: 0,
borderWidth: 2
},
credits: {
enabled: false
},
title: {
text: ''
},
legend: {
enabled: false,
},
xAxis: {
tickLength: 0,
categories: ['11', '12','13', '14', '15', '16', '17', '18', '19', '20', '21', '22', '23', '24'],
gridLineColor: '#cccccc',
gridLineWidth: 1,
tickmarkPlacement: 'on',
labels: {
formatter: function () {
return '<span style="fill: #0793D1;font-size:7px;">' + this.value + '</span>';
}
}
},
yAxis: {
tickInterval: 50,
max: 200,
min: -5,
startOnTick: false,
title: {
enabled: false
},
gridLineColor: '#cccccc',
gridLineWidth: 1,
labels: {
formatter: function () {
return '<span style="fill: #0793D1;font-size:9px;">' + this.value + '</span>';
}
},
offset:-5
},
series: [{
name: 'Reservierunge',
data: [<?php echo $hourlyData?>]
}],
plotOptions: {
series: {
marker: {
enabled: true
}
}
},
tooltip: {
enabled: false
},
exporting: {
enabled: false
},
colors: [
'#F80001',
'#0d233a',
'#8bbc21',
'#910000',
'#1aadce',
'#492970',
'#f28f43',
'#77a1e5',
'#c42525',
'#a6c96a'
]
});
You need to replace tickmarkPlacement: 'on' with tickmarkPlacement: 'between';
http://api.highcharts.com/highcharts#xAxis.tickmarkPlacement
EDIT:
Workaround:
You can move labels group by translate() funciton or move each element. Simple example: http://jsfiddle.net/Z3Yqx/1

remove grid line on 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/

Resources