4 import java.awt.Dimension;
5 import java.awt.Graphics;
6 import java.awt.event.MouseEvent;
8 import tim.prune.I18nManager;
9 import tim.prune.data.AltitudeRange;
10 import tim.prune.data.Track;
11 import tim.prune.data.TrackInfo;
14 * Chart component for the profile display
16 public class ProfileChart extends GenericChart
18 private double _xScaleFactor = 0.0;
19 private static final int[] ALTITUDE_SCALES = {10000, 5000, 2000, 1000, 500, 200, 100, 50};
20 private static final Color COLOR_LINES = Color.GRAY;
21 private static final Color COLOR_ALT_BARS = Color.BLUE;
22 private static final Color COLOR_SELECTED = Color.RED;
23 private static final Color COLOR_SELECTED_BG = Color.ORANGE;
24 private static final Color COLOR_ALT_SCALE = Color.RED;
29 * @param inTrackInfo Track info object
31 public ProfileChart(TrackInfo inTrackInfo)
34 MINIMUM_SIZE = new Dimension(200, 100);
35 addMouseListener(this);
40 * Override paint method to draw map
42 public void paint(Graphics g)
45 if (_track != null && _track.getNumPoints() > 0)
47 int width = getWidth();
48 int height = getHeight();
49 AltitudeRange altitudeRange = _track.getAltitudeRange();
50 int minAltitude = altitudeRange.getMinimum();
51 int maxAltitude = altitudeRange.getMaximum();
53 // message if no altitudes in track
54 if (minAltitude < 0 || maxAltitude < 0)
56 g.setColor(COLOR_LINES);
57 g.drawString(I18nManager.getText("display.noaltitudes"), 50, height/2);
62 int numPoints = _track.getNumPoints();
63 _xScaleFactor = 1.0 * (width - 2 * BORDER_WIDTH) / numPoints;
64 double yScaleFactor = 1.0 * (height - 2 * BORDER_WIDTH) /
65 (altitudeRange.getMaximum() - minAltitude);
66 int barWidth = (int) (_xScaleFactor + 1.0);
67 int selectedPoint = _trackInfo.getSelection().getCurrentPointIndex();
69 // horizontal lines for scale - set to round numbers eg 500m
70 int lineScale = getLineScale(minAltitude, maxAltitude);
75 g.setColor(COLOR_LINES);
76 while (altitude < maxAltitude)
78 if (altitude > minAltitude)
80 y = height - BORDER_WIDTH - (int) (yScaleFactor * (altitude - minAltitude));
81 g.drawLine(BORDER_WIDTH + 1, y, width - BORDER_WIDTH - 1, y);
83 altitude += lineScale;
87 // loop through points
88 int chartFormat = altitudeRange.getFormat();
89 for (int p = 0; p < numPoints; p++)
91 int x = (int) (_xScaleFactor * p);
92 if (p == selectedPoint)
94 g.setColor(COLOR_SELECTED_BG);
95 g.fillRect(BORDER_WIDTH + x, BORDER_WIDTH+1, barWidth, height-2*BORDER_WIDTH-2);
96 g.setColor(COLOR_SELECTED);
100 g.setColor(COLOR_ALT_BARS);
102 if (_track.getPoint(p).getAltitude().isValid())
104 altitude = _track.getPoint(p).getAltitude().getValue(chartFormat);
105 y = (int) (yScaleFactor * (altitude - minAltitude));
106 g.fillRect(BORDER_WIDTH+x, height-BORDER_WIDTH - y, barWidth, y);
109 // Draw numbers on top of the graph to mark scale
112 int textHeight = g.getFontMetrics().getHeight();
115 g.setColor(COLOR_ALT_SCALE);
116 while (altitude < maxAltitude)
118 if (altitude > minAltitude)
120 y = height - BORDER_WIDTH - (int) (yScaleFactor * (altitude - minAltitude));
121 // Limit y so String isn't above border
122 if (y < (BORDER_WIDTH + textHeight))
124 y = BORDER_WIDTH + textHeight;
126 g.drawString(""+altitude, BORDER_WIDTH + 5, y);
128 altitude += lineScale;
136 * Work out the scale for the horizontal lines
137 * @param inMin min altitude of data
138 * @param inMax max altitude of data
139 * @return scale separation, or -1 for no scale
141 private int getLineScale(int inMin, int inMax)
143 if ((inMax - inMin) < 50 || inMax < 0)
147 int numScales = ALTITUDE_SCALES.length;
151 for (int i=0; i<numScales; i++)
153 scale = ALTITUDE_SCALES[i];
158 while (altitude < inMax)
161 if (altitude > inMin)
172 // no suitable scale found so just use minimum
173 return ALTITUDE_SCALES[numScales-1];
178 * Method to inform map that data has changed
180 public void dataUpdated(Track inTrack)
188 * React to click on profile display
190 public void mouseClicked(MouseEvent e)
192 // ignore right clicks
193 if (_track != null && !e.isMetaDown())
195 int xClick = e.getX();
196 int yClick = e.getY();
197 // Check click is within main area (not in border)
198 if (xClick > BORDER_WIDTH && yClick > BORDER_WIDTH && xClick < (getWidth() - BORDER_WIDTH)
199 && yClick < (getHeight() - BORDER_WIDTH))
201 // work out which data point is nearest and select it
202 int pointNum = (int) ((e.getX() - BORDER_WIDTH) / _xScaleFactor);
203 _trackInfo.getSelection().selectPoint(pointNum);