Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Discover properly negative max values #8

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 19 additions & 13 deletions Sparklines/src/org/vaadin/sparklines/client/ui/SparklinesGWT.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
// TODO setDotSize
// TODO ? set
public class SparklinesGWT extends Composite {

private static final double MAXIMAL_VALUE = Double.MAX_VALUE;
private static final double MINIMAL_VALUE = -(Double.MAX_VALUE - 1);
public static String CLASSNAME = "v-sparkline";

private static int PAD = 4;
Expand All @@ -41,10 +42,10 @@ public class SparklinesGWT extends Composite {
protected String pathColor = "#ccc";
protected int pathWidth = 1;

protected double max = Double.MIN_VALUE;
protected double max = MINIMAL_VALUE;
protected double maxidx = 0;
protected double minidx = 0;
protected double min = Double.MAX_VALUE;
protected double min = MAXIMAL_VALUE;

protected int avg = 0;

Expand All @@ -71,7 +72,7 @@ public class SparklinesGWT extends Composite {
protected String valueColor = "#00f";

public SparklinesGWT(String caption, int graphWidth, int graphHeight,
int displayRangeMin, int displayRangeMax) {
int displayRangeMin, int displayRangeMax) {
this.graphHeight = graphHeight;
this.graphWidth = graphWidth;
this.displayHigh = displayRangeMax;
Expand Down Expand Up @@ -284,10 +285,10 @@ public void setValueDotVisible(boolean valueDotVisible) {
}

private void redraw() {
max = Double.MIN_VALUE;
max = MINIMAL_VALUE;
maxidx = 0;
minidx = 0;
min = Integer.MAX_VALUE;
min = MAXIMAL_VALUE;

avg = 0;

Expand Down Expand Up @@ -324,9 +325,8 @@ private void redraw() {
}
value.setText(String.valueOf(data[data.length - 1]));

double effectiveMin = (displayLow < Double.MAX_VALUE ? displayLow : min);
double effectiveMax = (displayHigh > Double.MIN_VALUE ? displayHigh
: max);
double effectiveMin = effectiveMin();
double effectiveMax = effectiveMax();

if (graphHeight < 1) {
// canvas.setHeight(effectiveMax - effectiveMin + PAD);
Expand All @@ -342,8 +342,7 @@ private void redraw() {
canvas.setWidth(graphWidth);
}

vscale = (double) (canvas.getHeight() - PAD)
/ (effectiveMax - effectiveMin);
vscale = (double) (canvas.getHeight() - PAD) / (effectiveMax - effectiveMin);
hscale = (double) (canvas.getWidth() - PAD) / (data.length - 1);

// average
Expand Down Expand Up @@ -392,9 +391,16 @@ private void redraw() {

}

private double effectiveMin() {
return displayLow < MAXIMAL_VALUE ? displayLow : min;
}

private double effectiveMax() {
return displayHigh > MINIMAL_VALUE ? displayHigh : max;
}

private int translateY(double y) {
double effectiveMin = (displayLow < Integer.MAX_VALUE ? displayLow
: min);
double effectiveMin = effectiveMin();
int newY = (int) ((y - effectiveMin) * vscale);
int res = (canvas.getHeight() - newY) - OFFSET;
return res;
Expand Down