KKK
KKK

Reputation: 1662

How to customize Google Chart

I'm creating new chart table with Google Chart. I want to get the result rank wise. For Example:

A:10 B:5 C:2 D:1

So the result should display something like this

D,C,B,A

Is there any way way to do that. (I've added minus to values. But its not proper way right?)

Upvotes: 1

Views: 927

Answers (1)

jmac
jmac

Reputation: 7128

Yes, it is possible.

Using the base code here as a base:

var drawVisualizations = function() {
  // Create and populate a data table.
  var data = new google.visualization.DataTable();
  data.addColumn('string', 'Force');
  data.addColumn('number', 'Level');

  // Add 2 rows.
  // google.visualization.DataTable.addRows() can take a 2-dim array of values.
  data.addRows([['Fire', 1], ['Water', 5]]);

  // Add one more row.
  data.addRow(['sand', 4]);

  // Draw a table with this data table.
  var originalVisualization = new google.visualization.Table(document.getElementById('original_data_table'));
  originalVisualization.draw(data);

  // Clone the data table and modify it a little.
  var modifiedData = data.clone();

  // Modify existing cell.
  modifiedData.setCell(1, 1, 666);

  // Sort the data by the 2nd column (counting from 0).
  modifiedData.sort(1);

  // Insert rows in the middle of the data table.
  modifiedData.insertRows(2, [['new fire', 14], ['new water', 41]]);

  // Draw a table with this data table.
  var modifiedVisualization = new google.visualization.Table(document.getElementById('modified_data_table'));
  modifiedVisualization.draw(modifiedData);
}

As you can see, using "sort()" on your datatable will sort. Documentation on the sort command is located here

Upvotes: 1

Related Questions