source: ether_megapoli/trunk/service/implementation/com/ether/EtherPlotContentServiceImpl.java @ 366

Last change on this file since 366 was 366, checked in by vmipsl, 12 years ago

Réparation bug chrome liens
Réparation chrome style
Data protocol (accueil, pdf, inscription)
Ajout période UTC dans plot 2D

File size: 29.3 KB
Line 
1package com.ether;
2
3import com.medias.database.objects.Parametre;
4import com.medias.database.objects.Plateforme;
5import gov.noaa.pmel.sgt.Axis;
6import gov.noaa.pmel.sgt.CartesianGraph;
7import gov.noaa.pmel.sgt.ColorMap;
8import gov.noaa.pmel.sgt.ContourLevels;
9import gov.noaa.pmel.sgt.Graph;
10import gov.noaa.pmel.sgt.GridAttribute;
11import gov.noaa.pmel.sgt.IndexedColorMap;
12import gov.noaa.pmel.sgt.JPane;
13import gov.noaa.pmel.sgt.Layer;
14import gov.noaa.pmel.sgt.LayerNotFoundException;
15import gov.noaa.pmel.sgt.LineAttribute;
16import gov.noaa.pmel.sgt.LineCartesianRenderer;
17import gov.noaa.pmel.sgt.LineKey;
18import gov.noaa.pmel.sgt.LinearTransform;
19import gov.noaa.pmel.sgt.PlainAxis;
20import gov.noaa.pmel.sgt.SGLabel;
21import gov.noaa.pmel.sgt.StackedLayout;
22import gov.noaa.pmel.sgt.TimeAxis;
23import gov.noaa.pmel.sgt.dm.SGTMetaData;
24import gov.noaa.pmel.sgt.dm.SimpleGrid;
25import gov.noaa.pmel.sgt.dm.SimpleLine;
26import gov.noaa.pmel.sgt.swing.JPlotLayout;
27import gov.noaa.pmel.util.Dimension2D;
28import gov.noaa.pmel.util.GeoDateArray;
29import gov.noaa.pmel.util.Point2D;
30import gov.noaa.pmel.util.Range2D;
31import gov.noaa.pmel.util.SoTPoint;
32import gov.noaa.pmel.util.SoTRange;
33import org.jetbrains.annotations.NotNull;
34import org.jetbrains.annotations.Nullable;
35import org.springframework.beans.factory.annotation.Required;
36
37import java.awt.*;
38import java.util.ArrayList;
39import java.util.Arrays;
40import java.util.Calendar;
41import java.util.Collections;
42import java.util.Date;
43import java.util.List;
44import java.util.Locale;
45import java.util.Random;
46import java.util.ResourceBundle;
47
48/**
49 * @author vmipsl
50 * @date 05 sept. 2011
51 */
52public class EtherPlotContentServiceImpl
53{
54    /**
55     * First layer contains axes, labels and the first set of data
56     * Others layers contains only the other sets of data
57     *
58     * @param megapoliPlot
59     * @param locale
60     * @return
61     */
62    @Nullable
63    public JPane createTimeSeriePlot( @NotNull final MegapoliPlot megapoliPlot, @NotNull final Integer plotWidth, @NotNull final Integer plotHeight, final Locale locale )
64            throws ServiceException
65    {
66        final JPane mainPane = new JPane( "Time Serie Pane", new Dimension( plotWidth, plotHeight ) );
67        mainPane.setLayout( new StackedLayout() );
68
69        // TODO : revoir les tailles ! mettre en automatique par rapport à plotWidth et plotHeight
70        /*
71        * xsize, ysize are the width and height in physical units
72        * of the Layer graphics region.
73        *
74        * xstart, xend are the start and end points for the TimeAxis
75        * ystart, yend are the start and end points for the Y axis
76        */
77        final double xsize = 5.0;
78        final double ysize = 3.5;
79        final double xstart = 0.3;
80        final double xend = xsize - xstart;
81        final double ystart = 0.5;
82        final double yend = ysize - ystart;
83        // Init the index to pick up the colors in the array
84        _indexColorArray = 0;
85
86        final Pair<List, List> listPairOfPIdPIdForTwoAxes = extractListPairForAxes( megapoliPlot.getPfIdPIdList() );
87        final List<Pair> pIdPIdListForFirstAxis = listPairOfPIdPIdForTwoAxes.getFirstValue();
88        final List<Pair> pIdPIdListForSecondAxis = listPairOfPIdPIdForTwoAxes.getSecondValue();
89
90        final List<SimpleLine> allLines = createLines( pIdPIdListForFirstAxis, megapoliPlot.getBeginDate(), megapoliPlot.getEndDate(), -2000, locale );
91        final List<SimpleLine> allLinesForSecondAxis = createLines( pIdPIdListForSecondAxis, megapoliPlot.getBeginDate(), megapoliPlot.getEndDate(), 50, locale );
92        List<SimpleLine> lines = extractRealLines( allLines );
93        List<SimpleLine> linesForSecondAxis = extractRealLines( allLinesForSecondAxis );
94
95        final List<SimpleLine> emptyLineToDisplayOnlyInLegend = createEmptyLinesList( allLines, allLinesForSecondAxis, lines );
96        if( lines.isEmpty() )
97        {
98            if( linesForSecondAxis.isEmpty() )
99                return null;
100            else
101            {
102                lines = linesForSecondAxis;
103                linesForSecondAxis = new ArrayList<SimpleLine>();
104            }
105        }
106
107        final SimpleLine firstLine = lines.get( 0 );
108
109        /** ********** AXIS RANGES ********* **/
110        final SoTRange xnRange = firstLine.getXRange();
111        final SoTRange yRange = firstLine.getYRange();
112        for( final SimpleLine line : lines )
113        {
114            yRange.add( line.getYRange() );
115            xnRange.add( line.getXRange() );
116        }
117
118        for( final SimpleLine line : linesForSecondAxis )
119            xnRange.add( line.getXRange() );
120
121        /** ********************************** **/
122        /** ************ FIRST AXIS ********** **/
123        /** ********************************** **/
124        final SoTRange ynRange = Graph.computeRange( yRange, _intervalsNumber );
125        final SoTRange xnRange2 = Graph.computeRange( xnRange, _intervalsNumber );
126        final SoTPoint originPoint = new SoTPoint( ynRange.getStart(), xnRange.getStart() );
127
128        // Time axis is the same for the two y-axis
129        final TimeAxis xTimeAxis = createXTimeAxis( xnRange, originPoint );
130        final PlainAxis yLeftAxis = createYAxis( firstLine, ynRange, originPoint, null );
131        final LinearTransform xt = new LinearTransform( new Range2D( xstart, xend ), xnRange );
132
133        // GRAPH
134        final CartesianGraph graph = createGraphForFirstLine( megapoliPlot, mainPane, xsize, ysize, ystart, yend, xTimeAxis, xt, firstLine, ynRange, yLeftAxis );
135        graph.addXAxis( xTimeAxis );
136
137        // LEGEND
138        final LineKey lkey = createLineKey( megapoliPlot, ysize, graph.getLayer(), firstLine, graph );
139
140        // OTHER LAYERS
141        for( final SimpleLine otherLine : lines.subList( 1, lines.size() ) )
142            addLineInNewGraph( megapoliPlot.getAxeType(), mainPane, xsize, ysize, otherLine, xt, (LinearTransform) graph.getYTransform(), lkey );
143
144
145        /** ********************************** **/
146        /** ************ SECOND AXIS ********* **/
147        /** ********************************** **/
148        if( !linesForSecondAxis.isEmpty() )
149        {
150            final SimpleLine firstLineForSecondAxis = linesForSecondAxis.get( 0 );
151            /** ********** AXIS RANGES ********* **/
152            final SoTRange yRangeForSecondAxis = firstLineForSecondAxis.getYRange();
153            for( final SimpleLine line : linesForSecondAxis )
154                yRangeForSecondAxis.add( line.getYRange() );
155
156            final SoTRange ynRangeForSecondAxis = Graph.computeRange( yRangeForSecondAxis, _intervalsNumber );
157            final SoTPoint endPointForSecondAxis = new SoTPoint( ynRangeForSecondAxis.getStart(), xnRange.getEnd() );
158            final PlainAxis yRightAxis = createYAxis( firstLineForSecondAxis, ynRangeForSecondAxis, endPointForSecondAxis, Axis.POSITIVE_SIDE );
159
160            // GRAPH
161            final CartesianGraph graph2 = createGraphForFirstLine( megapoliPlot, mainPane, xsize, ysize, ystart, yend, xTimeAxis, xt, firstLineForSecondAxis, ynRangeForSecondAxis, yRightAxis );
162
163            // LEGEND
164            lkey.addLineGraph( (LineCartesianRenderer) graph2.getRenderer(), firstLineForSecondAxis.getKeyTitle() );
165
166            // OTHER LAYERS
167            for( final SimpleLine otherLine : linesForSecondAxis.subList( 1, linesForSecondAxis.size() ) )
168                addLineInNewGraph( megapoliPlot.getAxeType(), mainPane, xsize, ysize, otherLine, xt, (LinearTransform) graph2.getYTransform(), lkey );
169        }
170
171        // Legend for empty lines
172        displayLegendForEmptyLines( emptyLineToDisplayOnlyInLegend, lkey, mainPane, xsize, ysize, xt, (LinearTransform) graph.getYTransform(), megapoliPlot.getAxeType() );
173
174        // Copyright
175        displayCopyright( mainPane, xsize, ysize, xsize - 0.15, 0.1 );
176
177        return mainPane;
178    }
179
180    @NotNull
181    public JPane create2DPlot( @NotNull final MegapoliPlot megapoliPlot, @NotNull final Integer plotWidth, @NotNull final Integer plotHeight, @Nullable final Locale locale )
182            throws ServiceException
183    {
184        final ResourceBundle bundle = WebHelper.getBundle( locale );
185
186        final List<Double> parameterValues = (List<Double>) megapoliPlot.getData().getFirstArray();
187        final List<Double> latitudeValues = (List<Double>) megapoliPlot.getData().getSecondArray();
188        final List<Double> longitudeValues = (List<Double>) megapoliPlot.getData().getThirdArray();
189        final double[] parameterValuesArray = EtherHelper.convertListDoubleToDoubleArray( parameterValues );
190        final double[] latitudeValuesArray = EtherHelper.convertListDoubleToDoubleArray( latitudeValues );
191        final double[] longitudeValuesArray = EtherHelper.convertListDoubleToDoubleArray( longitudeValues );
192
193        // Create the grid
194        final SGTMetaData xMeta = new SGTMetaData( bundle.getString( "plot.longitude" ), bundle.getString( "plot.degres" ) );
195        final SGTMetaData yMeta = new SGTMetaData( bundle.getString( "plot.latitude" ), bundle.getString( "plot.degres" ) );
196
197        final SimpleGrid simpleGrid = new SimpleGrid( parameterValuesArray, longitudeValuesArray, latitudeValuesArray, "Grid" );
198        simpleGrid.setXMetaData( xMeta );
199        simpleGrid.setYMetaData( yMeta );
200        simpleGrid.setRealFullGrid( false );
201
202        // Create the contour levels and color map
203        final Double min = Collections.min( parameterValues );
204        final Double max = Collections.max( parameterValues );
205        final Double delta = ( max - min ) / _numberOfColorsFor2D;
206        final Range2D datar = new Range2D( min, max, delta );
207        final ContourLevels clevels = ContourLevels.getDefault( datar );
208        final ColorMap cmap = createColorMap( datar );
209
210        // Create a gridAttribute for contour data
211        final GridAttribute gridAttribute = new GridAttribute( clevels );
212        gridAttribute.setColorMap( cmap );
213        gridAttribute.setStyle( GridAttribute.RASTER );
214
215        // Create the legend
216        final Pair<Integer, Integer> pIdPId = megapoliPlot.getPfIdPIdList().get( 0 );
217        final Integer parameterId = pIdPId.getSecondValue();
218
219        final Parametre parametre = _etherService.getParameterById( parameterId );
220        if( null == parametre )
221            throw new ServiceException( ServiceException.ServiceCode.PARAMETER_IS_NULL, new Throwable( ServiceException.ServiceCode.PARAMETER_IS_NULL.toString() ) );
222        final String legend = parametre.getParametreNom() + " (" + parametre.getUnite().getUniteCode() + ")";
223
224        // Create the plot
225        final JPlotLayout jPlotLayout = new JPlotLayout( true, false, false, "JPlotLayout Pane", null, true );
226        jPlotLayout.setEditClasses( false );
227        jPlotLayout.addData( simpleGrid, gridAttribute, legend );
228        jPlotLayout.setTitles( "", "", "" );
229        jPlotLayout.setSize( new Dimension( plotWidth, plotHeight ) );
230
231        try
232        {
233//            jPlotLayout.setKeyLayerSizeP( new Dimension2D( 12.0, 24.0 ) );
234//            jPlotLayout.setKeyBoundsP( new Rectangle2D.Double( 0.01, 1.01, 5.98, 1.0 ) );
235            final Layer keyLayer = jPlotLayout.getKeyPane().getLayer( "Key Layer" );
236            jPlotLayout.add( keyLayer );
237        }
238        catch( LayerNotFoundException e )
239        {
240            throw new ServiceException( ServiceException.ServiceCode.LEGEND_LAYER_NOT_FOUND, new Throwable( ServiceException.ServiceCode.LEGEND_LAYER_NOT_FOUND.toString() ) );
241        }
242
243        // Copyright
244        displayCopyright( jPlotLayout, 1.0, 1.0, 1.15, 0.03 );
245
246        // Period
247        displayPeriod( jPlotLayout, megapoliPlot.getBeginDate(), megapoliPlot.getEndDate() );
248
249        return jPlotLayout;
250    }
251
252    /**
253     * This method creates a graph in a new layer with the neede axis and the first line to content
254     *
255     * @param megapoliPlot
256     * @param mainPane     : the general pane
257     * @param xsize
258     * @param ysize
259     * @param ystart
260     * @param yend
261     * @param xTimeAxis
262     * @param xt
263     * @param firstLine
264     * @param ynRange
265     * @param yAxis
266     * @return
267     */
268    @NotNull
269    private CartesianGraph createGraphForFirstLine( @NotNull final MegapoliPlot megapoliPlot, @NotNull final JPane mainPane, final double xsize, final double ysize, final double ystart, final double yend, @NotNull final TimeAxis xTimeAxis, @NotNull final LinearTransform xt, @NotNull final SimpleLine firstLine, @NotNull final SoTRange ynRange, @NotNull final PlainAxis yAxis )
270    {
271        final Layer layer = new Layer( "Layer", new Dimension2D( xsize, ysize ) );
272        mainPane.add( layer );
273
274        final CartesianGraph graph = new CartesianGraph( "Graph" );
275        layer.setGraph( graph );
276        graph.addYAxis( yAxis );
277
278        final LinearTransform yt = new LinearTransform( new Range2D( ystart, yend ), ynRange );
279        graph.setYTransform( yt );
280        graph.setXTransform( xt );
281
282        final LineAttribute lineAttribute = createRandomLineAttribute( megapoliPlot.getAxeType() );
283        graph.setData( firstLine, lineAttribute );
284
285        return graph;
286    }
287
288    /**
289     * This method creates a List of SimpleLine with no data.
290     * This list is used to display legend for these lines
291     *
292     * @param allLines
293     * @param allLinesForSecondAxis
294     * @param lines
295     * @return
296     */
297    private List<SimpleLine> createEmptyLinesList( @NotNull final List<SimpleLine> allLines, @NotNull final List<SimpleLine> allLinesForSecondAxis, @NotNull final List<SimpleLine> lines )
298    {
299        final List<SimpleLine> emptyLinesList = allLines;
300        emptyLinesList.removeAll( lines );
301        emptyLinesList.addAll( allLinesForSecondAxis );
302        emptyLinesList.removeAll( allLinesForSecondAxis );
303
304        return emptyLinesList;
305    }
306
307    /**
308     * This method display on the quicklook the legend for the lines with no data.
309     *
310     * @param lines
311     * @param lkey
312     * @param mainPane
313     * @param xsize
314     * @param ysize
315     * @param xt
316     * @param yt
317     * @param axeType
318     */
319    private void displayLegendForEmptyLines( @NotNull final List<SimpleLine> lines, @NotNull final LineKey lkey, @NotNull final JPane mainPane, final double xsize, final double ysize, final LinearTransform xt, final LinearTransform yt, final String axeType )
320    {
321        for( final SimpleLine line : lines )
322        {
323            final Layer otherLayer = new Layer( "Other Layer", new Dimension2D( xsize, ysize ) );
324            mainPane.add( otherLayer );
325            final CartesianGraph otherGraph = new CartesianGraph( "Other Graph", xt, yt );
326            otherLayer.setGraph( otherGraph );
327
328            final LineAttribute otherLineAttribute = createRandomLineAttribute( axeType );
329            otherGraph.setData( line, otherLineAttribute );
330            lkey.addLineGraph( (LineCartesianRenderer) otherGraph.getRenderer(), line.getKeyTitle() );
331        }
332    }
333
334    /**
335     * This method display the copyright in the bottom right corner of the plot
336     *
337     * @param jPane
338     * @param xSize
339     * @param ySize
340     * @param xPosition
341     * @param yPosition
342     */
343    private void displayCopyright( @NotNull final JPane jPane, final double xSize, final double ySize, final double xPosition, final double yPosition )
344    {
345        final Layer copyrightLayer = new Layer( "CopyrightLayer", new Dimension2D( xSize, ySize ) );
346        jPane.add( copyrightLayer );
347
348        final Calendar calendar = Calendar.getInstance();
349        final int year = calendar.get( Calendar.YEAR );
350        final String copyright = "@" + year + _copyright;
351        final SGLabel copyrightLabel = new SGLabel( "Copyright", copyright, new Point2D.Double( xPosition, yPosition ) );
352        copyrightLabel.setAlign( SGLabel.TOP, SGLabel.CENTER );
353        copyrightLabel.setFont( _copyrightFont );
354
355        copyrightLayer.addChild( copyrightLabel );
356    }
357
358    /**
359     * This method display the period's dates given by the user for a 2D plot
360     *
361     * @param jPane
362     * @param beginDate
363     * @param endDate
364     */
365    private void displayPeriod( @NotNull final JPane jPane, @Nullable final Date beginDate, @Nullable final Date endDate )
366    {
367        if( null != beginDate || null != endDate )
368        {
369            String periodStr = null != beginDate ? "[" + DateHelper.formatDate( beginDate, DateHelper.ENGLISH_DATE_PATTERN ) : "[X";
370            periodStr += null != endDate ? " - " + DateHelper.formatDate( endDate, DateHelper.ENGLISH_DATE_PATTERN ) + "] (UTC)" : " - X] (UTC)";
371
372            final Layer periodLayer = new Layer( "PeriodLayer", new Dimension2D( 1.0, 1.0 ) );
373            jPane.add( periodLayer );
374
375            final SGLabel periodLabel = new SGLabel( "Period", periodStr, new Point2D.Double( 1.04, 0.99 ) );
376            periodLabel.setAlign( SGLabel.TOP, SGLabel.CENTER );
377            periodLabel.setFont( _periodFont );
378
379            periodLayer.addChild( periodLabel );
380        }
381    }
382
383    /**
384     * This method removes from a list the lines with no data
385     *
386     * @param allLines
387     * @return
388     */
389    @NotNull
390    private List<SimpleLine> extractRealLines( @NotNull final List<SimpleLine> allLines )
391    {
392        final List<SimpleLine> lines = new ArrayList<SimpleLine>();
393        for( final SimpleLine line : allLines )
394            if( 0 < line.getYArray().length )
395                lines.add( line );
396
397        return lines;
398    }
399
400    /**
401     * This method creates a legend
402     *
403     * @param megapoliPlot
404     * @param ysize
405     * @param layer
406     * @param firstLine
407     * @param graph
408     * @return
409     */
410    @NotNull
411    private LineKey createLineKey( final MegapoliPlot megapoliPlot, final double ysize, final Layer layer, final SimpleLine firstLine, final CartesianGraph graph )
412    {
413        final LineKey lkey = new LineKey();
414        if( !megapoliPlot.isLegendToHide() )
415        {
416            lkey.setId( "Legend" );
417            lkey.setLocationP( new Point2D.Double( 0.1, ysize - 0.1 ) );
418            lkey.setBorderStyle( LineKey.NO_BORDER );
419            lkey.setVAlign( LineKey.TOP );
420            lkey.setHAlign( LineKey.LEFT );
421            layer.addChild( lkey );
422            lkey.addLineGraph( (LineCartesianRenderer) graph.getRenderer(), firstLine.getKeyTitle() );
423        }
424        return lkey;
425    }
426
427    /**
428     * This method creates a x-axis for time serie
429     *
430     * @param xnRange
431     * @param originPoint
432     * @return
433     */
434    @NotNull
435    private TimeAxis createXTimeAxis( final SoTRange xnRange, final SoTPoint originPoint )
436    {
437        final TimeAxis xbot = new TimeAxis( "X-Axis", TimeAxis.AUTO );
438        xbot.setRangeU( xnRange );
439        xbot.setLocationU( originPoint );
440        xbot.setLabelFont( _axisFont );
441        xbot.setLabelHeightP( _heightAxisFont );
442        // TimeZone indication
443        xbot.setTitle( new SGLabel( "timeZone", " (UTC)", null ) );
444//        xbot.setMajorLabelFormat( _majorLabelFormat );
445        return xbot;
446    }
447
448    /**
449     * This method creates a vertical axis, set its range in user units and its origin and creates the axis title
450     *
451     * @param line
452     * @param ynRange
453     * @param originPoint
454     * @param labelPosition
455     * @return
456     */
457    @NotNull
458    private PlainAxis createYAxis( final SimpleLine line, final SoTRange ynRange, final SoTPoint originPoint, @Nullable final Integer labelPosition )
459    {
460        final PlainAxis yAxis = new PlainAxis( "Y-Axis" );
461        yAxis.setRangeU( ynRange );
462        yAxis.setLocationU( originPoint );
463        yAxis.setLabelFont( _axisFont );
464        if( null != labelPosition )
465            yAxis.setLabelPosition( labelPosition );
466
467        final String yLabel = line.getYMetaData().getName() + " (" + line.getYMetaData().getUnits() + ")";
468        final SGLabel ytitle = new SGLabel( "Y-Axis Title", yLabel, new Point2D.Double( 0.0, 0.0 ) );
469        ytitle.setFont( _axisFont );
470        ytitle.setHeightP( _heightAxisFont );
471        yAxis.setTitle( ytitle );
472        return yAxis;
473    }
474
475    /**
476     * This method adds layers corresponding to the other lines to display
477     *
478     * @param axeType
479     * @param mainPane
480     * @param xsize
481     * @param ysize
482     * @param line
483     * @param xt
484     * @param yt
485     * @param lkey
486     */
487    private void addLineInNewGraph( @NotNull final String axeType, @NotNull final JPane mainPane, final double xsize, final double ysize, @NotNull final SimpleLine line, @NotNull final LinearTransform xt, @NotNull final LinearTransform yt, @NotNull final LineKey lkey )
488    {
489        final Layer otherLayer = new Layer( "Other Layer", new Dimension2D( xsize, ysize ) );
490        mainPane.add( otherLayer );
491        final CartesianGraph otherGraph = new CartesianGraph( "Other Graph", xt, yt );
492        otherLayer.setGraph( otherGraph );
493
494        final LineAttribute otherLineAttribute = createRandomLineAttribute( axeType );
495        otherGraph.setData( line, otherLineAttribute );
496        lkey.addLineGraph( (LineCartesianRenderer) otherGraph.getRenderer(), line.getKeyTitle() );
497    }
498
499    /**
500     * This method returns a new LineAttribute with a random color
501     *
502     * @param axeType
503     * @return
504     */
505    @NotNull
506    private LineAttribute createRandomLineAttribute( @NotNull final String axeType )
507    {
508        final LineAttribute attr;
509        final Color color;
510        if( _indexColorArray >= _colorsArray.size() )
511            color = new Color( new Random().nextInt( 256 ), new Random().nextInt( 256 ), new Random().nextInt( 256 ) );
512        else
513            color = _colorsArray.get( _indexColorArray );
514        _indexColorArray++;
515
516        if( axeType.equals( AxeTypeForFixedPlateform.TIME_POINTS.toString() ) )
517            attr = new LineAttribute( LineAttribute.MARK, 1, color );
518        else
519            attr = new LineAttribute( LineAttribute.SOLID, 1, color );
520
521        attr.setMarkHeightP( _markHeight );
522
523        return attr;
524    }
525
526    /**
527     * This method extracts from a list of <plateformId, parameterId> two lists of <plateformId, parameterId> corresponding for the 2 axes needed for the graph
528     * The second list can be empty, in this case, only one axis is needed
529     *
530     * @param pfIdPIdList
531     * @return
532     */
533    @NotNull
534    private Pair<List, List> extractListPairForAxes( @NotNull final List<Pair<Integer, Integer>> pfIdPIdList )
535    {
536        final List<Pair<Integer, Integer>> pIdPIdListForFirstAxis = new ArrayList<Pair<Integer, Integer>>();
537        final List<Pair<Integer, Integer>> pIdPIdListForSecondAxis = new ArrayList<Pair<Integer, Integer>>();
538
539        for( final Pair pIdPId : pfIdPIdList )
540        {
541            final List<Integer> secondValuesForFirstAxis = EtherHelper.getSecondValues( pIdPIdListForFirstAxis );
542            final Integer parameterId = (Integer) pIdPId.getSecondValue();
543
544            if( pIdPIdListForFirstAxis.isEmpty() || secondValuesForFirstAxis.contains( parameterId ) )
545                pIdPIdListForFirstAxis.add( pIdPId );
546            else
547                pIdPIdListForSecondAxis.add( pIdPId );
548        }
549
550        return new Pair( pIdPIdListForFirstAxis, pIdPIdListForSecondAxis );
551    }
552
553    /**
554     * This method extracts data from BD for the plateform and parameter given as parameters
555     * and creates the corresponded simpleLine
556     * Some lines can be empty if there's no data in this period by example
557     *
558     * @param pIdPIdList
559     * @param beginDate
560     * @param endDate
561     * @param locale
562     * @return
563     * @throws ServiceException
564     */
565    @NotNull
566    private List<SimpleLine> createLines( @NotNull final List<Pair> pIdPIdList, @Nullable final Date beginDate, @Nullable final Date endDate, final Integer delta, final Locale locale )
567            throws ServiceException
568    {
569        final ResourceBundle bundle = WebHelper.getBundle( locale );
570        final String messageData = bundle.getString( "plot.noData" );
571
572        final List<SimpleLine> lines = new ArrayList<SimpleLine>( pIdPIdList.size() );
573        boolean isFirstLine = true;
574
575        for( final Pair<Integer, Integer> pIdPId : pIdPIdList )
576        {
577            final Integer plateformId = pIdPId.getFirstValue();
578            final Integer parameterId = pIdPId.getSecondValue();
579
580            final Plateforme plateform = _etherService.getPlateformById( plateformId );
581//            final Plateforme plateform = BouchonHelper.getPlateformById( plateformId );
582            if( null == plateform )
583                throw new ServiceException( ServiceException.ServiceCode.PLATEFORM_IS_NULL, new Throwable( ServiceException.ServiceCode.PLATEFORM_IS_NULL.toString() ) );
584
585            final Parametre parametre = _etherService.getParameterById( parameterId );
586//            final Parametre parametre = BouchonHelper.getParameterById( parameterId );
587            if( null == parametre )
588                throw new ServiceException( ServiceException.ServiceCode.PARAMETER_IS_NULL, new Throwable( ServiceException.ServiceCode.PARAMETER_IS_NULL.toString() ) );
589
590            final Data valuesLists = _etherService.getListsByPlateformByParameterByPeriodForTimeSerie( plateformId, parameterId, beginDate, endDate );
591//            final Data valuesLists = BouchonHelper.createValuesForTimeSerie( 50, delta );
592
593            final double[] dataArray = (double[]) valuesLists.getFirstArray();
594            final Date[] dateValues = (Date[]) valuesLists.getSecondArray();
595
596            final SimpleLine line = new SimpleLine( new GeoDateArray( dateValues ), dataArray, "legend" );
597            SGTMetaData meta = new SGTMetaData( "", "", false, false );
598            line.setXMetaData( meta );
599            final SGLabel sgLabel;
600            if( 0 < dataArray.length && 0 < dateValues.length )
601                sgLabel = new SGLabel( "Line", plateform.getPlateformeNom() + "-" + parametre.getParametreNom(), new Point2D.Double( 0.0, 0.0 ) );
602            else
603                sgLabel = new SGLabel( "Line", plateform.getPlateformeNom() + "-" + parametre.getParametreNom() + " " + messageData, new Point2D.Double( 0.0, 0.0 ) );
604            line.setKeyTitle( sgLabel );
605
606            if( isFirstLine )
607                meta = new SGTMetaData( parametre.getParametreNom(), parametre.getUnite().getUniteCode(), false, false );
608            else
609                meta = new SGTMetaData( "", "", false, false );
610            line.setYMetaData( meta );
611
612            lines.add( line );
613            isFirstLine = false;
614        }
615        return lines;
616    }
617
618    @NotNull
619    private ColorMap createColorMap( final Range2D datar )
620    {
621        final int[] red =
622                {0, 0, 0, 0, 0, 0, 0, 0,
623                        0, 0, 0, 0, 0, 0, 0, 0,
624                        0, 0, 0, 0, 0, 0, 0, 0,
625                        0, 7, 23, 39, 55, 71, 87, 103,
626                        119, 135, 151, 167, 183, 199, 215, 231,
627                        247, 255, 255, 255, 255, 255, 255, 255,
628                        255, 255, 255, 255, 255, 255, 255, 255,
629                        255, 246, 228, 211, 193, 175, 158, 140};
630        final int[] green =
631                {0, 0, 0, 0, 0, 0, 0, 0,
632                        0, 11, 27, 43, 59, 75, 91, 107,
633                        123, 139, 155, 171, 187, 203, 219, 235,
634                        251, 255, 255, 255, 255, 255, 255, 255,
635                        255, 255, 255, 255, 255, 255, 255, 255,
636                        255, 247, 231, 215, 199, 183, 167, 151,
637                        135, 119, 103, 87, 71, 55, 39, 23,
638                        7, 0, 0, 0, 0, 0, 0, 0};
639        final int[] blue =
640                {0, 143, 159, 175, 191, 207, 223, 239,
641                        255, 255, 255, 255, 255, 255, 255, 255,
642                        255, 255, 255, 255, 255, 255, 255, 255,
643                        255, 247, 231, 215, 199, 183, 167, 151,
644                        135, 119, 103, 87, 71, 55, 39, 23,
645                        7, 0, 0, 0, 0, 0, 0, 0,
646                        0, 0, 0, 0, 0, 0, 0, 0,
647                        0, 0, 0, 0, 0, 0, 0, 0};
648
649        final IndexedColorMap cmap = new IndexedColorMap( red, green, blue );
650        cmap.setTransform( new LinearTransform( 0.0, (double) red.length, datar.start, datar.end ) );
651        return cmap;
652    }
653
654    public EtherService getEtherService()
655    {
656        return _etherService;
657    }
658
659    @Required
660    public void setEtherService( final EtherService etherService )
661    {
662        _etherService = etherService;
663    }
664
665    private static Integer _indexColorArray = 0;
666    private static final List<Color> _colorsArray = new ArrayList<Color>( Arrays.asList( new Color( 0, 204, 204 ), new Color( 255, 0, 0 ), new Color( 102, 102, 204 ), new Color( 51, 255, 51 ),
667            new Color( 70, 130, 180 ), new Color( 178, 34, 34 ), new Color( 148, 0, 211 ), new Color( 193, 205, 201 ),
668            new Color( 0, 139, 69 ), new Color( 255, 215, 0 ), new Color( 255, 165, 79 ), new Color( 54, 54, 54 ) ) );
669
670    // Dimensions of the jPanes
671    protected static final int MAX_WIDTH = 800;
672    protected static final int MAX_HEIGHT = 700;
673    protected static final int MAX_HEIGHT_TOP = 100;
674    protected static final int MARGIN_LEFT_RIGHT = 50;
675    protected static final int LEGEND_2D_HEIGHT = 150;
676
677    protected static final int TITLE_FONT_SIZE = 20;
678    protected static final int FONT_SIZE = 12;
679    protected static final int ERROR_FONT_SIZE = 10;
680
681    protected static final Font _axisFont = new Font( "Helvetica", Font.PLAIN, 15 );
682    protected static final Font _copyrightFont = new Font( "Helvetica", Font.PLAIN, 8 );
683    protected static final Font _periodFont = new Font( "Helvetica", Font.PLAIN, 10 );
684    protected static final String _copyright = " CNES-CNRS";
685    protected static final double _heightAxisFont = 0.14;
686    protected static final double _markHeight = 0.1;
687    protected static final String _majorLabelFormat = "yyyy-MM-dd HH:mm";
688    // Number of intervals on a Y-Axis
689    protected Integer _intervalsNumber = 10;
690    protected Integer _numberOfColorsFor2D = 64;
691
692    private EtherService _etherService;
693}
Note: See TracBrowser for help on using the repository browser.