PDA

View Full Version : ScaleProvider that supports negative scale values for BarCharts



dardison
7 May 2009, 11:09 AM
Hi,

You can use this ScaleProvider to manage BarChart scale to graph not only positive but negative figures, and generates integer adhoc intervals.


public class BarScaleProvider implements ScaleProvider {

public Scale calcScale(double min, double max) {
Scale result=null;
int divideCount=0;
boolean found=false;
Double interval;
Double minValue;
Double maxValue;

double offset=max - min;
if(offset>1000){
while(!found){
offset=offset / 1000;
divideCount++;
if(offset<1000)
found=true;
}
}
interval=this.getInterval(offset);
for(int i=0; i<divideCount;i++){
interval=interval*1000;
}
interval=interval.intValue()*1.0;
minValue=min;
minValue=minValue.intValue()*1.0;
maxValue=max;
maxValue=maxValue.intValue()*1.0;
Double remainder=maxValue / interval;
remainder=remainder.intValue()*1.0;
maxValue=maxValue+interval-remainder;
if(min<0.0){
minValue=0.0;
while(minValue>min){
minValue=minValue-interval;
}
}
result=new Scale();
result.setMin(minValue);
result.setMax(maxValue);
result.setInterval(interval);
return result;
}

protected double getInterval(double value){
Range[] ranges={ new Range(500, 1000, 100), new Range(200, 500, 50),
new Range(100, 200, 20) , new Range(50, 100, 10),
new Range(20, 50, 5) , new Range(10, 20, 2),
new Range(5, 10, 1) , new Range(2, 5, 0.5),
new Range(1, 2, 0.2)};
double interval=0;
for(Range r : ranges){
if(value>=r.left && value<r.right){
interval=r.interval;
break;
}
}
return interval;

}

protected class Range {
double left;
double right;
double interval;

protected Range(double left, double right, double interval){
this.left=left;
this.right=right;
this.interval=interval;
}
}

}

sven
10 May 2009, 6:53 AM
I fixed the GXT scaleprovider to support also negative values the correct way. The fix will be part of M2