Merge pull request #38 from laggingreflex/flot-orderBars

update/fix flot-orderBars
pull/42/head
Aigars Silkalns 2016-02-26 16:53:53 +02:00
commit 838ad4db96
1 changed files with 36 additions and 22 deletions

View File

@ -1,7 +1,8 @@
/* /*
* 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,35 +32,44 @@
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
*/ */
function reOrderBars(plot, serie, datapoints){ function reOrderBars(plot, serie, datapoints){
var shiftedPoints = null; var shiftedPoints = null;
if(serieNeedToBeReordered(serie)){ if(serieNeedToBeReordered(serie)){
checkIfGraphIsHorizontal(serie); checkIfGraphIsHorizontal(serie);
calculPixel2XWidthConvert(plot); calculPixel2XWidthConvert(plot);
retrieveBarSeries(plot); retrieveBarSeries(plot);
calculBorderAndBarWidth(serie); calculBorderAndBarWidth(serie);
if(nbOfBarsToOrder >= 2){ if(nbOfBarsToOrder >= 2){
var position = findPosition(serie); var position = findPosition(serie);
var decallage = 0; var decallage = 0;
var centerBarShift = calculCenterBarShift(); var centerBarShift = calculCenterBarShift();
if (isBarAtLeftOfCenter(position)){ // If we haven't already calculated the decallage for this order value, do it.
decallage = -1*(sumWidth(orderedBarSeries,position-1,Math.floor(nbOfBarsToOrder / 2)-1)) - centerBarShift; if(typeof decallageByOrder[serie.bars.order] === 'undefined') {
}else{ if (isBarAtLeftOfCenter(position)){
decallage = sumWidth(orderedBarSeries,Math.ceil(nbOfBarsToOrder / 2),position-2) + centerBarShift + borderWidthInXabsWidth*2; decallageByOrder[serie.bars.order] = -1*(sumWidth(orderedBarSeries,position-1,Math.floor(nbOfBarsToOrder / 2)-1)) - centerBarShift;
}else{
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;
} }
} }
return shiftedPoints; return shiftedPoints;
} }
function serieNeedToBeReordered(serie){ function serieNeedToBeReordered(serie){
@ -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,10 +121,10 @@
} }
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;
} }
function checkIfGraphIsHorizontal(serie){ function checkIfGraphIsHorizontal(serie){
if(serie.bars.horizontal){ if(serie.bars.horizontal){
isHorizontal = true; isHorizontal = true;
@ -154,7 +169,7 @@
function shiftPoints(datapoints,serie,dx){ function shiftPoints(datapoints,serie,dx){
var ps = datapoints.pointsize; var ps = datapoints.pointsize;
var points = datapoints.points; var points = datapoints.points;
var j = 0; var j = 0;
for(var i = isHorizontal ? 1 : 0;i < points.length; i += ps){ for(var i = isHorizontal ? 1 : 0;i < points.length; i += ps){
points[i] += dx; points[i] += dx;
//Adding the new x value in the serie to be abble to display the right tooltip value, //Adding the new x value in the serie to be abble to display the right tooltip value,
@ -183,5 +198,4 @@
version: "0.2" version: "0.2"
}); });
})(jQuery) })(jQuery);