Highcharts dynamically restrict dragging limits - highcharts

I'd like to dynamically restrict dragging limits (different limits for each point). I tried this (ending with error):
chart.series[1].update({dragMinY: 80});
http://jsfiddle.net/petiska/6g9tqrh5/2/
var chart = new Highcharts.Chart({
chart: {
renderTo: 'container',
zoomType: 'xy',
animation: false,
type: 'line',
},
title: {
text: 'Highcharts draggable points demo'
},
xAxis: [{
categories: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'],
},{linkedTo:0, offset:-128.5, lineWidth: 1, tickInterval: 10, zIndex:4}],
yAxis: [{},{linkedTo:0,tickInterval:9.1}],
plotOptions: {
series: {
dataLabels:{useHTML: true},
point: {
events: {
drag: function (e) {
$('#drag').html('Dragging <b>' + this.series.name + '</b>, <b>' + this.category + '</b> to <b>' + Highcharts.numberFormat(e.y, 2) + '</b>');
//console.log(e);
//console.log(e.target)
//console.log(e.dragStart.x);
var point_index=e.target.index;
var point_oldX=e.dragStart.x;
var point_oldY=e.dragStart.y;
var point_newX=e.x;
var point_newY=e.y;
chart.series[1].update({dragMinY: 80});
},
drop: function () {
$('#drop').html('In <b>' + this.series.name + '</b>, <b>' + this.category + '</b> was set to <b>' + Highcharts.numberFormat(this.y, 2) + '</b>');
console.log("drop!")
}
}
},
stickyTracking: false
},
line: {
cursor: 'ns-resize'
}
},
tooltip: {
yDecimals: 2
},
series: [
{
type: "treemap",
layoutAlgorithm: 'stripes',
alternateStartingDirection: true,
// allowDrillToNode: true,
data: [{id: "NS",name: 'NS_area', value:5},
{id: "NS_ABCDE",name: 'NS_ABCDE',value: 6,parent: 'NS'},
{id: "NSA",name: 'NSA',value: 1,parent: 'NS_ABCDE'},
{id: "A5",name: 'A5',value: 1,parent: 'NSA'},
{id: "A4",name: 'A4',value: 1,parent: 'NSA'},
{id: "A3",name: 'A3',value: 1,parent: 'NSA'},
{id: "A2",name: 'A2',value: 1,parent: 'NSA'},
{id: "A1",name: 'A1',value: 1,parent: 'NSA'},
{id: "A0",name: 'A0',value: 1,parent: 'NSA'},
{id: "NSB",name: 'NSB',value: 1,parent: 'NS_ABCDE'},
{id: "B5",name: 'B5',value: 1,parent: 'NSB'},
{id: "B4",name: 'B4',value: 1,parent: 'NSB'},
{id: "B3",name: 'B3',value: 1,parent: 'NSB'},
{id: "B2",name: 'B2',value: 1,parent: 'NSB'},
{id: "B1",name: 'B1',value: 1,parent: 'NSB'},
{id: "B0",name: 'B0',value: 1,parent: 'NSB'},
{id: "NSC",name: 'NSC',value: 1,parent: 'NS_ABCDE'},
{id: "C5",name: 'C5',value: 1,parent: 'NSC'},
{id: "C4",name: 'C4',value: 1,parent: 'NSC'},
{id: "C3",name: 'C3',value: 1,parent: 'NSC'},
{id: "C2",name: 'C2',value: 1,parent: 'NSC'},
{id: "C1",name: 'C1',value: 1,parent: 'NSC'},
{id: "C0",name: 'C0',value: 1,parent: 'NSC'},
{id: "NSD",name: 'NSD',value: 1,parent: 'NS_ABCDE'},
{id: "D5",name: 'D5',value: 1,parent: 'NSD'},
{id: "D4",name: 'D4',value: 1,parent: 'NSD'},
{id: "D3",name: 'D3',value: 1,parent: 'NSD'},
{id: "D2",name: 'D2',value: 1,parent: 'NSD'},
{id: "D1",name: 'D1',value: 1,parent: 'NSD'},
{id: "D0",name: 'D0',value: 1,parent: 'NSD'},
{id: "NSE",name: 'NSE',value: 1,parent: 'NS_ABCDE'},
{id: "E5",name: 'E5',value: 1,parent: 'NSE'},
{id: "E4",name: 'E4',value: 1,parent: 'NSE'},
{id: "E3",name: 'E3',value: 1,parent: 'NSE'},
{id: "E2",name: 'E2',value: 1,parent: 'NSE'},
{id: "E1",name: 'E1',value: 1,parent: 'NSE'},
{id: "E0",name: 'E0',value: 1,parent: 'NSE'},
{id: "NS_NP1",name:" ",value: 1,parent: 'NS',color: "#FFFFFF"}, //this is only to display division lines, any id can be used - not accessed
{id: "NS_NP11",name: " ",value: 1,parent: 'NS_NP1'},
{id: "NS_NP11",name: " ",value: 1,parent: 'NS_NP1'},
{id: "NS_NP11",name: " ",value: 1,parent: 'NS_NP1'},
{id: "NS_NP11",name: " ",value: 1,parent: 'NS_NP1'},
{id: "NS_NP11",name: " ",value: 1,parent: 'NS_NP1'},
{id: "NS_NP2",name:" ",value: 1,parent: 'NS',color: "#FFFFFF"},
{id: "NS_NP11",name: " ",value: 1,parent: 'NS_NP2'},
{id: "NS_NP11",name: " ",value: 1,parent: 'NS_NP2'},
{id: "NS_NP11",name: " ",value: 1,parent: 'NS_NP2'},
{id: "NS_NP11",name: " ",value: 1,parent: 'NS_NP2'},
{id: "NS_NP11",name: " ",value: 1,parent: 'NS_NP2'},
{id: "NS_NP3",name:" ",value: 1,parent: 'NS',color: "#FFFFFF"},
{id: "NS_NP4",name:" ",value: 1,parent: 'NS',color: "#FFFFFF"},
{id: "NS_NP5",name:" ",value: 1,parent: 'NS',color: "#FFFFFF"},
//{id: "NS_NP",name:" ",value: 5,parent: 'NS',color: "#FFFFFF"},
{id: "LT",name: 'LT',value:3},
{id: "LT_FGH",value: 7,parent: 'LT'},
{id: "LT_FGH_R",parent: 'LT_FGH'}, //force to divide horizontaly or vertically, R-reverse
{id: "F",value: 6,parent: 'LT_FGH_R'},
{id: "LT_GH",value: 1,parent: 'LT_FGH_R'},
{id: "G",value: 2,parent: 'LT_GH'},
{id: "H",value: 1,parent: 'LT_GH'},
{id: "LT_NP",name:" ",value: 2,parent: 'LT',color: "#FFFFFF"},
{id: "LT_NP1",name:" ",value: 1,parent: 'LT_NP'}, //this is only to display division lines
{id: "LT_NP2",name:" ",value: 1,parent: 'LT_NP'}, //this is only to display division lines
{id: "LT_NP3",name:" ",value: 1,parent: 'LT_NP'}, //this is only to display division lines
{id: "LT_IJK",value: 2,parent: 'LT'},
{id: "LT_I",value: 1,parent: 'LT_IJK'},
{id: "I0",value: 2,parent: 'LT_I'},
{id: "I1",value: 2,parent: 'LT_I'},
{id: "LT_J",value: 1,parent: 'LT_IJK'},
{id: "J0",value: 2,parent: 'LT_J'},
{id: "J1",value: 2,parent: 'LT_J'},
{id: "LT_K",value: 1,parent: 'LT_IJK'},
{id: "K0",value: 2,parent: 'LT_K'},
{id: "K1",value: 2,parent: 'LT_K'},
{id: "SL",name: 'SL',value:1},
{id: "C",name: 'C',value:1},
]
},{
data: [[0, 90.9], [10,81.81], [20,72.72], [30,63.63],[40,63.63],[50,45.45],[70,45.45],[80,36.36],[85,35],[95,33],[100,33]],
step: 'left',
type: 'line',
yAxis: 0,
xAxis: 0,
draggableX: true,
draggableY: true,
dragMaxY: 90.9,
dragMinY: 30,
},
]
});
<script src="https://code.highcharts.com/highcharts.js"></script>
<script src="https://rawgithub.com/highcharts/draggable-points/master/draggable-points.js"></script>
<script src="https://code.highcharts.com/highcharts-more.js"></script>
<script src="https://code.highcharts.com/modules/exporting.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://code.highcharts.com/modules/treemap.js"></script>
<div id="container" style="height: 600px"></div>
<div id="drag"></div>
<div id="drop"></div>
<button onclick="drawsomething()">Click me</button>

