谷歌图表,平均线删除了我的另一条线

时间:2017-02-10 13:46:32

标签: javascript api charts google-visualization average

我有一个Google组合图表,其中一个值为条形,一个值为一条线,我还想要一条线来显示条形图的平均值,所以我发现代码会这样做,但是,当我的另一条线已经消失了。

这是我之前的图表。

enter image description here

这是在我实施了平均线后,我的另一条线消失了。

enter image description here

我不知道怎么做让他们都出演?

此行似乎与所有内容有关,将dv更改回data将显示我的图表在第一张图片上的样子,但我想我还需要更多内容改变使它全部工作?

chart.draw(dv, options);

这是代码。

<script>
    google.charts.load('current', {
        'packages': ['corechart']
    });
    google.charts.setOnLoadCallback(drawVisualization);


    function drawVisualization() {
        var data = google.visualization.arrayToDataTable([
            ['Day', 'Repetitions', 'Sets'],
            @foreach (var c in db.Query(Query, inputDate, endDate, baselift))
            {
                var totAvg = c.avg;
                var allReps = c.reps;
                var realAvg = (totAvg / allReps) * 100;

                //Writes out the data that will be shown in the chart.
                <text>['@c.date', @c.reps, @c.sets],</text>
            }
        ]);

            // Create a DataView that adds another column which is all the same (empty-string) to be able to aggregate on.
            var viewWithKey = new google.visualization.DataView(data);
                    viewWithKey.setColumns([0, 1, {
                        type: 'string',
                        label: '',
                        calc: function (d, r) {
                            return ''
                        }
                    }])

                    // Aggregate the previous view to calculate the average. This table should be a single table that looks like:
                    // [['', AVERAGE]], so you can get the Average with .getValue(0,1)
                    var group = google.visualization.data.group(viewWithKey, [2], [{
                        column: 1,
                        id: 'avg',
                        label: 'Average',
                        aggregation: google.visualization.data.avg,
                        'type': 'number'
                    }]);

                    // Create a DataView where the third column is the average.
                    var dv = new google.visualization.DataView(data);
                    dv.setColumns([0, 1, {
                        type: 'number',
                        label: 'Average',
                        calc: function (dt, row) {
                            return group.getValue(0, 1);
                        }
                    }]);

                    var options = {                            
                        title: 'Daily Repetition Statistics',
                        backgroundColor: { fill: 'transparent' },
                        explorer: { axis: 'horizontal' },
                        vAxes: {

                            0: { logScale: false, viewWindow: { min: 0 } },
                            1: { logScale: false, maxValue: 2 }

                        },
                        hAxis: { title: 'Day' },
                        seriesType: 'bars',
                        curveType: 'function',
                        series: {
                        0: {
                               targetAxisIndex: 0,
                               color: 'orange'                                       
                           },
                        1: { targetAxisIndex: 1 },
                        1: { targetAxisIndex: 1, type: "line" },
                        2: { targetAxisIndex: 1, type: "line" }
                    }
                };

                var chart = new google.visualization.ComboChart(document.getElementById('chart_div'));
                chart.draw(dv, options);
                }
            </script>

1 个答案:

答案 0 :(得分:1)

未向'sets'

提供setColumns的列索引

改变这个......

var dv = new google.visualization.DataView(data);
dv.setColumns([0, 1, {
    type: 'number',
    label: 'Average',
    calc: function (dt, row) {
        return group.getValue(0, 1);
    }
}]);

到..

var dv = new google.visualization.DataView(data);
dv.setColumns([0, 1, 2, {  // <-- add 'sets' column index 2
    type: 'number',
    label: 'Average',
    calc: function (dt, row) {
        return group.getValue(0, 1);
    }
}]);