Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
292 views
in Technique[技术] by (71.8m points)

javascript - Can we find breakdown point/ break down analysis in High chart line graph?

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. enter image description here

    
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>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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/

Annotated chart


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...