jquery.flot.categories.min.js 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. /* Flot plugin for plotting textual data or categories.
  2. Copyright (c) 2007-2013 IOLA and Ole Laursen.
  3. Licensed under the MIT license.
  4. Consider a dataset like [["February", 34], ["March", 20], ...]. This plugin
  5. allows you to plot such a dataset directly.
  6. To enable it, you must specify mode: "categories" on the axis with the textual
  7. labels, e.g.
  8. $.plot("#placeholder", data, { xaxis: { mode: "categories" } });
  9. By default, the labels are ordered as they are met in the data series. If you
  10. need a different ordering, you can specify "categories" on the axis options
  11. and list the categories there:
  12. xaxis: {
  13. mode: "categories",
  14. categories: ["February", "March", "April"]
  15. }
  16. If you need to customize the distances between the categories, you can specify
  17. "categories" as an object mapping labels to values
  18. xaxis: {
  19. mode: "categories",
  20. categories: { "February": 1, "March": 3, "April": 4 }
  21. }
  22. If you don't specify all categories, the remaining categories will be numbered
  23. from the max value plus 1 (with a spacing of 1 between each).
  24. Internally, the plugin works by transforming the input data through an auto-
  25. generated mapping where the first category becomes 0, the second 1, etc.
  26. Hence, a point like ["February", 34] becomes [0, 34] internally in Flot (this
  27. is visible in hover and click events that return numbers rather than the
  28. category labels). The plugin also overrides the tick generator to spit out the
  29. categories as ticks instead of the values.
  30. If you need to map a value back to its label, the mapping is always accessible
  31. as "categories" on the axis object, e.g. plot.getAxes().xaxis.categories.
  32. */(function(e){function n(e,t,n,r){var i=t.xaxis.options.mode=="categories",s=t.yaxis.options.mode=="categories";if(!i&&!s)return;var o=r.format;if(!o){var u=t;o=[],o.push({x:!0,number:!0,required:!0}),o.push({y:!0,number:!0,required:!0});if(u.bars.show||u.lines.show&&u.lines.fill){var a=!!(u.bars.show&&u.bars.zero||u.lines.show&&u.lines.zero);o.push({y:!0,number:!0,required:!1,defaultValue:0,autoscale:a}),u.bars.horizontal&&(delete o[o.length-1].y,o[o.length-1].x=!0)}r.format=o}for(var f=0;f<o.length;++f)o[f].x&&i&&(o[f].number=!1),o[f].y&&s&&(o[f].number=!1)}function r(e){var t=-1;for(var n in e)e[n]>t&&(t=e[n]);return t+1}function i(e){var t=[];for(var n in e.categories){var r=e.categories[n];r>=e.min&&r<=e.max&&t.push([r,n])}return t.sort(function(e,t){return e[0]-t[0]}),t}function s(t,n,r){if(t[n].options.mode!="categories")return;if(!t[n].categories){var s={},u=t[n].options.categories||{};if(e.isArray(u))for(var a=0;a<u.length;++a)s[u[a]]=a;else for(var f in u)s[f]=u[f];t[n].categories=s}t[n].options.ticks||(t[n].options.ticks=i),o(r,n,t[n].categories)}function o(e,t,n){var i=e.points,s=e.pointsize,o=e.format,u=t.charAt(0),a=r(n);for(var f=0;f<i.length;f+=s){if(i[f]==null)continue;for(var l=0;l<s;++l){var c=i[f+l];if(c==null||!o[l][u])continue;c in n||(n[c]=a,++a),i[f+l]=n[c]}}}function u(e,t,n){s(t,"xaxis",n),s(t,"yaxis",n)}function a(e){e.hooks.processRawData.push(n),e.hooks.processDatapoints.push(u)}var t={xaxis:{categories:null},yaxis:{categories:null}};e.plot.plugins.push({init:a,options:t,name:"categories",version:"1.0"})})(jQuery);