Douglas F Shearer

Ruby Google Charts API Data Encoding


On Friday, Google released their Charts API. You pass your data in a URL, google give you a nice graph back.

The hard part is encoding you’re raw data, into one of Google’s three formats. So I thought I’d help all out!

All of the methods take an array of integers and the maximum value of those integers. It then returns the data in the relevant format.


my_data = [1,2,3,4,5]
my_max_value = my_data.sort.last
simple_encode(my_data, my_max_value)
text_encode(my_data, my_max_value)
extended_encode(my_data, my_max_value)

Simple Encoding

Documentation


    def text_encode(data_arr, max_value)
      # Douglas F Shearer 2007
      # http://douglasfshearer.com/blog/ruby-google-charts-api-data-encoding
      data = ''
      data_arr.each do |value|
        if value.is_a?(Integer) && value >= 0
          data << ((999.0 * value.to_f / max_value.to_f).round/10.to_f).to_s
        else
          data << '-1'
        end
        data << ','
      end
      data.chop
    end

Text Encoding

Documentation


    def text_encode(data_arr, max_value)
      # Douglas F Shearer 2007
      # http://douglasfshearer.com/blog/ruby-google-charts-api-data-encoding
      data = ''
      data_arr.each do |value|
        if value.is_a?(Integer) && value >= 0
          data << ((999.0 * value.to_f / max_value.to_f).round/10.to_f).to_s
        else
          data << '-1'
        end
        data << ','
      end
      data.chop
    end

Extended Encoding

Documentation


    def extended_encode(data_arr, max_value)
      # Douglas F Shearer 2007
      # http://douglasfshearer.com/blog/ruby-google-charts-api-data-encoding
      characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-.'.split(//)
      data = ''
      data_arr.each do |value|
        if value.is_a?(Integer) && value >= 0
          new_value = ((4095 * value.to_f / max_value.to_f).to_i)
          sixtyfours = new_value/64
          units = new_value%64
          p '64:' + sixtyfours.to_s
          p 'units: ' + units.to_s
          data << characters[sixtyfours] + characters[units]
        else
          data << '__'
        end
      end
      data
    end

A Plugin

I may at some point release a ruby wrapper for this as a plugin. Currently I have a fairly good, tested prototype that covers most of the features for line and bar charts. More will be added as I need it, but release may not be imminent.

Any errata or comments for the above should be emailed to my usual address, or just leave a comment below.

Tags

, , , .

December 09 2007 20:12 | Posted in Coding | comments (2)
 

Comments


Gravatar

blj

December 27 2007 07:08

Douglas,

I had started a rails plugin for the google charts API. Wondering if you would mind if I use your encoding methods. I already have the text encoding but in need of the extended version. Appreciate if you would like to join the project. Thanks.

Google charts for rails is here
http://code.googl...

Gravatar

Douglas F Shearer

December 29 2007 09:00

Hi blj.

Yeah, go for it, attribution is all I ask. :o)

Add Your Comments


(Required)

Your email address to get your Gravatar. Address itself is not shown.

(Include the http://)

(Required)

 

You Are Here


Douglas F Shearer

This is the homepage of Douglas F Shearer, a software developer and mountainbike racer. Find out more at the About page.

Hire Me!


Liked my articles on Rails? I'm available for hire for Ruby, Java and PHP work. Find out more.

Gallery Latest


Hiding Behind the Christmas Tree Snow at Secret Location #1 IMG_0181_2 Car Photo Shoot Slide-out Status Panel Dual Redundant Power Supplies

Stay Informed


What is RSS?

Categories


  1. Bike (77)
  2. Coding (77)
  3. Other (44)

Top Tags