Upgrade to Pro — share decks privately, control downloads, hide ads and more …

js_charts

Gordon Diggs
June 14, 2013
68

 js_charts

Gordon Diggs

June 14, 2013
Tweet

Transcript

  1. My considerations • Lots of data • Need labels to

    be relevant/understandable • Raw numbers more significant than comparisons Friday, June 14, 13
  2. Google Charts API • Not actually javascript • Static images

    • Pass all the data in the URL • Will do automatic gradient • Deprecated Friday, June 14, 13
  3. Google Charts API (js) • Great interactivity, can bind events

    • Auto-coloring • Pain to set up • Data format kind of weird • Not really open source Friday, June 14, 13
  4.            var  drawChart  =  function(elem)  {

                   var  options  =  {                            backgroundColor:  {                                fill:  '#2F2F2F',                                stroke:  '#2F2F2F'                            },                            pieSliceBorderColor:  '#2F2F2F',                            pieSliceText:  'label',                            pieSliceTextStyle:  {  color:  'black',  fontSize:  '12'  },                            legend:  {position:  'none'},                            chartArea:  {width:  '800',  height:  '500'}                        };                  $.getJSON('/occurrences',  {  field:  $(elem).attr('data-­‐field')  },  function(response)  {                    console.log("response  for  field:",  $(elem).attr('data-­‐field'),  response);                    var  data  =  google.visualization.arrayToDataTable(response.occurrence);                    var  chart  =  new  google.visualization.PieChart(elem)                    chart.draw(data,  options);                      //  add  select  listener  to  jump  to  search  results  from  pie  charts                    google.visualization.events.addListener(chart,  'select',  function()  {                        var  selected  =  chart.getSelection()//,                                label  =  data.getFormattedValue(selected[0].row,  0);                          if(confirm("Go  to  results  for  '"  +  label  +  "'?"))  {                            window.location.href  =  '/?search='  +  encodeURIComponent(label);                        }                    });                });              };              $('.chart').each(function(i)  {                drawChart(this);            }); Friday, June 14, 13
  5. Chart.js • Canvas • Good initialization • Nice animation •

    Limited Interactivity • No labels Friday, June 14, 13
  6.        $('.chart').each(function(i)  {          

     var  $elem  =  $(this),                    ctx  =  $elem.find('canvas')[0].getContext("2d");            $.getJSON('/stats',  {  field:  $elem.attr('data-­‐field')  },  function(response)  {                var  data  =  [],                        colors  =  colorGradient('224466',  '6699bb',  response.length);                $.each(response,  function(i,  datum)  {                    datum.color  =  colors[i  %  colors.length];                    data[i]  =  datum;                });                new  Chart(ctx).Pie(data,  {                    segmentStrokeColor:  '#2F2F2F',                    segmentStrokeWidth:  1,                    animation:  false                });            });        }); Friday, June 14, 13
  7. Chartkick • One line of ruby • Takes ruby data

    structures • Interactivity and labels Friday, June 14, 13