javascript - rails and highcharts column chart -
hi im stuck im using highcharts in rails app , trying column chart show how many male users have compared female users in database cant seem figure out how data read users database table. here user model
def self.gender_analysis(gender) user.where(:gender => male).count user.where(:gender => female).count end
here user.js file crate chart
$(function(){ new highcharts.chart({ chart:{ renderto: "pie_chart", type:"column" }, title: { text: "gender" }, xaxis: { type: "percent correct"}, yaxis: { title: { text: "gender" } }, series: [{ data: <%= users.each |user| %> }] }); });
and user view.html.erb looks like
<div id="pie_chart" style="width:568px; height:300px;"></div> <h1>listing users</h1> <table> <thead> <tr> <th>number</th> <th>gender</th> <th>hobby</th> <th>created</th> <th></th> <th></th> <th></th> </tr> </thead> <tbody> <% @users.each |user| %> <tr> <td><%= user.number %></td> <td><%= user.gender %></td> <td><%= user.hobby %></td> <td><%= user.created_at %></td>
i new rails developing , appreciate given
it's simple: in view
, mean in html.erb
put:
<script src="http://code.highcharts.com/highcharts.js"></script> <script src="http://code.highcharts.com/modules/exporting.js"></script> <div id="container" style="min-width: 310px; height: 400px; margin: 0 auto"></div>
and add script
:
$(function () { $('#container').highcharts({ chart: { type: 'column' }, title: { text: 'total user' }, subtitle: { text: 'source: unknown' }, xaxis: { categories: ['male', 'female'], title: { text: null } }, yaxis: { min: 0, title: { text: 'total number', align: 'high' }, labels: { overflow: 'justify' } }, tooltip: { valuesuffix: '' }, plotoptions: { bar: { datalabels: { enabled: true } } }, credits: { enabled: false }, series: [{ name: 'users', data: [<%= user.where(:gender => 'male').count %>, <%= user.where(:gender => 'female').count %>] }] }); });
also here check out fiddle.
happy coding! :)
Comments
Post a Comment