00001
00002
00003
00004
00005
00006
00007 package org.swtchart.internal.axis;
00008
00009 import java.util.ArrayList;
00010 import java.util.Arrays;
00011 import java.util.Collection;
00012 import java.util.HashMap;
00013 import java.util.Set;
00014
00015 import org.eclipse.swt.SWT;
00016 import org.swtchart.Chart;
00017 import org.swtchart.IAxis;
00018 import org.swtchart.IAxis.Direction;
00019 import org.swtchart.IAxisSet;
00020 import org.swtchart.ISeries;
00021 import org.swtchart.internal.series.SeriesSet;
00022
00026 public class AxisSet implements IAxisSet {
00027
00029 private HashMap<Integer, Axis> xAxisMap;
00030
00032 private HashMap<Integer, Axis> yAxisMap;
00033
00035 private Chart chart;
00036
00043 public AxisSet(Chart chart) {
00044 this.chart = chart;
00045
00046 xAxisMap = new HashMap<Integer, Axis>();
00047 yAxisMap = new HashMap<Integer, Axis>();
00048
00049
00050 Axis xAxis = new Axis(0, Direction.X, chart);
00051 Axis yAxis = new Axis(0, Direction.Y, chart);
00052 xAxisMap.put(0, xAxis);
00053 yAxisMap.put(0, yAxis);
00054 }
00055
00063 private HashMap<Integer, Axis> getAxisMap(Direction direction) {
00064 if (direction == Direction.X) {
00065 return xAxisMap;
00066 }
00067 return yAxisMap;
00068 }
00069
00070
00071
00072
00073 public int createXAxis() {
00074 return createAxis(Direction.X);
00075 }
00076
00077
00078
00079
00080 public int createYAxis() {
00081 return createAxis(Direction.Y);
00082 }
00083
00091 private int createAxis(Direction direction) {
00092 int id = getUniqueId(direction);
00093 Axis axis = new Axis(id, direction, chart);
00094 getAxisMap(direction).put(id, axis);
00095 chart.updateLayout();
00096
00097 SeriesSet series = (SeriesSet) chart.getSeriesSet();
00098 if (series != null) {
00099 series.compressAllSeries();
00100 }
00101 return id;
00102 }
00103
00111 private int getUniqueId(Direction direction) {
00112 Set<Integer> keySet = getAxisMap(direction).keySet();
00113
00114 int i = 0;
00115 while (keySet.contains(i)) {
00116 i++;
00117 }
00118 return i;
00119 }
00120
00121
00122
00123
00124 public IAxis getXAxis(int id) {
00125 return getAxis(id, Direction.X);
00126 }
00127
00128
00129
00130
00131 public IAxis getYAxis(int id) {
00132 return getAxis(id, Direction.Y);
00133 }
00134
00144 private IAxis getAxis(int id, Direction direction) {
00145 return getAxisMap(direction).get(id);
00146 }
00147
00148
00149
00150
00151 public IAxis[] getXAxes() {
00152 Collection<Axis> values = xAxisMap.values();
00153 return values.toArray(new Axis[values.size()]);
00154 }
00155
00156
00157
00158
00159 public IAxis[] getYAxes() {
00160 Collection<Axis> values = yAxisMap.values();
00161 return values.toArray(new Axis[values.size()]);
00162 }
00163
00164
00165
00166
00167 public IAxis[] getAxes() {
00168 Collection<Axis> axes = new ArrayList<Axis>();
00169 axes.addAll(xAxisMap.values());
00170 axes.addAll(yAxisMap.values());
00171 return axes.toArray(new Axis[axes.size()]);
00172 }
00173
00174
00175
00176
00177 public int[] getXAxisIds() {
00178 return getAxisIds(Direction.X);
00179 }
00180
00181
00182
00183
00184 public int[] getYAxisIds() {
00185 return getAxisIds(Direction.Y);
00186 }
00187
00195 private int[] getAxisIds(Direction direction) {
00196 Set<Integer> keySet = getAxisMap(direction).keySet();
00197 Integer[] array = keySet.toArray(new Integer[keySet.size()]);
00198
00199 int[] ids = new int[array.length];
00200 for (int i = 0; i < ids.length; i++) {
00201 ids[i] = array[i];
00202 }
00203 Arrays.sort(ids);
00204
00205 return ids;
00206 }
00207
00208
00209
00210
00211 public void deleteXAxis(int id) {
00212 deleteAxis(id, Direction.X);
00213 }
00214
00215
00216
00217
00218 public void deleteYAxis(int id) {
00219 deleteAxis(id, Direction.Y);
00220 }
00221
00230 private void deleteAxis(int id, Direction direction) {
00231 if (id == 0) {
00232 SWT.error(SWT.ERROR_CANNOT_BE_ZERO);
00233 }
00234
00235 if (getAxisMap(direction).get(id) == null) {
00236 throw new IllegalArgumentException("Given axis id doesn't exist");
00237 }
00238
00239 ((Axis) getAxis(id, direction)).dispose();
00240 getAxisMap(direction).remove(id);
00241
00242 for (ISeries series : chart.getSeriesSet().getSeries()) {
00243 if (direction == Direction.X) {
00244 if (series.getXAxisId() == id) {
00245 series.setXAxisId(0);
00246 }
00247 } else {
00248 if (series.getYAxisId() == id) {
00249 series.setYAxisId(0);
00250 }
00251 }
00252 }
00253 chart.updateLayout();
00254 }
00255
00256
00257
00258
00259 public void adjustRange() {
00260 for (IAxis axis : getAxes()) {
00261 ((Axis) axis).adjustRange(false);
00262 }
00263 chart.updateLayout();
00264 }
00265
00266
00267
00268
00269 public void zoomIn() {
00270 for (IAxis axis : getAxes()) {
00271 axis.zoomIn();
00272 }
00273 }
00274
00275
00276
00277
00278 public void zoomOut() {
00279 for (IAxis axis : getAxes()) {
00280 axis.zoomOut();
00281 }
00282 }
00283
00287 public void updateLayoutData() {
00288 for (IAxis axis : getAxes()) {
00289 ((Axis) axis).updateLayoutData();
00290 }
00291 }
00292
00296 public void refresh() {
00297 for (IAxis axis : getAxes()) {
00298 ((Axis) axis).refresh();
00299 }
00300 }
00301
00305 public void dispose() {
00306 for (IAxis axis : getAxes()) {
00307 ((Axis) axis).dispose();
00308 }
00309 }
00310 }