root / org.gvsig.chart / trunk / org.gvsig.chart / org.gvsig.chart.app / org.gvsig.chart.app.legendplugin / src / main / java / org / gvsig / project / documents / view / legend / BarsChartLegend.java @ 571
History | View | Annotate | Download (18.7 KB)
1 |
package org.gvsig.project.documents.view.legend; |
---|---|
2 |
|
3 |
import java.awt.Color; |
4 |
import java.awt.Graphics2D; |
5 |
import java.awt.image.BufferedImage; |
6 |
import java.util.ArrayList; |
7 |
import java.util.Arrays; |
8 |
import java.util.List; |
9 |
import java.util.Map; |
10 |
|
11 |
import org.cresques.cts.ICoordTrans; |
12 |
|
13 |
import org.gvsig.chart.ChartLocator; |
14 |
import org.gvsig.chart.ChartService; |
15 |
import org.gvsig.chart.legend.IBarsChartLegend; |
16 |
import org.gvsig.chart.legend.IChartLegend; |
17 |
import org.gvsig.chart.legend.symbols.IChartSymbol; |
18 |
import org.gvsig.chart.model.ChartDataSet; |
19 |
import org.gvsig.chart.renderer.ChartSerieEntity; |
20 |
import org.gvsig.fmap.dal.exception.DataException; |
21 |
import org.gvsig.fmap.dal.feature.Feature; |
22 |
import org.gvsig.fmap.dal.feature.FeatureQuery; |
23 |
import org.gvsig.fmap.dal.feature.FeatureSelection; |
24 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
25 |
import org.gvsig.fmap.mapcontext.MapContextException; |
26 |
import org.gvsig.fmap.mapcontext.MapContextLocator; |
27 |
import org.gvsig.fmap.mapcontext.MapContextManager; |
28 |
import org.gvsig.fmap.mapcontext.ViewPort; |
29 |
import org.gvsig.fmap.mapcontext.rendering.legend.LegendException; |
30 |
import org.gvsig.fmap.mapcontext.rendering.symbols.ISymbol; |
31 |
import org.gvsig.symbology.SymbologyLocator; |
32 |
import org.gvsig.symbology.fmap.mapcontext.rendering.legend.impl.VectorialUniqueValueLegend; |
33 |
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.fill.ISimpleFillSymbol; |
34 |
import org.gvsig.tools.ToolsLocator; |
35 |
import org.gvsig.tools.dynobject.DynStruct; |
36 |
import org.gvsig.tools.persistence.PersistenceManager; |
37 |
import org.gvsig.tools.persistence.PersistentState; |
38 |
import org.gvsig.tools.persistence.exception.PersistenceException; |
39 |
import org.gvsig.tools.task.Cancellable; |
40 |
import org.gvsig.tools.util.Callable; |
41 |
|
42 |
import org.jfree.chart.LegendItem; |
43 |
import org.slf4j.Logger; |
44 |
import org.slf4j.LoggerFactory; |
45 |
|
46 |
public class BarsChartLegend extends VectorialUniqueValueLegend implements IBarsChartLegend { |
47 |
|
48 |
final static private Logger LOGGER = LoggerFactory.getLogger(BarsChartLegend.class); |
49 |
|
50 |
public static class RegisterLegend implements Callable { |
51 |
|
52 |
@Override
|
53 |
public Object call() throws Exception { |
54 |
MapContextManager manager = MapContextLocator.getMapContextManager(); |
55 |
|
56 |
manager.registerLegend(IChartLegend.BARSCHART_LEGEND_NAME, |
57 |
BarsChartLegend.class); |
58 |
|
59 |
return Boolean.TRUE; |
60 |
} |
61 |
|
62 |
} |
63 |
|
64 |
public static final String BARS_CHART_LEGEND_PERSISTENCE_DEFINITION_NAME = "BarsChartLegend"; |
65 |
|
66 |
private static final String FIELD_NAMES = "FieldLegendNames"; |
67 |
private static final String FIELD_LABEL_NAMES = "FieldLabelNames"; |
68 |
private static final String FIELD_LABEL_COLOR = "FieldLabelColor"; |
69 |
|
70 |
protected ISymbol backgroundSymbol;
|
71 |
protected String[] fields; |
72 |
protected String[] labels; |
73 |
protected int size = 150; |
74 |
protected boolean is3D; |
75 |
|
76 |
protected boolean isOutlineShow; |
77 |
protected Color outlineColor; |
78 |
protected int outlineWidth; |
79 |
protected boolean onlySelection = false; |
80 |
protected int referenceSystem = 0; |
81 |
protected String fieldSize; |
82 |
protected String fieldNormalize = "---"; |
83 |
protected int sizeFrom = 10; |
84 |
protected int sizeTo = 1000; |
85 |
protected int unit = -1; |
86 |
protected int sizeOption = IChartSymbol.SIZEFIXED; |
87 |
protected boolean isActiveLimits; |
88 |
protected double minFeature; |
89 |
protected double maxFeature; |
90 |
protected Color[] colors = null; |
91 |
protected IChartSymbol symbol;
|
92 |
private double maxField; |
93 |
protected FeatureSelection featSelection;
|
94 |
|
95 |
public BarsChartLegend() {
|
96 |
} |
97 |
|
98 |
public BarsChartLegend(int shapeType) { |
99 |
super(shapeType);
|
100 |
} |
101 |
|
102 |
@Override
|
103 |
public void draw(BufferedImage image, Graphics2D g, ViewPort viewPort, |
104 |
Cancellable cancel, double scale, Map queryParameters, |
105 |
ICoordTrans coordTrans, FeatureStore featureStore, |
106 |
FeatureQuery featureQuery) throws LegendException {
|
107 |
try {
|
108 |
this.featSelection = featureStore.getFeatureSelection();
|
109 |
System.out.println("CHARTLEGEND: " + featSelection.getSelectedCount() + " seleccionados."); |
110 |
} catch (DataException e) {
|
111 |
e.printStackTrace(); |
112 |
} |
113 |
|
114 |
super.draw(image, g, viewPort, cancel, scale, queryParameters, coordTrans,
|
115 |
featureStore, featureQuery); |
116 |
} |
117 |
|
118 |
@Override
|
119 |
public boolean is3D() { |
120 |
return is3D;
|
121 |
} |
122 |
|
123 |
@Override
|
124 |
public void setIs3D(boolean b) { |
125 |
is3D = b; |
126 |
} |
127 |
|
128 |
@Override
|
129 |
public int getSize() { |
130 |
return size;
|
131 |
} |
132 |
|
133 |
@Override
|
134 |
public void setSize(int size) { |
135 |
this.size = size;
|
136 |
} |
137 |
|
138 |
@Override
|
139 |
public boolean isOutlineShow() { |
140 |
return isOutlineShow;
|
141 |
} |
142 |
|
143 |
@Override
|
144 |
public void setOutlineShow(boolean isOutlineShow) { |
145 |
this.isOutlineShow = isOutlineShow;
|
146 |
} |
147 |
|
148 |
@Override
|
149 |
public Color getOutlineColor() { |
150 |
return outlineColor;
|
151 |
} |
152 |
|
153 |
@Override
|
154 |
public void setOutlineColor(Color outlineColor) { |
155 |
this.outlineColor = outlineColor;
|
156 |
} |
157 |
|
158 |
@Override
|
159 |
public int getOutlineWidth() { |
160 |
return outlineWidth;
|
161 |
} |
162 |
|
163 |
@Override
|
164 |
public void setOutlineWidth(int outlineWidth) { |
165 |
this.outlineWidth = outlineWidth;
|
166 |
} |
167 |
|
168 |
@Override
|
169 |
public void setOnlySelection(boolean onlySelection) { |
170 |
this.onlySelection = onlySelection;
|
171 |
} |
172 |
|
173 |
@Override
|
174 |
public boolean isOnlySelection() { |
175 |
return onlySelection;
|
176 |
} |
177 |
|
178 |
@Override
|
179 |
public ISymbol getBackgroundSymbol() {
|
180 |
return backgroundSymbol;
|
181 |
} |
182 |
|
183 |
@Override
|
184 |
public void setBackgroundSymbol(ISymbol backgroundSymbol) { |
185 |
this.backgroundSymbol = backgroundSymbol;
|
186 |
} |
187 |
|
188 |
@Override
|
189 |
public int getSizeOption() { |
190 |
return this.sizeOption; |
191 |
} |
192 |
|
193 |
@Override
|
194 |
public String getFieldSize() { |
195 |
return this.fieldSize; |
196 |
} |
197 |
|
198 |
@Override
|
199 |
public String getFieldNormalize() { |
200 |
return this.fieldNormalize; |
201 |
} |
202 |
|
203 |
@Override
|
204 |
public int getSizeFrom() { |
205 |
return this.sizeFrom; |
206 |
} |
207 |
|
208 |
@Override
|
209 |
public int getSizeTo() { |
210 |
return this.sizeTo; |
211 |
} |
212 |
|
213 |
@Override
|
214 |
public int getUnit() { |
215 |
return unit;
|
216 |
} |
217 |
|
218 |
@Override
|
219 |
public void setSizeFrom(int s) { |
220 |
this.sizeFrom = s;
|
221 |
} |
222 |
|
223 |
@Override
|
224 |
public void setSizeTo(int s) { |
225 |
this.sizeTo = s;
|
226 |
} |
227 |
|
228 |
@Override
|
229 |
public void setUnit(int unit) { |
230 |
this.unit = unit;
|
231 |
} |
232 |
|
233 |
@Override
|
234 |
public void setFieldSize(String f) { |
235 |
this.fieldSize = f;
|
236 |
} |
237 |
|
238 |
@Override
|
239 |
public void setFieldNormalize(String f) { |
240 |
this.fieldNormalize = f;
|
241 |
} |
242 |
|
243 |
@Override
|
244 |
public void setSizeOption(int option) { |
245 |
this.sizeOption = option;
|
246 |
} |
247 |
|
248 |
@Override
|
249 |
public boolean isActiveLimits() { |
250 |
return isActiveLimits;
|
251 |
} |
252 |
|
253 |
@Override
|
254 |
public void setActiveLimits(boolean b) { |
255 |
this.isActiveLimits = b;
|
256 |
} |
257 |
|
258 |
@Override
|
259 |
public Color[] getColors() { |
260 |
return colors;
|
261 |
} |
262 |
|
263 |
@Override
|
264 |
public void setColors(Color[] colors) { |
265 |
this.colors = colors;
|
266 |
updateSymbols(); |
267 |
} |
268 |
|
269 |
@Override
|
270 |
public int getReferenceSystem() { |
271 |
return referenceSystem;
|
272 |
} |
273 |
|
274 |
@Override
|
275 |
public void setReferenceSystem(int rs) { |
276 |
this.referenceSystem = rs;
|
277 |
} |
278 |
|
279 |
@Override
|
280 |
public void setMinFeature(double min) { |
281 |
this.minFeature = min;
|
282 |
} |
283 |
|
284 |
@Override
|
285 |
public void setMaxFeature(double max) { |
286 |
this.maxFeature = max;
|
287 |
} |
288 |
|
289 |
@Override
|
290 |
public ISymbol getSymbolByFeature(Feature feat) throws MapContextException { |
291 |
Feature feat2 = feat.getCopy(); |
292 |
if (this.onlySelection) { |
293 |
if (!featSelection.isSelected(feat)) {
|
294 |
return null; |
295 |
} |
296 |
} |
297 |
|
298 |
// TODO: AQU? PRODR?AMOS RECUPERAR VARIOS SIMBOLOS. POR AHORA, SUPONEMOS QUE EL DEFAULTSYMBOL ES EL QUE TIENE EL CHARTSYMBOL
|
299 |
symbol = (IChartSymbol) getDefaultSymbol(); |
300 |
if (symbol == null) { |
301 |
symbol = (IChartSymbol) MapContextLocator |
302 |
.getSymbolManager().createSymbol(IChartSymbol.SYMBOL_NAME); |
303 |
|
304 |
} |
305 |
symbol.setOnlySelection(onlySelection); |
306 |
double size1 = 0d; |
307 |
|
308 |
ChartDataSet dataSet = ChartLocator.getServiceManager() |
309 |
.getChartManager().createChartDataSet(); |
310 |
|
311 |
String[] names = getFieldNames(); |
312 |
|
313 |
if (names != null) { |
314 |
ArrayList groupedBy = new ArrayList(); |
315 |
ArrayList serie = new ArrayList(); |
316 |
ArrayList category = new ArrayList(); |
317 |
for (String name : names) { |
318 |
double val = feat2.getDouble(name);
|
319 |
if (sizeOption == IChartSymbol.SIZESUM) {
|
320 |
size1 += val; |
321 |
} |
322 |
groupedBy.add(name); |
323 |
serie.add("default");
|
324 |
category.add(val); |
325 |
} |
326 |
dataSet.addData("groupby", groupedBy);
|
327 |
dataSet.addData("series", serie);
|
328 |
dataSet.addData("categories", category);
|
329 |
|
330 |
symbol.getChart().setChartDataSet(dataSet); |
331 |
ChartService cs = symbol.getChart(); |
332 |
cs.getChartProperties().setChartDimension(this.is3D);
|
333 |
|
334 |
List<ChartSerieEntity> lst = cs.getChartLegendItems();
|
335 |
|
336 |
for (int j = 0; j < names.length; j++) { |
337 |
colors[j] = getSymbolByValue(names[j]).getColor(); |
338 |
LegendItem item = new LegendItem(names[j], colors[j]);
|
339 |
item.setSeriesKey(names[j]); |
340 |
ChartSerieEntity e = cs.createNewChartSerieEntity(item); |
341 |
if (e != null) { |
342 |
lst.add(e); |
343 |
} |
344 |
} |
345 |
cs.setCategoriesPaint(Arrays.asList(colors));
|
346 |
} |
347 |
double separation = sizeTo - sizeFrom;
|
348 |
|
349 |
switch (sizeOption) {
|
350 |
case IChartSymbol.SIZESUM:
|
351 |
if (isActiveLimits()) {
|
352 |
double difFeat = maxFeature - minFeature;
|
353 |
double step = difFeat / separation;
|
354 |
size1 = sizeFrom + ((size1 - minFeature) / step); |
355 |
} |
356 |
symbol.setSize(size1); |
357 |
break;
|
358 |
case IChartSymbol.SIZEFIELD:
|
359 |
if (separation == 0) { |
360 |
separation = 1;
|
361 |
} |
362 |
double value = feat2.getDouble(getFieldSize());
|
363 |
if (!getFieldNormalize().equals("---")) { |
364 |
double normalize = feat2.getDouble(getFieldNormalize());
|
365 |
if (normalize != 0) { |
366 |
value = value / normalize; |
367 |
} |
368 |
if (isActiveLimits()) {
|
369 |
size1 = sizeFrom + (value * separation); |
370 |
} else {
|
371 |
size1 = value; |
372 |
} |
373 |
} else {
|
374 |
if (isActiveLimits()) {
|
375 |
double difFeat = maxFeature - minFeature;
|
376 |
double step = difFeat / separation;
|
377 |
size1 = sizeFrom + ((value - minFeature) / step); |
378 |
} else {
|
379 |
size1 = value; |
380 |
} |
381 |
} |
382 |
symbol.setSize(size1); |
383 |
break;
|
384 |
default:
|
385 |
symbol.setSize(getSize()); |
386 |
break;
|
387 |
} |
388 |
return symbol;
|
389 |
|
390 |
} |
391 |
|
392 |
public String[] getFieldNames() { |
393 |
return fields;
|
394 |
} |
395 |
|
396 |
@Override
|
397 |
public String[] getFieldLabels() { |
398 |
if (labels == null) { |
399 |
return new String[0]; |
400 |
} |
401 |
return labels;
|
402 |
} |
403 |
|
404 |
@Override
|
405 |
public void setFields(String[] fields) { |
406 |
this.fields = fields;
|
407 |
updateSymbols(); |
408 |
} |
409 |
|
410 |
@Override
|
411 |
public void setLabels(String[] fields) { |
412 |
this.labels = fields;
|
413 |
updateSymbols(); |
414 |
} |
415 |
|
416 |
@SuppressWarnings({"unchecked", "rawtypes"}) |
417 |
@Override
|
418 |
public void loadFromState(PersistentState state) |
419 |
throws PersistenceException {
|
420 |
// Set parent properties
|
421 |
super.loadFromState(state);
|
422 |
|
423 |
this.fields = state.getStringArray(FIELD_NAMES);
|
424 |
this.labels = state.getStringArray(FIELD_LABEL_NAMES);
|
425 |
this.colors = (Color[]) state.getArray(FIELD_LABEL_COLOR, Color.class); |
426 |
// Set own properties
|
427 |
this.backgroundSymbol = (ISymbol) state.get("backgroundSymbol"); |
428 |
this.size = state.getInt("size"); |
429 |
this.is3D = state.getBoolean("is3D"); |
430 |
|
431 |
this.isOutlineShow = state.getBoolean("isOutlineShow"); |
432 |
this.onlySelection = state.getBoolean("onlySelection"); |
433 |
this.isActiveLimits = state.getBoolean("isActiveLimits"); |
434 |
|
435 |
this.outlineWidth = state.getInt("outlineWidth"); |
436 |
this.referenceSystem = state.getInt("referenceSystem"); |
437 |
this.sizeFrom = state.getInt("sizeFrom"); |
438 |
this.sizeTo = state.getInt("sizeTo"); |
439 |
this.unit = state.getInt("unit"); |
440 |
this.sizeOption = state.getInt("sizeOption"); |
441 |
|
442 |
this.outlineColor = (Color) state.get("outlineColor"); |
443 |
this.symbol = (IChartSymbol) state.get("symbol"); |
444 |
this.featSelection = (FeatureSelection) state.get("featSelection"); |
445 |
|
446 |
this.fieldSize = state.getString("fieldSize"); |
447 |
this.fieldNormalize = state.getString("fieldNormalize"); |
448 |
|
449 |
this.minFeature = state.getDouble("minFeature"); |
450 |
this.maxFeature = state.getDouble("maxFeature"); |
451 |
this.maxField = state.getDouble("maxField"); |
452 |
} |
453 |
|
454 |
@Override
|
455 |
public void saveToState(PersistentState state) throws PersistenceException { |
456 |
// Save parent properties
|
457 |
super.saveToState(state);
|
458 |
// Save own properties
|
459 |
state.set(FIELD_NAMES, this.fields);
|
460 |
state.set(FIELD_LABEL_NAMES, this.labels);
|
461 |
state.set(FIELD_LABEL_COLOR, this.colors);
|
462 |
|
463 |
state.set("backgroundSymbol", this.backgroundSymbol); |
464 |
state.set("size", this.size); |
465 |
state.set("is3D", this.is3D); |
466 |
|
467 |
state.set("isOutlineShow", this.isOutlineShow); |
468 |
state.set("onlySelection", this.onlySelection); |
469 |
state.set("isActiveLimits", this.isActiveLimits); |
470 |
|
471 |
state.set("outlineWidth", this.outlineWidth); |
472 |
state.set("referenceSystem", this.referenceSystem); |
473 |
state.set("sizeFrom", this.sizeFrom); |
474 |
state.set("sizeTo", this.sizeTo); |
475 |
state.set("unit", this.unit); |
476 |
state.set("sizeOption", this.sizeOption); |
477 |
|
478 |
state.set("outlineColor", this.outlineColor); |
479 |
state.set("symbol", this.symbol); |
480 |
state.set("featSelection", this.featSelection); |
481 |
|
482 |
state.set("fieldSize", this.fieldSize); |
483 |
state.set("fieldNormalize", this.fieldNormalize); |
484 |
|
485 |
state.set("minFeature", this.minFeature); |
486 |
state.set("maxFeature", this.maxFeature); |
487 |
state.set("maxField", this.maxField); |
488 |
|
489 |
} |
490 |
|
491 |
public static class RegisterPersistence implements Callable { |
492 |
|
493 |
@Override
|
494 |
public Object call() throws Exception { |
495 |
PersistenceManager manager = ToolsLocator.getPersistenceManager(); |
496 |
if (manager.getDefinition(BARS_CHART_LEGEND_PERSISTENCE_DEFINITION_NAME) == null) { |
497 |
DynStruct definition = manager.addDefinition( |
498 |
BarsChartLegend.class, |
499 |
BARS_CHART_LEGEND_PERSISTENCE_DEFINITION_NAME, |
500 |
BARS_CHART_LEGEND_PERSISTENCE_DEFINITION_NAME + " Persistence definition",
|
501 |
null,
|
502 |
null
|
503 |
); |
504 |
// Extend the Classified Vector Legend base definition
|
505 |
definition.extend(manager.getDefinition(VECTORIAL_UNIQUE_VALUE_LEGEND_PERSISTENCE_DEFINITION_NAME)); |
506 |
|
507 |
// Field names
|
508 |
definition.addDynFieldArray(FIELD_NAMES) |
509 |
.setClassOfItems(String.class);
|
510 |
definition.addDynFieldArray(FIELD_LABEL_NAMES) |
511 |
.setClassOfItems(String.class);
|
512 |
definition.addDynFieldArray(FIELD_LABEL_COLOR) |
513 |
.setClassOfItems(Color.class);
|
514 |
|
515 |
definition.addDynField("backgroundSymbol").setClassOfValue(ISymbol.class);
|
516 |
definition.addDynFieldInt("size");
|
517 |
definition.addDynFieldBoolean("is3D");
|
518 |
|
519 |
definition.addDynFieldBoolean("isOutlineShow");
|
520 |
definition.addDynFieldBoolean("onlySelection");
|
521 |
definition.addDynFieldBoolean("isActiveLimits");
|
522 |
|
523 |
definition.addDynFieldInt("outlineWidth");
|
524 |
definition.addDynFieldInt("referenceSystem");
|
525 |
definition.addDynFieldInt("sizeFrom");
|
526 |
definition.addDynFieldInt("sizeTo");
|
527 |
definition.addDynFieldInt("unit");
|
528 |
definition.addDynFieldInt("sizeOption");
|
529 |
|
530 |
definition.addDynFieldObject("outlineColor").setClassOfValue(Color.class); |
531 |
definition.addDynFieldObject("symbol").setClassOfValue(IChartSymbol.class);
|
532 |
definition.addDynFieldObject("featSelection").setClassOfValue(FeatureSelection.class);
|
533 |
|
534 |
definition.addDynFieldString("fieldSize");
|
535 |
definition.addDynFieldString("fieldNormalize");
|
536 |
|
537 |
definition.addDynFieldDouble("minFeature");
|
538 |
definition.addDynFieldDouble("maxFeature");
|
539 |
definition.addDynFieldDouble("maxField");
|
540 |
} |
541 |
return Boolean.TRUE; |
542 |
} |
543 |
|
544 |
} |
545 |
|
546 |
protected void updateSymbols() { |
547 |
ISymbol[] symbols = getSymbols();
|
548 |
for (ISymbol symbol1 : symbols) {
|
549 |
delSymbol(getSymbolKey(symbol1)); |
550 |
} |
551 |
if (fields != null && colors != null && labels != null) { |
552 |
|
553 |
if (fields.length != colors.length) {
|
554 |
LOGGER.warn("The length of fields and colors isn't the same: fields.length = " + fields.length
|
555 |
+ " colors.length = " + colors.length);
|
556 |
} |
557 |
|
558 |
if (fields.length != labels.length) {
|
559 |
LOGGER.warn("The length of fields and labels isn't the same: fields.length = " + fields.length
|
560 |
+ " labels.length = " + labels.length);
|
561 |
} |
562 |
|
563 |
for (int i = 0; i < Math.min(fields.length, Math.min(labels.length, colors.length)); i++) { |
564 |
ISimpleFillSymbol symbol1 = SymbologyLocator.getSymbologyManager().createSimpleFillSymbol(); |
565 |
symbol1.setColor(colors[i]); |
566 |
symbol1.setDescription(labels[i]); |
567 |
addSymbol(fields[i], symbol1); |
568 |
} |
569 |
} |
570 |
} |
571 |
|
572 |
} |