Reputation: 3052
i am trying to figure out how to input data values into highcharts. Firstly i have a home object(@home) that has many energies(energy table consists of three columns, namely, consumption, home_id and consumption_date)
This is what i have so far:
<script type="text/javascript">
var chart;
$(function() {
new Highcharts.Chart({
chart: {
renderTo: "consumption_chart",
},
title: {
text: "Consumption - <%= @home.name %>"
},
xAxis: {
type: 'datetime',
},
yAxis: {
title: {
text: "Power"
}
},
series: [{
data: <%= @data_set %>
}]
});
});
</script>
For the data part i get all the values like this
@data_set = @home.energies.collect{|e| e.consumption_date.utc}.zip(@home.energies.collect{|g| g.consumption})
This returns a nested set of arrays like this:
[[2012-03-02 09:06:00 UTC, 1200], [2012-04-30 00:00:00 UTC, 1145], [2012-04-30 00:00:00 UTC, 1158], [2012-05-31 00:00:00 UTC, 1145]]
Each nested array is just of the form [consumption_date, consumption]. However this does not work(chart does not render any data), please can someone point out my errors and tell me what i am doing incorrectly. thanks
Upvotes: 2
Views: 1400
Reputation: 987
Could you try to use lazy-high-chart gem? It's easy and it will save your time when doing this.
https://github.com/michelson/lazy_high_charts
e.g. In controller,
@h = LazyHighCharts::HighChart.new('graph') do |f|
f.options[:chart][:defaultSeriesType] = "area"
f.series(:name=>'John', :data=>[3, 20, 3, 5, 4, 10, 12 ,3, 5,6,7,7,80,9,9])
f.series(:name=>'Jane', :data=> [1, 3, 4, 3, 3, 5, 4,-46,7,8,8,9,9,0,0,9] )
end
In view,
<%= high_chart("my_id", @h) %>
Upvotes: 3