I have a chart page with 12 charts on it. Each chart is in a self
contained partial. Each chart is a different group of data however the
common filter is a date range which I pass into the partial.
I implemented a jQRangeSlider to select the date range and pass that to
the controller which in turn re-renders the page.
I can’t seem to get the charts to refresh showing the new data. I can
see in the terminal window the queries being called and they have the
right date however the chart is not refreshing.
HELP !!!
John
Part of the Controller
class ChartsController < ApplicationController
def charts
if params.has_key?(:startdate) @startdate = params[:startdate] @enddate = params[:enddate]
end @displaystartdate = @startdate.to_date.strftime("%m/%d/%Y") @displayenddate = @enddate.to_date.strftime("%m/%d/%Y")
respond_to do |format|
format.html
format.js
end
end
end
Primary Chart View
<%= link_to 'Back to Main Menu', :controller => 'home',
:action => 'menu' %>
Dates from <%=
@displaystartdate %> to <%= @displayenddate %>
Your using a $.ajax call but not handling the response in any way. It
also
seems that the data for your charts is being driven by instance
variables
from the controller. This means that the data is set for the chart
once,
when the page is rendered and then never again.
I think what you want to do here is have your front-end charts populated
through an ajax call, wrapped in a method. This same method is used
when
you need to refresh the data on the page. Essentially the HTML is
rendered
to give you the page structure and populate this with data using the
ajax
call, as opposed to using iVars from the controller. Take a look at the
jQuery documentation to get an idea on how to handle the ajax response.
You will want to write handler code for error responses as well to
improve
your debugging experience.
Does this make sense/help you out? If not I can toss a small example of
what I’m talking about here.
On Monday, February 15, 2016 at 11:50:47 AM UTC-8, Ruby-Forum.com User
So in your _chart8 partial you are populating chart.series.data using
the
proved startdate and enddate variables. You will want to change these
values when your ajax function returns data meaning you should first
persist your chart object instead of just creating it without
assignment.
Something simple would be
$(function() {
window.chart8 = new Highcharts.Chart({
// etc…
Once this chart is accessible you can update your ajax query to do
something like this (forgive me I’m not familiar with the Chart API you
are
using)
$.ajax({
url: “/charts”,
type: ‘POST’,
data: { startdate: newstartdate, enddate : newenddate},
cache: false,
dataType: ‘json’
}).done(function(data) {
window.chart8.series.data = data // or some such thing
}).fail(function(msg) {
//something went wrong with the request
});