How to draw a Highcharts tree map where sum of children values are not equal to parent value - highcharts

In a highcharts tree map I know the value of the parent and the values of some children - not of all children.
data:[{ id: 'C1', parent: 'P', value: 25, color: 'blue'},
{ id: 'C2', parent: 'P', value: 35, color: 'yellow'},
{ id: 'P', value: 100, color: 'red'}]
As you can see, there are missing children with a sum of 40.
When I draw this chart, I get P with an area of 100, but the children get enlared to 42 and 58, so they fill the complete area of P. If I omit the value in P, the children are sized correctly, but P has only a size of 60.
The wished result would be P with a size of 100 , and the children with 25 and 35, leaving 40 free (shown red).
The only way I could manage this was some dummy C3 child with the value of (P-C1-C2), but I would prefer not to do this calculation.
Is there a setting I can use? ignoreHiddenPoint: true did not help.

The best approach for that would be to add a hidden point (child), since the tree map calculates the children and accumulates these values at parent.
let Data = [{
id: 'C1',
parent: 'P',
value: 25,
color: 'blue'
},
{
id: 'C2',
parent: 'P',
value: 35,
color: 'yellow'
},
{
id: 'C3',
parent: 'P',
value: 40,
color: 'transparent',
},
{
id: 'P',
value: null,
color: 'red'
}
]
Highcharts.chart('container', {
series: [{
opacity: 1,
type: 'treemap',
layoutAlgorithm: 'strip',
allowDrillToNode: true,
animationLimit: 1000,
dataLabels: {
enabled: false
},
levelIsConstant: true,
levels: [{
level: 1,
dataLabels: {
enabled: true
},
borderWidth: 3
}],
data: Data
}],
});
<script src="https://code.highcharts.com/highcharts.js"></script>
<script src="https://code.highcharts.com/modules/treemap.js"></script>
<div id="container"></div>
Demo: https://jsfiddle.net/BlackLabel/g81L4xfp/

Related

Highcharts/Highstock How to show original OHLC values in tooltip for chart drawn using adjusted OHLC values?

