Statistics
| Revision:

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 / BasePieChartLegend.java @ 571

History | View | Annotate | Download (18.6 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.List;
8
import java.util.Map;
9

    
10
import org.cresques.cts.ICoordTrans;
11
import org.jfree.chart.LegendItem;
12
import org.slf4j.Logger;
13
import org.slf4j.LoggerFactory;
14

    
15
import org.gvsig.chart.ChartLocator;
16
import org.gvsig.chart.ChartService;
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.ViewPort;
28
import org.gvsig.fmap.mapcontext.rendering.legend.LegendException;
29
import org.gvsig.fmap.mapcontext.rendering.legend.ZSort;
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.persistence.PersistentState;
35
import org.gvsig.tools.persistence.exception.PersistenceException;
36
import org.gvsig.tools.task.Cancellable;
37

    
38
/**
39
 * @author Fjp
40
 *
41
 * TODO: CAMBIAR EL NOMBRE DE ESTA LEYENDA A CHARTLEGEND, PORQUE EN REALIDAD
42
 * ESTA SE USA PARA TODO TIPO DE CHARTS. (BarsChartLegend no se usa, borrarla)
43
 */
44
public class BasePieChartLegend extends VectorialUniqueValueLegend {
45

    
46
    final static private Logger LOG = LoggerFactory.getLogger(BasePieChartLegend.class);
47

    
48
    public static final String BASE_PIE_CHART_LEGEND_PERSISTENCE_DEFINITION_NAME = "BasePieChartLegend";
49

    
50
    private static final String FIELD_NAMES = "FieldLegendNames";
51
    private static final String FIELD_LABEL_NAMES = "FieldLabelNames";
52
    private static final String FIELD_LABEL_COLOR = "FieldLabelColor";
53

    
54
    protected ISymbol backgroundSymbol;
55
    protected String[] fields;
56
    protected String[] labels;
57
    protected int size = 150;
58
    protected boolean is3D;
59

    
60
    protected boolean isOutlineShow;
61
    protected Color outlineColor;
62
    protected int outlineWidth;
63
    protected boolean onlySelection = false;
64
    protected int referenceSystem = 0;
65
    protected String fieldSize;
66
    protected String fieldNormalize = "---";
67
    protected int sizeFrom = 10;
68
    protected int sizeTo = 1000;
69
    protected int unit = -1;
70
    protected int sizeOption = IChartSymbol.SIZEFIXED;
71
    protected ZSort zSort = null;
72
    protected boolean isActiveLimits;
73
    protected double minFeature;
74
    protected double maxFeature;
75
    protected Color[] colors = null;
76
    protected IChartSymbol symbol;
77
    private double maxField;
78
    protected FeatureSelection featSelection;
79

    
80
    public BasePieChartLegend() {
81
    }
82

    
83
    public BasePieChartLegend(int shapeType) {
84
        super(shapeType);
85
    }
86

    
87
    @Override
88
    public void draw(BufferedImage image, Graphics2D g, ViewPort viewPort,
89
            Cancellable cancel, double scale, Map queryParameters,
90
            ICoordTrans coordTrans, FeatureStore featureStore,
91
            FeatureQuery featureQuery) throws LegendException {
92
        try {
93
            this.featSelection = featureStore.getFeatureSelection();
94
            System.out.println("CHARTLEGEND: " + featSelection.getSelectedCount() + " seleccionados.");
95
        } catch (DataException e) {
96
            e.printStackTrace();
97
        }
98

    
99
        super.draw(image, g, viewPort, cancel, scale, queryParameters, coordTrans,
100
                featureStore, featureQuery);
101
    }
102

    
103
    /* (non-Javadoc)
104
         * @see org.gvsig.symbology.fmap.mapcontext.rendering.legend.IChartLegend#is3D()
105
     */
106
    public boolean is3D() {
107
        return is3D;
108
    }
109

    
110
    /* (non-Javadoc)
111
         * @see org.gvsig.symbology.fmap.mapcontext.rendering.legend.IChartLegend#setIs3D(boolean)
112
     */
113
    public void setIs3D(boolean b) {
114
        is3D = b;
115
    }
116

    
117
    /* (non-Javadoc)
118
         * @see org.gvsig.symbology.fmap.mapcontext.rendering.legend.IChartLegend#getSize()
119
     */
120
    public int getSize() {
121
        return size;
122
    }
123

    
124
    /* (non-Javadoc)
125
         * @see org.gvsig.symbology.fmap.mapcontext.rendering.legend.IChartLegend#setSize(int)
126
     */
127
    public void setSize(int size) {
128
        this.size = size;
129
    }
130

    
131
    /* (non-Javadoc)
132
         * @see org.gvsig.symbology.fmap.mapcontext.rendering.legend.IChartLegend#isOutlineShow()
133
     */
134
    public boolean isOutlineShow() {
135
        return isOutlineShow;
136
    }
137

    
138
    /* (non-Javadoc)
139
         * @see org.gvsig.symbology.fmap.mapcontext.rendering.legend.IChartLegend#setOutlineShow(boolean)
140
     */
141
    public void setOutlineShow(boolean isOutlineShow) {
142
        this.isOutlineShow = isOutlineShow;
143
    }
144

    
145
    /* (non-Javadoc)
146
         * @see org.gvsig.symbology.fmap.mapcontext.rendering.legend.IChartLegend#getOutlineColor()
147
     */
148
    public Color getOutlineColor() {
149
        return outlineColor;
150
    }
151

    
152
    /* (non-Javadoc)
153
         * @see org.gvsig.symbology.fmap.mapcontext.rendering.legend.IChartLegend#setOutlineColor(java.awt.Color)
154
     */
155
    public void setOutlineColor(Color outlineColor) {
156
        this.outlineColor = outlineColor;
157
    }
158

    
159
    /* (non-Javadoc)
160
         * @see org.gvsig.symbology.fmap.mapcontext.rendering.legend.IChartLegend#getOutlineWidth()
161
     */
162
    public int getOutlineWidth() {
163
        return outlineWidth;
164
    }
165

    
166
    /* (non-Javadoc)
167
         * @see org.gvsig.symbology.fmap.mapcontext.rendering.legend.IChartLegend#setOutlineWidth(int)
168
     */
169
    public void setOutlineWidth(int outlineWidth) {
170
        this.outlineWidth = outlineWidth;
171
    }
172

    
173
    /* (non-Javadoc)
174
         * @see org.gvsig.symbology.fmap.mapcontext.rendering.legend.IChartLegend#setOnlySelection(boolean)
175
     */
176
    public void setOnlySelection(boolean onlySelection) {
177
        this.onlySelection = onlySelection;
178
    }
179

    
180
    /* (non-Javadoc)
181
         * @see org.gvsig.symbology.fmap.mapcontext.rendering.legend.IChartLegend#isOnlySelection()
182
     */
183
    public boolean isOnlySelection() {
184
        return onlySelection;
185
    }
186

    
187
    public ISymbol getBackgroundSymbol() {
188
        return backgroundSymbol;
189
    }
190

    
191
    public void setBackgroundSymbol(ISymbol backgroundSymbol) {
192
        this.backgroundSymbol = backgroundSymbol;
193
    }
194

    
195
    public int getSizeOption() {
196
        return this.sizeOption;
197
    }
198

    
199
    public String getFieldSize() {
200
        return this.fieldSize;
201
    }
202

    
203
    public String getFieldNormalize() {
204
        return this.fieldNormalize;
205
    }
206

    
207
    public int getSizeFrom() {
208
        return this.sizeFrom;
209
    }
210

    
211
    public int getSizeTo() {
212
        return this.sizeTo;
213
    }
214

    
215
    public int getUnit() {
216
        return unit;
217
    }
218

    
219
    public void setSizeFrom(int s) {
220
        this.sizeFrom = s;
221
    }
222

    
223
    public void setSizeTo(int s) {
224
        this.sizeTo = s;
225
    }
226

    
227
    public void setUnit(int unit) {
228
        this.unit = unit;
229
    }
230

    
231
    public void setFieldSize(String f) {
232
        this.fieldSize = f;
233
    }
234

    
235
    public void setFieldNormalize(String f) {
236
        this.fieldNormalize = f;
237
    }
238

    
239
    public void setSizeOption(int option) {
240
        this.sizeOption = option;
241
    }
242

    
243
    public boolean isActiveLimits() {
244
        return isActiveLimits;
245
    }
246

    
247
    public void setActiveLimits(boolean b) {
248
        this.isActiveLimits = b;
249
    }
250

    
251
    public Color[] getColors() {
252
        return colors;
253
    }
254

    
255
    public void setColors(Color[] colors) {
256
        this.colors = colors;
257
        updateSymbols();
258
    }
259

    
260
    public int getReferenceSystem() {
261
        return referenceSystem;
262
    }
263

    
264
    public void setReferenceSystem(int rs) {
265
        this.referenceSystem = rs;
266
    }
267

    
268
    public void setMinFeature(double min) {
269
        this.minFeature = min;
270
    }
271

    
272
    public void setMaxFeature(double max) {
273
        this.maxFeature = max;
274
    }
275

    
276
    @Override
277
    public ISymbol getSymbolByFeature(Feature feat) throws MapContextException {
278

    
279
        if (this.onlySelection) {
280
            if (!featSelection.isSelected(feat)) {
281
                return null;
282
            }
283
        }
284

    
285
        // TODO: AQU? PRODR?AMOS RECUPERAR VARIOS SIMBOLOS. POR AHORA, SUPONEMOS QUE EL DEFAULTSYMBOL ES EL QUE TIENE EL CHARTSYMBOL
286
        symbol = (IChartSymbol) getDefaultSymbol();
287
        if (symbol == null) {
288
            symbol = (IChartSymbol) MapContextLocator
289
                    .getSymbolManager().createSymbol(IChartSymbol.SYMBOL_NAME);
290

    
291
        }
292
        symbol.setOnlySelection(onlySelection);
293
        double size = 0;
294

    
295
        ChartDataSet dataSet = ChartLocator.getServiceManager()
296
                .getChartManager().createChartDataSet();
297

    
298
        String[] names = getFieldNames();
299
        String[] labels = getFieldLabels();
300
        if (names != null && labels != null && labels.length == names.length) {
301

    
302
            ArrayList serie = new ArrayList();
303
            ArrayList category = new ArrayList();
304
            for (int i = 0; i < names.length; i++) {
305
                double val = feat.getDouble(names[i]);
306
                if (sizeOption == IChartSymbol.SIZESUM) {
307
                    size += val;
308
                }
309
                if (!this.onlySelection || featSelection.isSelected(feat)) {
310
                    serie.add(labels[i]);
311
                    category.add(val);
312
                }
313
            }
314
            dataSet.addData("series", serie);
315
            dataSet.addData("categories", category);
316

    
317
            if (!this.onlySelection || featSelection.isSelected(feat)) {
318
                symbol.getChart().setChartDataSet(dataSet);
319
            }
320
            ChartService cs = symbol.getChart();
321

    
322
            List<ChartSerieEntity> lst = cs.getChartLegendItems();
323

    
324
            for (int j = 0; j < names.length; j++) {
325
                LegendItem item = new LegendItem(names[j], getSymbolByValue(names[j]).getColor());
326
                item.setSeriesKey(names[j]);
327
                ChartSerieEntity e = cs.createNewChartSerieEntity(item);
328
                if (e != null) {
329
                    lst.add(e);
330
                }
331
            }
332
        }
333
        double separation = sizeTo - sizeFrom;
334

    
335
        switch (sizeOption) {
336
            case IChartSymbol.SIZESUM:
337
                if (isActiveLimits()) {
338
                    double difFeat = maxFeature - minFeature;
339
                    double step = difFeat / separation;
340
                    size = sizeFrom + ((size - minFeature) / step);
341
                }
342
                symbol.setSize(size);
343
                break;
344
            case IChartSymbol.SIZEFIELD:
345
                if (separation == 0) {
346
                    separation = 1;
347
                }
348
                double value = feat.getDouble(getFieldSize());
349
                if (!getFieldNormalize().equals("---")) {
350
                    double normalize = feat.getDouble(getFieldNormalize());
351
                    if (normalize != 0) {
352
                        value = value / normalize;
353
                    }
354
                    if (isActiveLimits()) {
355
                        size = sizeFrom + (value * separation);
356
                    } else {
357
                        size = value;
358
                    }
359
                } else {
360
                    if (isActiveLimits()) {
361
                        double difFeat = maxFeature - minFeature;
362
                        double step = difFeat / separation;
363
                        size = sizeFrom + ((value - minFeature) / step);
364
                    } else {
365
                        size = value;
366
                    }
367
                }
368
                symbol.setSize(size);
369
                break;
370
            default:
371
                symbol.setSize(getSize());
372
                break;
373
        }
374
        return symbol;
375

    
376
    }
377

    
378
    public String[] getFieldNames() {
379
        return fields;
380
    }
381

    
382
    public String[] getFieldLabels() {
383
        if (labels == null) {
384
            return new String[0];
385
        }
386
        return labels;
387
    }
388

    
389
    public void setFields(String[] fields) {
390
        this.fields = fields;
391
        updateSymbols();
392
    }
393

    
394
    public void setLabels(String[] fields) {
395
        this.labels = fields;
396
        updateSymbols();
397
    }
398

    
399
    @SuppressWarnings({"unchecked", "rawtypes"})
400
    public void loadFromState(PersistentState state)
401
            throws PersistenceException {
402
        // Set parent properties
403
        super.loadFromState(state);
404

    
405
        this.fields = state.getStringArray(FIELD_NAMES);
406
        this.labels = state.getStringArray(FIELD_LABEL_NAMES);
407
        this.colors = (Color[]) state.getArray(FIELD_LABEL_COLOR, Color.class);
408
        // Set own properties
409
        this.backgroundSymbol = (ISymbol) state.get("backgroundSymbol");
410
        this.size = state.getInt("size");
411
        this.is3D = state.getBoolean("is3D");
412

    
413
        this.isOutlineShow = state.getBoolean("isOutlineShow");
414
        this.onlySelection = state.getBoolean("onlySelection");
415
        this.isActiveLimits = state.getBoolean("isActiveLimits");
416

    
417
        this.outlineWidth = state.getInt("outlineWidth");
418
        this.referenceSystem = state.getInt("referenceSystem");
419
        this.sizeFrom = state.getInt("sizeFrom");
420
        this.sizeTo = state.getInt("sizeTo");
421
        this.unit = state.getInt("unit");
422
        this.sizeOption = state.getInt("sizeOption");
423

    
424
        this.outlineColor = (Color) state.get("outlineColor");
425
        this.symbol = (IChartSymbol) state.get("symbol");
426
        this.featSelection = (FeatureSelection) state.get("featSelection");
427

    
428
        this.fieldSize = state.getString("fieldSize");
429
        this.fieldNormalize = state.getString("fieldNormalize");
430

    
431
        this.minFeature = state.getDouble("minFeature");
432
        this.maxFeature = state.getDouble("maxFeature");
433
        this.maxField = state.getDouble("maxField");
434
    }
435

    
436
    public void saveToState(PersistentState state) throws PersistenceException {
437
        // Save parent properties
438
        super.saveToState(state);
439
        // Save own properties
440
        state.set(FIELD_NAMES, this.fields);
441
        state.set(FIELD_LABEL_NAMES, this.labels);
442
        state.set(FIELD_LABEL_COLOR, this.colors);
443

    
444
        state.set("backgroundSymbol", this.backgroundSymbol);
445
        state.set("size", this.size);
446
        state.set("is3D", this.is3D);
447

    
448
        state.set("isOutlineShow", this.isOutlineShow);
449
        state.set("onlySelection", this.onlySelection);
450
        state.set("isActiveLimits", this.isActiveLimits);
451

    
452
        state.set("outlineWidth", this.outlineWidth);
453
        state.set("referenceSystem", this.referenceSystem);
454
        state.set("sizeFrom", this.sizeFrom);
455
        state.set("sizeTo", this.sizeTo);
456
        state.set("unit", this.unit);
457
        state.set("sizeOption", this.sizeOption);
458

    
459
        state.set("outlineColor", this.outlineColor);
460
        state.set("symbol", this.symbol);
461
        state.set("featSelection", this.featSelection);
462

    
463
        state.set("fieldSize", this.fieldSize);
464
        state.set("fieldNormalize", this.fieldNormalize);
465

    
466
        state.set("minFeature", this.minFeature);
467
        state.set("maxFeature", this.maxFeature);
468
        state.set("maxField", this.maxField);
469

    
470
    }
471

    
472
//        public static class RegisterPersistence implements Callable {
473
//
474
//                public Object call() throws Exception {
475
//                        PersistenceManager manager = ToolsLocator.getPersistenceManager();
476
//                        if( manager.getDefinition(BASE_PIE_CHART_LEGEND_PERSISTENCE_DEFINITION_NAME)==null ) {
477
//                                DynStruct definition = manager.addDefinition(
478
//                                                BasePieChartLegend.class,
479
//                                                BASE_PIE_CHART_LEGEND_PERSISTENCE_DEFINITION_NAME,
480
//                                                BASE_PIE_CHART_LEGEND_PERSISTENCE_DEFINITION_NAME+" Persistence definition",
481
//                                                null,
482
//                                                null
483
//                                );
484
//                                // Extend the Classified Vector Legend base definition
485
//                                definition.extend(manager.getDefinition(VECTORIAL_UNIQUE_VALUE_LEGEND_PERSISTENCE_DEFINITION_NAME));
486
//
487
//                                // Field names
488
//                                definition.addDynFieldArray(FIELD_NAMES)
489
//                                .setClassOfItems(String.class);
490
//                                definition.addDynFieldArray(FIELD_LABEL_NAMES)
491
//                                        .setClassOfItems(String.class);
492
//                                definition.addDynFieldArray(FIELD_LABEL_COLOR)
493
//                                .setClassOfItems(Color.class);
494
//
495
//                                definition.addDynField("backgroundSymbol").setClassOfValue(ISymbol.class);
496
//                                definition.addDynFieldInt("size");
497
//                                definition.addDynFieldBoolean("is3D");
498
//
499
//                                definition.addDynFieldBoolean("isOutlineShow");
500
//                                definition.addDynFieldBoolean("onlySelection");
501
//                                definition.addDynFieldBoolean("isActiveLimits");
502
//
503
//                                definition.addDynFieldInt("outlineWidth");
504
//                                definition.addDynFieldInt("referenceSystem");
505
//                                definition.addDynFieldInt("sizeFrom");
506
//                                definition.addDynFieldInt("sizeTo");
507
//                                definition.addDynFieldInt("unit");
508
//                                definition.addDynFieldInt("sizeOption");
509
//
510
//                                definition.addDynFieldObject("outlineColor").setClassOfValue(Color.class);
511
//                                definition.addDynFieldObject("symbol").setClassOfValue(IChartSymbol.class);
512
//                                definition.addDynFieldObject("featSelection").setClassOfValue(FeatureSelection.class);
513
//
514
//                                definition.addDynFieldString("fieldSize");
515
//                                definition.addDynFieldString("fieldNormalize");
516
//
517
//                                definition.addDynFieldDouble("minFeature");
518
//                                definition.addDynFieldDouble("maxFeature");
519
//                                definition.addDynFieldDouble("maxField");
520
//                        }
521
//                        return Boolean.TRUE;
522
//                }
523
//
524
//        }
525
    protected void updateSymbols() {
526
        ISymbol[] symbols = getSymbols();
527
        for (ISymbol symbol1 : symbols) {
528
            delSymbol(getSymbolKey(symbol1));
529
        }
530
        if (fields != null && colors != null && labels != null) {
531

    
532
            if (fields.length != colors.length) {
533
                LOG.warn("The length of fields and colors isn't the same: fields.length = " + fields.length
534
                        + " fields.length = " + colors.length);
535
            }
536

    
537
            if (fields.length != labels.length) {
538
                LOG.warn("The length of fields and labels isn't the same: fields.length = " + fields.length
539
                        + " labels.length = " + labels.length);
540
            }
541

    
542
            for (int i = 0; i < Math.min(fields.length, Math.min(labels.length, colors.length)); i++) {
543
                ISimpleFillSymbol symbol1 = SymbologyLocator.getSymbologyManager().createSimpleFillSymbol();
544
                symbol1.setColor(colors[i]);
545
                symbol1.setDescription(labels[i]);
546
                addSymbol(fields[i], symbol1);
547
            }
548
        }
549
    }
550
}