As an example of handling dragMinY and dragMaxY more dynamically I've slightly modified draggable-points.js, essentially allowing for a dragMin and dragMax function instead (or rather, it has higher priority than dragMinY and dragMaxY). The functions dragMin and dragMax get two parameters, XorY specifying the axis and point, being the dragged point.
The change is to the following functions signature and functionality:
/**
* Filter by dragMin and dragMax
*/
function filterRange(newY, point, series, XOrY) {
var options = series.options,
dragMin = pick(options.dragMin ? options.dragMin(XOrY, point) : undefined, options['dragMin' + XOrY], undefined),
dragMax = pick(options.dragMax ? options.dragMax(XOrY, point) : undefined, options['dragMax' + XOrY], undefined),
precision = pick(options['dragPrecision' + XOrY], undefined);
if (!isNaN(precision)) {
newY = Math.round(newY / precision) * precision;
}
if (newY < dragMin) {
newY = dragMin;
} else if (newY > dragMax) {
newY = dragMax;
}
return newY;
}
The product can be seen here: GitRaw GitHub
Or in this JSFiddle example, where the drag is limited on X between the left and right neighbor, and Y lower than left and higher than right neighbor, as in the below example code:
var chart = new Highcharts.Chart({
chart: {
renderTo: 'container',
},
series: [{
data: [[0, 90.9], [10,81.81], [20,72.72], [30,63.63],[40,63.63],[50,45.45],[70,45.45],[80,36.36],[85,35],[95,33],[100,33]],
step: 'left',
type: 'line',
draggableX: true,
draggableY: true,
dragMin: function(XorY, point) {
leftPoint = point.series.data[point.index-1];
rightPoint = point.series.data[point.index+1];
if('X' == XorY) {
// Strictly higher than left point, if any
if(leftPoint) {
return leftPoint.x + 1;
}
return undefined;
}
else {
// Lower than right point, if any
if(rightPoint) {
return rightPoint.y;
}
return undefined;
}
},
dragMax: function(XorY, point) {
leftPoint = point.series.data[point.index-1];
rightPoint = point.series.data[point.index+1];
if('X' == XorY) {
// Strictly lower than right point, if any
if(rightPoint) {
return rightPoint.x - 1;
}
return undefined;
}
else {
// Higher than left point, if any
if(leftPoint) {
return leftPoint.y;
}
return undefined;
}
}
}]
});
Note that I've simply edited the code created by Torstein Honsi of Highsoft under the MIT License.

Related

put labels on the right of the chart area in HighCharts

In a Highcharts line chart, I need to put the series name on the right of the last point of each one.
Unfortunately, even if I set chart.spacingRight and chart.marginRight, the space on the right is not used.
Is there any solution/workaround?
Jsfiddle
Highcharts.chart('container', {
chart: {
spacingRight: 200
},
plotOptions: {
series: {
label: {
enabled:false
},
pointStart: 2010,
dataLabels: {
enabled:true,
align:'left',
verticalAlign:'middle',
x:10,
formatter: function() {
return this.series.data.indexOf( this.point ) == this.series.data.length - 1 ? this.series.name : '';
}
}
}
},
series: [{
name: 'Installation',
data: [43934, 52503, 57177, 69658, 97031, 119931, 137133, 154175]
}, {
name: 'Manufacturing',
data: [24916, 24064, 29742, 29851, 32490, 30282, 38121, 40434]
}, {
name: 'Sales & Distribution',
data: [11744, 17722, 16005, 19771, 20185, 24377, 32147, 39387]
}, {
name: 'Project Development',
data: [null, null, 7988, 12169, 15112, 22452, 34400, 34227]
}, {
name: 'Other',
data: [12908, 5948, 8105, 11248, 8989, 11816, 18274, 18111]
}],
});
You can do that with dataLabels.crop:false and dataLabels.overflow: 'allow'
dataLabels: {
overflow: 'allow',
crop:false,
...
Api documentation currently broken, so impossible to put the real link
Fiddle

Highcharts:Grouped and stacked legend

I currently have a legend like Joe and John. I am needing to have a legend on Male and Female as well. How to add male and female legend as well to this. kindly help me out.
This is the JSFiddle of the code which I have tried so far.
Have also created a runnable snippet below if it would be of any help.
Highcharts.chart('container', {
chart: {
type: 'column'
},
title: {
text: 'Total fruit consumtion, grouped by gender'
},
xAxis: {
categories: ['Apples', 'Oranges', 'Pears', 'Grapes', 'Bananas']
},
yAxis: {
allowDecimals: false,
min: 0,
title: {
text: 'Number of fruits'
}
},
tooltip: {
formatter: function() {
return '<b>' + this.x + '</b><br/>' +
this.series.name + ': ' + this.y + '<br/>' +
'Total: ' + this.point.stackTotal;
}
},
plotOptions: {
column: {
stacking: 'normal'
}
},
series: [{
name: 'John',
data: [5, 3, 4, 7, 2],
stack: 'male',
color: "#1f77ac",
id: 'John'
}, {
name: 'Joe',
data: [3, 4, 4, 2, 5],
stack: 'male',
color: "#098ebb",
id: 'Joe'
}, {
name: 'John',
data: [2, 5, 6, 2, 1],
stack: 'female',
linkedTo: 'John',
color: "#1f77ac"
}, {
name: 'Joe',
data: [3, 2, 4, 4, 3],
stack: 'female',
linkedTo: 'Joe',
color: "#098ebb",
}]
});
<script src="https://code.highcharts.com/highcharts.js"></script>
<script src="https://code.highcharts.com/modules/exporting.js"></script>
<script src="https://code.highcharts.com/modules/export-data.js"></script>
<div id="container" style="min-width: 310px; height: 400px; margin: 0 auto"></div>
You can add empty series with names: male and female. To handle the click event use legendItemClick function:
series: [..., {
name: 'male',
events: {
legendItemClick: function() {
// hide some series
}
}
}, {
name: 'female',
events: {
legendItemClick: function() {
// hide some series
}
}
}]
Live demo: https://jsfiddle.net/BlackLabel/6x52kzqd/
API Reference: https://api.highcharts.com/highcharts/series.line.events.legendItemClick

Type Labels on Stacked and grouped column - Highcharts

I am trying to get the labels on the stacked bar graphs. The stack type in this is either male/female and I would like show that on the chart. Is there anyway to show it ?
Here is the link
https://jsfiddle.net/jq2e51Lx/
Here is the html:
<script src="https://code.highcharts.com/highcharts.js"></script>
<script src="https://code.highcharts.com/modules/exporting.js"></script>
<div id="container" style="min-width: 310px; height: 400px; margin: 0 auto"></div>
Javascript
Highcharts.chart('container', {
chart: {
type: 'column'
},
title: {
text: 'Total fruit consumtion, grouped by gender'
},
xAxis: {
categories: ['Apples', 'Oranges', 'Pears', 'Grapes', 'Bananas']
},
yAxis: {
allowDecimals: false,
min: 0,
title: {
text: 'Number of fruits'
}
},
tooltip: {
formatter: function () {
return '<b>' + this.x + '</b><br/>' +
this.series.name + ': ' + this.y + '<br/>' +
'Total: ' + this.point.stackTotal;
}
},
plotOptions: {
column: {
stacking: 'normal'
}
},
series: [{
name: 'John',
data: [5, 3, 4, 7, 2],
stack: 'male'
}, {
name: 'Joe',
data: [3, 4, 4, 2, 5],
stack: 'male'
}, {
name: 'Jane',
data: [2, 5, 6, 2, 1],
stack: 'female'
}, {
name: 'Janet',
data: [3, 0, 4, 4, 3],
stack: 'female'
}]
});
The OP wrote in a comment:
Thank you so much !, Works like a charm :)
Based on fiddle sample by #Barbara Laird
Highcharts.chart('container', {
chart: {
type: 'column'
},
title: {
text: 'Total fruit consumtion, grouped by gender'
},
xAxis: {
categories: ['Apples', 'Oranges', 'Pears', 'Grapes', 'Bananas'],
labels: {
y: 40
}
},
yAxis: {
allowDecimals: false,
min: 0,
title: {
text: 'Number of fruits'
},
stackLabels: {
enabled: true,
verticalAlign: 'bottom',
crop: false,
overflow: 'none',
y: 20,
formatter: function() {
return this.stack;
},
style: {
fontSize: '9px'
}
}
},
legend: {
labelFormatter: function() {
return this.name + ' (' + this.userOptions.stack + ')';
}
},
tooltip: {
formatter: function() {
var stackName = this.series.userOptions.stack;
return '<b>Stack name: </b>' + stackName + '<br/><b>' + this.x + '</b><br/>' +
this.series.name + ': ' + this.y + '<br/>' +
'Total: ' + this.point.stackTotal;
}
},
plotOptions: {
column: {
stacking: 'normal'
}
},
series: [{
name: 'John',
data: [5, 3, 4, 7, 2],
stack: 'male'
}, {
name: 'Joe',
data: [3, 4, 4, 2, 5],
stack: 'male'
}, {
name: 'Jane',
data: [2, 5, 6, 2, 1],
stack: 'female'
}, {
name: 'Janet',
data: [3, 0, 4, 4, 3],
stack: 'female'
}]
});
<script src="https://code.highcharts.com/highcharts.js"></script>
<script src="https://code.highcharts.com/modules/exporting.js"></script>
<div id="container" style="min-width: 310px; height: 400px; margin: 0 auto"></div>

