RW Hammond
RW Hammond

Reputation: 113

Google Line Charts

I am currently importing data from a Google Sheet so that editors can make simple updates.

The issue I am running into is I cannot get the labels/annotations to appear on the graph. I'd like it to display the column name, ideally name and data if possible.

Any help is appreciated. 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);
  }

Upvotes: 1

Views: 62

Answers (2)

RW Hammond
RW Hammond

Reputation: 113

Thanks to whitehat for the help. just need to add json data for each column. If you're looking to do this, i also added another column with the header rows as columns to get both pieces of data to display.

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 );
  }

Upvotes: 0

WhiteHat
WhiteHat

Reputation: 61275

the first argument of the draw method should be the data table / view.
in this case, replace ticketsData with view

chart.draw(view, options);

Upvotes: 0

Related Questions