This snippet shows both original OHLC and adjusted OHLC data in separate plots:
<html>
<head>
<title>
How to show original values in tooltip?
</title>
<script src="https://code.highcharts.com/stock/highstock.js"></script>
<script src="https://code.highcharts.com/modules/data.js"></script>
</head>
<body>
<div id="chart-container" style="width: 100%; height: 100%; margin: 0 auto"></div>
<pre id="csv" style="display: none">
date,open,high,low,close,volume,ex_dividend,split_ratio,adj_open,adj_high,adj_low,adj_close,adj_volume
2014-05-30,637.98,644.17,628.9,633.0,20143600.0,0.0,1.0,85.783881043984,86.616199022075,84.562968727173,85.114261733663,141005200.0
2014-06-02,633.96,634.83,622.5,628.65,13191100.0,0.0,1.0,85.243344974207,85.36032666095,83.702413790213,84.529353299948,92337700.0
2014-06-03,628.46,638.74,628.25,637.54,10453900.0,0.0,1.0,84.503805575257,85.886071942748,84.475568616388,85.724717892068,73177300.0
2014-06-04,637.44,647.89,636.11,644.82,11981500.0,0.0,1.0,85.711271721178,87.116396579183,85.532437648341,86.70359913286,83870500.0
2014-06-05,646.2,649.3699,642.61,647.35,10850200.0,0.0,1.0,86.889156291142,87.315386462184,86.406438756191,87.043787256377,75951400.0
2014-06-06,649.9,651.26,644.47,645.57,12497800.0,0.0,1.0,87.386664614072,87.569532538176,86.656537534745,86.804445414535,87484600.0
2014-06-09,92.7,93.88,91.75,93.7,75414997.0,0.0,7.0,87.252202905172,88.362856620685,86.358032540987,88.193434867471,75414997.0
2014-06-10,94.73,95.05,93.57,94.25,62777000.0,0.0,1.0,89.16290378864,89.464098016576,88.071074712372,88.711112446736,62777000.0
2014-06-11,94.13,94.76,93.47,93.86,45681000.0,0.0,1.0,88.59816461126,89.191140747509,87.976951516142,88.344031981439,45681000.0
2014-06-12,94.04,94.12,91.9,92.29,54749000.0,0.0,1.0,88.513453734653,88.588752291637,86.499217335332,86.866297800629,54749000.0
2014-06-13,92.2,92.44,90.88,91.28,54525000.0,0.0,1.0,86.781586924022,87.007482594974,85.539160733786,85.915653518706,54525000.0
2014-06-16,91.51,92.75,91.45,92.2,35561000.0,0.0,1.0,86.132136870035,87.299264503287,86.075662952297,86.781586924022,35561000.0
</pre>
<script type="text/javascript">
Highcharts.stockChart('chart-container', {
title: {
text: 'Plot Title',
align: 'left',
floating: false
},
exporting: {
enabled: true,
sourceWidth: 900,
sourceHeight: 600
},
legend: {
enabled: false,
floating: true,
verticalAlign: 'top',
align:'center'
},
rangeSelector: {
buttons: [{
type: 'month',
count: 6,
text: '6m'
}, {
type: 'year',
count: 1,
text: '1y'
}, {
type: 'year',
count: 2,
text: '2y'
}, {
type: 'year',
count: 3,
text: '3y'
}, {
type: 'year',
count: 4,
text: '4y'
}, {
type: 'all',
text: 'All'
}],
selected: 3
},
credits: {
enabled: false
},
plotOptions: {
series: {
visible: false,
turboThreshold: 0 // Comment out this code to display error
},
ohlc: {
color: 'black'
}
},
data: {
csv: document.getElementById('csv').innerHTML,
firstRowAsNames: false,
startRow: 1,
seriesMapping: [{
x: 0,
open: 1,
high: 2,
low: 3,
close: 4
}, {
x: 0,
open: 8,
high: 9,
low: 10,
close: 11
}, {
x: 0,
y: 9
}, {
x: 0,
y: 10
}]
},
series: [{
name: 'Values',
type: 'ohlc', // bars
visible: true,
// set color in plotOptions
tooltip: {
// set tooltip options here
}
}, {
name: 'Adj_Values',
type: 'ohlc', // bars
visible: true,
// set color in plotOptions
tooltip: {
// set tooltip options here
}
}, {
name: 'Adj_High',
type: 'line', // high
visible: false,
color: 'blue'
}, {
name: 'Adj_Low',
type: 'line', // low
visible: false,
color: 'gray'
}]
});
</script>
</body>
</html>
If possible I want to (1) make the original OHLC plot invisible; (2) plot only the adjusted OHLC bars; and (3) show the original OHLC values in the tooltip attached to the adjusted OHLC bars shown in the plot.
I can find examples of a shared tooltip but no information on the options that would replace the adjusted OHLC with original OHLC data. Alternatively I might want to add original OHLC data to show both original and adjusted in the tooltip associated with the adjusted data plot.
You can disable visible property for the first series and use tooltip's formatter function to find a matched point from the hidden series and show it's values.
tooltip: {
formatter: function(tooltip) {
const originalFormat = tooltip.defaultFormatter.call(this, tooltip);
const point = this.points[0].point;
const originalPoint = tooltip.chart.series[0].points.find(
p => p.x === point.x
);
originalFormat[1] = `<span style="color:black">●</span> <b> ${point.series.name}</b><br/>Open: ${originalPoint.open}<br/>High: ${originalPoint.high}<br/>Low: ${originalPoint.low}<br/>Close: ${originalPoint.close}<br/>`;
return originalFormat;
}
},
series: [{
name: 'Values',
type: 'ohlc',
visible: false
}, {
name: 'Adj_Values',
type: 'ohlc',
visible: true
}, ...]
Live demo: http://jsfiddle.net/BlackLabel/k2mhvdp6/
API Reference: https://api.highcharts.com/highcharts/tooltip.formatter

PivotPoints R & S alternate colors in Highstock

Using PivotPoint in HighStock and would like to have the resistance(R1,R2,R3) in a different color from the support(S1,S2,S3) lines.
Considered using threshold and negative colors but not sure how to programatically add the threshold to match the P line.
{
type: 'pivotpoints',
linkedTo: 'MV1',
zIndex: 0,
lineWidth: 1,
color:'#707073',
negativeColor: 'red',
threshold: 180,
visible: false,
clip: true,
//color: red,
dataLabels: {
overflow: 'none',
crop: false,
y: 4,
style: {
fontSize: 9
}
},
params: {
//period: 90
}
}
Clearly this would not work, as it needs to adapt as the data changes.
The end result if possible
There is no option to have a dynamic threshold that would follow the P line across different values for different points when using available API.
Instead you could create two pivotpoint indicators that will generate some lines in one color - one color for each indicator. To prevent overlapping you could supply custom algorithms for calculating the lines by setting nulls for all lines that shouldn't be visible for each of the indicators. P line (middle one) must be calculated for every algorithm to have dataLabels displayed correctly.
Here's a demo (and a copy in JSFiddle):
(function(H){
H.seriesTypes.pivotpoints.prototype['std-topPlacement'] = function (values) {
var diff = values[1] - values[2],
avg = [
null,
null,
values[0] + diff,
values[0] * 2 - values[2],
values[0], // middle
null, //values[0] * 2 - values[1],
null, //values[0] - diff,
null,
null
];
return avg;
};
H.seriesTypes.pivotpoints.prototype['std-bottomPlacement'] = function (values) {
var diff = values[1] - values[2],
avg = [
null,
null,
null, //values[0] + diff,
null, //values[0] * 2 - values[2],
values[0], // middle
values[0] * 2 - values[1],
values[0] - diff,
null,
null
];
return avg;
};
}(Highcharts))
$.getJSON('https://www.highcharts.com/samples/data/aapl-ohlc.json', function (data) {
Highcharts.stockChart('container', {
rangeSelector: {
selected: 2
},
title: {
text: 'AAPL Stock Price'
},
legend: {
enabled: true
},
plotOptions: {
series: {
showInLegend: true
}
},
series: [{
type: 'ohlc',
id: 'aapl',
name: 'AAPL Stock Price',
data: data,
zIndex: 1
}, {
showInLegend: false,
type: 'pivotpoints',
linkedTo: 'aapl',
zIndex: 0,
lineWidth: 3,
color: 'red',
params: {
algorithm: 'std-bottom'
},
dataLabels: {
overflow: 'none',
crop: false,
y: 4,
style: {
fontSize: 9
}
}
}, {
type: 'pivotpoints',
linkedTo: 'aapl',
zIndex: 0,
lineWidth: 3,
color: 'black',
params: {
algorithm: 'std-top'
},
dataLabels: {
overflow: 'none',
crop: false,
y: 4,
style: {
fontSize: 9
}
}
}]
});
});
<script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
<script src="https://code.highcharts.com/stock/highstock.js"></script>
<script src="https://code.highcharts.com/stock/modules/exporting.js"></script>
<script src="https://code.highcharts.com/stock/indicators/indicators.js"></script>
<script src="https://code.highcharts.com/stock/indicators/pivot-points.js"></script>
<div id="container" style="height: 400px; min-width: 310px"></div>

Highcharts treemap data labels overflow

