update/fix flot-orderBars

https://github.com/emmerich/flot-orderBars
pull/38/head
laggingreflex 9 years ago
parent 10f514a543
commit 469bacffbf

@ -2,6 +2,7 @@
* Flot plugin to order bars side by side. * Flot plugin to order bars side by side.
* *
* Released under the MIT license by Benjamin BUFFET, 20-Sep-2010. * Released under the MIT license by Benjamin BUFFET, 20-Sep-2010.
* Modifications made by Steven Hall <github.com/emmerich>, 01-May-2013.
* *
* This plugin is an alpha version. * This plugin is an alpha version.
* *
@ -20,6 +21,8 @@
* *
*/ */
// INFO: decalage/decallage is French for gap. It's used to denote the spacing applied to each
// bar.
(function($){ (function($){
function init(plot){ function init(plot){
var orderedBarSeries; var orderedBarSeries;
@ -29,6 +32,9 @@
var pixelInXWidthEquivalent = 1; var pixelInXWidthEquivalent = 1;
var isHorizontal = false; var isHorizontal = false;
// A mapping of order integers to decallage.
var decallageByOrder = {};
/* /*
* This method add shift to x values * This method add shift to x values
*/ */
@ -47,12 +53,18 @@
var centerBarShift = calculCenterBarShift(); var centerBarShift = calculCenterBarShift();
// If we haven't already calculated the decallage for this order value, do it.
if(typeof decallageByOrder[serie.bars.order] === 'undefined') {
if (isBarAtLeftOfCenter(position)){ if (isBarAtLeftOfCenter(position)){
decallage = -1*(sumWidth(orderedBarSeries,position-1,Math.floor(nbOfBarsToOrder / 2)-1)) - centerBarShift; decallageByOrder[serie.bars.order] = -1*(sumWidth(orderedBarSeries,position-1,Math.floor(nbOfBarsToOrder / 2)-1)) - centerBarShift;
}else{ }else{
decallage = sumWidth(orderedBarSeries,Math.ceil(nbOfBarsToOrder / 2),position-2) + centerBarShift + borderWidthInXabsWidth*2; decallageByOrder[serie.bars.order] = sumWidth(orderedBarSeries,Math.ceil(nbOfBarsToOrder / 2),position-2) + centerBarShift + borderWidthInXabsWidth*2;
}
} }
// Lookup the decallage based on the series' order value.
decallage = decallageByOrder[serie.bars.order];
shiftedPoints = shiftPoints(datapoints,serie,decallage); shiftedPoints = shiftPoints(datapoints,serie,decallage);
datapoints.points = shiftedPoints; datapoints.points = shiftedPoints;
} }
@ -76,8 +88,8 @@
function getAxeMinMaxValues(series,AxeIdx){ function getAxeMinMaxValues(series,AxeIdx){
var minMaxValues = new Array(); var minMaxValues = new Array();
for(var i = 0; i < series.length; i++){ for(var i = 0; i < series.length; i++){
minMaxValues[0] = series[i].data[0][AxeIdx]; minMaxValues[0] = (series[i].data[0]) ? series[i].data[0][AxeIdx]: null;
minMaxValues[1] = series[i].data[series[i].data.length - 1][AxeIdx]; minMaxValues[1] = (series[i].data[series[i].data.length - 1]) ? series[i].data[series[i].data.length - 1][AxeIdx]: null;
} }
return minMaxValues; return minMaxValues;
} }
@ -89,13 +101,16 @@
function findOthersBarsToReOrders(series){ function findOthersBarsToReOrders(series){
var retSeries = new Array(); var retSeries = new Array();
var orderValuesSeen = [];
for(var i = 0; i < series.length; i++){ for(var i = 0; i < series.length; i++){
if(series[i].bars.order != null && series[i].bars.show){ if(series[i].bars.order != null && series[i].bars.show &&
orderValuesSeen.indexOf(series[i].bars.order) < 0){
orderValuesSeen.push(series[i].bars.order);
retSeries.push(series[i]); retSeries.push(series[i]);
} }
} }
return retSeries.sort(sortByOrder); return retSeries.sort(sortByOrder);
} }
@ -106,7 +121,7 @@
} }
function calculBorderAndBarWidth(serie){ function calculBorderAndBarWidth(serie){
borderWidth = serie.bars.lineWidth ? serie.bars.lineWidth : 2; borderWidth = typeof serie.bars.lineWidth !== 'undefined' ? serie.bars.lineWidth : 2;
borderWidthInXabsWidth = borderWidth * pixelInXWidthEquivalent; borderWidthInXabsWidth = borderWidth * pixelInXWidthEquivalent;
} }
@ -183,5 +198,4 @@
version: "0.2" version: "0.2"
}); });
})(jQuery) })(jQuery);
Loading…
Cancel
Save