How to add trend line to high charts

后端 未结 2 888
梦谈多话
梦谈多话 2021-01-23 22:33

This is the high chart graph code.



    
        

        
相关标签:
2条回答
  • 2021-01-23 22:59

    Numerous problems.

    1.) You didn't really integrate the regresssion code into your plot, you just cut/pasted from the example and are over-drawing your plot. You need to add the regression line as a second series to your plot:

            series: [{
                name: 'RNA - CP ( Radio Network Availability - Customer Perceived)',
                data: sourceData
            },{
                type: 'line',
                marker: { enabled: false },
                /* function returns data for trend-line */
                data: (function() {
                  return fitData(sourceData).data;
                })()
            }]
    

    2.) This is not valid javascript:

     var sourceData = [
          [18-Jul-14, 99.75], [19-Jul-14, 99.77],
          [20-Jul-14, 99.78], [21-Jul-14, 99.84],
          [22-Jul-14, 99.82], [23-Jul-14, 99.82],
          [24-Jul-14, 99.76], [25-Jul-14, 99.78],
          [26-Jul-14, 99.8], [27-Jul-14, 99.65],
          [28-Jul-14, 99.94], [29-Jul-14, 99.8]
      ];
    

    Those are strings and they aren't quoted. Regardless, strings won't cut it for the regression, it needs numbers. Since your dates are really categories, just use:

     var sourceData = [
          [0, 99.75], [1, 99.77],
          [2, 99.78], [3, 99.84],
          [4, 99.82], [5, 99.82],
          [6, 99.76], [7, 99.78],
          [8, 99.8], [9, 99.65],
          [10, 99.94], [11, 99.8]
      ];
    

    3.) Your series name is way too long for a right side legend (it squished the plot). In my example I moved it to the bottom.

    Here's a example putting all this together.

    0 讨论(0)
  • 2021-01-23 23:00

    You can use this Plugin that I believe will do what you want to.

    $(function() {
    
        $.getJSON('http://www.highcharts.com/samples/data/jsonp.php?filename=aapl-c.json&callback=?', function(data) {
            $('#container').highcharts('StockChart', {
    
                title : {
                    text : 'AAPL Stock Price'
                },
    
                subtitle: {
                    text: 'From may 15, 2006 to May 10, 2013'
                },
                
                xAxis: {
                    ordinal: false
                },
    
                yAxis: {
                    title : {
                        text : 'Price'
                    }
                },
    
                legend: {
                    enabled: true,
                    layout: 'vertical',
                    align: 'right',
                    verticalAlign: 'middle',
                    borderWidth: 0
                },
                
                series : [{
                    name: 'Stock Price',
                    type : 'line',
                    id: 'primary',
                    data : data
                }, {
                    name: 'Linear Trendline',
                    linkedTo: 'primary',
                    showInLegend: true,
                    enableMouseTracking: false,
                    type: 'trendline',
                    algorithm: 'linear'
                }]
            });
        });
    
    });
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <script src="http://code.highcharts.com/stock/highstock.js"></script>
    <script src="http://code.highcharts.com/stock/modules/exporting.js"></script>
    <script src="https://rawgithub.com/laff/technical-indicators/master/technical-indicators.src.js"></script>
    <div id="container" style="min-width: 500x; height: 400px; margin: 0 auto"></div>

    Source: http://www.highcharts.com/plugin-registry/single/16/technical-indicators

    0 讨论(0)
提交回复
热议问题