001: /* SimpleXYModel.java
002:
003: {{IS_NOTE
004: Purpose:
005:
006: Description:
007:
008: History:
009: Thu Aug 14 11:30:51 2006, Created by henrichen
010: }}IS_NOTE
011:
012: Copyright (C) 2006 Potix Corporation. All Rights Reserved.
013:
014: {{IS_RIGHT
015: This program is distributed under GPL Version 2.0 in the hope that
016: it will be useful, but WITHOUT ANY WARRANTY.
017: }}IS_RIGHT
018: */
019: package org.zkoss.zul;
020:
021: import org.zkoss.lang.Objects;
022: import org.zkoss.zul.event.ChartDataEvent;
023: import org.zkoss.zul.event.ChartDataListener;
024:
025: import java.util.Map;
026: import java.util.List;
027: import java.util.HashMap;
028: import java.util.Iterator;
029: import java.util.ArrayList;
030: import java.util.Collection;
031:
032: /**
033: * A XY data model implementation of {@link XYModel}.
034: * A XY model is an N series of (X, Y) data objects .
035: *
036: * @author henrichen
037: * @see XYModel
038: * @see Chart
039: */
040: public class SimpleXYModel extends AbstractChartModel implements
041: XYModel {
042: private Map _seriesMap = new HashMap(13); //(series, SimplePieModel)
043: private List _seriesList = new ArrayList(13);
044: private boolean _autoSort = true;
045:
046: //-- XYModel --//
047: public Comparable getSeries(int index) {
048: return (Comparable) _seriesList.get(index);
049: }
050:
051: public Collection getSeries() {
052: return _seriesList;
053: }
054:
055: public int getDataCount(Comparable series) {
056: final List xyPairs = (List) _seriesMap.get(series);
057: return xyPairs != null ? xyPairs.size() : 0;
058: }
059:
060: public Number getX(Comparable series, int index) {
061: final List xyPairs = (List) _seriesMap.get(series);
062:
063: if (xyPairs != null) {
064: return ((XYPair) xyPairs.get(index)).getX();
065: }
066: return null;
067: }
068:
069: public Number getY(Comparable series, int index) {
070: final List xyPairs = (List) _seriesMap.get(series);
071:
072: if (xyPairs != null) {
073: return ((XYPair) xyPairs.get(index)).getY();
074: }
075: return null;
076: }
077:
078: public void addValue(Comparable series, Number x, Number y) {
079: List xyPairs = (List) _seriesMap.get(series);
080: if (xyPairs == null) {
081: xyPairs = new ArrayList(13);
082: _seriesMap.put(series, xyPairs);
083: _seriesList.add(series);
084: }
085: xyPairs.add(new XYPair(x, y));
086: fireEvent(ChartDataEvent.CHANGED, series, null);
087: }
088:
089: public void setAutoSort(boolean auto) {
090: _autoSort = auto;
091: }
092:
093: public boolean isAutoSort() {
094: return _autoSort;
095: }
096:
097: public void removeSeries(Comparable series) {
098: _seriesMap.remove(series);
099: _seriesList.remove(series);
100: fireEvent(ChartDataEvent.REMOVED, (String) series, null);
101: }
102:
103: public void removeValue(Comparable series, int index) {
104: List xyPairs = (List) _seriesMap.get(series);
105: if (xyPairs == null) {
106: return;
107: }
108: xyPairs.remove(index);
109: fireEvent(ChartDataEvent.REMOVED, series, null);
110: }
111:
112: public void clear() {
113: _seriesMap.clear();
114: _seriesList.clear();
115: fireEvent(ChartDataEvent.REMOVED, null, null);
116: }
117:
118: //-- internal class --//
119: private static class XYPair {
120: private Number _x;
121: private Number _y;
122:
123: private XYPair(Number x, Number y) {
124: _x = x;
125: _y = y;
126: }
127:
128: private Number getX() {
129: return _x;
130: }
131:
132: private Number getY() {
133: return _y;
134: }
135: }
136: }
|