<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: graphs code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Fri, 25 Jul 2008 16:36:40 GMT</pubDate>
    <description>DZone Snippets: graphs code</description>
    <item>
      <title>Rails helpers for A List Apart No. 256</title>
      <link>http://snippets.dzone.com/posts/show/5435</link>
      <description>I cooked up some Ruby on Rails helpers for testing the techniques in the article &lt;a href="http://www.alistapart.com/articles/accessibledatavisualization"&gt;Accessible Data Visualization with Web Standards&lt;/a&gt; from A List Apart &lt;a href="http://www.alistapart.com/issues/256"&gt;No. 256&lt;/a&gt;. If you want to know more about Rails, check out &lt;a href="http://www.alistapart.com/issues/257"&gt;No. 257&lt;/a&gt;. If you want to be smarter, read &lt;a href="http://www.alistapart.com/"&gt;A List Apart every week&lt;/a&gt;.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;def chartlist(data)&lt;br /&gt;  total = data.inject(0.0) { |sum, datum| sum + datum[:count] }&lt;br /&gt;  bars = ''&lt;br /&gt;&lt;br /&gt;  data.each do |datum|&lt;br /&gt;    link  = content_tag 'a', datum[:name], :href =&gt; datum[:href]&lt;br /&gt;    count = content_tag 'span', datum[:count], :class =&gt; 'count'&lt;br /&gt;    index = content_tag 'span', "(#{(datum[:count]/total*100).to_i}%)", :class =&gt; 'index', :style =&gt; "width: #{(datum[:count]/total*100).to_i}%"&lt;br /&gt;    bars &lt;&lt; content_tag('li', link &lt;&lt; count &lt;&lt; index)&lt;br /&gt;  end&lt;br /&gt;&lt;br /&gt;  content_tag 'ul', bars, :class =&gt; 'chartlist'&lt;br /&gt;end&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Sample data for a chartlist (&lt;a href="http://www.alistapart.com/d/accessibledata/example-barchart.html"&gt;example&lt;/a&gt;)&lt;br /&gt;&lt;br /&gt;&lt;code&gt;data = [{ :name =&gt; 'Apples',   :count =&gt; 420, :href =&gt; 'http://www.example.com/fruits/apples/' },&lt;br /&gt;        { :name =&gt; 'Bananas',  :count =&gt; 280, :href =&gt; 'http://www.example.com/fruits/bananas/' },&lt;br /&gt;        { :name =&gt; 'Cherries', :count =&gt; 200, :href =&gt; 'http://www.example.com/fruits/cherries/' },&lt;br /&gt;        { :name =&gt; 'Dates',    :count =&gt; 100, :href =&gt; 'http://www.example.com/fruits/dates/' }]&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;&lt;code&gt;def sparkline(data)&lt;br /&gt;  max = data.sort.last.to_f&lt;br /&gt;  sparklines = ''&lt;br /&gt;&lt;br /&gt;  data.each_with_index do |datum, index|&lt;br /&gt;    count_string = datum.to_s&lt;br /&gt;    '(' &lt;&lt; count_string if index == 0&lt;br /&gt;    count_string &lt;&lt; ',' if index != data.length&lt;br /&gt;    count_string &lt;&lt; ')' if index == data.length&lt;br /&gt;    count = content_tag 'span', count_string, :class =&gt; 'count', :style =&gt; "height: #{(datum/max*100).to_i}%"&lt;br /&gt;    index = content_tag 'span', count &lt;&lt; ' ', :class =&gt; 'index'&lt;br /&gt;    sparklines &lt;&lt; index&lt;br /&gt;  end&lt;br /&gt;&lt;br /&gt;  content_tag('span', sparklines, :class =&gt; 'sparkline')&lt;br /&gt;end&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Sample data for sparklines (&lt;a href="http://www.alistapart.com/d/accessibledata/example-sparklines.html"&gt;example&lt;/a&gt;)&lt;br /&gt;&lt;br /&gt;&lt;code&gt;data = [60, 220, 140, 80, 110, 90, 180, 140, 120, 160, 175, 225, 175, 125]&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;&lt;code&gt;def timeline(data)&lt;br /&gt;  max = data.sort { |a, b| a[:count] &lt;=&gt; b[:count] }.last[:count]&lt;br /&gt;  bars = ''&lt;br /&gt;&lt;br /&gt;  data.each do |datum|&lt;br /&gt;    label = content_tag 'span', datum[:label], :class =&gt; 'label'&lt;br /&gt;    count = content_tag 'span', "(#{datum[:count]})", :class =&gt; 'count', :style =&gt; "height: #{(datum[:count]/max.to_f*100).to_i}%"&lt;br /&gt;    link  = content_tag 'a', label &lt;&lt; count, :href =&gt; datum[:href], :title =&gt; "#{datum[:label]}: #{datum[:count]}"&lt;br /&gt;    bars &lt;&lt; content_tag('li', link, :style =&gt; "width: #{(100.0/data.length).to_i}%")&lt;br /&gt;  end&lt;br /&gt;&lt;br /&gt;  content_tag 'ul', bars, :class =&gt; 'timeline'&lt;br /&gt;end&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Sample data for a timeline (&lt;a href="http://www.alistapart.com/d/accessibledata/example-timeline.html"&gt;example&lt;/a&gt;)&lt;br /&gt;&lt;br /&gt;&lt;code&gt;data = [{ :date =&gt; '2007-12-01', :count =&gt;  40, label =&gt; '1' },&lt;br /&gt;        { :date =&gt; '2007-12-02', :count =&gt; 100, label =&gt; '2' },&lt;br /&gt;        { :date =&gt; '2007-12-03', :count =&gt; 150, label =&gt; '3' }]&lt;/code&gt;</description>
      <pubDate>Sun, 27 Apr 2008 23:01:52 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5435</guid>
      <author>sporkyy (Todd Sayre)</author>
    </item>
  </channel>
</rss>