while using Highcharts multi level treemap I ran into data labels overflow issue. Data labels overflows to other area. This works fine at level 1 & 2 when I use option overflow: 'crop' but does not work at level 3. Any idea where it is going wrong.
Here in example area with name:F data label overflows but data label with name:C at level 2 does not overflow.
Here is fiddle example.
$(function() {
var H = Highcharts;
$('#container').highcharts({
chart: {
type: 'treemap',
width: 500
},
title: {
text: null
},
plotOptions: {
series: {
layoutAlgorithm: 'stripes',
alternateStartingDirection: true,
levels: [{
level: 1,
dataLabels: {
enabled: true,
align: 'left',
verticalAlign: 'top'
}
}, {
level: 2,
dataLabels: {
formatter: function () {
return this.point.realValue;
},
overflow: 'crop'
}
}, {
level: 3,
dataLabels: {
formatter: function () {
return this.point.realValue;
},
overflow: 'crop'
}
}]
}
},
series: [{
data: [{
name: 'A',
id: 'A',
value: 1,
color: '#FFFFFF'
}, {
name: 'B',
id: 'B',
value: 1,
color: '#FFFFFF'
}, {
name: 'C',
parent: 'A',
id: 'A-1',
value: 10,
realValue: 'thisisveryveryveryveryveryverylongteststring',
color: '#ECEA8E'
}, {
name: 'D',
parent: 'A',
id: 'A-2',
color: '#FFFFFF'
}, {
name: 'E',
parent: 'A-2',
id: 'A-2-1',
value: 10,
realValue: 'A',
color: '#599753'
}, {
name: 'F',
parent: 'A-2',
id: 'A-2-2',
value: 10,
realValue: 'thisisveryveryveryverylongteststring',
color: '#1B3C40'
}, {
name: 'G',
parent: 'B',
id: 'B-1',
value: 10,
realValue: 'A',
color: '#ECEA8E'
}, {
name: 'H',
parent: 'B',
id: 'B-2',
color: '#FFFFFF'
}, {
name: 'I',
parent: 'B-2',
id: 'B-2-1',
value: 10,
realValue: 'A',
color: '#599753'
}, {
name: 'J',
parent: 'B-2',
id: 'B-2-2',
value: 10,
realValue: 'A',
color: '#1B3C40'
}]
}]
});
});
Unfortunately, Highcharts doesn't hide labels when they don't fit treemap point area. As a workaround, you can loop through each data point and check if label related to it is wider. If it is wider just hide it.
Code:
chart: {
type: 'treemap',
width: 500,
events: {
load: function() {
var chart = this,
series = chart.series[0],
pointWidth,
labelWidth;
series.data.forEach(function(point) {
if (point.dataLabel) {
pointWidth = point.shapeArgs.width;
labelWidth = point.dataLabel.width;
if (labelWidth > pointWidth) {
point.dataLabel.hide();
}
}
});
}
}
}
Demo:
https://jsfiddle.net/BlackLabel/h65xm4qb/
API reference:
https://api.highcharts.com/class-reference/Highcharts.SVGElement#hide

Highcharts - Tree map rectangles are showing the next level

I am using Highcharts Treemap and I want to prevent from showing the next level in every level of the treemap.
I want that every object on the treemap will be with only one color.
Here is my code:
Highcharts.chart('myChart', {
tooltip: {
backgroundColor: 'black',
style: {
"color": "white",
"font": "Roboto"
},
borderColor: 'black',
borderRadius: 5,
},
colorAxis: {
minColor: '#ffcfab',
maxColor: '#ff5e43' //'#FF5E43',
},
legend: {
enabled: false
},
series: [{
type: 'treemap',
layoutAlgorithm: 'squarified',
allowDrillToNode: true,
animationLimit: 1000,
dataLabels: {
enabled: false
},
levelIsConstant: false,
levels: [{
level: 1,
dataLabels: {
enabled: true
},
borderWidth: 3
}],
data: result
}],
title: {
text: 'All Data Sources',
margin: 1,
style: {
font: 'Roboto Condensed,sans-serif',
fontSize: '24px',
fontFamily: 'Roboto Condensed',
fontWeight: 700,
display: 'block',
}
}
})
Thanks In Advance.
Setting opacity to 0 for the series seems to be fixing your problem.
Demo: http://jsfiddle.net/qa2czu8y/
$(function() {
Highcharts.chart('container', {
title: {
text: 'All Data Sources',
margin: 1,
style: {
font: 'Roboto Condensed,sans-serif',
fontSize: '24px',
fontFamily: 'Roboto Condensed',
fontWeight: 700,
display: 'block',
}
},
series: [{
type: 'treemap',
layoutAlgorithm: 'squarified',
allowDrillToNode: true,
animationLimit: 1000,
dataLabels: {
enabled: false
},
levelIsConstant: false,
levels: [{
level: 1,
dataLabels: {
enabled: true
},
borderWidth: 3,
}],
opacity: 0,
data: [{
id: 'A',
name: 'Apples'
}, {
id: 'B',
name: 'Bananas'
}, {
id: 'O',
name: 'Oranges'
}, {
name: 'Anne',
parent: 'A',
value: 5,
colorValue: 5
}, {
name: 'Rick',
parent: 'A',
value: 3,
colorValue: 3
}, {
name: 'Peter',
parent: 'A',
value: 4,
colorValue: 4
}, {
name: 'Anne',
parent: 'B',
value: 4,
colorValue: 4
}, {
name: 'Rick',
parent: 'B',
value: 10,
colorValue: 10
}, {
name: 'Peter',
parent: 'B',
value: 1,
colorValue: 1
}, {
name: 'Anne',
parent: 'O',
value: 1,
colorValue: 1
}, {
name: 'Rick',
parent: 'O',
value: 3,
colorValue: 3
}, {
name: 'Peter',
parent: 'O',
value: 3,
colorValue: 3
}, {
name: 'Susanne',
parent: 'Kiwi',
value: 2,
colorValue: 2
}]
}],
tooltip: {
backgroundColor: 'black',
style: {
"color": "white",
"font": "Roboto"
},
borderColor: 'black',
borderRadius: 5
},
colorAxis: {
minColor: '#ffcfab',
maxColor: '#ff5e43' //'#FF5E43',
},
legend: {
enabled: false
},
});
});
#container {
min-width: 300px;
max-width: 600px;
margin: 0 auto;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://code.highcharts.com/highcharts.js"></script>
<script src="https://code.highcharts.com/modules/heatmap.js"></script>
<script src="https://code.highcharts.com/modules/treemap.js"></script>
<div id="container"></div>

