如何计算趋势线

时间:2014-09-02 11:42:20

标签: javascript html css highcharts

根据我从这个网站得到的公式,我试图计算一个趋势线:

http://classroom.synonym.com/calculate-trendline-2709.html

我通过添加代码并动态传递值来完成High Charts中的趋势线,这是我的代码:

<!DOCTYPE HTML>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
        <title>Highcharts Example</title>

        <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>

        <script type="text/javascript" src="regression.js"></script>

        <script type="text/javascript" src="http://code.highcharts.com/highcharts.js"></script>

        <script type="text/javascript">

    $(function () {        
        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]
        ];

        $('#container').highcharts({
            title: {
                text: 'RNA',
                x: -20 //center
            },
            subtitle: {
                text: 'Outage Reasons',
                x: -20
            },
            xAxis: {
                categories: ['18-Jul-14', '19-Jul-14', '20-Jul-14', '21-Jul-14', '22-Jul-14', '23-Jul-14',
                    '24-Jul-14', '25-Jul-14', '26-Jul-14', '27-Jul-14', '28-Jul-14', '29-Jul-14']
            },
            yAxis: {
                title: {
                    text: 'Percent'
                },
                plotLines: [{
                    value: 0,
                    width: 1,
                    color: '#808080'
                }]
            },
            tooltip: {
                valueSuffix: '%'
            },
            legend: {
                layout: 'vertical',
                //align: 'right',
                //verticalAlign: 'middle',
                borderWidth: 0
            },
            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;
                })()
            }],
            credits: {
                enabled: false
            }
      });
});


        </script>
    </head>
    <body>

<script src="exporting.js"></script>

<div id="container" style="min-width: 310px; height: 400px; margin: 0 auto"></div>

    </body>
</html>

这是生成的图表:

enter image description here

我想知道的是:

  1. 值如何根据公式计算值以及值是什么

  2. 趋势线如何在图表上绘图。

  3. 我知道x轴上的日期不能用公式计算。

  4. 我已经根据值

    进行了计算

    [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]

  5. 但是图表上绘制的值不同。

    谢谢

1 个答案:

答案 0 :(得分:0)

它不是内置版,但您可以尝试使用plugin