index.html 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
  2. <html>
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  5. <title>Flot Examples: Selection</title>
  6. <link href="../examples.css" rel="stylesheet" type="text/css">
  7. <!--[if lte IE 8]><script language="javascript" type="text/javascript" src="../../excanvas.min.js"></script><![endif]-->
  8. <script language="javascript" type="text/javascript" src="../../jquery.js"></script>
  9. <script language="javascript" type="text/javascript" src="../../jquery.flot.js"></script>
  10. <script language="javascript" type="text/javascript" src="../../jquery.flot.selection.js"></script>
  11. <script type="text/javascript">
  12. $(function() {
  13. // Shim allowing us to get the state of the check-box on jQuery versions
  14. // prior to 1.6, when prop was added. The reason we don't just use attr
  15. // is because it doesn't work in jQuery versions 1.9.x and later.
  16. // TODO: Remove this once Flot's minimum supported jQuery reaches 1.6.
  17. if (typeof $.fn.prop != 'function') {
  18. $.fn.prop = $.fn.attr;
  19. }
  20. var data = [{
  21. label: "United States",
  22. data: [[1990, 18.9], [1991, 18.7], [1992, 18.4], [1993, 19.3], [1994, 19.5], [1995, 19.3], [1996, 19.4], [1997, 20.2], [1998, 19.8], [1999, 19.9], [2000, 20.4], [2001, 20.1], [2002, 20.0], [2003, 19.8], [2004, 20.4]]
  23. }, {
  24. label: "Russia",
  25. data: [[1992, 13.4], [1993, 12.2], [1994, 10.6], [1995, 10.2], [1996, 10.1], [1997, 9.7], [1998, 9.5], [1999, 9.7], [2000, 9.9], [2001, 9.9], [2002, 9.9], [2003, 10.3], [2004, 10.5]]
  26. }, {
  27. label: "United Kingdom",
  28. data: [[1990, 10.0], [1991, 11.3], [1992, 9.9], [1993, 9.6], [1994, 9.5], [1995, 9.5], [1996, 9.9], [1997, 9.3], [1998, 9.2], [1999, 9.2], [2000, 9.5], [2001, 9.6], [2002, 9.3], [2003, 9.4], [2004, 9.79]]
  29. }, {
  30. label: "Germany",
  31. data: [[1990, 12.4], [1991, 11.2], [1992, 10.8], [1993, 10.5], [1994, 10.4], [1995, 10.2], [1996, 10.5], [1997, 10.2], [1998, 10.1], [1999, 9.6], [2000, 9.7], [2001, 10.0], [2002, 9.7], [2003, 9.8], [2004, 9.79]]
  32. }, {
  33. label: "Denmark",
  34. data: [[1990, 9.7], [1991, 12.1], [1992, 10.3], [1993, 11.3], [1994, 11.7], [1995, 10.6], [1996, 12.8], [1997, 10.8], [1998, 10.3], [1999, 9.4], [2000, 8.7], [2001, 9.0], [2002, 8.9], [2003, 10.1], [2004, 9.80]]
  35. }, {
  36. label: "Sweden",
  37. data: [[1990, 5.8], [1991, 6.0], [1992, 5.9], [1993, 5.5], [1994, 5.7], [1995, 5.3], [1996, 6.1], [1997, 5.4], [1998, 5.4], [1999, 5.1], [2000, 5.2], [2001, 5.4], [2002, 6.2], [2003, 5.9], [2004, 5.89]]
  38. }, {
  39. label: "Norway",
  40. data: [[1990, 8.3], [1991, 8.3], [1992, 7.8], [1993, 8.3], [1994, 8.4], [1995, 5.9], [1996, 6.4], [1997, 6.7], [1998, 6.9], [1999, 7.6], [2000, 7.4], [2001, 8.1], [2002, 12.5], [2003, 9.9], [2004, 19.0]]
  41. }];
  42. var options = {
  43. series: {
  44. lines: {
  45. show: true
  46. },
  47. points: {
  48. show: true
  49. }
  50. },
  51. legend: {
  52. noColumns: 2
  53. },
  54. xaxis: {
  55. tickDecimals: 0
  56. },
  57. yaxis: {
  58. min: 0
  59. },
  60. selection: {
  61. mode: "x"
  62. }
  63. };
  64. var placeholder = $("#placeholder");
  65. placeholder.bind("plotselected", function (event, ranges) {
  66. $("#selection").text(ranges.xaxis.from.toFixed(1) + " to " + ranges.xaxis.to.toFixed(1));
  67. var zoom = $("#zoom").prop("checked");
  68. if (zoom) {
  69. $.each(plot.getXAxes(), function(_, axis) {
  70. var opts = axis.options;
  71. opts.min = ranges.xaxis.from;
  72. opts.max = ranges.xaxis.to;
  73. });
  74. plot.setupGrid();
  75. plot.draw();
  76. plot.clearSelection();
  77. }
  78. });
  79. placeholder.bind("plotunselected", function (event) {
  80. $("#selection").text("");
  81. });
  82. var plot = $.plot(placeholder, data, options);
  83. $("#clearSelection").click(function () {
  84. plot.clearSelection();
  85. });
  86. $("#setSelection").click(function () {
  87. plot.setSelection({
  88. xaxis: {
  89. from: 1994,
  90. to: 1995
  91. }
  92. });
  93. });
  94. // Add the Flot version string to the footer
  95. $("#footer").prepend("Flot " + $.plot.version + " &ndash; ");
  96. });
  97. </script>
  98. </head>
  99. <body>
  100. <div id="header">
  101. <h2>Selection</h2>
  102. </div>
  103. <div id="content">
  104. <div class="demo-container">
  105. <div id="placeholder" class="demo-placeholder"></div>
  106. </div>
  107. <p>1000 kg. CO<sub>2</sub> emissions per year per capita for various countries (source: <a href="http://en.wikipedia.org/wiki/List_of_countries_by_carbon_dioxide_emissions_per_capita">Wikipedia</a>).</p>
  108. <p>Flot supports selections through the selection plugin. You can enable rectangular selection or one-dimensional selection if the user should only be able to select on one axis. Try left-click and drag on the plot above where selection on the x axis is enabled.</p>
  109. <p>You selected: <span id="selection"></span></p>
  110. <p>The plot command returns a plot object you can use to control the selection. Click the buttons below.</p>
  111. <p>
  112. <button id="clearSelection">Clear selection</button>
  113. <button id="setSelection">Select year 1994</button>
  114. </p>
  115. <p>Selections are really useful for zooming. Just replot the chart with min and max values for the axes set to the values in the "plotselected" event triggered. Enable the checkbox below and select a region again.</p>
  116. <p><label><input id="zoom" type="checkbox"></input>Zoom to selection.</label></p>
  117. </div>
  118. <div id="footer">
  119. Copyright &copy; 2007 - 2014 IOLA and Ole Laursen
  120. </div>
  121. </body>
  122. </html>