dimanche 25 octobre 2015

Highcharts, ROR basic visualisation of table data

I am trying to get a bar chart to show data from a table. The table is called notes and I want to be able to show the total notes created by users on each day over a period of time. A basic chart.

I followed the railscast tutorial on this.

The chart interacts with the data but I get crazy numbers.

<script>
 $(function() {
  new Highcharts.Chart({
    chart: {
      renderTo: "notes_chart",
      type: "column"
    },
    title: {
      text: "Notes by Day"
    },
    xAxis: {
      type: "datetime"
    },
    yAxis: {
      title: {
        text: "Notes",
      }
    },
    tooltip: {
      formatter: function() {
        return Highcharts.dateFormat("%B %e, %Y", this.x) + ':' +
         "Number of Notes:" + Highcharts.numberFormat(this.y, 0);
      }
    },
    series: [{
      pointInterval: <%= 1.day * 1000 %>,
      pointStart: <%= 9.weeks.ago.to_i * 1000 %>,
      data: <%= (9.weeks.ago.to_date..Date.today).map { |date| Note.created_at(date)}.inspect %>
    }]
  });
 });
 </script>

Note.rb

def self.created_at(date)  
    where("date(created_at) = ?",date).sum(:created_at) 
    end  

The y axis show the number of notes in 1,000's (don't know why). When I create a new note it shows I have created 2,000 and something notes. I just want it to show how many notes were created on such a date.

Would appreciate any guidance. Perhaps I am going about it totally the wrong way with the created_at column?

Here is my table

<table class="table table-striped border table-bordered table-hover success panel-body" id="datatable">


  <thead>
    <tr class="tableheaders danger">
      <th>Created at</th>
      <th>Updated at</th>
      <th>Teacher ID</th>
      <th>Name</th>
      <th><%= sort_link(@q, :email) %></th>
      <th><%= sort_link(@q, :Student_Alias) %></th>
      <th><%= sort_link(@q, :subject_type) %></th>
      <th>Grievance</th>
      <th>Penalty</th>
      <th colspan="3"></th>
    </tr>
  </thead>
  <tbody>
    <% @notes.each do |note| %>
      <tr>
        <td><%= note.created_at %></td>
        <td><%= note.updated_at.localtime %></td>
        <td><%= note.user_id %></td>
        <td><%= note.user.full_name %></td>
        <td><%= note.user.email %></td>
        <td><%= note.studentname %></td>
        <td><%= note.subject_type %></td>
        <td><%= note.grievance %></td>
        <td><%= note.penalty %></td>
        <td><%= link_to 'Show', note, class: 'btn btn-primary btn-xs' %></td>

          <% if note.user==current_user %>
          <td><%= link_to 'Edit', edit_note_path(note), class: 'btn btn-info btn-xs' %></td>
          <% end %>
          <% if current_user.admin %>
          <td><%= link_to 'Destroy', note, method: :delete, data: { confirm: 'Are you sure?' }, class: 'btn btn-danger btn-xs' %></td>
          <% end %>

        </tr>
      <% end %>
    </tbody>
  </table>

I just want to create a simple bar or line chart that shows the number of notes on each day over a given period. Hope someone can help. Thanks.

Aucun commentaire:

Enregistrer un commentaire