Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Postive/Negative Chart in Google Visualization API

I need to generate a chart like this one:

positve/negative chart

Specifically, I want to show both a positive value and a negative value for a time period (could be an hour, minute, etc.) and display it like this.

I could have sworn I saw something like this on the Google Visualization API Gallery the other day, but I can't find it now, and am not even sure what this kind of chart is called.

First, do you know what this kind of chart is called so I can possibly find documentation? Second, is there any way to implement such a chart with the Google Visualization API? If not, is there another common charting solution for web that I can achieve this with?

Thank you for your time.

like image 906
Brad Avatar asked Dec 10 '22 10:12

Brad


1 Answers

This is called a "Stacked Bar Chart", and can indeed be created with the Google Visualisation API.

Simply use the "isStacked" property (described here; http://code.google.com/apis/visualization/documentation/gallery/barchart.html).

Here's some sample code (based off the default bar chart example provided by Google and updated to show the use of isStacked and some sample data from your example);

function drawVisualization() {

  var data = new google.visualization.DataTable();
  data.addColumn('string', 'Month');
  data.addColumn('number');
  data.addColumn('number');

  data.addRows(12);

  data.setCell(0, 0, 'January');
  data.setCell(1, 0, 'February');
  data.setCell(2, 0, 'March');
  data.setCell(3, 0, 'April');
  data.setCell(4, 0, 'May');
  data.setCell(5, 0, 'June');
  data.setCell(6, 0, 'July');
  data.setCell(7, 0, 'August');
  data.setCell(8, 0, 'September');
  data.setCell(9, 0, 'October');
  data.setCell(10, 0, 'November');
  data.setCell(11, 0, 'December');

  data.setCell(0, 1, 19);
  data.setCell(1, 1, 18);
  data.setCell(2, 1, 20);
  data.setCell(3, 1, 19);
  data.setCell(4, 1, 18);
  data.setCell(5, 1, 20);
  data.setCell(6, 1, 19);
  data.setCell(7, 1, 18);
  data.setCell(8, 1, 20);
  data.setCell(9, 1, 19);
  data.setCell(10, 1, 18);
  data.setCell(11, 1, 20);

  data.setCell(0, 2, -12);
  data.setCell(1, 2, -13);
  data.setCell(2, 2, -11);
  data.setCell(3, 2, -12);
  data.setCell(4, 2, -13);
  data.setCell(5, 2, -11);
  data.setCell(6, 2, -12);
  data.setCell(7, 2, -13);
  data.setCell(8, 2, -11);
  data.setCell(9, 2, -12);
  data.setCell(10, 2, -13);
  data.setCell(11, 2, -11);
  data.setCell(0, 2, -12);
  data.setCell(1, 2, -13);
  data.setCell(2, 2, -11);

  // Create and draw the visualization.
  new google.visualization.ColumnChart(document.getElementById('visualization')).
      draw(data,
           {title:"S&P 500 Up/Down Performance Since 1980", 
            width:600, height:400,
            isStacked:"true",
            legend:"none" }
      );
}

And the results...

Stacked Bar Chart

like image 152
Chris McAtackney Avatar answered Dec 18 '22 07:12

Chris McAtackney