jquery.flot.flatdata.js 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. /* Support for flat 1D data series.
  2. A 1D flat data series is a data series in the form of a regular 1D array. The
  3. main reason for using a flat data series is that it performs better, consumes
  4. less memory and generates less garbage collection than the regular flot format.
  5. Example:
  6. plot.setData([[[0,0], [1,1], [2,2], [3,3]]]); // regular flot format
  7. plot.setData([{flatdata: true, data: [0, 1, 2, 3]}]); // flatdata format
  8. Set series.flatdata to true to enable this plugin.
  9. You can use series.start to specify the starting index of the series (default is 0)
  10. You can use series.step to specify the interval between consecutive indexes of the series (default is 1)
  11. */
  12. /* global jQuery*/
  13. (function ($) {
  14. 'use strict';
  15. function process1DRawData(plot, series, data, datapoints) {
  16. if (series.flatdata === true) {
  17. var start = series.start || 0;
  18. var step = typeof series.step === 'number' ? series.step : 1;
  19. datapoints.pointsize = 2;
  20. for (var i = 0, j = 0; i < data.length; i++, j += 2) {
  21. datapoints.points[j] = start + (i * step);
  22. datapoints.points[j + 1] = data[i];
  23. }
  24. if (datapoints.points !== undefined) {
  25. datapoints.points.length = data.length * 2;
  26. } else {
  27. datapoints.points = [];
  28. }
  29. }
  30. }
  31. $.plot.plugins.push({
  32. init: function(plot) {
  33. plot.hooks.processRawData.push(process1DRawData);
  34. },
  35. name: 'flatdata',
  36. version: '0.0.2'
  37. });
  38. })(jQuery);