【问题标题】:Google Line Charts谷歌折线图
【发布时间】:2019-03-14 17:37:43
【问题描述】:

我目前正在从 Google 表格导入数据,以便编辑人员可以进行简单的更新。

我遇到的问题是我无法让标签/注释出现在图表上。如果可能的话,我希望它显示列名,最好是名称和数据。

感谢任何帮助。 https://jsfiddle.net/rwhammond/g0fvw3do/8/

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

  function initialize() {
    // The URL of the spreadsheet to source data from.
    var query = new google.visualization.Query('https://docs.google.com/spreadsheets/d/1uD7OFv6xDKJdvB7gLBm5WMjeE8442NWZE3XP8gvpqhc/edit#gid=0&range=A1:I7');
    query.send(draw);
  }

  function draw(response) {
    var ticketsData = response.getDataTable();
    var options = {
        legend: 'top',
    };
    var view = new google.visualization.DataView(ticketsData);
    view.setColumns([0, 1, {
        calc: "stringify",
        sourceColumn: 1,
        type: "string",
        role: "annotation"
    }]);
    var chart = new google.visualization.LineChart(document.getElementById('chart_div'));
    chart.draw(ticketsData, options, view);
  }

【问题讨论】:

    标签: google-visualization


    【解决方案1】:

    draw 方法的第一个参数应该是数据表/视图。
    在这种情况下,将ticketsData 替换为view

    chart.draw(view, options);
    

    【讨论】:

    • 100% 谢谢,所以我只需要修改为:view.setColumns([0, 1, { calc: "stringify", sourceColumn: 1, type: "string", role: " annotation" }, 2, { calc: "stringify", sourceColumn: 1, type: "string", role: "annotation" } ]);
    【解决方案2】:

    感谢白帽的帮助。只需要为每一列添加json数据。如果您希望这样做,我还添加了另一列,其中标题行作为列来显示两条数据。

    google.charts.load("current", {
        packages: ['corechart']
      });
      google.charts.setOnLoadCallback(initialize);
    
      function initialize() {
        // The URL of the spreadsheet to source data from.
        var query = new google.visualization.Query('https://docs.google.com/spreadsheets/d/1uD7OFv6xDKJdvB7gLBm5WMjeE8442NWZE3XP8gvpqhc/edit#gid=0&range=A1:I7');
        query.send(draw);
      }
    
      function draw(response) {
        var ticketsData = response.getDataTable();
        var options = {
            legend: 'top',
        };
        var view = new google.visualization.DataView(ticketsData);
        view.setColumns([0, 
            1,
            {
          calc: "stringify",
          sourceColumn: 1,
          type: "string",
          role: "annotation",
          },
          {
          calc: "stringify",
          sourceColumn: 9,
          type: "string",
          role: "annotation",
          },
          2,
            {
          calc: "stringify",
          sourceColumn: 1,
          type: "string",
          role: "annotation",
          },
          {
          calc: "stringify",
          sourceColumn: 9,
          type: "string",
          role: "annotation",
          },
          3,
            {
          calc: "stringify",
          sourceColumn: 1,
          type: "string",
          role: "annotation",
          },
          {
          calc: "stringify",
          sourceColumn: 9,
          type: "string",
          role: "annotation",
          },
          4,
            {
          calc: "stringify",
          sourceColumn: 1,
          type: "string",
          role: "annotation",
          },
          {
          calc: "stringify",
          sourceColumn: 9,
          type: "string",
          role: "annotation",
          },
          5,
            {
          calc: "stringify",
          sourceColumn: 1,
          type: "string",
          role: "annotation",
          },
          {
          calc: "stringify",
          sourceColumn: 9,
          type: "string",
          role: "annotation",
          },
          6,
            {
          calc: "stringify",
          sourceColumn: 1,
          type: "string",
          role: "annotation",
          },
          {
          calc: "stringify",
          sourceColumn: 9,
          type: "string",
          role: "annotation",
          },
          7,
            {
          calc: "stringify",
          sourceColumn: 1,
          type: "string",
          role: "annotation",
          },
          {
          calc: "stringify",
          sourceColumn: 9,
          type: "string",
          role: "annotation",
          },
        ]);
    
        var chart = new google.visualization.LineChart(document.getElementById('chart_div'));
        chart.draw(view, options );
      }
    

    【讨论】:

      猜你喜欢
      • 2022-01-16
      • 1970-01-01
      • 1970-01-01
      • 2014-12-27
      • 2020-08-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多