import jlab.graphics.DWindow;
public class LineGraph {
private DWindow window;
private double ymin, ymax;
public LineGraph(int w, int h) {
window = new DWindow(w, h);
}
public void plot(int color, TimeSeries ts) {
if (ymin == ymax) {
ymin = ts.min(); ymax = ts.max();
}
int n = ts.size();
double width = window.getWidth();
double height = window.getHeight();
for (int i = 0; i < n-1; i++) {
double sx0 = toScreen(i, 0, n-1, width);
double sx1 = toScreen(i+1, 0, n-1, width);
double sy0 = toScreen(ts.get(i), ymax, ymin, height);
double sy1 = toScreen(ts.get(i+1), ymax, ymin, height);
window.drawLine(color, sx0, sy0, sx1, sy1);
}
}
private static double toScreen(double v, double min,
double max, double len) {
return (v - min) * (len-1) / (max - min);
}
}
|