ruby on rails - how to display charts/graphs based on user specific data -
in ror project, use sqlite database storing registered users data. using highcharts , able generate charts based on data database. need generate graphs each user based on specific data in database.
my database columns
id, name, category 1 , category2, catogery 3
in sqlite3 database, need when user logs-in shld able see pie-graph using data category1, category2 , category 3 columns .
any appreciated.
there's few ways of doing this, here's one
controllers/charts_controller.rb
class chartscontroller < applicationcontroller def pie_chart_for_users @this_user = user.find(1) end end
views/charts/pie_chart_for_users.html.erb
<script type="text/javascript"> var $j = jquery.noconflict(); $j(document).ready(function() { new highcharts.chart({ chart: { renderto: 'users_chart', plotbackgroundcolor: null, plotborderwidth: null, plotshadow: false }, title: { text: 'unknown user data' }, plotoptions: { pie: { allowpointselect: true, cursor: 'pointer', datalabels: { enabled: false } } }, series: [{ type: 'pie', name: 'user data', data: [ ['category one', <%= @this_user.category_one %>], ['category two', <%= @this_user.category_two %>], ['category three', <%= @this_user.category_three %>] ] }] }); }); </script> <div id="users_chart"></div>
config/routes.rb
map.pie_chart_for_users 'charts/pie_chart_for_users', :controller => 'charts', :action => 'pie_chart_for_users'
based around highcharts pie chart demo, used named route demo controller , action names call - should able use resources instead. think should give idea although haven't run test it.
Comments
Post a Comment