I write something like this out to the page:
<script type="text/javascript" charset="utf-8">
//<![CDATA[
scheduleData = $j.parseJSON('{\"lines\":
[[[\"2011-10-04T01:00:00-07:00\",0],[\"2011-10-05T01:00:00-07:00\",0],
[\"2011-10-06T01:00:00-07:00\",0][\"2012-03-04T00:00:00-08:00\",0]]],
\"y_label\":\"# Tests\",\"chart_start_date\":\"2011-10-02 01:00:00
-0700\",\"chart_end_date\":\"2012-03-04 00:00:00 -0800\",,
\"y_axis_height\":60}');
// ]]>
</script>
How you write that out will depend on how you're implementing your web
app, but here is how I do it with a Rails 3.2 app:
<script type="text/javascript" charset="utf-8">
//<![CDATA[
scheduleData = $j.parseJSON('<%=j
@schedule_data.to_json.html_safe -%>');
// ]]>
</script>
And @schedule_data is just a hash
{ :lines => get_lines,
:chart_start_date => chart_start_date.to_s,
:chart_end_date => chart_end_date.to_s,
:y_axis_height => get_y_axis_height(test_total) }
Then I can reference it like this, passing it to the method that will
use it.
// If there is a schedule, draw it
$j(".schedule").each(function(index) {
drawSchedule('mainSchedule', scheduleData);
});
And here's a portion of that method showing how I access the lines and
a variable:
// Draw a schedule
function drawSchedule(targetId, scheduleData) {
$j('#' + targetId).empty();
$j.jqplot(targetId, scheduleData.lines, {
axes: {
xaxis: {
renderer: $j.jqplot.DateAxisRenderer,
labelRenderer: $j.jqplot.CanvasAxisLabelRenderer,
tickRenderer: $j.jqplot.CanvasAxisTickRenderer,
min: scheduleData.chart_start_date, max:
scheduleData.chart_end_date
},
> snippet.txt
> 2KViewDownload