fbpx

Now before i get into the nuts and bolts of my problem. I must confess that i was quite skeptical about the google charts API, mainly because of using the mostly awesome Jfreecharts in the past. However having used it for a bit now, i am warming up to the idea of it, because quite frankly it is much simpler to use. You are likely to have a “cool” looking chart with tooltips etc in a much shorter time when compared to JFreeCharts.

Anyway so about my problem, recently i was asked to create a column chart such that each column is colored differently.  My data looked something like this

  var data google.visualization.arrayToDataTable([
    [‘Year’‘Games Finished’],
    [‘2003’,  12],
    [‘2004’,  6],
    [‘2005’,  15],
  ]);

The problem with this was that each column in the column chart is the same color,  you see it seems like the google charts API assigns a color per column. So one of the workarounds i.e. a hack for this was to represent the data as an adjacency matrix and exploit the stacking functionality of the column charts. In the adjacency matrix the values for each data point will be across the diagonal, and everything else would be 0. What i mean is the data should be  structured like this

 function drawVisualization({
  var data google.visualization.arrayToDataTable([
    [‘Year’‘2003’‘2004’‘2005’],
    [‘2003’,  12,0,0],
    [‘2004’,  0,6,0],
    [‘2005’,  0,0,15],
  ]);
  new google.visualization.ColumnChart(document.getElementById(‘visualization’)).
      draw(data,
           {title:“Games Finished per Year”,
            width:600height:400,
            hAxis{title“Year”},
            vAxis:{title:“Games Finished”},
            isStacked:true}
      );
}

This solution isn’t perfect, but it is a solution none the less.  You can give it a go in the Google visualization playground.

I will write a follow up post (at some point) with a function that will take two js arrays and build a dataTable object which will be an adj matrix of the data in the arrays.

asdmbnld

Like the blog? Subscribe for updates

As usual, if you find any of my posts useful support us by  buying or even trying one of our products and leave us a review on the app store.

‎My Day To-Do - Smart Task List
‎My Day To-Do - Smart Task List
‎AUD $ Currency Converter
‎AUD $ Currency Converter
Developer: Bhuman Soni
Price: $0.99
‎My Day To-Do Lite - Task list
‎My Day To-Do Lite - Task list
‎Snap! I was there
‎Snap! I was there
Developer: Bhuman Soni
Price: Free
Numbers Game: Calculation Master
Numbers Game: Calculation Master
‎Simple 'N' Easy: Todos & food
‎Simple 'N' Easy: Todos & food
‎Captain's Personal Log
‎Captain's Personal Log
Developer: Bhuman Soni
Price: $4.99
My Simple Notes
My Simple Notes
Developer: Bhuman Soni
Price: Free
‎My Simple Notes - Dictate
‎My Simple Notes - Dictate
Developer: Bhuman Soni
Price: $2.99

Categories: Javascript

0 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *