31 var borderWidthInXabsWidth;
32 var pixelInXWidthEquivalent = 1;
33 var isHorizontal =
false;
36 var decallageByOrder = {};
41 function reOrderBars(plot, serie, datapoints){
42 var shiftedPoints = null;
44 if(serieNeedToBeReordered(serie)){
45 checkIfGraphIsHorizontal(serie);
46 calculPixel2XWidthConvert(plot);
47 retrieveBarSeries(plot);
48 calculBorderAndBarWidth(serie);
50 if(nbOfBarsToOrder >= 2){
51 var position = findPosition(serie);
54 var centerBarShift = calculCenterBarShift();
57 if(typeof decallageByOrder[serie.bars.order] ===
'undefined') {
58 if (isBarAtLeftOfCenter(position)){
59 decallageByOrder[serie.bars.order] = -1*(sumWidth(orderedBarSeries,position-1,Math.floor(nbOfBarsToOrder / 2)-1)) - centerBarShift;
61 decallageByOrder[serie.bars.order] = sumWidth(orderedBarSeries,Math.ceil(nbOfBarsToOrder / 2),position-2) + centerBarShift + borderWidthInXabsWidth*2;
66 decallage = decallageByOrder[serie.bars.order];
68 shiftedPoints = shiftPoints(datapoints,serie,decallage);
69 datapoints.points = shiftedPoints;
75 function serieNeedToBeReordered(serie){
76 return serie.bars != null
78 && serie.bars.order != null;
81 function calculPixel2XWidthConvert(plot){
82 var gridDimSize = isHorizontal ? plot.getPlaceholder().innerHeight() : plot.getPlaceholder().innerWidth();
83 var minMaxValues = isHorizontal ? getAxeMinMaxValues(plot.getData(),1) : getAxeMinMaxValues(plot.getData(),0);
84 var AxeSize = minMaxValues[1] - minMaxValues[0];
85 pixelInXWidthEquivalent = AxeSize / gridDimSize;
88 function getAxeMinMaxValues(series,AxeIdx){
89 var minMaxValues =
new Array();
90 for(var i = 0; i < series.length; i++){
91 minMaxValues[0] = (series[i].data[0]) ? series[i].data[0][AxeIdx]: null;
92 minMaxValues[1] = (series[i].data[series[i].data.length - 1]) ? series[i].data[series[i].data.length - 1][AxeIdx]: null;
97 function retrieveBarSeries(plot){
98 orderedBarSeries = findOthersBarsToReOrders(plot.getData());
99 nbOfBarsToOrder = orderedBarSeries.length;
102 function findOthersBarsToReOrders(series){
103 var retSeries =
new Array();
104 var orderValuesSeen = [];
106 for(var i = 0; i < series.length; i++){
107 if(series[i].bars.order != null && series[i].bars.show &&
108 orderValuesSeen.indexOf(series[i].bars.order) < 0){
110 orderValuesSeen.push(series[i].bars.order);
111 retSeries.push(series[i]);
114 return retSeries.sort(sortByOrder);
117 function sortByOrder(serie1,serie2){
118 var x = serie1.bars.order;
119 var y = serie2.bars.order;
120 return ((x < y) ? -1 : ((x > y) ? 1 : 0));
123 function calculBorderAndBarWidth(serie){
124 borderWidth = typeof serie.bars.lineWidth !==
'undefined' ? serie.bars.lineWidth : 2;
125 borderWidthInXabsWidth = borderWidth * pixelInXWidthEquivalent;
128 function checkIfGraphIsHorizontal(serie){
129 if(serie.bars.horizontal){
134 function findPosition(serie){
136 for (var i = 0; i < orderedBarSeries.length; ++i) {
137 if (serie == orderedBarSeries[i]){
146 function calculCenterBarShift(){
149 if(nbOfBarsToOrder%2 != 0)
150 width = (orderedBarSeries[Math.ceil(nbOfBarsToOrder / 2)].bars.barWidth)/2;
155 function isBarAtLeftOfCenter(position){
156 return position <= Math.ceil(nbOfBarsToOrder / 2);
159 function sumWidth(series,start,end){
162 for(var i = start; i <= end; i++){
163 totalWidth += series[i].bars.barWidth+borderWidthInXabsWidth*2;
169 function shiftPoints(datapoints,serie,dx){
170 var ps = datapoints.pointsize;
171 var points = datapoints.points;
173 for(var i = isHorizontal ? 1 : 0;i < points.length; i += ps){
177 serie.data[j][3] = points[i];
184 plot.hooks.processDatapoints.push(reOrderBars);
194 $.plot.plugins.push({