Display Stack label Highcharts

Im using the stacked charts from Highcharts but would like to include the stack name below the stacked bar charts.
http://jsfiddle.net/gh/get/jquery/1.9.1/highslide-software/highcharts.com/tree/master/samples/highcharts/demo/column-stacked-and-grouped/
js
$(function () {
$('#container').highcharts({
chart: {
type: 'column'
},
title: {
text: 'Total fruit consumtion, grouped by gender'
},
xAxis: {
categories: ['Apples', 'Oranges', 'Pears', 'Grapes', 'Bananas']
},
yAxis: {
allowDecimals: false,
min: 0,
title: {
text: 'Number of fruits'
}
},
tooltip: {
formatter: function () {
return '<b>' + this.x + '</b><br/>' +
this.series.name + ': ' + this.y + '<br/>' +
'Total: ' + this.point.stackTotal;
}
},
plotOptions: {
column: {
stacking: 'normal'
}
},
series: [{
name: 'John',
data: [5, 3, 4, 7, 2],
stack: 'male'
}, {
name: 'Joe',
data: [3, 4, 4, 2, 5],
stack: 'male'
}, {
name: 'Jane',
data: [2, 5, 6, 2, 1],
stack: 'female'
}, {
name: 'Janet',
data: [3, 0, 4, 4, 3],
stack: 'female'
}]
});
});
Iin this chart, I want to show 'MALE' & 'FEMALE' below the stack.
How can I do it using Highcharts
Something like below..Where you display the stack name - "Maintenance", "other" and "peak"
I think that in your case you may use grouped-categories plugin. You may find this plugin on a Highcharts plugins repository:
http://www.highcharts.com/plugin-registry/single/11/Grouped-Categories
$(function() {
var chart = new Highcharts.Chart({
chart: {
renderTo: "container",
type: "column",
},
plotOptions: {
series: {
stacking: 'normal'
}
},
series: [{
data: [4, 14, 18, 5, 6, 5]
}, {
data: [4, 14, 18, 5, 6, 5]
}, {
data: [4, 14, 18, 5, 6, 5]
}],
xAxis: {
categories: [{
name: "Fruit",
categories: ["Apple", "Banana"]
}, {
name: "Vegetable",
categories: ["Carrot", "Potato"]
}, {
name: "Fish",
categories: ["Cod", "Salmon"]
}]
}
});
});
And here you can find simple example how your chart may work with this plugin: http://jsfiddle.net/TFhd7/943/