How to create a single series bar graph with Highcharts

I'm trying to create a simple Highcharts bar graph with a single series and multiple labels in the legend. How is this done?
Here is an example:
$('#container').highcharts({
chart: {
type: 'bar',
},
legend: {
enabled: true,
layout: 'vertical',
align: 'right',
verticalAlign: 'middle',
labelFormatter: function() {
return this.name + " - <span class='total'>"+this.y+"</span>"
}
},
title: {
text: 'Simple Bar Graph'
},
xAxis: {
categories: ['First', 'Second', 'Third', 'Fourth', , 'Fifth'],
allowDecimals: false
},
yAxis: {
allowDecimals: false
},
series: [
{
data: [
{y: 6, name: 'First', color: 'blue'},
{y: 7, name: 'Second', color: 'green'},
{y: 9, name: 'Third', color: 'yellow'},
{y: 1, name: 'Fourth', color: 'orange'},
{y: 1, name: 'Fifth', color: 'red'}
]
}
],
});
Luckily, Highcharts is pretty flexible.
We can do some tricks (maybe hacks?) to achieve this kind of "non-convential" tasks.
What you could do in your case is create "fake" serieses, and use custom event handlers:
series: [
{
pointWidth:20,
color: colors[0],
showInLegend:false,
data: [
{y: 6, name: 'First', color: colors[0]},
{y: 7, name: 'Second', color: colors[1]},
{y: 9, name: 'Third', color: colors[2]},
{y: 1, name: 'Fourth', color: colors[3]},
{y: 1, name: 'Fifth', color: colors[4]}
]
},
{color: 'blue'},
{color: 'green'},
{color: 'yellow'},
{color: 'orange'},
{color: 'red'}
],
In order to format the legend labels we can use labelFormatter for the legend:
legend: {
labelFormatter: function(){
return names[this.index-1];
}
},
this will set the legend label to the name of the corresponding point.
And finally we need to handle the legend click, to imitate the "normal" behaviour:
plotOptions: {
series: {
events: {
legendItemClick: function (x) {
var i = this.index - 1;
var series = this.chart.series[0];
var point = series.points[i];
if(point.oldY == undefined)
point.oldY = point.y;
point.update({y: point.y != null ? null : point.oldY});
}
}
}
},
These are just examples, you can obviously improve this and adjust to your own need.
Good Luck!
http://jsfiddle.net/otm0oq2c/3/
A simpler way than the accepted answer is by setting the categories key as an array of 'labels' and the series.data as an array of 'values'.
Here is an example that uses a collection (Array of objects) or a flat object to populate a bar chart in Highcharts (jsfiddle):
The example assumes a an array of objects with keys name/value, or a simple object of key/value:
var chart = Highcharts.chart('container', {
title: {
text: 'Simple Bar'
},
chart: {type: 'bar'},
xAxis: {
// categories: Object.keys(obj)
// or
categories: collection.map(i => i.name)
},
series: [{
name: 'Usage',
colorByPoint: true,
// data: Object.values(obj)
// or:
data: collection.map(i => i.value)
}],
legend: false
});

Resources