javascript - 如何计算趋势线

标签 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]

但图表上绘制的值不同。

谢谢

最佳答案

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

关于javascript - 如何计算趋势线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25622582/

相关文章:

javascript 调用类方法

Javascript textarea 展平字符串

css - 如何在wordpress的特定页面中定位一个类

html - 如何使 gridview 在表格单元格的顶部对齐

html - 具有 3 个固定宽度列的页面

html - 如何将具有定义宽度和高度的 div 定位在另一个 div 的中心?

javascript - 数组 JavaScript 中的复选框值

javascript - React.js 获取 SVG 路径上的事件

javascript - 如何查找 Chrome 中调用方法的位置?

javascript - 使用 jquery 选择器追加元素