1 package com.billkuker.rocketry.motorsim.visual;
\r
3 import java.awt.BasicStroke;
\r
4 import java.awt.BorderLayout;
\r
5 import java.awt.Color;
\r
6 import java.awt.Font;
\r
7 import java.awt.Stroke;
\r
8 import java.lang.reflect.InvocationTargetException;
\r
9 import java.lang.reflect.Method;
\r
10 import java.util.Collection;
\r
11 import java.util.Iterator;
\r
12 import java.util.concurrent.ExecutorService;
\r
13 import java.util.concurrent.Executors;
\r
14 import java.util.concurrent.ThreadFactory;
\r
16 import javax.measure.quantity.Area;
\r
17 import javax.measure.quantity.Length;
\r
18 import javax.measure.quantity.Quantity;
\r
19 import javax.measure.quantity.Volume;
\r
20 import javax.measure.unit.SI;
\r
21 import javax.measure.unit.Unit;
\r
22 import javax.swing.JFrame;
\r
23 import javax.swing.JPanel;
\r
24 import javax.swing.SwingUtilities;
\r
26 import org.apache.log4j.Logger;
\r
27 import org.jfree.chart.ChartFactory;
\r
28 import org.jfree.chart.ChartPanel;
\r
29 import org.jfree.chart.JFreeChart;
\r
30 import org.jfree.chart.plot.Marker;
\r
31 import org.jfree.chart.plot.PlotOrientation;
\r
32 import org.jfree.chart.plot.ValueMarker;
\r
33 import org.jfree.data.xy.XYSeries;
\r
34 import org.jfree.data.xy.XYSeriesCollection;
\r
35 import org.jfree.ui.RectangleAnchor;
\r
36 import org.jfree.ui.RectangleInsets;
\r
37 import org.jfree.ui.TextAnchor;
\r
38 import org.jscience.physics.amount.Amount;
\r
40 import com.billkuker.rocketry.motorsim.RocketScience;
\r
41 import com.billkuker.rocketry.motorsim.grain.CoredCylindricalGrain;
\r
43 public class Chart<X extends Quantity, Y extends Quantity> extends JPanel implements RocketScience.UnitPreferenceListener {
\r
44 private static final long serialVersionUID = 1L;
\r
45 private static Logger log = Logger.getLogger(Chart.class);
\r
47 private final Stroke dashed = new BasicStroke(1, 1, 1, 1, new float[]{2,4}, 0);
\r
48 private final Font labelFont = new Font(Font.DIALOG, Font.BOLD, 10);
\r
50 private static ThreadFactory fastTF = new ThreadFactory() {
\r
51 public Thread newThread(Runnable r) {
\r
52 Thread t = new Thread(r);
\r
54 t.setName("Fast Chart Draw");
\r
58 private static ThreadFactory slowTF = new ThreadFactory() {
\r
59 public Thread newThread(Runnable r) {
\r
60 Thread t = new Thread(r);
\r
62 t.setName("Slow Chart Draw");
\r
66 private static ExecutorService fast = Executors.newFixedThreadPool(2, fastTF);
\r
67 private static ExecutorService slow = Executors.newFixedThreadPool(2, slowTF);
\r
70 public class IntervalDomain implements Iterable<Amount<X>> {
\r
72 Amount<X> low, high, delta;
\r
75 public IntervalDomain(Amount<X> low, Amount<X> high) {
\r
78 delta = high.minus(low).divide(steps);
\r
81 public IntervalDomain(Amount<X> low, Amount<X> high, int steps) {
\r
85 delta = high.minus(low).divide(steps);
\r
88 public Iterator<Amount<X>> iterator() {
\r
89 return new Iterator<Amount<X>>() {
\r
90 Amount<X> current = low;
\r
92 public boolean hasNext() {
\r
93 return current.isLessThan(high.plus(delta));
\r
96 public Amount<X> next() {
\r
97 Amount<X> ret = current;
\r
98 current = current.plus(delta);
\r
102 public final void remove() {
\r
103 throw new UnsupportedOperationException(
\r
104 "Chart domain iterators are not modifiable.");
\r
111 XYSeriesCollection dataset = new XYSeriesCollection();
\r
120 Iterable<Amount<X>> domain;
\r
122 public Chart(Unit<X> xUnit, Unit<Y> yUnit, Object source, String method)
\r
123 throws NoSuchMethodException {
\r
124 super(new BorderLayout());
\r
125 f = source.getClass().getMethod(method, Amount.class);
\r
127 this.source = source;
\r
129 this.xUnit = xUnit;
\r
130 this.yUnit = yUnit;
\r
132 RocketScience.addUnitPreferenceListener(this);
\r
137 private void setup(){
\r
139 this.xUnit = RocketScience.UnitPreference.getUnitPreference()
\r
140 .getPreferredUnit(xUnit);
\r
141 this.yUnit = RocketScience.UnitPreference.getUnitPreference()
\r
142 .getPreferredUnit(yUnit);
\r
144 chart = ChartFactory.createXYLineChart(f.getName().substring(0, 1)
\r
146 + f.getName().substring(1), // Title
\r
147 this.xUnit.toString(), // x-axis Label
\r
148 this.yUnit.toString(), // y-axis Label
\r
149 dataset, PlotOrientation.VERTICAL, // Plot Orientation
\r
150 false, // Show Legend
\r
151 true, // Use tool tips
\r
152 false // Configure chart to generate URLs?
\r
154 add(new ChartPanel(chart));
\r
159 public void preferredUnitsChanged() {
\r
166 public void addDomainMarker(Amount<X> x, String label, Color c){
\r
167 double xVal = x.doubleValue(xUnit);
\r
168 Marker marker = new ValueMarker(xVal);
\r
169 marker.setStroke(dashed);
\r
170 marker.setPaint(c);
\r
171 marker.setLabelPaint(c);
\r
172 marker.setLabelFont(labelFont);
\r
173 marker.setLabel(label + ": " + RocketScience.ammountToRoundedString(x));
\r
174 marker.setLabelTextAnchor(TextAnchor.TOP_LEFT);
\r
175 marker.setLabelOffset(new RectangleInsets(0, -5, 0, 0));
\r
176 chart.getXYPlot().addDomainMarker(marker);
\r
179 public void addRangeMarker(Amount<Y> y, String label, Color c){
\r
180 double yVal = y.doubleValue(yUnit);
\r
181 Marker marker = new ValueMarker(yVal);
\r
182 marker.setStroke(dashed);
\r
183 marker.setPaint(c);
\r
184 marker.setLabelPaint(c);
\r
185 marker.setLabelFont(labelFont);
\r
186 marker.setLabel(label + ": " + RocketScience.ammountToRoundedString(y));
\r
187 marker.setLabelTextAnchor(TextAnchor.TOP_LEFT);
\r
188 marker.setLabelOffset(new RectangleInsets(0, 5, 0, 0));
\r
189 chart.getXYPlot().addRangeMarker(marker);
\r
192 private Marker focusMarkerX, focusMarkerY;
\r
194 public void mark(Amount<X> m) {
\r
195 if (focusMarkerX != null)
\r
196 chart.getXYPlot().removeDomainMarker(focusMarkerX);
\r
197 if (focusMarkerY != null)
\r
198 chart.getXYPlot().removeRangeMarker(focusMarkerY);
\r
201 focusMarkerX = new ValueMarker(m.doubleValue(xUnit));
\r
202 focusMarkerX.setPaint(Color.blue);
\r
203 focusMarkerX.setAlpha(0.8f);
\r
205 chart.getXYPlot().addDomainMarker(focusMarkerX);
\r
207 Amount<Y> val = getNear(m);
\r
208 if ( val != null ){
\r
209 focusMarkerY = new ValueMarker(val.doubleValue(yUnit));
\r
210 focusMarkerY.setPaint(Color.BLUE);
\r
211 focusMarkerY.setLabelAnchor(RectangleAnchor.TOP_RIGHT);
\r
212 focusMarkerY.setLabelTextAnchor(TextAnchor.TOP_RIGHT);
\r
213 focusMarkerY.setLabelPaint(Color.BLUE);
\r
214 focusMarkerY.setLabelFont(labelFont);
\r
215 focusMarkerY.setLabelOffset(new RectangleInsets(0,5,0,0));
\r
216 chart.getXYPlot().addRangeMarker(focusMarkerY);
\r
217 focusMarkerY.setLabel(RocketScience.ammountToRoundedString(val));
\r
223 * Get the Y value at or near a given X
\r
224 * For display use only!
\r
229 private Amount<Y> getNear(final Amount<X> ax){
\r
230 if ( dataset.getSeriesCount() != 1 )
\r
232 final XYSeries s = dataset.getSeries(0);
\r
233 final double x = ax.doubleValue(xUnit);
\r
234 int idx = s.getItemCount() / 2;
\r
235 int delta = s.getItemCount() / 4;
\r
237 if ( s.getX(idx).doubleValue() < x ){
\r
246 final double lowerX = s.getX(idxL).doubleValue();
\r
247 final double higherX = s.getX(idxH).doubleValue();
\r
248 final double sampleXDiff = higherX - lowerX;
\r
249 final double xDiff = x - lowerX;
\r
250 final double dist = xDiff / sampleXDiff;
\r
251 final double lowerY = s.getY(idxL).doubleValue();
\r
252 final double higherY = s.getY(idxH).doubleValue();
\r
253 final double y = lowerY + dist * (higherY - lowerY);
\r
255 return Amount.valueOf( y, yUnit);
\r
260 private void drawDone(){
\r
264 private volatile boolean stop = false;
\r
265 private volatile int lastSkipStepShown;
\r
266 public void setDomain(final Iterable<Amount<X>> d) {
\r
267 chart.getXYPlot().clearDomainMarkers();
\r
268 chart.getXYPlot().clearRangeMarkers();
\r
269 lastSkipStepShown = Integer.MAX_VALUE;
\r
272 fast.submit(new Thread() {
\r
273 public void run() {
\r
276 slow.submit(new Thread() {
\r
277 public void run() {
\r
287 @SuppressWarnings("unchecked")
\r
288 private synchronized void fill(Iterable<Amount<X>> d, final int requestedSkip) {
\r
291 log.debug(f.getName() + " " + requestedSkip + " Start");
\r
294 int calculatedSkip = requestedSkip;
\r
295 if (d instanceof Collection) {
\r
296 sz = ((Collection<Amount<X>>) d).size();
\r
297 int sk2 = sz / 200;
\r
298 if (calculatedSkip < sk2)
\r
299 calculatedSkip = sk2;
\r
304 final XYSeries newSeries = new XYSeries(f.getName());
\r
306 Amount<X> last = null;
\r
307 for (Amount<X> ax : d) {
\r
309 log.debug(f.getName() + " " + calculatedSkip + " Abort");
\r
313 if (cnt % calculatedSkip == 0) {
\r
314 Amount<Y> y = (Amount<Y>) f.invoke(source, ax);
\r
315 newSeries.add(ax.doubleValue(xUnit), y.doubleValue(yUnit));
\r
319 Amount<Y> y = (Amount<Y>) f.invoke(source, last);
\r
320 newSeries.add(last.doubleValue(xUnit), y.doubleValue(yUnit));
\r
321 SwingUtilities.invokeLater(new Thread() {
\r
323 public void run() {
\r
324 if ( requestedSkip < lastSkipStepShown ){
\r
325 lastSkipStepShown = requestedSkip;
\r
326 dataset.removeAllSeries();
\r
327 dataset.addSeries(newSeries);
\r
328 log.debug(f.getName() + " Replaced with " + requestedSkip);
\r
330 if ( requestedSkip == 1 ){
\r
335 } catch (IllegalArgumentException e) {
\r
337 } catch (IllegalAccessException e) {
\r
339 } catch (InvocationTargetException e) {
\r
342 log.debug(f.getName() + " " + calculatedSkip + " Done");
\r
345 public void show() {
\r
347 private static final long serialVersionUID = 1L;
\r
349 setContentPane(Chart.this);
\r
351 setDefaultCloseOperation(DISPOSE_ON_CLOSE);
\r
353 }.setVisible(true);
\r
356 public static void main(String args[]) throws Exception {
\r
357 CoredCylindricalGrain g = new CoredCylindricalGrain();
\r
358 g.setLength(Amount.valueOf(70, SI.MILLIMETER));
\r
359 g.setOD(Amount.valueOf(30, SI.MILLIMETER));
\r
360 g.setID(Amount.valueOf(10, SI.MILLIMETER));
\r
362 Chart<Length, Area> c = new Chart<Length, Area>(SI.MILLIMETER,
\r
363 SI.MILLIMETER.pow(2).asType(Area.class), g, "surfaceArea");
\r
365 c.setDomain(c.new IntervalDomain(Amount.valueOf(0, SI.CENTIMETER), g
\r
370 Chart<Length, Volume> v = new Chart<Length, Volume>(SI.MILLIMETER,
\r
371 SI.MILLIMETER.pow(3).asType(Volume.class), g, "volume");
\r
373 v.setDomain(c.new IntervalDomain(Amount.valueOf(0, SI.CENTIMETER), g
\r