forked from pkoltermann/SideBySideImproved
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.flot.orderBars.js
273 lines (230 loc) · 9.2 KB
/
jquery.flot.orderBars.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
/*
* Flot plugin to order bars side by side. This is improved version of Benjamin BUFFET work
* originally from http://en.benjaminbuffet.com/labs/flot/.
*
* Released under the MIT license by Przemyslaw Koltermann, 12-Feb-2013.
*
* This plugin is an alpha version.
*
* To activate the plugin you must specify the parameter "order" for the specific serie :
*
* $.plot($("#placeholder"), [{ data: [ ... ], bars :{ order = null or integer }])
*
* If 2 series have the same order param, they are displayed in the same position;
*
* The plugin adjust the point by adding a value depanding of the barwidth
* Exemple for 3 series (barwidth : 0.1) :
*
* first bar décalage : -0.15
* second bar décalage : -0.05
* third bar décalage : 0.05
*
*/
(function($){
function init(plot){
var orderedBarSeries;
var nbOfBarsToOrder;
var seriesPos = new Array();
var sameSeries = new Array();
var borderWidth;
var borderWidthInXabsWidth;
var pixelInXWidthEquivalent = 1;
var isHorizontal = false;
/*
* This method add shift to x values
*/
function reOrderBars(plot, serie, datapoints){
var shiftedPoints = null;
if(serieNeedToBeReordered(serie)){
checkIfGraphIsHorizontal(serie);
calculPixel2XWidthConvert(plot);
retrieveBarSeries(plot);
calculBorderAndBarWidth(serie);
if(nbOfBarsToOrder >= 2){
var position = findPosition(serie);
var decallage = 0;
var centerBarShift = calculCenterBarShift();
if (isBarAtLeftOfCenter(position)){
decallage = -1*(sumWidth(orderedBarSeries,position-1,Math.floor(nbOfBarsToOrder / 2)-1)) - centerBarShift;
}else{
decallage = sumWidth(orderedBarSeries,Math.ceil(nbOfBarsToOrder / 2),position-2) + centerBarShift + borderWidthInXabsWidth*2;
}
shiftedPoints = shiftPoints(datapoints,serie,decallage);
datapoints.points = shiftedPoints;
}
}
return shiftedPoints;
}
function serieNeedToBeReordered(serie){
return serie.bars != null
&& serie.bars.show
&& serie.bars.order != null;
}
function calculPixel2XWidthConvert(plot){
var gridDimSize = isHorizontal ? plot.getPlaceholder().innerHeight() : plot.getPlaceholder().innerWidth();
var minMaxValues = isHorizontal ? getAxeMinMaxValues(plot.getData(),1) : getAxeMinMaxValues(plot.getData(),0);
var AxeSize = minMaxValues[1] - minMaxValues[0];
pixelInXWidthEquivalent = AxeSize / gridDimSize;
}
function getAxeMinMaxValues(series,AxeIdx){
var minMaxValues = new Array();
for(var i = 0; i < series.length; i++){
minMaxValues[0] = series[i].data[0][AxeIdx];
minMaxValues[1] = series[i].data[series[i].data.length - 1][AxeIdx];
}
if(typeof minMaxValues[0] == 'string'){
minMaxValues[0] = 0;
minMaxValues[1] = series[0].data.length - 1;
}
return minMaxValues;
}
function retrieveBarSeries(plot){
orderedBarSeries = findOthersBarsToReOrders(plot.getData());
nbOfBarsToOrder = orderedBarSeries.length;
}
function findOthersBarsToReOrders(series){
var retSeries = new Array();
for(var i = 0; i < series.length; i++){
if(series[i].bars.order != null && series[i].bars.show){
retSeries.push(series[i]);
}
}
return sortByOrder(retSeries);
}
function sortByOrder(series){
var n = series.length;
do {
for (var i=0; i < n - 1; i++) {
if (series[i].bars.order > series[i + 1].bars.order) {
var tmp = series[i];
series[i] = series[i + 1];
series[i + 1] = tmp;
}
else if (series[i].bars.order == series[i + 1].bars.order) {
//check if any of the series has set sameSeriesArrayIndex
var sameSeriesIndex;
if (series[i].sameSeriesArrayIndex) {
if(series[i + 1].sameSeriesArrayIndex !== undefined) {
sameSeriesIndex = series[i].sameSeriesArrayIndex;
series[i + 1].sameSeriesArrayIndex = sameSeriesIndex;
sameSeries[sameSeriesIndex].push(series[i + 1]);
sameSeries[sameSeriesIndex].sort(sortByWidth);
series[i] = sameSeries[sameSeriesIndex][0];
removeElement(series, i + 1);
}
}
else if (series[i + 1].sameSeriesArrayIndex) {
if(series[i].sameSeriesArrayIndex !== undefined) {
sameSeriesIndex = series[i + 1].sameSeriesArrayIndex;
series[i].sameSeriesArrayIndex = sameSeriesIndex;
sameSeries[sameSeriesIndex].push(series[i]);
sameSeries[sameSeriesIndex].sort(sortByWidth);
series[i] = sameSeries[sameSeriesIndex][0];
removeElement(series, i + 1);
}
}
else {
sameSeriesIndex = sameSeries.length;
sameSeries[sameSeriesIndex] = new Array();
series[i].sameSeriesArrayIndex = sameSeriesIndex;
series[i + 1].sameSeriesArrayIndex = sameSeriesIndex;
sameSeries[sameSeriesIndex].push(series[i]);
sameSeries[sameSeriesIndex].push(series[i + 1]);
sameSeries[sameSeriesIndex].sort(sortByWidth);
series[i] = sameSeries[sameSeriesIndex][0];
removeElement(series, i + 1);
}
i--;
n--;
//leave the wider serie and the other one move to
}
}
n = n-1;
}
while (n>1);
for (var i=0; i < series.length; i++) {
if (series[i].sameSeriesArrayIndex) {
seriesPos[series[i].sameSeriesArrayIndex] = i;
}
}
return series;
}
function sortByWidth(serie1,serie2){
var x = serie1.bars.barWidth ? serie1.bars.barWidth : 1;
var y = serie2.bars.barWidth ? serie2.bars.barWidth : 1;
return ((x < y) ? -1 : ((x > y) ? 1 : 0));
}
function removeElement(arr, from, to) {
var rest = arr.slice((to || from) + 1 || arr.length);
arr.length = from < 0 ? arr.length + from : from;
arr.push.apply(arr, rest);
return arr;
}
function calculBorderAndBarWidth(serie){
borderWidth = typeof serie.bars.lineWidth === "number" ? serie.bars.lineWidth : 2;
borderWidthInXabsWidth = borderWidth * pixelInXWidthEquivalent;
}
function checkIfGraphIsHorizontal(serie){
if(serie.bars.horizontal){
isHorizontal = true;
}
}
function findPosition(serie){
var ss = sameSeries;
var pos = 0;
if (serie.sameSeriesArrayIndex) {
pos = seriesPos[serie.sameSeriesArrayIndex];
}
else {
for (var i = 0; i < orderedBarSeries.length; ++i) {
if (serie == orderedBarSeries[i]){
pos = i;
break;
}
}
}
return pos+1;
}
function calculCenterBarShift(){
var width = 0;
if(nbOfBarsToOrder%2 != 0)
width = (orderedBarSeries[Math.ceil(nbOfBarsToOrder / 2)].bars.barWidth)/2;
return width;
}
function isBarAtLeftOfCenter(position){
return position <= Math.ceil(nbOfBarsToOrder / 2);
}
function sumWidth(series,start,end){
var totalWidth = 0;
for(var i = start; i <= end; i++){
totalWidth += series[i].bars.barWidth+borderWidthInXabsWidth*2;
}
return totalWidth;
}
function shiftPoints(datapoints,serie,dx){
var ps = datapoints.pointsize;
var points = datapoints.points;
var j = 0;
for(var i = isHorizontal ? 1 : 0;i < points.length; i += ps){
points[i] += dx;
//Adding the new x value in the serie to be abble to display the right tooltip value,
//using the index 3 to not overide the third index.
serie.data[j][3] = points[i];
j++;
}
return points;
}
plot.hooks.processDatapoints.push(reOrderBars);
}
var options = {
series : {
bars: {order: null} // or number/string
}
};
$.plot.plugins.push({
init: init,
options: options,
name: "orderBars",
version: "0.2"
});
})(jQuery)