简体   繁体   English

谷歌图表时间序列中的多个折线图

[英]Google Charts Multiple Line Charts in Time Series

How do I make a google charts that has multiple line charts.如何制作具有多个折线图的谷歌图表。 I know how to do it in chartsjs but it confuses me in google charts.我知道如何在 chartsjs 中做到这一点,但它在谷歌图表中让我感到困惑。 My attempt:我的尝试:

   <script>

    google.charts.load('current', {packages: ['corechart', 'line']});
    google.charts.setOnLoadCallback(drawCurveTypes);

function drawCurveTypes() {
      var data = new google.visualization.DataTable();
      data.addColumn('date', 'X');
      data.addColumn('number', 'Person 1');
      data.addColumn('number', 'Person 2');

        data.addRows(9);

        data.setValue(0, 0, new Date(2010, 1, 1));
        data.setValue(0, 1, 215);
        data.setValue(0, 2, 215);

        data.setValue(1, 0, new Date(2010, 2, 1));
        data.setValue(1, 2, 213);

        data.setValue(2, 0, new Date(2010, 2, 4));
        data.setValue(2, 2, 213);

        data.setValue(3, 0, new Date(2010, 2, 8));
        data.setValue(3, 2, 213);


        data.setValue(4, 0, new Date(2010, 3, 1));
        data.setValue(4, 2, 220);

        data.setValue(5, 0, new Date(2010, 4, 1));
        data.setValue(5, 2, 190);

      var options = {
        {#chartArea:{ width:"100%", height:"100%"},#}
        title: "Persons Performance over Time",
        height: 500,
        width: 1300,
        hAxis: {
          title: 'Time'
        },
        vAxis: {
          title: 'Scores'
        },
        series: {
          1: {curveType: 'function'}
        },

      };

      var chart = new google.visualization.LineChart(document.getElementById('chart_div'));
      chart.draw(data, options);
    }


    </script>

But it seems to only make one line chart.但它似乎只制作一张折线图。 I have scores for person 1 and person 2 over time, so its a timeseries data.随着时间的推移,我有第 1 个人和第 2 个人的分数,所以它是一个时间序列数据。 This is my output goal:这是我的输出目标:

在此处输入图片说明

looks like you have the right idea, you just need to add values for the first series.看起来您的想法是正确的,您只需要为第一个系列添加值。

here, you've added values for both series.在这里,您已经为两个系列添加了值。

data.setValue(0, 0, new Date(2010, 1, 1));
data.setValue(0, 1, 215);  // series 1
data.setValue(0, 2, 215);  // series 2

but since they are both on the same y-axis value, only one is visible.但由于它们都在相同的 y 轴值上,因此只有一个可见。
and no more values for line 1 are ever added, so only a single point is drawn.并且不再添加第 1 行的值,因此只绘制一个点。

data.setValue(1, 0, new Date(2010, 2, 1));
data.setValue(1, 2, 213);  // series 2

data.setValue(2, 0, new Date(2010, 2, 4));
data.setValue(2, 2, 213);  // series 2

data.setValue(3, 0, new Date(2010, 2, 8));
data.setValue(3, 2, 213);  // series 2

data.setValue(4, 0, new Date(2010, 3, 1));
data.setValue(4, 2, 220);  // series 2

data.setValue(5, 0, new Date(2010, 4, 1));
data.setValue(5, 2, 190);  // series 2

to display series 1, you need to add values for data table column 1.要显示系列 1,您需要为数据表第 1 列添加值。
so it would need to be similar to the following...所以它需要类似于以下内容......

data.setValue(1, 0, new Date(2010, 2, 1));
data.setValue(1, 1, 200);  // series 1
data.setValue(1, 2, 213);  // series 2

data.setValue(2, 0, new Date(2010, 2, 4));
data.setValue(2, 1, 200);  // series 1
data.setValue(2, 2, 213);  // series 2

data.setValue(3, 0, new Date(2010, 2, 8));
data.setValue(3, 1, 210);  // series 1
data.setValue(3, 2, 213);  // series 2

data.setValue(4, 0, new Date(2010, 3, 1));
data.setValue(4, 1, 207);  // series 1
data.setValue(4, 2, 220);  // series 2

data.setValue(5, 0, new Date(2010, 4, 1));
data.setValue(5, 1, 177);  // series 1
data.setValue(5, 2, 190);  // series 2

EDIT编辑

the setValue method takes the following arguments... setValue方法采用以下参数...

setValue(rowIndex, columnIndex, value);

the first argument is the row index in the data table to update.第一个参数是数据表中要更新的行索引。
the second argument is the column index in the data table to update.第二个参数是数据表中要更新的列索引。

here, you are adding 9 blank rows...在这里,您要添加 9 个空白行...

        data.addRows(9);

which then requires you to go back and update each row and column individually.然后需要您返回并单独更新每一行和每一列。

    data.setValue(0, 0, new Date(2010, 1, 1));
    data.setValue(0, 1, 215);
    data.setValue(0, 2, 215);

but if you already have the values for the row,但如果您已经拥有该行的值,
you can use the addRow method.您可以使用addRow方法。

so, instead of adding the 9 blank rows, just add the row values one at a time...因此,不要添加 9 个空白行,只需一次添加一个行值...

data.addRow([new Date(2010, 1, 1), 215, 215]);

or if all of your data is in the form of an array,或者如果你所有的数据都是数组的形式,
you can add all the rows at the same time using the addRows method,您可以使用addRows方法同时添加所有行,
without setting the number of rows...没有设置行数...

data.addRows([
  [new Date(2010, 1, 1), 215, 215],
  [new Date(2010, 2, 1), 215, 215],
  [new Date(2010, 3, 1), 215, 215]
]);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM