PageRenderTime 54ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/swt-chart-0.8.0/org.swtchart/src/org/swtchart/internal/axis/AxisSet.java

#
Java | 412 lines | 226 code | 49 blank | 137 comment | 40 complexity | 977eadfb0ddf44a6886db7f438a31b90 MD5 | raw file
  1. /*******************************************************************************
  2. * Copyright (c) 2008-2011 SWTChart project. All rights reserved.
  3. *
  4. * This code is distributed under the terms of the Eclipse Public License v1.0
  5. * which is available at http://www.eclipse.org/legal/epl-v10.html
  6. *******************************************************************************/
  7. package org.swtchart.internal.axis;
  8. import java.util.ArrayList;
  9. import java.util.Arrays;
  10. import java.util.Collection;
  11. import java.util.HashMap;
  12. import java.util.Set;
  13. import org.eclipse.swt.SWT;
  14. import org.swtchart.Chart;
  15. import org.swtchart.IAxis;
  16. import org.swtchart.IAxisSet;
  17. import org.swtchart.ISeries;
  18. import org.swtchart.IAxis.Direction;
  19. import org.swtchart.internal.ChartLayout;
  20. import org.swtchart.internal.ChartLayoutData;
  21. import org.swtchart.internal.Legend;
  22. import org.swtchart.internal.Title;
  23. import org.swtchart.internal.series.SeriesSet;
  24. /**
  25. * An axis container. By default, axis set has X Axis and Y axis with axis id 0.
  26. */
  27. public class AxisSet implements IAxisSet {
  28. /** the set of X axes */
  29. private HashMap<Integer, Axis> xAxisMap;
  30. /** the set of Y axes */
  31. private HashMap<Integer, Axis> yAxisMap;
  32. /** the chart */
  33. private Chart chart;
  34. /**
  35. * Constructor.
  36. *
  37. * @param chart
  38. * the chart
  39. */
  40. public AxisSet(Chart chart) {
  41. this.chart = chart;
  42. xAxisMap = new HashMap<Integer, Axis>();
  43. yAxisMap = new HashMap<Integer, Axis>();
  44. // add default axes
  45. Axis xAxis = new Axis(0, Direction.X, chart);
  46. Axis yAxis = new Axis(0, Direction.Y, chart);
  47. xAxisMap.put(0, xAxis);
  48. yAxisMap.put(0, yAxis);
  49. }
  50. /**
  51. * Gets the axis map for given direction.
  52. *
  53. * @param direction
  54. * the direction
  55. * @return the axis map
  56. */
  57. private HashMap<Integer, Axis> getAxisMap(Direction direction) {
  58. if (direction == Direction.X) {
  59. return xAxisMap;
  60. }
  61. return yAxisMap;
  62. }
  63. /*
  64. * @see IAxisSet#createXAxis()
  65. */
  66. public int createXAxis() {
  67. return createAxis(Direction.X);
  68. }
  69. /*
  70. * @see IAxisSet#createYAxis()
  71. */
  72. public int createYAxis() {
  73. return createAxis(Direction.Y);
  74. }
  75. /**
  76. * Creates the axis for given direction.
  77. *
  78. * @param direction
  79. * the direction of axis
  80. * @return the created axis id
  81. */
  82. private int createAxis(Direction direction) {
  83. int id = getUniqueId(direction);
  84. Axis axis = new Axis(id, direction, chart);
  85. getAxisMap(direction).put(id, axis);
  86. chart.updateLayout();
  87. SeriesSet series = (SeriesSet) chart.getSeriesSet();
  88. if (series != null) {
  89. series.compressAllSeries();
  90. }
  91. return id;
  92. }
  93. /**
  94. * Gets a unique axis id.
  95. *
  96. * @param direction
  97. * the axis direction
  98. * @return a unique axis id
  99. */
  100. private int getUniqueId(Direction direction) {
  101. Set<Integer> keySet = getAxisMap(direction).keySet();
  102. int i = 0;
  103. while (keySet.contains(i)) {
  104. i++;
  105. }
  106. return i;
  107. }
  108. /*
  109. * @see IAxisSet#getXAxis(int)
  110. */
  111. public IAxis getXAxis(int id) {
  112. return getAxis(id, Direction.X);
  113. }
  114. /*
  115. * @see IAxisSet#getYAxis(int)
  116. */
  117. public IAxis getYAxis(int id) {
  118. return getAxis(id, Direction.Y);
  119. }
  120. /**
  121. * Gets the axis with axis id for given direction.
  122. *
  123. * @param id
  124. * the axis id
  125. * @param direction
  126. * the direction
  127. * @return the axis
  128. */
  129. private IAxis getAxis(int id, Direction direction) {
  130. return getAxisMap(direction).get(id);
  131. }
  132. /*
  133. * @see IAxisSet#getXAxes()
  134. */
  135. public IAxis[] getXAxes() {
  136. return xAxisMap.values().toArray(new IAxis[0]);
  137. }
  138. /*
  139. * @see IAxisSet#getYAxes()
  140. */
  141. public IAxis[] getYAxes() {
  142. return yAxisMap.values().toArray(new IAxis[0]);
  143. }
  144. /*
  145. * @see IAxisSet#getAxes()
  146. */
  147. public IAxis[] getAxes() {
  148. Collection<Axis> axes = new ArrayList<Axis>();
  149. axes.addAll(xAxisMap.values());
  150. axes.addAll(yAxisMap.values());
  151. return axes.toArray(new Axis[0]);
  152. }
  153. /*
  154. * @see IAxisSet#getXAxisIds()
  155. */
  156. public int[] getXAxisIds() {
  157. return getAxisIds(Direction.X);
  158. }
  159. /*
  160. * @see IAxisSet#getYAxisIds()
  161. */
  162. public int[] getYAxisIds() {
  163. return getAxisIds(Direction.Y);
  164. }
  165. /**
  166. * Gets the axis ids for given direction.
  167. *
  168. * @param direction
  169. * the direction
  170. * @return the axis ids
  171. */
  172. private int[] getAxisIds(Direction direction) {
  173. Integer[] array = getAxisMap(direction).keySet()
  174. .toArray(new Integer[0]);
  175. int[] ids = new int[array.length];
  176. for (int i = 0; i < ids.length; i++) {
  177. ids[i] = array[i];
  178. }
  179. Arrays.sort(ids);
  180. return ids;
  181. }
  182. /*
  183. * @see IAxisSet#deleteXAxis(int)
  184. */
  185. public void deleteXAxis(int id) {
  186. deleteAxis(id, Direction.X);
  187. }
  188. /*
  189. * @see IAxisSet#deleteYAxis(int)
  190. */
  191. public void deleteYAxis(int id) {
  192. deleteAxis(id, Direction.Y);
  193. }
  194. /**
  195. * Deletes the axis with the axis id for given direction.
  196. *
  197. * @param id
  198. * the axis id
  199. * @param direction
  200. * the direction
  201. */
  202. private void deleteAxis(int id, Direction direction) {
  203. if (id == 0) {
  204. SWT.error(SWT.ERROR_CANNOT_BE_ZERO);
  205. }
  206. if (getAxisMap(direction).get(id) == null) {
  207. throw new IllegalArgumentException("Given axis id doesn't exist");
  208. }
  209. ((Axis) getAxis(id, direction)).dispose();
  210. getAxisMap(direction).remove(id);
  211. for (ISeries series : chart.getSeriesSet().getSeries()) {
  212. if (direction == Direction.X) {
  213. if (series.getXAxisId() == id) {
  214. series.setXAxisId(0);
  215. }
  216. } else {
  217. if (series.getYAxisId() == id) {
  218. series.setYAxisId(0);
  219. }
  220. }
  221. }
  222. chart.updateLayout();
  223. }
  224. /*
  225. * @see IAxisSet#adjustRange()
  226. */
  227. public void adjustRange() {
  228. for (IAxis axis : getAxes()) {
  229. ((Axis) axis).adjustRange(false);
  230. }
  231. chart.updateLayout();
  232. }
  233. /*
  234. * @see IAxisSet#zoomIn()
  235. */
  236. public void zoomIn() {
  237. for (IAxis axis : getAxes()) {
  238. axis.zoomIn();
  239. }
  240. }
  241. /*
  242. * @see IAxisSet#zoomOut()
  243. */
  244. public void zoomOut() {
  245. for (IAxis axis : getAxes()) {
  246. axis.zoomOut();
  247. }
  248. }
  249. /**
  250. * Updates the layout data.
  251. */
  252. public void updateLayoutData() {
  253. IAxis[] horizontalAxes;
  254. IAxis[] verticalAxes;
  255. if (chart.getOrientation() == SWT.HORIZONTAL) {
  256. horizontalAxes = getXAxes();
  257. verticalAxes = getYAxes();
  258. } else {
  259. horizontalAxes = getYAxes();
  260. verticalAxes = getXAxes();
  261. }
  262. updateAxesLayoutData(horizontalAxes);
  263. updateVerticalTick(horizontalAxes, verticalAxes);
  264. updateAxesLayoutData(verticalAxes);
  265. updateHorizontalTick(horizontalAxes, verticalAxes);
  266. }
  267. /**
  268. * Updates the layout data
  269. *
  270. * @param axes
  271. * The axes
  272. */
  273. private void updateAxesLayoutData(IAxis[] axes) {
  274. for (IAxis axis : axes) {
  275. ((Axis) axis).updateLayoutData();
  276. }
  277. }
  278. /**
  279. * Updates the horizontal tick.
  280. *
  281. * @param horizontalAxes
  282. * the horizontal axes
  283. * @param verticalAxes
  284. * the vertical axes
  285. */
  286. private void updateHorizontalTick(IAxis[] horizontalAxes,
  287. IAxis[] verticalAxes) {
  288. int legendPosition = ((Legend) chart.getLegend()).getPosition();
  289. int legendWidth = ((ChartLayoutData) ((Legend) chart.getLegend())
  290. .getLayoutData()).widthHint;
  291. int axesWidth = 0;
  292. for (IAxis axis : verticalAxes) {
  293. axesWidth += ((ChartLayoutData) ((Title) ((Axis) axis).getTitle())
  294. .getLayoutData()).widthHint
  295. + ((Axis) axis).getTick().getAxisTickLabels()
  296. .getLayoutData().widthHint
  297. + ((Axis) axis).getTick().getAxisTickMarks()
  298. .getLayoutData().widthHint;
  299. }
  300. int axisWidth = chart.getClientArea().width
  301. - axesWidth
  302. - ChartLayout.MARGIN
  303. * 2
  304. - (legendPosition == SWT.LEFT || legendPosition == SWT.RIGHT ? legendWidth
  305. + (legendWidth == 0 ? 0 : ChartLayout.PADDING)
  306. : 0);
  307. for (IAxis axis : horizontalAxes) {
  308. ((Axis) axis).getTick().updateTick(axisWidth);
  309. }
  310. }
  311. /**
  312. * Updates the vertical tick.
  313. *
  314. * @param horizontalAxes
  315. * the horizontal axes
  316. * @param verticalAxes
  317. * the vertical axes
  318. */
  319. private void updateVerticalTick(IAxis[] horizontalAxes, IAxis[] verticalAxes) {
  320. int legendPosition = ((Legend) chart.getLegend()).getPosition();
  321. int legendHeight = ((ChartLayoutData) ((Legend) chart.getLegend())
  322. .getLayoutData()).heightHint;
  323. int titleHeight = ((ChartLayoutData) ((Title) chart.getTitle())
  324. .getLayoutData()).heightHint;
  325. int axesHeight = 0;
  326. for (IAxis axis : horizontalAxes) {
  327. axesHeight += ((ChartLayoutData) ((Title) ((Axis) axis).getTitle())
  328. .getLayoutData()).heightHint
  329. + ((Axis) axis).getTick().getAxisTickLabels()
  330. .getLayoutData().heightHint
  331. + ((Axis) axis).getTick().getAxisTickMarks()
  332. .getLayoutData().heightHint;
  333. }
  334. int axisHeight = chart.getClientArea().height
  335. - titleHeight
  336. - axesHeight
  337. - ChartLayout.MARGIN
  338. * 2
  339. - (titleHeight == 0 ? 0 : ChartLayout.PADDING)
  340. - ((legendPosition == SWT.TOP || legendPosition == SWT.BOTTOM) ? legendHeight
  341. + (legendHeight == 0 ? 0 : ChartLayout.PADDING)
  342. : 0);
  343. for (IAxis axis : verticalAxes) {
  344. ((Axis) axis).getTick().updateTick(axisHeight);
  345. }
  346. }
  347. /**
  348. * Refreshes the cache.
  349. */
  350. public void refresh() {
  351. for (IAxis axis : getAxes()) {
  352. ((Axis) axis).refresh();
  353. }
  354. }
  355. /**
  356. * Disposes the resources.
  357. */
  358. public void dispose() {
  359. for (IAxis axis : getAxes()) {
  360. ((Axis) axis).dispose();
  361. }
  362. }
  363. }