Mercurial > dive4elements > gnv-client
comparison gnv-artifacts/src/main/java/de/intevation/gnv/jfreechart/PolygonSeries.java @ 875:5e9efdda6894
merged gnv-artifacts/1.0
author | Thomas Arendsen Hein <thomas@intevation.de> |
---|---|
date | Fri, 28 Sep 2012 12:13:56 +0200 |
parents | 22c18083225e |
children | f953c9a559d8 |
comparison
equal
deleted
inserted
replaced
722:bb3ffe7d719e | 875:5e9efdda6894 |
---|---|
1 package de.intevation.gnv.jfreechart; | |
2 | |
3 import java.util.HashMap; | |
4 import java.util.Map; | |
5 | |
6 import org.jfree.data.Range; | |
7 | |
8 import org.jfree.data.general.Series; | |
9 | |
10 /** | |
11 * This class represents a series of polygon items. | |
12 * | |
13 * @author <a href="mailto:sascha.teichmann@intevation.de">Sascha Teichmann</a> | |
14 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a> | |
15 */ | |
16 public class PolygonSeries | |
17 extends Series | |
18 { | |
19 /** | |
20 * Polygons. | |
21 */ | |
22 protected CompactXYItems [] rings; | |
23 | |
24 /** | |
25 * A map containing attribues. | |
26 */ | |
27 protected Map attributes; | |
28 | |
29 /** | |
30 * The unique key of this series. | |
31 */ | |
32 private static long uniqueKey; | |
33 | |
34 /** | |
35 * | |
36 * @return a unique key. | |
37 */ | |
38 protected synchronized static Long createUniqueKey() { | |
39 return new Long(uniqueKey++); | |
40 } | |
41 | |
42 /** | |
43 * Constructor to create an empty PolygonSeries with a unique key. | |
44 */ | |
45 public PolygonSeries() { | |
46 this(createUniqueKey(), null); | |
47 } | |
48 | |
49 /** | |
50 * | |
51 * @param key The key used for this series. | |
52 * @param rings Polygons. | |
53 */ | |
54 public PolygonSeries(Comparable key, CompactXYItems [] rings) { | |
55 this(key, null, rings, new HashMap()); | |
56 } | |
57 | |
58 /** | |
59 * | |
60 * @param key The key used for this series. | |
61 * @param description A description of this series. | |
62 * @param rings Polygons. | |
63 */ | |
64 public PolygonSeries( | |
65 Comparable key, | |
66 String description, | |
67 CompactXYItems[] rings | |
68 ) { | |
69 this(key, description, rings, new HashMap()); | |
70 } | |
71 | |
72 /** | |
73 * | |
74 * @param key The key used for this series. | |
75 * @param description A description of this series. | |
76 * @param rings Polygons. | |
77 * @param attributes Some attribues. | |
78 */ | |
79 public PolygonSeries( | |
80 Comparable key, | |
81 String description, | |
82 CompactXYItems [] rings, | |
83 Map attributes | |
84 ) { | |
85 super(key, description); | |
86 this.rings = rings; | |
87 this.attributes = attributes; | |
88 } | |
89 | |
90 | |
91 public void setRings(CompactXYItems [] rings) { | |
92 this.rings = rings; | |
93 } | |
94 | |
95 | |
96 public CompactXYItems [] getRings() { | |
97 return rings; | |
98 } | |
99 | |
100 | |
101 public void addRing(CompactXYItems newRing) { | |
102 if (rings == null) { | |
103 rings = new CompactXYItems [] { newRing }; | |
104 } | |
105 else { | |
106 CompactXYItems [] nRings = new CompactXYItems[rings.length + 1]; | |
107 System.arraycopy(rings, 0, nRings, 0, rings.length); | |
108 nRings[rings.length] = newRing; | |
109 rings = nRings; | |
110 } | |
111 } | |
112 | |
113 | |
114 public void addRings(CompactXYItems [] newRings) { | |
115 if (newRings == null || newRings.length == 0) { | |
116 return; | |
117 } | |
118 if (rings == null || rings.length == 0) { | |
119 rings = newRings; | |
120 } | |
121 else { | |
122 CompactXYItems [] both = | |
123 new CompactXYItems[rings.length + newRings.length]; | |
124 System.arraycopy(rings, 0, both, 0, rings.length); | |
125 System.arraycopy(newRings, 0, both, rings.length, newRings.length); | |
126 rings = both; | |
127 } | |
128 } | |
129 | |
130 | |
131 public Object getAttribute(Object key) { | |
132 return attributes.get(key); | |
133 } | |
134 | |
135 | |
136 public Object setAttribute(Object key, Object value) { | |
137 return attributes.put(key, value); | |
138 } | |
139 | |
140 | |
141 public int getItemCount() { | |
142 return rings != null ? rings.length : 0; | |
143 } | |
144 | |
145 | |
146 public CompactXYItems getItem(int idx) { | |
147 return rings[idx]; | |
148 } | |
149 | |
150 | |
151 public Object removeAttribute(Object key) { | |
152 return attributes.remove(key); | |
153 } | |
154 | |
155 | |
156 public boolean hasAttribute(Object key) { | |
157 return attributes.containsKey(key); | |
158 } | |
159 | |
160 | |
161 /** | |
162 * | |
163 * @return the range of the x axis. | |
164 */ | |
165 public Range getDomainBounds() { | |
166 double upper = Double.NEGATIVE_INFINITY; | |
167 double lower = Double.POSITIVE_INFINITY; | |
168 int count = getItemCount(); | |
169 | |
170 for (int i = 0; i < count; i++) { | |
171 CompactXYItems items = getItem(i); | |
172 double minX = items.getMinX(); | |
173 double maxX = items.getMaxX(); | |
174 | |
175 if (!Double.isNaN(minX)) { | |
176 lower = Math.min(lower, minX); | |
177 } | |
178 | |
179 if (!Double.isNaN(maxX)) { | |
180 upper = Math.max(upper, maxX); | |
181 } | |
182 } | |
183 | |
184 if (lower > upper) { | |
185 return null; | |
186 } | |
187 | |
188 return new Range(lower, upper); | |
189 } | |
190 | |
191 | |
192 /** | |
193 * | |
194 * @return the range of the y axis. | |
195 */ | |
196 public Range getRangeBounds() { | |
197 double upper = Double.NEGATIVE_INFINITY; | |
198 double lower = Double.POSITIVE_INFINITY; | |
199 int count = getItemCount(); | |
200 | |
201 for (int i = 0; i < count; i++) { | |
202 CompactXYItems items = getItem(i); | |
203 double minY = items.getMinY(); | |
204 double maxY = items.getMaxY(); | |
205 | |
206 if (!Double.isNaN(minY)) { | |
207 lower = Math.min(lower, minY); | |
208 } | |
209 | |
210 if (!Double.isNaN(maxY)) { | |
211 upper = Math.max(upper, maxY); | |
212 } | |
213 } | |
214 | |
215 if (lower > upper) { | |
216 return null; | |
217 } | |
218 | |
219 return new Range(lower, upper); | |
220 } | |
221 } | |
222 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 : |