提交 | 用户 | 时间
|
58d006
|
1 |
/* Flot plugin for plotting textual data or categories. |
A |
2 |
|
|
3 |
Copyright (c) 2007-2013 IOLA and Ole Laursen. |
|
4 |
Licensed under the MIT license. |
|
5 |
|
|
6 |
Consider a dataset like [["February", 34], ["March", 20], ...]. This plugin |
|
7 |
allows you to plot such a dataset directly. |
|
8 |
|
|
9 |
To enable it, you must specify mode: "categories" on the axis with the textual |
|
10 |
labels, e.g. |
|
11 |
|
|
12 |
$.plot("#placeholder", data, { xaxis: { mode: "categories" } }); |
|
13 |
|
|
14 |
By default, the labels are ordered as they are met in the data series. If you |
|
15 |
need a different ordering, you can specify "categories" on the axis options |
|
16 |
and list the categories there: |
|
17 |
|
|
18 |
xaxis: { |
|
19 |
mode: "categories", |
|
20 |
categories: ["February", "March", "April"] |
|
21 |
} |
|
22 |
|
|
23 |
If you need to customize the distances between the categories, you can specify |
|
24 |
"categories" as an object mapping labels to values |
|
25 |
|
|
26 |
xaxis: { |
|
27 |
mode: "categories", |
|
28 |
categories: { "February": 1, "March": 3, "April": 4 } |
|
29 |
} |
|
30 |
|
|
31 |
If you don't specify all categories, the remaining categories will be numbered |
|
32 |
from the max value plus 1 (with a spacing of 1 between each). |
|
33 |
|
|
34 |
Internally, the plugin works by transforming the input data through an auto- |
|
35 |
generated mapping where the first category becomes 0, the second 1, etc. |
|
36 |
Hence, a point like ["February", 34] becomes [0, 34] internally in Flot (this |
|
37 |
is visible in hover and click events that return numbers rather than the |
|
38 |
category labels). The plugin also overrides the tick generator to spit out the |
|
39 |
categories as ticks instead of the values. |
|
40 |
|
|
41 |
If you need to map a value back to its label, the mapping is always accessible |
|
42 |
as "categories" on the axis object, e.g. plot.getAxes().xaxis.categories. |
|
43 |
|
|
44 |
*/(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); |