Oatmeal
Oatmeal

Reputation: 759

how to display charts/graphs based on user specific data

In my ROR project, I use sqlite database for storing my registered users data. I am using highcharts and I am able to generate charts based on data from my database. Now I need to generate graphs for each user based on their specific data in database.

My database columns

id, name, category 1 , category2, catogery 3
in sqlite3 database, I need when a user logs-in he shld be able to see a pie-graph using data from his category1, category2 and category 3 columns .

Any help is appreciated.

Upvotes: 0

Views: 1083

Answers (1)

Pasted
Pasted

Reputation: 864

There's a few ways of doing this, but 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 a named route just to demo which controller and action names to call - you should be able to just use resources instead. Think that should give you an idea although I haven't run this up to test it.

Upvotes: 1

Related Questions