-1

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>
Sahil Dhir
  • 4,162
  • 1
  • 13
  • 33

1 Answers1

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/

Annotated chart

morganfree
  • 12,254
  • 1
  • 14
  • 21
  • Thanks a lot @morganfree.. But Is there any way to also calculate the time (Breakdown in 3months) like in the graph I have shown.. ? – Sahil Dhir Feb 20 '17 at 15:05
  • This point is already calculated, it the intersection point - all you need to do is to convert point.x to a month / year fraction and format it - https://jsfiddle.net/858b4x0c/2/ – morganfree Feb 21 '17 at 11:32
  • Great.. Thanks Again !! – Sahil Dhir Feb 21 '17 at 11:35
  • I am using chart.js library now. Can you help me producing the same thing? @morganfree – Sahil Dhir Mar 06 '17 at 07:36
  • here is the link to the question https://stackoverflow.com/questions/42621329/intersection-point-in-line-graph-in-chart-js – Sahil Dhir Mar 06 '17 at 09:09