为什么移动平均线在这种情况下不能按预期显示

时间:2016-01-08 10:33:30

标签: google-visualization

我正在尝试在蜡烛图表上绘制SMA。

这是我的代码

google.load("visualization", "1", {
    packages: ["corechart"]
});

google.setOnLoadCallback(drawChart);


var mydata = [
    ['13-Oct', 1097.95, 1113.45, 1109.95, 1132],
    ['14-Oct', 1095.6, 1101.15, 1113.45, 1117],
    ['15-Oct', 1092.1, 1129.2, 1116, 1132],
    ['16-Oct', 1130, 1170.3, 1130, 1182.4],
    ['19-Oct', 1144.5, 1162.15, 1174, 1182.2]
];


function drawChart() {

    var data = google.visualization.arrayToDataTable(mydata, true);

    var options = {
        legend: 'none',
        colors: ['red', 'brown'],
        candlestick: {
            fallingColor: {
                fill: "orange",
                strokeWidth: 0.5,
                stroke: 'black'
            },
            risingColor: {
                fill: "yellowgreen",
                strokeWidth: 0.5,
                stroke: 'black'
            }
        }
    };


    var days = 5;
    var view = new google.visualization.DataView(data);
    view.setColumns([0, 1, 2, 3, 4, {
        type: 'number',
        label: days + '-day Moving Average',
        calc: function (dt, row) {
            // calculate average of closing value for last x days,
            // if we are x or more days into the data set
            if (row >= days - 1) {
                var total = 0;
                for (var i = 0; i < data; i++) {
                    total += data.getValue(row - i, 3);
                }
                var avg = total / days;
                return {v: avg, f: avg.toFixed(2)};
            }
            else {
                // return null for < x days
                return null;
            }
        }
    }]);

    var chart = new google.visualization.CandlestickChart(document.getElementById('chart_div'));

        chart.draw(view, {
        height: 400,
        width: 600,
        chartArea: {
            left: '7%',
            width: '70%'
        },
        series: {
            0: {
                type: 'candlesticks'
            },
            1: {
                type: 'line'
            }
        }
    });


}

你能否告诉我如何正确地看到SMA?

在添加SMA之前,一切都在使用我的数据点。

这是我的小提琴

http://jsfiddle.net/ovog4njt/13/

1 个答案:

答案 0 :(得分:0)

您的 SMA 行不可见,因为计算列的前四行返回null,最后一行返回零。

calc函数中,row从零开始 使用days = 5时,if (row >= days - 1)不会传递到最后一行...

我有点困惑你想要计算移动平均线,但我拍了一下。

使用数据中的第四列,我计算平均值......

  

第0行:价值:1,109.95
  第1行:值:1,111.70 - &gt; (1,109.95 + 1,113.45)/ 2
  第2行:值:1,113.13 - &gt; (1,109.95 + 1,113.45 + 1,116.00)/ 3
  等...

&#13;
&#13;
google.load("visualization", "1", {packages: ["corechart"]});
google.setOnLoadCallback(drawChart);

var mydata = [
    ['13-Oct', 1097.95, 1113.45, 1109.95, 1132],
    ['14-Oct', 1095.6, 1101.15, 1113.45, 1117],
    ['15-Oct', 1092.1, 1129.2, 1116, 1132],
    ['16-Oct', 1130, 1170.3, 1130, 1182.4],
    ['19-Oct', 1144.5, 1162.15, 1174, 1182.2]
];

function drawChart() {
    var data = google.visualization.arrayToDataTable(mydata, true);
    var days = mydata.length;
    var view = new google.visualization.DataView(data);

    view.setColumns([0, 1, 2, 3, 4, {
        type: 'number',
        label: days + '-day Moving Average',
        calc: function (dt, row) {
            // calculate average of closing value for last x days,
            // if we are x or more days into the data set
            var avg = 0;
            var total = 0;

            for (var i = 0; i <= row; i++) {
              total = total + data.getValue(i, 3);
            }
            avg = total / (row + 1);
            return {v: avg, f: avg.toFixed(2)};
        }
    }]);

    var chart = new google.visualization.CandlestickChart(document.getElementById('chart_div'));

    chart.draw(view, {
        height: 400,
        width: 600,
        chartArea: {
            left: '7%',
            width: '70%'
        },
        series: {
            0: {
                type: 'candlesticks'
            },
            1: {
                type: 'line'
            }
        }
    });
}
&#13;
<script src="https://www.google.com/jsapi"></script>
<div id="chart_div" style="width: 900px; height: 500px;"></div>
&#13;
&#13;
&#13;