I'm trying to create a pie chart using jqPlot, gathering data from a Rails database.
My problem occurs because jqplot doesn't understand the strings generated from event.name. If I manually insert a couple of strings, without going through Rails, it works fine.nI believe it has something to do with escaping characters it shouldn't be.
The error I get from Firebug shows events = [["Title", 1234566]]
. Thus jqplot tries to create this chart with "Title"
as a key. It doesn't like that.
I've tried using html_safe, to no avail.
<% things = [] %>
<% @topEvents.each do |event| %>
<% things << [event.name, event.total] %>
<% end %>
$.jqplot('topEvents', [ <%= things %> ], {
seriesDefaults : {
renderer : jQuery.jqplot.PieRenderer,
rendererOptions : {
showDataLabels: true
}
}
});
For future googlers...
Although "string"
and 'string'
may be equivalent once they are declared in Ruby, this fact is irrelevant when it comes to javascript. Within a <script>
tag, ruby strings will show up as being double quoted. If you want single quotes, you're going to have to import the string as something other than a string, and then manually add the quotes later.
Javascript will understand '<%= event.total %>'
just fine. It will use Rails to print out the total, and then the script will append single quotes to either side.
Hope this helps anyone!