first commit
[wnstats.git] / public / javascripts / jqplot / examples / data-renderers.html
blobba835d584a8d2f9bd4023733a17a2367eace223e
1 <!DOCTYPE html>
3 <html>
4 <head>
6 <title>AJAX and JSON Data Loading via Data Renderers</title>
8 <link class="include" rel="stylesheet" type="text/css" href="../jquery.jqplot.min.css" />
9 <link rel="stylesheet" type="text/css" href="examples.min.css" />
10 <link type="text/css" rel="stylesheet" href="syntaxhighlighter/styles/shCoreDefault.min.css" />
11 <link type="text/css" rel="stylesheet" href="syntaxhighlighter/styles/shThemejqPlot.min.css" />
13 <!--[if lt IE 9]><script language="javascript" type="text/javascript" src="../excanvas.js"></script><![endif]-->
14 <script class="include" type="text/javascript" src="../jquery.min.js"></script>
17 </head>
18 <body>
19 <div class="logo">
20 <div class="nav">
21 <a class="nav" href="../../../index.php"><span>&gt;</span>Home</a>
22 <a class="nav" href="../../../docs/"><span>&gt;</span>Docs</a>
23 <a class="nav" href="../../download/"><span>&gt;</span>Download</a>
24 <a class="nav" href="../../../info.php"><span>&gt;</span>Info</a>
25 <a class="nav" href="../../../donate.php"><span>&gt;</span>Donate</a>
26 </div>
27 </div>
28 <div class="example-content">
30 <div class="example-nav">
31 <a href="dashedLines.html">Previous</a> <a href="./">Examples</a> <a href="date-axes.html">Next</a></div>
34 <!-- Example scripts go here -->
36 <p>Data renderers allow jqPlot to pull data from any external source (e.g. a function implementing an AJAX call). Simply assign the external source to the "dataRenderer" plot option. The only requirement on data renderers is that it must return a valid jqPlot data array.</p>
38 <div id="chart1" style="height:300px; width:500px;"></div>
40 <pre class="code prettyprint brush: js"></pre>
43 <p>Data renderers get passed options by the plot. The signiture for a data renderer is:</p>
46 <pre class="brush: js">
47 function(userData, plotObject, options) {
48 ...
49 return data;
51 </pre>
54 <p>Where userData is whatever data was passed into the plot, plotObject is a reference back to the plot itself, and options are any options passed into the plots "dataRendererOption" option. The following example shows a more complicated example which uses ajax pulls data from an external json data source.</p>
56 <div id="chart2" style="height:300px; width:500px;"></div>
58 <pre class="code prettyprint brush: js"></pre>
61 <script class="code" type="text/javascript">
62 $(document).ready(function(){
63 // Our data renderer function, returns an array of the form:
64 // [[[x1, sin(x1)], [x2, sin(x2)], ...]]
65 var sineRenderer = function() {
66 var data = [[]];
67 for (var i=0; i<13; i+=0.5) {
68 data[0].push([i, Math.sin(i)]);
70 return data;
73 // we have an empty data array here, but use the "dataRenderer"
74 // option to tell the plot to get data from our renderer.
75 var plot1 = $.jqplot('chart1',[],{
76 title: 'Sine Data Renderer',
77 dataRenderer: sineRenderer
78 });
79 });
80 </script>
83 <script class="code" type="text/javascript">
84 $(document).ready(function(){
85 // Our ajax data renderer which here retrieves a text file.
86 // it could contact any source and pull data, however.
87 // The options argument isn't used in this renderer.
88 var ajaxDataRenderer = function(url, plot, options) {
89 var ret = null;
90 $.ajax({
91 // have to use synchronous here, else the function
92 // will return before the data is fetched
93 async: false,
94 url: url,
95 dataType:"json",
96 success: function(data) {
97 ret = data;
99 });
100 return ret;
103 // The url for our json data
104 var jsonurl = "./jsondata.txt";
106 // passing in the url string as the jqPlot data argument is a handy
107 // shortcut for our renderer. You could also have used the
108 // "dataRendererOptions" option to pass in the url.
109 var plot2 = $.jqplot('chart2', jsonurl,{
110 title: "AJAX JSON Data Renderer",
111 dataRenderer: ajaxDataRenderer,
112 dataRendererOptions: {
113 unusedOptionalUrl: jsonurl
117 </script>
120 <!-- End example scripts -->
122 <!-- Don't touch this! -->
125 <script class="include" type="text/javascript" src="../jquery.jqplot.min.js"></script>
126 <script type="text/javascript" src="syntaxhighlighter/scripts/shCore.min.js"></script>
127 <script type="text/javascript" src="syntaxhighlighter/scripts/shBrushJScript.min.js"></script>
128 <script type="text/javascript" src="syntaxhighlighter/scripts/shBrushXml.min.js"></script>
129 <!-- End Don't touch this! -->
131 <!-- Additional plugins go here -->
133 <script class="include" language="javascript" type="text/javascript" src="../plugins/jqplot.json2.min.js"></script>
135 <!-- End additional plugins -->
138 </div>
139 <script type="text/javascript" src="example.min.js"></script>
141 </body>
144 </html>