Highcharts drilldown to pie chart - Clicking on axis label with multiple series causes pie charts to overlap

I have a stacked column chart that drills down to a pie chart. The drilldown works fine if the data point is clicked. However, the xAxis labels are also clickable which displays the all pie charts for those points in the series. Unfortunately they overlap.
Is there some way to disable drilldown on the xAxis labels?
Here is a JSFiddle: http://jsfiddle.net/uEQL2/
$(function () {
// Create the chart
$('#container').highcharts({
chart: {
type: 'column'
},
title: {
text: 'Basic drilldown'
},
xAxis: {
type: 'category'
},
plotOptions: {
column : {
stacking : 'normal'
}
},
series: [{
name: 'On Time',
data: [{
name: 'Location 1',
y: 5,
drilldown: 'l1-ot'
}, {
name: 'Location 2',
y: 2,
drilldown: 'l2-ot'
}, {
name: 'Location 3',
y: 4,
drilldown: 'l3-ot'
}]
},{
name: 'Late',
data: [{
name: 'Location 1',
y: 5,
drilldown: 'l1-l'
}, {
name: 'Location 2',
y: 8,
drilldown: 'l2-l'
}, {
name: 'Location 3',
y: 6,
drilldown: 'l3-l'
}]
}],
drilldown: {
series: [{
type: 'pie',
id: 'l1-ot',
data: [
{name: 'Widget A', y: 2},
{name: 'Widget B', y: 3},
]
}, {
type: 'pie',
id: 'l1-l',
data: [
{name: 'Widget A', y: 1},
{name: 'Widget B', y: 4},
]
}, {
type: 'pie',
id: 'l2-ot',
data: [
{name: 'Widget A', y: 1},
{name: 'Widget B', y: 1},
]
}, {
type: 'pie',
id: 'l2-l',
data: [
{name: 'Widget A', y: 2},
{name: 'Widget B', y: 6},
]
},{
type: 'pie',
id: 'l3-ot',
data: [
{name: 'Widget A', y: 1},
{name: 'Widget B', y: 3},
]
}, {
type: 'pie',
id: 'l3-l',
data: [
{name: 'Widget A', y: 3},
{name: 'Widget B', y: 3},
]
}
]
}
})
});
This is how you can make x-axis labels unlickable on drilldown. Take a look at this demo: JSFIDDLE
code:
$(function () {
(function (H) {
//For X-axis labels
H.wrap(H.Point.prototype, 'init', function (proceed, series, options, x) {
var point = proceed.call(this, series, options, x),
chart = series.chart,
tick = series.xAxis && series.xAxis.ticks[x],
tickLabel = tick && tick.label;
//console.log("series");
//console.log(series);
if (point.drilldown) {
if (tickLabel) {
if (!tickLabel._basicStyle) {
tickLabel._basicStyle = tickLabel.element.getAttribute('style');
}
tickLabel.addClass('highcharts-drilldown-axis-label') .css({
'text-decoration': 'none',
'font-weight': 'normal',
'cursor': 'auto'
}).on('click', function () {
if (point.doDrilldown) {
return false;
}
});//remove this "on" block to make axis labels clickable
}
}
else if (tickLabel && tickLabel._basicStyle)
{
}
return point;
});
})(Highcharts);
// Create the chart
$('#container').highcharts({
.....
.......

Resources