PageRenderTime 47ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/examples/data-renderers.php

https://bitbucket.org/cleonello/jqplot/
PHP | 107 lines | 67 code | 26 blank | 14 comment | 2 complexity | 305bf069c811450212dbfc51a3c3325c MD5 | raw file
Possible License(s): LGPL-3.0, GPL-2.0, MIT
  1. <?php
  2. $title = "AJAX and JSON Data Loading via Data Renderers";
  3. // $plotTargets = array (array('id'=>'chart1', 'width'=>600, 'height'=>400));
  4. ?>
  5. <?php include "opener.php"; ?>
  6. <!-- Example scripts go here -->
  7. <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>
  8. <div id="chart1" style="height:300px; width:500px;"></div>
  9. <pre class="code prettyprint brush: js"></pre>
  10. <p>Data renderers get passed options by the plot. The signiture for a data renderer is:</p>
  11. <pre class="brush: js">
  12. function(userData, plotObject, options) {
  13. ...
  14. return data;
  15. }
  16. </pre>
  17. <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>
  18. <div id="chart2" style="height:300px; width:500px;"></div>
  19. <pre class="code prettyprint brush: js"></pre>
  20. <script class="code" type="text/javascript">
  21. $(document).ready(function(){
  22. // Our data renderer function, returns an array of the form:
  23. // [[[x1, sin(x1)], [x2, sin(x2)], ...]]
  24. var sineRenderer = function() {
  25. var data = [[]];
  26. for (var i=0; i<13; i+=0.5) {
  27. data[0].push([i, Math.sin(i)]);
  28. }
  29. return data;
  30. };
  31. // we have an empty data array here, but use the "dataRenderer"
  32. // option to tell the plot to get data from our renderer.
  33. var plot1 = $.jqplot('chart1',[],{
  34. title: 'Sine Data Renderer',
  35. dataRenderer: sineRenderer
  36. });
  37. });
  38. </script>
  39. <script class="code" type="text/javascript">
  40. $(document).ready(function(){
  41. // Our ajax data renderer which here retrieves a text file.
  42. // it could contact any source and pull data, however.
  43. // The options argument isn't used in this renderer.
  44. var ajaxDataRenderer = function(url, plot, options) {
  45. var ret = null;
  46. $.ajax({
  47. // have to use synchronous here, else the function
  48. // will return before the data is fetched
  49. async: false,
  50. url: url,
  51. dataType:"json",
  52. success: function(data) {
  53. ret = data;
  54. }
  55. });
  56. return ret;
  57. };
  58. // The url for our json data
  59. var jsonurl = "./jsondata.txt";
  60. // passing in the url string as the jqPlot data argument is a handy
  61. // shortcut for our renderer. You could also have used the
  62. // "dataRendererOptions" option to pass in the url.
  63. var plot2 = $.jqplot('chart2', jsonurl,{
  64. title: "AJAX JSON Data Renderer",
  65. dataRenderer: ajaxDataRenderer,
  66. dataRendererOptions: {
  67. unusedOptionalUrl: jsonurl
  68. }
  69. });
  70. });
  71. </script>
  72. <!-- End example scripts -->
  73. <!-- Don't touch this! -->
  74. <?php include "commonScripts.html" ?>
  75. <!-- End Don't touch this! -->
  76. <!-- Additional plugins go here -->
  77. <script class="include" language="javascript" type="text/javascript" src="../src/plugins/jqplot.json2.js"></script>
  78. <!-- End additional plugins -->
  79. <?php include "closer.php"; ?>