jDiagram Updates

thobaben_diagram
Harald Wolff 2016-11-21 16:08:52 +01:00
parent 9da6af56e4
commit cf7426b593
7 changed files with 143 additions and 95 deletions

View File

@ -71,6 +71,9 @@ public class JDiagram extends JComponent implements PlotProviderListener {
private PlotPainter[]
plotPainters;
private Integer selectedPlot;
public JDiagram(){
setMinimumSize(new Dimension(80, 80));
@ -129,16 +132,27 @@ public class JDiagram extends JComponent implements PlotProviderListener {
this.drawVerticalGrid = drawVerticalGrid;
}
public Integer getSelectedPlot() {
return selectedPlot;
}
public void setSelectedPlot(Integer selectedPlot) {
this.selectedPlot = selectedPlot;
}
private void fundamentalsChanged(){
ordinateViews = new OrdinateView[ plotProvider.getMaxOrdinate() + 1 ];
for (int n=0; n < plotProvider.getMaxOrdinate() + 1; n++)
ordinateViews[n] = new OrdinateView(n);
PlotPainter pp = new LinePlotPainter();
plotPainters = new PlotPainter[plotProvider.getNumGraphs()];
for (int n=0;n<plotProvider.getNumGraphs();n++)
plotPainters[n] = pp;
if (plotProvider != null){
ordinateViews = new OrdinateView[ plotProvider.getMaxOrdinate() + 1 ];
for (int n=0; n < plotProvider.getMaxOrdinate() + 1; n++)
ordinateViews[n] = new OrdinateView(n);
PlotPainter pp = new LinePlotPainter();
plotPainters = new PlotPainter[plotProvider.getNumGraphs()];
for (int n=0;n<plotProvider.getNumGraphs();n++)
plotPainters[n] = pp;
} else {
ordinateViews = new OrdinateView[0];
plotPainters = new PlotPainter[0];
}
}
public Scaler getScaler(int ordinate){
@ -261,13 +275,15 @@ public class JDiagram extends JComponent implements PlotProviderListener {
g.setColor(getBackground());
g.fillRect(0, 0, getWidth(), getHeight());
if (autoScale)
this.autoscale();
paintOrdinates ((Graphics2D) g);
paintAbszisse ((Graphics2D) g);
paintGraphs ((Graphics2D) g);
if (plotProvider != null){
if (autoScale)
this.autoscale();
paintOrdinates ((Graphics2D) g);
paintAbszisse ((Graphics2D) g);
paintGraphs ((Graphics2D) g);
}
}
public double[] getordinateLabelHints(int ordinate){
@ -389,9 +405,14 @@ public class JDiagram extends JComponent implements PlotProviderListener {
void paintAbszisse(Graphics2D g){
int nMarker = nAbszissLabels;
PlotLabeler labeler = this.defaultLabeler;
if (this.abszissLabeler != null)
labeler = this.abszissLabeler;
if (nMarker == 0){
int w = g.getFontMetrics().stringWidth( this.defaultLabeler.getAbzisseLabel(this, (double)this.plotProvider.getPositionMaximum()));
nMarker = plotWidth / (w*2);
int w = g.getFontMetrics().stringWidth( labeler.getAbzisseLabel(this, (double)this.plotProvider.getPositionMaximum()));
nMarker = plotWidth / (w*8/7);
}
abszissMinimum = this.plotProvider.getPositionMinimum();
@ -412,7 +433,7 @@ public class JDiagram extends JComponent implements PlotProviderListener {
int xpos = plotWidth * n / nMarker;
double pos = abszissMinimum + (abszissWindow * n / nMarker);
String xlabel = this.defaultLabeler.getAbzisseLabel(this, pos);
String xlabel = labeler.getAbzisseLabel(this, pos);
int xlwidth = g.getFontMetrics().stringWidth(xlabel);
g.drawString(xlabel, getWidth() - bRight - plotWidth + xpos - (xlwidth / 2) , getHeight() - bBottom);
@ -444,6 +465,7 @@ public class JDiagram extends JComponent implements PlotProviderListener {
Color[] graphColors = this.plotProvider.getColors();
for (int graph=0; graph < this.plotProvider.getNumGraphs(); graph++){
boolean isSelected = ((selectedPlot != null) && selectedPlot.equals(graph));
this.plotPainters[ graph ].reset();
ordinate = this.plotProvider.getOrdinate(graph);
@ -466,7 +488,7 @@ public class JDiagram extends JComponent implements PlotProviderListener {
x = getWidth() - bRight - plotWidth + (int)((position - abszissMinimum) * plotWidth / abszissWindow);
y = bTop + plotHeight - this.ordinateViews[ ordinate ].scaler.getPosition(value);
this.plotPainters[ graph ].paintPoint(g, graphColor, x, y);
this.plotPainters[ graph ].paintPoint(g, graphColor, x, y, isSelected);
};
}
@ -474,68 +496,6 @@ public class JDiagram extends JComponent implements PlotProviderListener {
}
/*
g.setColor(foreground);
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);
Color[] colors = this.plotProvider.getColors();
double[] min,max;
min = new double[this.plotProvider.getMaxOrdinate()+1];
max = new double[this.plotProvider.getMaxOrdinate()+1];
Arrays.fill(min, double.MAX_VALUE);
Arrays.fill(max, double.MIN_VALUE);
for (int n=0;n<this.plotProvider.getNumGraphs();n++){
int ordinate = this.plotProvider.getOrdinate(n);
plotPainter.reset();
for (int i=0;i<this.plotProvider.getLength();i++){
int x,y;
double v = this.plotProvider.getValue(i, n);
if (v != null){
x = point2x(i);
y = value2y(ordinate,v);
plotPainter.paintPoint(g, colors[n], x, y);
if (v < min[ordinate])
min[ordinate] = v;
if (v > max[ordinate])
max[ordinate] = v;
} else {
plotPainter.reset();
}
}
}
for (int i=0;i<this.autoscale.length;i++){
if (this.autoscale[i]){
if (!(this.minValues[i].equals(min[i]) && this.maxValues[i].equals(max[i]))){
this.minValues[i] = min[i];
this.maxValues[i] = max[i];
this.autoscale(i);
rescaled = true;
}
}
}
if (rescaled){
this.plot(g,width,height);
}
}
*/
class OrdinateView {

View File

@ -50,6 +50,19 @@ public class AnnotatedPlotProvider implements PlotProvider2{
return clazz;
}
public void setPlotEnabled(int plot,boolean enabled){
this.graphDefinitions[plot].setEnabled(enabled);
}
public boolean isPlotEnabled(int plot){
return this.graphDefinitions[plot].isEnabled();
}
public void setPlotDifferentiated(int plot,boolean differentiated){
this.graphDefinitions[plot].setDifferentiated(differentiated);
}
public boolean isPlotDifferentiated(int plot){
return this.graphDefinitions[plot].isDifferentiated();
}
public void setValues(Object[] values) {
@ -76,8 +89,14 @@ public class AnnotatedPlotProvider implements PlotProvider2{
for (Method m: this.clazz.getDeclaredMethods()){
Plot plot = m.getAnnotation(Plot.class);
if (plot != null){
if (plot.diagram() == this.diagram)
gdl.add(new GraphDefinition(m));
if (plot.diagram() == this.diagram) {
if (m.getParameterCount()==1){
for (int i=0;i<plot.instances();i++)
gdl.add(new GraphDefinition(m,i));
} else {
gdl.add(new GraphDefinition(m));
}
}
}
}
@ -125,7 +144,19 @@ public class AnnotatedPlotProvider implements PlotProvider2{
Object o = this.values.get(x);
if (o==null)
return null;
return this.graphDefinitions[graph].getValue(o);
if (this.graphDefinitions[graph].isDifferentiated() && (x > 0)){
Float v,l;
v = this.graphDefinitions[graph].getValue(o);
l = this.graphDefinitions[graph].getValue(this.values.get(x-1));
if ((v != null) && (l!=null)){
return v - l;
} else {
return null;
}
} else {
return this.graphDefinitions[graph].getValue(o);
}
}
public int getOrdinate(int graph){
@ -141,19 +172,43 @@ public class AnnotatedPlotProvider implements PlotProvider2{
int ordinate;
Color color;
boolean
isMultiGetter;
int
multiInstance;
boolean enabled;
boolean differentiated;
public GraphDefinition(Method method){
this.method = method;
this.isMultiGetter = (this.method.getParameterCount() == 1);
this.multiInstance = 0;
this.setPlot(method.getAnnotation(Plot.class));
this.enabled = true;
}
public GraphDefinition(Method method,int multiInstance){
this.method = method;
this.isMultiGetter = (this.method.getParameterCount() == 1);
this.multiInstance = multiInstance;
this.setPlot(method.getAnnotation(Plot.class));
this.enabled = true;
}
public GraphDefinition(Field field){
this.field = field;
this.field.setAccessible(true);
this.setPlot(field.getAnnotation(Plot.class));
this.enabled = true;
}
private void setPlot(Plot plot){
this.label = plot.label();
if (plot.instances()!=1){
this.label = String.format(plot.label(),multiInstance);
} else {
this.label = plot.label();
}
this.ordinate = plot.ordinate();
this.color = new Color(plot.r(), plot.g(), plot.b());
}
@ -162,14 +217,33 @@ public class AnnotatedPlotProvider implements PlotProvider2{
return ordinate;
}
public String getLabel() {
return label;
return this.label;
}
public Color getColor() {
return color;
}
public boolean isEnabled() {
return enabled;
}
public void setEnabled(boolean enabled) {
this.enabled = enabled;
fireFundamentalsChanged();
}
public boolean isDifferentiated() {
return differentiated;
}
public void setDifferentiated(boolean differentiated) {
this.differentiated = differentiated;
fireFundamentalsChanged();
}
public Float getValue(Object o){
if (!isEnabled())
return null;
try {
if (field != null){
if (field.getType().equals(Integer.class)){
@ -183,14 +257,25 @@ public class AnnotatedPlotProvider implements PlotProvider2{
}
}
if (method != null){
Object v;
if (isMultiGetter){
v = method.invoke(o, multiInstance);
} else {
v = method.invoke(o);
}
if (method.getReturnType().equals(Integer.class)){
return ((Integer)method.invoke(o, null)).floatValue();
return ((Integer)v).floatValue();
}
if (method.getReturnType().equals(Double.class)){
return ((Double)method.invoke(o, null)).floatValue();
return ((Double)v).floatValue();
}
if (method.getReturnType().equals(Float.class)){
return ((Float)method.invoke(o, null));
return ((Float)v);
}
if (method.getReturnType().equals(float.class)){
return ((float)v);
}
}
} catch (IllegalAccessException illegalAccessException){

View File

@ -20,7 +20,7 @@ public class CirclePlotPainter implements PlotPainter {
}
@Override
public void paintPoint(Graphics2D g, Color color, int x, int y) {
public void paintPoint(Graphics2D g, Color color, int x, int y,boolean isSelected) {
g.setColor(color);
g.drawArc(x - radius, y - radius, 2*radius, 2*radius, 0, 360);
g.fillArc(x - radius, y - radius, 2*radius, 2*radius, 0, 360);

View File

@ -10,7 +10,8 @@ public class LinePlotPainter implements PlotPainter {
int lx,ly;
float width;
Stroke stroke;
Stroke stroke,
selectedStroke;
Color color;
@ -25,10 +26,10 @@ public class LinePlotPainter implements PlotPainter {
}
@Override
public void paintPoint(Graphics2D g, Color color, int x, int y) {
public void paintPoint(Graphics2D g, Color color, int x, int y,boolean isSelected) {
if (lx != -1){
g.setColor(this.color != null ? this.color : color);
Stroke s = g.getStroke();
Stroke s = isSelected ? this.selectedStroke : g.getStroke();
g.setStroke(stroke);
g.drawLine(lx, ly, x, y);
g.setStroke(s);
@ -41,6 +42,7 @@ public class LinePlotPainter implements PlotPainter {
public void setWidth(float width) {
this.width = width;
this.stroke = new BasicStroke(width);
this.selectedStroke = new BasicStroke(width * 2);
}
public float getWidth() {
return width;

View File

@ -6,6 +6,6 @@ import java.awt.Graphics2D;
public interface PlotPainter {
void reset();
void paintPoint(Graphics2D g,Color color,int x,int y);
void paintPoint(Graphics2D g,Color color,int x,int y,boolean isSelected);
}

View File

@ -43,7 +43,7 @@ public class SimplePlotLabeler implements PlotLabeler {
);
} else if (digits == 0){
return String.format("%1.2f",value);
} else if (digits > 0){
} else {
return String.format(
String.format("%%%d.2f",digits),
value

View File

@ -14,4 +14,5 @@ public @interface Plot {
public int b() default 0;
public int diagram() default 0;
public int instances() default 1;
}