morrisjs / morris.js

Pretty time-series line graphs
http://morrisjs.github.com/morris.js/
6.92k stars 1.23k forks source link

Donut chart rendered incorrectly when using double quotes #74

Closed luizkowalski closed 12 years ago

luizkowalski commented 12 years ago

I have this JSON

[{"label":"Auxilio de NF-e","value":"1"},{"label":"Manuten\u00e7\u00e3o de ativos","value":"9"},{"label":"Auxilio de CT-e","value":"1"},{"label":"Ajuda a cliente interno","value":"2"}]

When I try to use it on donut chart, like this

<script>
Morris.Donut({
  element: 'donut',
  data: <%= @result_donut.to_json.to_s.html_safe %>
});
</script>

the output is rendered like this picture Imgur

if I remove the double quotes on value, it works, but this data comes from a sql which I convert to JSON

oesmith commented 12 years ago

You're nearly there with removing the quotes from value -- Morris.js requires numerical values in that field.

Try using a helper (or maybe a decorator?) to map #to_i against the values.

luizkowalski commented 12 years ago

I'm still searching for a way to do this... Any piece of code?

oesmith commented 12 years ago

Something like the following, perhaps?

# in application_helper.rb
def donut_json
  @result_donut.map { |x| {:label => x[:label], :value => x[:value].to_i} }.to_json.to_s.html_safe
end

.. and then, in your view

<script>
Morris.Donut({
  element: 'donut',
  data: <%= donut_json %>
});
</script>

Disclaimer: this is quick and dirty code, so you may want to tidy/reorganise it as you see fit.

luizkowalski commented 12 years ago

changed to this:

<%= @donut.map { |x| {:label => x['label'], :value => x['value'].to_i} }.to_json.to_s.html_safe %>

Worked...now, as I'm working with worked and business hours, would be nice if Donuts graphics could have a formatter to the values, because I'm getting the values in seconds, but this should be another issue :)

luizkowalski commented 12 years ago

I'm closing this one