235 lines
5.3 KiB
Java
235 lines
5.3 KiB
Java
package org.hwo.ui.diagram;
|
|
|
|
import java.awt.Color;
|
|
import java.awt.Desktop;
|
|
import java.awt.Font;
|
|
import java.awt.Graphics2D;
|
|
import java.awt.Toolkit;
|
|
import java.awt.image.BufferedImage;
|
|
import java.io.ObjectInputStream.GetField;
|
|
import java.util.ArrayList;
|
|
import java.util.LinkedList;
|
|
import java.util.List;
|
|
|
|
public class Diagram {
|
|
|
|
private PlotProvider plotProvider;
|
|
private PlotPainter plotPainter;
|
|
|
|
private int x0,
|
|
x1,
|
|
y0,
|
|
y1;
|
|
|
|
int yh,xw;
|
|
|
|
private float yscale;
|
|
private float yoffset;
|
|
|
|
private Font font;
|
|
|
|
private int numLabels;
|
|
|
|
private String labelFormatSpec;
|
|
|
|
|
|
private LinkedList<ColoredBackground> coloredBackgrounds;
|
|
|
|
public Diagram(){
|
|
coloredBackgrounds = new LinkedList<ColoredBackground>();
|
|
yoffset = -1.0f;
|
|
yscale = 2.0f;
|
|
plotProvider = new SimplePlotProvider(2, 2);
|
|
plotPainter = new LinePlotPainter();
|
|
font = new Font("Arial", Font.PLAIN, 24);
|
|
numLabels = 0;
|
|
labelFormatSpec = "%.4f";
|
|
|
|
coloredBackgrounds.add(new ColoredBackground(new Color(0.7f, 1.0f, 0.7f),-0.25f,0.25f));
|
|
coloredBackgrounds.add(new ColoredBackground(new Color(1.0f, 1.0f, 0.5f),-0.5f,-0.25f));
|
|
coloredBackgrounds.add(new ColoredBackground(new Color(1.0f, 1.0f, 0.5f),0.25f,0.5f));
|
|
|
|
|
|
}
|
|
|
|
public float getYoffset() {
|
|
return yoffset;
|
|
}
|
|
public void setYoffset(float yoffset) {
|
|
this.yoffset = yoffset;
|
|
}
|
|
public float getYscale() {
|
|
return yscale;
|
|
}
|
|
public void setYscale(float yscale) {
|
|
this.yscale = yscale;
|
|
}
|
|
|
|
public Font getFont() {
|
|
return font;
|
|
}
|
|
public void setFont(Font font) {
|
|
this.font = font;
|
|
}
|
|
|
|
public int getNumLabels() {
|
|
return numLabels;
|
|
}
|
|
public void setNumLabels(int numLabels) {
|
|
this.numLabels = numLabels;
|
|
}
|
|
|
|
public PlotPainter getPlotPainter() {
|
|
return plotPainter;
|
|
}
|
|
public void setPlotPainter(PlotPainter plotPainter) {
|
|
this.plotPainter = plotPainter;
|
|
}
|
|
|
|
public PlotProvider getPlotProvider() {
|
|
return plotProvider;
|
|
}
|
|
public void setPlotProvider(PlotProvider plotProvider) {
|
|
this.plotProvider = plotProvider;
|
|
}
|
|
|
|
public String getLabelFormatSpec() {
|
|
return labelFormatSpec;
|
|
}
|
|
public void setLabelFormatSpec(String labelFormatSpec) {
|
|
this.labelFormatSpec = labelFormatSpec;
|
|
}
|
|
|
|
public List<ColoredBackground> getColoredBackgrounds() {
|
|
return coloredBackgrounds;
|
|
}
|
|
|
|
|
|
public void autoScale(){
|
|
Float max = Float.MIN_VALUE,min = Float.MAX_VALUE;
|
|
Float[][] matrix = this.plotProvider.getMatrix();
|
|
|
|
|
|
for (Float[] p:matrix){
|
|
for (Float value: p){
|
|
if (value < min)
|
|
min = value;
|
|
if (value > max)
|
|
max = value;
|
|
}
|
|
}
|
|
|
|
yoffset = min;
|
|
yscale = max - min;
|
|
|
|
System.err.println(String.format("AutoScale: %f x %f", yoffset, yscale));
|
|
}
|
|
|
|
public BufferedImage plot(int width,int height){
|
|
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_4BYTE_ABGR);
|
|
|
|
plot(image.createGraphics(),width,height);
|
|
|
|
return image;
|
|
}
|
|
|
|
private int value2y(float value){
|
|
return y0 - (int)(yh * ((value - yoffset)/yscale));
|
|
}
|
|
private int point2x(int point){
|
|
return x0 + (xw * point / (this.plotProvider.getPoints()-1));
|
|
}
|
|
|
|
public void plot(Graphics2D g,int width,int height){
|
|
int lineHeight = font.getSize() * Toolkit.getDefaultToolkit().getScreenResolution() / 72 ;
|
|
int nLabels = numLabels;
|
|
|
|
g.setColor(Color.WHITE);
|
|
g.fillRect(0, 0, width, height);
|
|
|
|
y0 = height - lineHeight - 10;
|
|
y1 = 10;
|
|
yh = y0 - y1;
|
|
x0 = 0;
|
|
x1 = width - 10;
|
|
|
|
if (nLabels == 0){
|
|
nLabels = ((y0 - y1) / lineHeight);
|
|
}
|
|
if (nLabels == 0)
|
|
nLabels = 1;
|
|
|
|
for (ColoredBackground bg: coloredBackgrounds){
|
|
int ya = value2y(bg.getMin());
|
|
int yb = value2y(bg.getMax());
|
|
|
|
g.setColor(bg.getColor());
|
|
g.fillRect(0, yb, width, ya - yb);
|
|
}
|
|
|
|
g.setColor(Color.BLACK);
|
|
if (this.font != null)
|
|
g.setFont(this.font);
|
|
|
|
for (int i=0;i<=nLabels;i++){
|
|
String l = String.format(labelFormatSpec, (yoffset + (yscale * i / nLabels)));
|
|
int lw = g.getFontMetrics().stringWidth(l);
|
|
|
|
if (lw > x0)
|
|
x0 = lw;
|
|
}
|
|
for (int i=0;i<=nLabels;i++){
|
|
String l = String.format(labelFormatSpec, (yoffset + (yscale * i / nLabels)));
|
|
int lw = g.getFontMetrics().stringWidth(l);
|
|
|
|
g.drawString(l, 2 + (x0 - lw), y0 - (yh * i / nLabels) + (lineHeight/4));
|
|
}
|
|
x0 += 10;
|
|
xw = x1 - x0;
|
|
|
|
for (int i=0;i<=nLabels;i++){
|
|
g.drawLine(x0 - 5, y0 - (yh * i / nLabels) , x0, y0 - (yh * i / nLabels));
|
|
}
|
|
|
|
for (int i=0;i<this.plotProvider.getPoints();i++){
|
|
g.drawLine(point2x(i), y0, point2x(i), y0 + 5);
|
|
|
|
String xlabel = this.plotProvider.getPointLabel(i);
|
|
int lw = g.getFontMetrics().stringWidth(xlabel);
|
|
|
|
g.drawString(xlabel, point2x(i) - (lw/2), y0 + lineHeight);
|
|
}
|
|
|
|
g.setColor(Color.BLACK);
|
|
|
|
g.drawLine(x0, y0 + 5, x0, y1 - 10);
|
|
g.drawLine(x0, y1 - 10 , x0 - 5, y1 - 5);
|
|
g.drawLine(x0, y1 - 10 , x0 + 5, y1 - 5);
|
|
|
|
g.drawLine(x0, y0, x1 + 10, y0);
|
|
g.drawLine(x1 + 10 , y0, x1 + 5, y0 - 5);
|
|
g.drawLine(x1 + 10 , y0, x1 + 5, y0 + 5);
|
|
|
|
g.setColor(Color.RED);
|
|
|
|
Float[][] matrix = this.plotProvider.getMatrix();
|
|
Color[] colors = this.plotProvider.getColors();
|
|
|
|
for (int n=0;n<this.plotProvider.getPlots();n++){
|
|
plotPainter.reset();
|
|
for (int i=0;i<this.plotProvider.getPoints();i++){
|
|
int x,y;
|
|
|
|
if (matrix[n][i] != null){
|
|
x = point2x(i);
|
|
y = value2y(matrix[n][i]);
|
|
|
|
plotPainter.paintPoint(g, colors[n], x, y);
|
|
};
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
}
|