问题
I want a graph like this image. I have tried making this using high chart , But I am unable to calculate break point (the point where 2 line graphs are colliding. Does anyone know how to do that , like the image below.
Highcharts.chart('container1', {
chart: {
type: 'column'
},
credits: {
enabled: false
},
legend: {
align: 'right',
verticalAlign: 'top',
layout: 'vertical',
x: 0,
y: 100
},
title: {
text: 'Cash Flow Analysis and Breakdown',
align: 'left',
x: 0,
style: {
color: '#000000',
fontWeight: 'normal',
textTransform : 'Uppercase',
fontSize : '14px',
}
},
xAxis: {
categories: ['Year 0', 'Year 1', 'Year 2', 'Year 3', 'Year 4', 'Year 5'],
lineColor: '#000000',
lineWidth: 2
},
yAxis: {
min: 0,
tickInterval: 50,
title: {
text: ''
},
gridLineWidth : 0,
lineColor: '#cccccc',
lineWidth: 1
},
tooltip: {
pointFormat: '<span style="color:{series.color}">{series.name}</span>: <b>{point.y}</b> ({point.percentage:.0f}%)<br/>',
shared: true
},
plotOptions: {
column: {
stacking: 'normal'
},
bar:{
groupPadding:0.1,
pointWidth:20,
},
series:{
pointWidth: 40
}
},
series: [{
name: 'Benifit(One time)',
data: [0,50, 60, 80, 50, 60],
color: '#005998'
},{
name: 'Benifit(Recurring)',
data: [0,150, 150, 180, 120, 140],
color: '#0FAAFF'
}, {
type: 'line',
name: 'Cost',
data: [35, 15, 25, 14, 10, 7],
color: '#E35500',
marker: {
lineWidth: 2,
lineColor: '#E35500',
fillColor: '#E35500'
}
},{
type: 'line',
name: 'Cash Flow',
data: [-50, 180, 170, 220, 160, 190],
color: '#FFC000',
marker: {
lineWidth: 2,
lineColor: '#FFC000',
fillColor: '#FFC000'
}
}
]
});
<script src="http://code.highcharts.com/highcharts.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="container1"></div>
回答1:
You need to find the intersection points of the series - and having coordinates you can plot a line (line with an arrow) and a text - both can be rendered with Renderer.
chart: {
type: 'column',
events: {
load: function() {
const points1 = this.series[2].points;
const points2 = this.series[3].points;
const intersection = [];
for (let i = 1; i < points1.length; i++) {
const intersect = getLineIntersection(points1[i - 1], points1[i], points2[i - 1], points2[i]);
if (intersect) {
intersection.push(intersect);
}
}
const xAxis = this.xAxis[0];
const yAxis = this.yAxis[0];
intersection.forEach(coord => {
const text = this.renderer.text('break point', -999, -999).add();
const anchorX = xAxis.toPixels(coord[0]);
const anchorY = yAxis.toPixels(coord[1]);
const connector = this.renderer.path([
'M', anchorX, anchorY,
'L', anchorX - 5, anchorY - 100
]).attr({
stroke: 'black',
'stroke-width': 1,
zIndex: 99
}).add();
text.attr({
align: 'center',
x: anchorX - 5,
y: anchorY - 110,
zIndex: 99
}).css({
color: 'black',
fontSize: '14px',
fontWeight: 'bold'
});
})
}
}
},
Live example and output
https://jsfiddle.net/858b4x0c/
来源:https://stackoverflow.com/questions/42340398/can-we-find-breakdown-point-break-down-analysis-in-high-chart-line-graph