| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
package dr.app.gui.chart; |
| 27 |
|
| 28 |
import dr.app.gui.chart.*; |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
public class JGridChart extends JChart { |
| 35 |
|
| 36 |
private final CustomAxis xVariableAxis; |
| 37 |
private final CustomAxis yVariableAxis; |
| 38 |
|
| 39 |
private int rowCount; |
| 40 |
private int columnCount; |
| 41 |
|
| 42 |
public JGridChart(double aspectRatio) { |
| 43 |
super(null, null, aspectRatio); |
| 44 |
|
| 45 |
xVariableAxis = new CustomAxis(1, 2); |
| 46 |
yVariableAxis = new CustomAxis(1, 2); |
| 47 |
|
| 48 |
|
| 49 |
setXAxis(xVariableAxis); |
| 50 |
setYAxis(yVariableAxis); |
| 51 |
|
| 52 |
|
| 53 |
rowCount = 0; |
| 54 |
columnCount = 0; |
| 55 |
} |
| 56 |
|
| 57 |
public JGridChart(int rowCount, int columnCount, double aspectRatio) { |
| 58 |
super(null, null, aspectRatio); |
| 59 |
|
| 60 |
this.rowCount = rowCount; |
| 61 |
this.columnCount = columnCount; |
| 62 |
|
| 63 |
xVariableAxis = new CustomAxis(1, 2); |
| 64 |
yVariableAxis = new CustomAxis(1, 2); |
| 65 |
|
| 66 |
|
| 67 |
setXAxis(xVariableAxis); |
| 68 |
setYAxis(yVariableAxis); |
| 69 |
|
| 70 |
} |
| 71 |
|
| 72 |
public void setDimensions(int rowCount, int columnCount) { |
| 73 |
this.rowCount = rowCount; |
| 74 |
this.columnCount = columnCount; |
| 75 |
|
| 76 |
xVariableAxis.setRange(1.0, columnCount); |
| 77 |
yVariableAxis.setRange(1.0, rowCount); |
| 78 |
|
| 79 |
recalibrate(); |
| 80 |
repaint(); |
| 81 |
} |
| 82 |
|
| 83 |
|
| 84 |
@Override |
| 85 |
public void addPlot(Plot plot) { |
| 86 |
|
| 87 |
super.addPlot(plot); |
| 88 |
|
| 89 |
int rowCount = this.rowCount; |
| 90 |
int columnCount = this.columnCount; |
| 91 |
|
| 92 |
if (rowCount < 1 || columnCount < 1) { |
| 93 |
|
| 94 |
int k = (int)Math.sqrt(getPlotCount()); |
| 95 |
rowCount = getPlotCount() / k; |
| 96 |
columnCount = getPlotCount() % k; |
| 97 |
|
| 98 |
if (plot.getXLocation() < 0 || plot.getYLocation() < 0) { |
| 99 |
int x = 0; |
| 100 |
int y = 0; |
| 101 |
for (Plot p : getPlots()) { |
| 102 |
p.setLocation(x, y); |
| 103 |
x++; |
| 104 |
if (x >= columnCount) { |
| 105 |
x = 0; |
| 106 |
y++; |
| 107 |
} |
| 108 |
} |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
xVariableAxis.setRange(1.0, columnCount); |
| 113 |
yVariableAxis.setRange(1.0, rowCount); |
| 114 |
|
| 115 |
|
| 116 |
recalibrate(); |
| 117 |
repaint(); |
| 118 |
} |
| 119 |
|
| 120 |
@Override |
| 121 |
protected String getXAxisLabel(double value) { |
| 122 |
return getPlotName(value); |
| 123 |
} |
| 124 |
|
| 125 |
@Override |
| 126 |
protected String getYAxisLabel(double value) { |
| 127 |
return getPlotName(value); |
| 128 |
} |
| 129 |
|
| 130 |
private String getPlotName(double value) { |
| 131 |
int index = (int)(value); |
| 132 |
if (index >= 1 && index <= getPlotCount()) { |
| 133 |
Plot plot = getPlot(index - 1); |
| 134 |
return plot.getName(); |
| 135 |
} else { |
| 136 |
return ""; |
| 137 |
} |
| 138 |
|
| 139 |
} |
| 140 |
|
| 141 |
} |