source: ether_megapoli/trunk/service/implementation/com/ether/EtherPlotServiceImpl.java @ 178

Last change on this file since 178 was 178, checked in by vmipsl, 13 years ago

Servlet _ contour ok

File size: 39.2 KB
Line 
1package com.ether;
2
3import com.medias.megapoli.utils.MegapoliInitialisation;
4import gov.noaa.pmel.sgt.CartesianGraph;
5import gov.noaa.pmel.sgt.ColorMap;
6import gov.noaa.pmel.sgt.ContourLevels;
7import gov.noaa.pmel.sgt.Graph;
8import gov.noaa.pmel.sgt.GridAttribute;
9import gov.noaa.pmel.sgt.IndexedColorMap;
10import gov.noaa.pmel.sgt.JPane;
11import gov.noaa.pmel.sgt.Layer;
12import gov.noaa.pmel.sgt.LineAttribute;
13import gov.noaa.pmel.sgt.LineCartesianRenderer;
14import gov.noaa.pmel.sgt.LineKey;
15import gov.noaa.pmel.sgt.LinearTransform;
16import gov.noaa.pmel.sgt.PlainAxis;
17import gov.noaa.pmel.sgt.SGLabel;
18import gov.noaa.pmel.sgt.StackedLayout;
19import gov.noaa.pmel.sgt.TimeAxis;
20import gov.noaa.pmel.sgt.demo.TestData;
21import gov.noaa.pmel.sgt.dm.SGTData;
22import gov.noaa.pmel.sgt.dm.SGTMetaData;
23import gov.noaa.pmel.sgt.dm.SimpleGrid;
24import gov.noaa.pmel.sgt.dm.SimpleLine;
25import gov.noaa.pmel.sgt.swing.JPlotLayout;
26import gov.noaa.pmel.util.Dimension2D;
27import gov.noaa.pmel.util.GeoDate;
28import gov.noaa.pmel.util.GeoDateArray;
29import gov.noaa.pmel.util.IllegalTimeValue;
30import gov.noaa.pmel.util.Point2D;
31import gov.noaa.pmel.util.Range2D;
32import gov.noaa.pmel.util.SoTPoint;
33import gov.noaa.pmel.util.SoTRange;
34import gov.noaa.pmel.util.TimeRange;
35import org.apache.commons.logging.Log;
36import org.apache.commons.logging.LogFactory;
37import org.jetbrains.annotations.NotNull;
38import org.jetbrains.annotations.Nullable;
39
40import javax.swing.*;
41import javax.swing.border.EtchedBorder;
42import java.awt.*;
43import java.awt.image.BufferedImage;
44import java.util.Calendar;
45import java.util.Date;
46import java.util.Locale;
47import java.util.ResourceBundle;
48
49/**
50 * @author vmipsl
51 * @date 05 july 2011
52 */
53public class EtherPlotServiceImpl
54        implements EtherPlotService
55{
56    public static Integer getMaxWidth()
57    {
58        final Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
59        return Math.min( Double.valueOf( screenSize.getWidth() ).intValue(), MAX_WIDTH );
60    }
61
62    public static Integer getMaxHeight()
63    {
64        final Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
65        return Math.min( Double.valueOf( screenSize.getHeight() ).intValue(), MAX_HEIGHT );
66    }
67
68    /**
69     * Create the main JPane with the 3 jPanes
70     *
71     * @param megapoliPlot
72     * @param locale
73     * @return
74     */
75    @NotNull
76    public BufferedImage createMainPane( @NotNull final MegapoliPlot megapoliPlot, @Nullable final Locale locale )
77    {
78        final Integer maxWidth = getMaxWidth();
79        final Integer maxHeight = getMaxHeight();
80
81        final JPane jPane = new JPane( "Main jPane", new Dimension( maxWidth, maxHeight ) );
82        jPane.setLayout( new BorderLayout() );
83        jPane.setBackground( Color.white );
84        jPane.setOpaque( true );
85
86        // Top Pane
87        final JPane jPaneTop = createTopPane( megapoliPlot );
88        jPane.add( jPaneTop, BorderLayout.NORTH );
89
90        final Integer valuesNumber = megapoliPlot.getValuesNumber();
91
92        // Bottom Pane
93        final JPane jPaneBottom = createBottomPane( valuesNumber, locale );
94        jPane.add( jPaneBottom, BorderLayout.SOUTH );
95
96        // Center Pane
97//        final Integer centerHeight = maxHeight - jPaneTop.getHeight() - jPaneBottom.getHeight();
98//        final JPane jPaneCenter;
99//        if( valuesNumber > 0 )
100//            jPaneCenter = createCenterPane( megapoliPlot, maxWidth, centerHeight, locale );
101//        else
102//            jPaneCenter = createEmptyCenterPane( locale );
103//        jPane.add( jPaneCenter, BorderLayout.CENTER );
104
105        if( AxeTypeForFixedPlateform.TIME_LINE.name().equals( megapoliPlot.getAxeType() ) || AxeTypeForFixedPlateform.TIME_POINTS.name().equals( megapoliPlot.getAxeType() ) )
106        {
107            final JPane jPaneGraph = createTimeSeriePlot( megapoliPlot );
108            final BufferedImage bufferedImage = new BufferedImage( 600, 600, BufferedImage.TYPE_INT_BGR );
109            final Graphics2D graphics2D = bufferedImage.createGraphics();
110//        graphics2D.setBackground( Color.WHITE );
111            jPaneGraph.addNotify();
112            jPaneGraph.validate();
113            jPaneGraph.draw( graphics2D );
114
115//        final JPanelToImageManager jPanelToImageManager = new JPanelToImageManager( jPane );
116//        return jPanelToImageManager.saveAsImage();
117            return bufferedImage;
118        }
119        else
120        {
121            final JPane jPaneGraph = create2DPlot( megapoliPlot );
122            final BufferedImage bufferedImage = new BufferedImage( 600, 600, BufferedImage.TYPE_INT_BGR );
123            final Graphics2D graphics2D = bufferedImage.createGraphics();
124//        graphics2D.setBackground( Color.WHITE );
125            jPaneGraph.addNotify();
126            jPaneGraph.validate();
127            jPaneGraph.draw( graphics2D );
128
129//        final JPanelToImageManager jPanelToImageManager = new JPanelToImageManager( jPane );
130//        return jPanelToImageManager.saveAsImage();
131            return bufferedImage;
132        }
133    }
134
135    /**
136     * Create the top JPane with the logos and title
137     *
138     * @param megapoliPlot
139     * @return
140     */
141    @NotNull
142    public JPane createTopPane( @Nullable final MegapoliPlot megapoliPlot )
143    {
144        // Logos
145        final ImageIcon logoMegapoli = new ImageIcon( megapoliPlot.getLogoMegapoli() );
146        final JLabel jLabelLogoMegapoli = new JLabel( logoMegapoli );
147
148        final ImageIcon logoEther = new ImageIcon( megapoliPlot.getLogoEther() );
149        final JLabel jLabelLogoEther = new JLabel( logoEther );
150
151        // Max height
152        final Integer maxHeights = Math.max( logoMegapoli.getIconHeight(), logoEther.getIconHeight() );
153
154        // jPane
155        final JPane jPaneTop = new JPane( "Top jPane", new Dimension( 0, maxHeights ) );
156        jPaneTop.setLayout( new BorderLayout() );
157
158        jPaneTop.add( jLabelLogoMegapoli, BorderLayout.LINE_START );
159        jPaneTop.add( jLabelLogoEther, BorderLayout.LINE_END );
160
161
162        // Title
163        final String formatedTitle = formatTitle( megapoliPlot.getTitle() );
164        final JLabel jLabelTitle = new JLabel( formatedTitle, JLabel.CENTER );
165        final Font police = new Font( "Arial", Font.BOLD, TITLE_FONT_SIZE );
166        jLabelTitle.setFont( police );
167        jPaneTop.add( jLabelTitle, BorderLayout.CENTER );
168
169        return jPaneTop;
170    }
171
172    /**
173     * Create the center JPane with the graph
174     *
175     * @param megapoliPlot
176     * @param maxWidth
177     * @param maxHeight
178     * @return
179     */
180    @NotNull
181    public JPane createCenterPaneOld( @NotNull final MegapoliPlot megapoliPlot, @NotNull final Integer maxWidth, @NotNull final Integer maxHeight, @Nullable final Locale locale )
182    {
183        final Integer plotWidth = Math.min( maxWidth - MARGIN_LEFT_RIGHT, maxWidth );
184        final Integer plotHeight = Math.min( maxHeight - MARGIN_LEFT_RIGHT, maxHeight );
185
186        final JPlotLayout jPlotLayout;
187        final BufferedImage bufferedImage;
188        if( megapoliPlot.isTimeSerie() )
189        {
190            jPlotLayout = createJPlotLayoutForTimeSerie( megapoliPlot, plotWidth, plotHeight );
191
192            // TODO : remove this change to bufferedImage to insert directly the jPlotLayout !!!!
193            bufferedImage = new BufferedImage( jPlotLayout.getWidth(), jPlotLayout.getHeight(), BufferedImage.TYPE_INT_RGB );
194            final Graphics2D graphics2D = bufferedImage.createGraphics();
195            jPlotLayout.draw( graphics2D );
196        }
197        else
198        {
199            jPlotLayout = createJPlotLayoutFor2D( megapoliPlot, plotWidth, plotHeight, locale );
200
201            final JPanelToImageManager jPanelToImageManager = new JPanelToImageManager( jPlotLayout );
202            bufferedImage = jPanelToImageManager.saveAsImage();
203        }
204
205        final ImageIcon imageIcon = new ImageIcon( bufferedImage );
206        final JLabel jLabelPlot = new JLabel( imageIcon, JLabel.CENTER );
207        jLabelPlot.setBorder( BorderFactory.createLineBorder( Color.black, 1 ) );
208
209        final JPane jPaneCenter = new JPane( "Center jPane", new Dimension( jPlotLayout.getWidth(), jPlotLayout.getHeight() ) );
210        jPaneCenter.setLayout( new GridBagLayout() );
211        jPaneCenter.add( jLabelPlot,
212                new GridBagConstraints( 0, 0, 1, 1, 0, 0,
213                        GridBagConstraints.CENTER,
214                        GridBagConstraints.CENTER,
215                        new Insets( 0, 0, 0, 0 ), 0, 0 ) );
216
217        return jPaneCenter;
218    }
219
220    /**
221     * Create the center JPane with the graph
222     *
223     * @param megapoliPlot
224     * @param maxWidth
225     * @param maxHeight
226     * @return
227     */
228    @NotNull
229    public JPane createCenterPane( @NotNull final MegapoliPlot megapoliPlot, @NotNull final Integer maxWidth, @NotNull final Integer maxHeight, @Nullable final Locale locale )
230    {
231
232        final JPane jPaneTimeSeriePlot = createTimeSeriePlot( megapoliPlot );
233
234        final Integer plotWidth = Math.min( maxWidth - MARGIN_LEFT_RIGHT, maxWidth );
235        final Integer plotHeight = Math.min( maxHeight - MARGIN_LEFT_RIGHT, maxHeight );
236
237//        final JPlotLayout jPlotLayout;
238//        final BufferedImage bufferedImage;
239//        if( megapoliPlot.isTimeSerie() )
240//        {
241//            jPlotLayout = createJPlotLayoutForTimeSerie( megapoliPlot, plotWidth, plotHeight );
242//
243//            // TODO : remove this change to bufferedImage to insert directly the jPlotLayout !!!!
244//            bufferedImage = new BufferedImage( jPlotLayout.getWidth(), jPlotLayout.getHeight(), BufferedImage.TYPE_INT_RGB );
245//            final Graphics2D graphics2D = bufferedImage.createGraphics();
246//            jPlotLayout.draw( graphics2D );
247//        }
248//        else
249//        {
250//            jPlotLayout = createJPlotLayoutFor2D( megapoliPlot, plotWidth, plotHeight, locale );
251//
252//            final JPanelToImageManager jPanelToImageManager = new JPanelToImageManager( jPlotLayout );
253//            bufferedImage = jPanelToImageManager.saveAsImage();
254//        }
255//
256//        final ImageIcon imageIcon = new ImageIcon( bufferedImage );
257//        final JLabel jLabelPlot = new JLabel( imageIcon, JLabel.CENTER );
258//        jLabelPlot.setBorder( BorderFactory.createLineBorder( Color.black, 1 ) );
259//
260//        final JPane jPaneCenter = new JPane( "Center jPane", new Dimension( jPlotLayout.getWidth(), jPlotLayout.getHeight() ) );
261//        jPaneCenter.setLayout( new GridBagLayout() );
262//        jPaneCenter.add( jLabelPlot,
263//                new GridBagConstraints( 0, 0, 1, 1, 0, 0,
264//                        GridBagConstraints.CENTER,
265//                        GridBagConstraints.CENTER,
266//                        new Insets( 0, 0, 0, 0 ), 0, 0 ) );
267//
268//        return jPaneCenter;
269        return jPaneTimeSeriePlot;
270    }
271
272    /**
273     * Create the bottom JPane with the text "published .. (date)"
274     *
275     * @param dataNumber : the number of datas extracted from the base
276     * @param locale
277     * @return
278     */
279    @NotNull
280    public JPane createBottomPane( @Nullable final Integer dataNumber, @Nullable final Locale locale )
281    {
282        final ResourceBundle bundle = WebHelper.getBundle( locale );
283
284        final JPane jPaneBottom = new JPane();
285        jPaneBottom.setLayout( new BorderLayout() );
286
287        // Published
288        final Calendar calendar = Calendar.getInstance();
289        final String messagePublished = bundle.getString( "plot.published" );
290        final JLabel jLabel = new JLabel( messagePublished + ' ' + DateHelper.formatDate( calendar.getTime(), DateHelper.ENGLISH_DATE_PATTERN ) + " " );
291        final Font police = new Font( "Arial", Font.BOLD, FONT_SIZE );
292        jLabel.setFont( police );
293        jPaneBottom.add( jLabel, BorderLayout.EAST );
294
295        if( null != dataNumber )
296        {
297            final String messageDataNumber = bundle.getString( "plot.dataNumber" );
298            final JLabel jLabelDataNumber = new JLabel( messageDataNumber + ' ' + dataNumber );
299            jLabelDataNumber.setFont( police );
300            jPaneBottom.add( jLabelDataNumber, BorderLayout.WEST );
301        }
302
303        return jPaneBottom;
304    }
305
306    /**
307     * Create an error JPane when plot can be created
308     *
309     * @param mainError
310     * @param errorText
311     * @param locale
312     * @return
313     */
314    @NotNull
315    public BufferedImage createErrorPane( @NotNull final String mainError, @Nullable final String errorText, @Nullable final Locale locale )
316    {
317        // Image
318        final ImageIcon errorImage = new ImageIcon( MegapoliInitialisation.pathImages + "/erreur.gif" );
319        final JLabel jLabelErrorImage = new JLabel( errorImage );
320
321        // Text
322        final ResourceBundle bundle = WebHelper.getBundle( locale );
323        final String mainMessage = bundle.getString( "plot.errorMessage" );
324
325        final JLabel jLabelError;
326        if( null != errorText )
327            jLabelError = new JLabel( "<html>" + mainMessage + "<br><br>" + mainError + "<br>" + errorText + "</html>", JLabel.CENTER );
328        else
329            jLabelError = new JLabel( "<html>" + mainMessage + "<br><br>" + mainError + "</html>", JLabel.CENTER );
330
331        final Font police = new Font( "Arial", Font.BOLD, ERROR_FONT_SIZE );
332        jLabelError.setFont( police );
333
334        // jPane
335        final JPane jPane = new JPane( "jPane", new Dimension( MAX_WIDTH, errorImage.getIconHeight() ) );
336        jPane.setLayout( new BorderLayout() );
337        jPane.setBackground( Color.white );
338        jPane.setOpaque( true );
339        jPane.add( jLabelErrorImage, BorderLayout.WEST );
340        jPane.add( jLabelError, BorderLayout.CENTER );
341
342        final JPanelToImageManager jPanelToImageManager = new JPanelToImageManager( jPane );
343        return jPanelToImageManager.saveAsImage();
344    }
345
346    @NotNull
347    public JPanel createControlPanel()
348    {
349        final JPanel controlPanel = new JPanel();
350        final ButtonGroup gridtype = new ButtonGroup();
351        final JCheckBox stacked = new JCheckBox();
352        final JCheckBox grid = new JCheckBox();
353
354        controlPanel.setLayout( new GridBagLayout() );
355//    controlPanel.setBackground(new java.awt.Color(200,200,200));
356        controlPanel.setBounds( 0, 679, 426, 33 );
357        controlPanel.setBorder( new EtchedBorder() );
358        gridtype.add( stacked );
359        stacked.setSelected( true );
360        stacked.setText( "Overlayed" );
361
362        GridBagConstraints gbc = new GridBagConstraints();
363        gbc.gridx = 0;
364        gbc.gridy = 0;
365        gbc.gridwidth = 1;
366        gbc.gridheight = 1;
367        gbc.weightx = 0.0;
368        gbc.weighty = 1.0;
369        gbc.anchor = GridBagConstraints.EAST;
370        gbc.fill = GridBagConstraints.BOTH;
371        gbc.insets = new Insets( 5, 15, 5, 5 );
372        gbc.ipadx = 0;
373        gbc.ipady = 0;
374        controlPanel.add( stacked, gbc );
375        stacked.setBounds( 15, 5, 84, 23 );
376        gridtype.add( grid );
377        grid.setText( "Grid" );
378
379        gbc.gridx = 1;
380        gbc.gridy = 0;
381        gbc.gridwidth = 1;
382        gbc.gridheight = 1;
383        gbc.weightx = 0.5;
384        gbc.weighty = 1.0;
385        gbc.anchor = GridBagConstraints.WEST;
386        gbc.fill = GridBagConstraints.VERTICAL;
387        gbc.insets = new Insets( 5, 5, 5, 0 );
388        gbc.ipadx = 0;
389        gbc.ipady = 0;
390        controlPanel.add( grid, gbc );
391        grid.setBounds( 109, 5, 53, 23 );
392        final JButton showTree = new JButton();
393        showTree.setText( "Show Class Tree" );
394
395        gbc.gridx = 2;
396        gbc.gridy = 0;
397        gbc.gridwidth = 1;
398        gbc.gridheight = 1;
399        gbc.weightx = 1.0;
400        gbc.weighty = 1.0;
401        gbc.anchor = GridBagConstraints.EAST;
402        gbc.fill = GridBagConstraints.VERTICAL;
403        gbc.insets = new Insets( 5, 5, 5, 15 );
404        gbc.ipadx = 0;
405        gbc.ipady = 0;
406        controlPanel.add( showTree, gbc );
407        showTree.setBackground( Color.yellow.brighter() );
408        showTree.setBounds( 302, 5, 109, 23 );
409
410        final SymItem lSymItem = new SymItem( stacked, grid );
411        stacked.addItemListener( lSymItem );
412        grid.addItemListener( lSymItem );
413//        SymAction lSymAction = new SymAction();
414//        showTree.addActionListener( lSymAction );
415
416        return controlPanel;
417    }
418
419    /**
420     * First layer contains axes, labels and the first set of data
421     * Others layers contains only the other sets of data
422     *
423     * @param megapoliPlot
424     * @return
425     */
426    @NotNull
427    public JPane createTimeSeriePlot( @NotNull final MegapoliPlot megapoliPlot )
428    {
429        final JPane mainPane = new JPane( "zz", new Dimension( 600, 600 ) );
430        mainPane.setLayout( new StackedLayout() );
431        mainPane.setBorder( BorderFactory.createLineBorder( Color.pink ) );
432
433//        // Graph
434////        final JPlotLayoutEther jPlotLayout = new JPlotLayoutEther( false, true, false, "Trajectory data", null, megapoliPlot.isLegendToHide() );
435//        final JPlotLayout jPlotLayout = new JPlotLayout( data, "Profile Demo", null, false );
436//        jPlotLayout.setTitles( "", "", "" );
437
438        /** ***************************** **/
439        /** *********** LINES ************ **/
440        /** ***************************** **/
441        final double[] dataArray = (double[]) megapoliPlot.getData().getFirstArray();
442        final Date[] dateValues = (Date[]) megapoliPlot.getData().getSecondArray();
443
444        final SimpleLine line0 = new SimpleLine( new GeoDateArray( dateValues ), dataArray, "legend" );
445        SGTMetaData meta = new SGTMetaData( "", "", false, false );
446        line0.setXMetaData( meta );
447
448        meta = new SGTMetaData( megapoliPlot.getParameterName(), megapoliPlot.getParameterUnitCode(), false, false );
449        line0.setYMetaData( meta );
450
451
452        SimpleLine line;
453        GeoDate start = null;
454        GeoDate stop = null;
455        TimeRange tr;
456        TestData td;
457        try
458        {
459            start = new GeoDate( "1992-11-01", "yyyy-MM-dd" );
460            stop = new GeoDate( "1993-02-20", "yyyy-MM-dd" );
461        }
462        catch( IllegalTimeValue e )
463        {
464        }
465        tr = new TimeRange( start, stop );
466        td = new TestData( TestData.TIME_SERIES, tr, 1.0f,
467                TestData.RANDOM, 1.2f, 0.5f, 30.0f );
468        line = (SimpleLine) td.getSGTData();
469
470
471        /** ***************************** **/
472        /** *********** AXIS ************ **/
473        /** ***************************** **/
474        final SoTRange ynRange;
475        final SoTRange yRange;
476        final SoTRange tnRange;
477        final String yLabel;
478        yRange = line.getYRange();
479//        yRange.add( line2.getYRange() );
480        tnRange = line.getXRange();
481        final Integer intervalsNumber = 10;
482        ynRange = Graph.computeRange( yRange, intervalsNumber );
483        yLabel = line.getYMetaData().getName();
484
485
486        /** ***************************** **/
487        /** *********** LAYERS ********** **/
488        /** ***************************** **/
489        /*
490        * xsize, ysize are the width and height in physical units
491        * of the Layer graphics region.
492        *
493        * xstart, xend are the start and end points for the TimeAxis
494        * ystart, yend are the start and end points for the Y axis
495        */
496        final double xsize = 4.0;
497        final double xstart = 0.6;
498        final double xend = 3.25;
499        final double ysize = 4.0;
500        final double ystart = 0.6;
501        final double yend = 2.50;
502
503        Layer layer2;
504        SGLabel label, title, ytitle;
505        CartesianGraph graph, graph2;
506        LinearTransform xt, yt;
507        PlainAxis yleft;
508        TimeAxis xbot;
509        LineKey lkey;
510        GeoDate stime;
511
512        final Layer layer = new Layer( "First Layer", new Dimension2D( xsize, ysize ) );
513
514        /** ***************************** **/
515        /** *********** LEGENDS ********* **/
516        /** ***************************** **/
517        /*
518        * create a LineKey
519        * the LineKey will be a legend for the two lines created
520        * position the key in the upper right corner
521        * and add to the first layer
522        */
523        lkey = new LineKey();
524        lkey.setId( "Legend" );
525        lkey.setLocationP( new Point2D.Double( xsize - 1, ysize - 1 ) );
526        lkey.setVAlign( LineKey.TOP );
527        lkey.setHAlign( LineKey.RIGHT );
528        layer.addChild( lkey );
529        /*
530        * add the first layer to the Pane
531        */
532        mainPane.add( layer );
533        /*
534        * create first CartesianGraph and transforms
535        */
536        graph = new CartesianGraph( "First Graph" );
537        xt = new LinearTransform( new Range2D( xstart, xend ), tnRange );
538        graph.setXTransform( xt );
539        yt = new LinearTransform( new Range2D( ystart, yend ), ynRange );
540        graph.setYTransform( yt );
541        /*
542        * Create the time axis, set its range in user units
543        * and its origin. Add the axis to the first graph.
544        */
545        SoTPoint point = new SoTPoint( ynRange.getStart(), tnRange.getStart() );
546        xbot = new TimeAxis( "Bottom Axis", TimeAxis.MONTH_YEAR );
547        xbot.setRangeU( tnRange );
548        xbot.setLocationU( point );
549        Font xbfont = new Font( "Helvetica", Font.ITALIC, 14 );
550        xbot.setLabelFont( xbfont );
551        xbot.setMinorLabelInterval( 1 );
552        graph.addXAxis( xbot );
553        /*
554        * Create the vertical axis, set its range in user units
555        * and its origin.  Create the axis title and add the
556        * axis to the first graph.
557        */
558        yleft = new PlainAxis( "Left Axis" );
559        yleft.setRangeU( ynRange );
560        yleft.setLocationU( point );
561        yleft.setLabelFont( xbfont );
562        ytitle = new SGLabel( "Y-Axis Title", yLabel,
563                new Point2D.Double( 0.0, 0.0 ) );
564        Font ytfont = new Font( "Helvetica", Font.PLAIN, 14 );
565        ytitle.setFont( ytfont );
566        ytitle.setHeightP( 0.2 );
567        yleft.setTitle( ytitle );
568        graph.addYAxis( yleft );
569        /*
570        * Add the first graph to the first layer.
571        */
572        layer.setGraph( graph );
573        /*
574        * Create a LineAttribute for the display of the first
575        * line. Associate the attribute and the line with the
576        * first graph.  Add the line to the LineKey.
577        */
578        LineAttribute attr;
579
580        attr = new LineAttribute( LineAttribute.MARK, 20, Color.red );
581        attr.setMarkHeightP( 0.1 );
582        graph.setData( line, attr );
583        lkey.addLineGraph( (LineCartesianRenderer) graph.getRenderer(),
584                new SGLabel( "1st line", "Red Data",
585                        new Point2D.Double( 0.0, 0.0 ) ) );
586        /*
587        * Create the second layer and add it the the Pane.
588        * Create the second graph and associate it with the
589        * second layer.
590        */
591//        layer2 = new Layer( "Second Layer", new Dimension2D( xsize, ysize ) );
592//        mainPane.add( layer2 );
593//        graph2 = new CartesianGraph( "Second Graph", xt, yt );
594//        layer2.setGraph( graph2 );
595        /*
596        * Create a LineAttribute for the display of the second
597        * line. Associate the attribute and the line with the
598        * second graph.  Add the line to the LineKey.
599        */
600//        LineAttribute attr2;
601//        attr2 = new LineAttribute( LineAttribute.MARK, 2, Color.blue );
602//        attr2.setMarkHeightP( 0.1 );
603//        graph2.setData( line2, attr2 );
604//        lkey.addLineGraph( (LineCartesianRenderer) graph2.getRenderer(),
605//                new SGLabel( "2nd line", "Blue Data",
606//                        new Point2D.Double( 0.0, 0.0 ) ) );
607
608        return mainPane;
609    }
610
611    public JPane create2DPlot( @NotNull final MegapoliPlot megapoliPlot )
612    {
613        /*
614       * This example uses a pre-created "Layout" for raster time
615       * series to simplify the construction of a plot. The
616       * JPlotLayout can plot a single grid with
617       * a ColorKey, time series with a LineKey, point collection with a
618       * PointCollectionKey, and general X-Y plots with a
619       * LineKey. JPlotLayout supports zooming, object selection, and
620       * object editing.
621        */
622        SGTData newData;
623        TestData td;
624        JPlotLayout rpl;
625        ContourLevels clevels;
626        /*
627       * Create a test grid with sinasoidal-ramp data.
628        */
629        Range2D xr = new Range2D( 190.0f, 250.0f, 1.0f );
630        Range2D yr = new Range2D( 0.0f, 45.0f, 1.0f );
631        td = new TestData( TestData.XY_GRID, xr, yr,
632                TestData.SINE_RAMP, 12.0f, 30.f, 5.0f );
633        newData = td.getSGTData();
634        /*
635       * Create the layout without a Logo image and with the
636       * ColorKey on a separate Pane object.
637        */
638        rpl = new JPlotLayout( true, false, false, "JGridDemo Pane", null, true );
639        rpl.setEditClasses( false );
640        /*
641       * Create a GridAttribute for CONTOUR style.
642        */
643        Range2D datar = new Range2D( -20.0f, 45.0f, 5.0f );
644        clevels = ContourLevels.getDefault( datar );
645        GridAttribute gridAttr_ = new GridAttribute( clevels );
646        /*
647       * Create a ColorMap and change the style to RASTER_CONTOUR.
648        */
649        ColorMap cmap = createColorMap( datar );
650        gridAttr_.setColorMap( cmap );
651        gridAttr_.setStyle( GridAttribute.RASTER_CONTOUR );
652        /*
653       * Add the grid to the layout and give a label for
654       * the ColorKey.
655        */
656        rpl.addData( newData, gridAttr_, "First Data" );
657        /*
658       * Change the layout's three title lines.
659        */
660        rpl.setTitles( "Raster Plot Demo",
661                "using a JPlotLayout",
662                "" );
663        /*
664       * Resize the graph  and place in the "Center" of the frame.
665        */
666        rpl.setSize( new Dimension( 600, 400 ) );
667        /*
668       * Resize the key Pane, both the device size and the physical
669       * size. Set the size of the key in physical units and place
670       * the key pane at the "South" of the frame.
671        */
672//    rpl.setKeyLayerSizeP(new Dimension2D(6.0, 1.02));
673//    rpl.setKeyBoundsP(new Rectangle2D.Double(0.01, 1.01, 5.98, 1.0));
674
675        return rpl;
676    }
677
678    /**
679     * Create the center JPane with no graph (if no value)
680     *
681     * @param locale
682     * @return
683     */
684    public JPane createEmptyCenterPane( @Nullable final Locale locale )
685    {
686        final ResourceBundle bundle = WebHelper.getBundle( locale );
687        final String messageData = bundle.getString( "plot.noData" );
688
689        final JPane jPaneCenter = new JPane();
690        jPaneCenter.setLayout( new BorderLayout() );
691
692        final JLabel jLabelData = new JLabel( messageData, JLabel.CENTER );
693        final Font police = new Font( "Arial", Font.BOLD, FONT_SIZE );
694        jLabelData.setFont( police );
695
696        jPaneCenter.add( jLabelData, BorderLayout.CENTER );
697        return jPaneCenter;
698    }
699
700    @NotNull
701    private JPlotLayout createJPlotLayoutForTimeSerie( final MegapoliPlot megapoliPlot, final Integer plotWidth, final Integer plotHeight )
702    {
703        final double[] dataArray = (double[]) megapoliPlot.getData().getFirstArray();
704        final Date[] dateValues = (Date[]) megapoliPlot.getData().getSecondArray();
705
706        final SimpleLine data = new SimpleLine( new GeoDateArray( dateValues ), dataArray, "legend" );
707        SGTMetaData meta = new SGTMetaData( null, null, false, false );
708        data.setXMetaData( meta );
709
710        meta = new SGTMetaData( megapoliPlot.getParameterName(), megapoliPlot.getParameterUnitCode(), false, false );
711        data.setYMetaData( meta );
712
713        // Graph
714//        final JPlotLayoutEther jPlotLayout = new JPlotLayoutEther( false, true, false, "Trajectory data", null, megapoliPlot.isLegendToHide() );
715        final JPlotLayout jPlotLayout = new JPlotLayout( data, "Profile Demo", null, false );
716        jPlotLayout.setTitles( "", "", "" );
717
718        // Replace legend
719//        final String legend = megapoliPlot.getData().getTitle();
720//        final Dimension2D layerSizeP = jPlotLayout.getLayerSizeP();
721//        final double factorForPlotWidthInPhysicalUnits = layerSizeP.getWidth() / Double.valueOf( plotWidth );
722//        final double legendSizeInPhysicalUnits = legend.length() * factorForPlotWidthInPhysicalUnits * 7;
723//        jPlotLayout.setKeyLocationP( new Point2D.Double( layerSizeP.getWidth() - legendSizeInPhysicalUnits, layerSizeP.getHeight() - 0.1 ) );
724
725        // Add data and legend
726        jPlotLayout.addData( data, "" );
727        jPlotLayout.setSize( plotWidth, plotHeight );
728        jPlotLayout.setVisible( true );
729
730        jPlotLayout.addNotify();
731        jPlotLayout.validate();
732        jPlotLayout.setOpaque( true );
733        return jPlotLayout;
734    }
735
736    @NotNull
737    private JPlotLayout createJPlotLayoutFor2D( final MegapoliPlot megapoliPlot, final Integer plotWidth, final Integer plotHeight, @Nullable final Locale locale )
738    {
739        SGTData newData;
740        TestData td;
741        JPlotLayout rpl;
742        ContourLevels clevels;
743        /*
744       * Create a test grid with sinasoidal-ramp data.
745        */
746        Range2D xr = new Range2D( 190.0f, 250.0f, 1.0f );
747        Range2D yr = new Range2D( 0.0f, 45.0f, 1.0f );
748        td = new TestData( TestData.XY_GRID, xr, yr,
749                TestData.SINE_RAMP, 12.0f, 30.f, 5.0f );
750        newData = td.getSGTData();
751        /*
752       * Create the layout without a Logo image and with the
753       * ColorKey on a separate Pane object.
754        */
755        rpl = new JPlotLayout( true, false, false, "JGridDemo Pane", null, true );
756        rpl.setEditClasses( false );
757        /*
758       * Create a GridAttribute for CONTOUR style.
759        */
760        Range2D datar = new Range2D( -20.0f, 45.0f, 5.0f );
761        clevels = ContourLevels.getDefault( datar );
762        final GridAttribute gridAttr_ = new GridAttribute( clevels );
763        /*
764       * Create a ColorMap and change the style to RASTER_CONTOUR.
765        */
766        ColorMap cmap = createColorMap( datar );
767        gridAttr_.setColorMap( cmap );
768        gridAttr_.setStyle( GridAttribute.RASTER_CONTOUR );
769        /*
770       * Add the grid to the layout and give a label for
771       * the ColorKey.
772        */
773        rpl.addData( newData, gridAttr_, "First Data" );
774        /*
775       * Change the layout's three title lines.
776        */
777        rpl.setTitles( "Raster Plot Demo", "using a JPlotLayout", "" );
778        /*
779       * Resize the graph  and place in the "Center" of the frame.
780        */
781        rpl.setSize( new Dimension( 600, 400 ) );
782        /*
783       * Resize the key Pane, both the device size and the physical
784       * size. Set the size of the key in physical units and place
785       * the key pane at the "South" of the frame.
786        */
787//    rpl.setKeyLayerSizeP(new Dimension2D(6.0, 1.02));
788//    rpl.setKeyBoundsP(new Rectangle2D.Double(0.01, 1.01, 5.98, 1.0));
789
790        return rpl;
791    }
792
793    @NotNull
794    private JPlotLayout createJPlotLayoutFor2DOrig( final MegapoliPlot megapoliPlot, final Integer plotWidth, final Integer plotHeight, @Nullable final Locale locale )
795    {
796        final ResourceBundle bundle = WebHelper.getBundle( locale );
797
798        final double[] parameterArray = (double[]) megapoliPlot.getData().getFirstArray();
799        final double[] latitudeArray = (double[]) megapoliPlot.getData().getSecondArray();
800        final double[] longitudeValues = (double[]) megapoliPlot.getData().getThirdArray();
801
802//        final SimpleLine data = new SimpleLine( longitudeValues, latitudeArray, "legend" );
803        final SimpleGrid data = new SimpleGrid( parameterArray, longitudeValues, latitudeArray, "Test Series" );
804        SGTMetaData meta = new SGTMetaData( bundle.getString( "plot.longitude" ), bundle.getString( "plot.degres" ), false, true );
805        // TODO : vérifier modulo = 360
806//        meta.setModuloValue( 360 );
807        data.setXMetaData( meta );
808
809        meta = new SGTMetaData( bundle.getString( "plot.latitude" ), bundle.getString( "plot.degres" ), false, true );
810        // TODO : vérifier modulo = 360
811//        meta.setModuloValue( 360 );
812        data.setYMetaData( meta );
813
814        // Graph
815        final JPlotLayout jPlotLayout = new JPlotLayout( true, false, false, "JGridDemo Pane", null, true );
816        jPlotLayout.setEditClasses( false );
817        /*
818       * Create a GridAttribute for CONTOUR style.
819        */
820        final Range2D datar = new Range2D( -20.0f, 45.0f, 5.0f );
821        final ContourLevels clevels = ContourLevels.getDefault( datar );
822        final GridAttribute gridAttr = new GridAttribute( clevels );
823        /*
824       * Create a ColorMap and change the style to RASTER_CONTOUR.
825        */
826        final ColorMap cmap = createColorMap( datar );
827        gridAttr.setColorMap( cmap );
828        gridAttr.setStyle( GridAttribute.RASTER_CONTOUR );
829        /*
830       * Add the grid to the layout and give a label for
831       * the ColorKey.
832        */
833        jPlotLayout.addData( data, gridAttr, "First Data" );
834
835//        final JPlotLayoutEther jPlotLayout = new JPlotLayoutEther( false, megapoliPlot.isXTime(), false, "Trajectory data", null, megapoliPlot.isLegendToHide() );
836//        jPlotLayout.setTitles( "", "", "" );
837
838        // Replace legend
839//        final String legend = megapoliPlot.getData().getTitle();
840//        final Dimension2D layerSizeP = jPlotLayout.getLayerSizeP();
841//        final double factorForPlotWidthInPhysicalUnits = layerSizeP.getWidth() / Double.valueOf( plotWidth );
842//        final double legendSizeInPhysicalUnits = legend.length() * factorForPlotWidthInPhysicalUnits * 7;
843//        jPlotLayout.setKeyLocationP( new Point2D.Double( layerSizeP.getWidth() - legendSizeInPhysicalUnits, layerSizeP.getHeight() - 0.1 ) );
844
845        // Add data and legend
846//        jPlotLayout.addData( megapoliPlot.getData(), legend );
847        jPlotLayout.setSize( plotWidth, plotHeight );
848        jPlotLayout.setVisible( true );
849
850        jPlotLayout.addNotify();
851        jPlotLayout.validate();
852        jPlotLayout.setOpaque( true );
853        return jPlotLayout;
854    }
855
856    @NotNull
857    private String formatTitle( @Nullable final String title )
858    {
859        if( null == title )
860            return "";
861        else
862        {
863            final String formatTitle = title.replaceAll( "\\n", "<br>" );
864            return "<html>" + formatTitle + "</html>";
865        }
866    }
867
868    @NotNull
869    private ColorMap createColorMap( final Range2D datar )
870    {
871        final int[] red =
872                {0, 0, 0, 0, 0, 0, 0, 0,
873                        0, 0, 0, 0, 0, 0, 0, 0,
874                        0, 0, 0, 0, 0, 0, 0, 0,
875                        0, 7, 23, 39, 55, 71, 87, 103,
876                        119, 135, 151, 167, 183, 199, 215, 231,
877                        247, 255, 255, 255, 255, 255, 255, 255,
878                        255, 255, 255, 255, 255, 255, 255, 255,
879                        255, 246, 228, 211, 193, 175, 158, 140};
880        final int[] green =
881                {0, 0, 0, 0, 0, 0, 0, 0,
882                        0, 11, 27, 43, 59, 75, 91, 107,
883                        123, 139, 155, 171, 187, 203, 219, 235,
884                        251, 255, 255, 255, 255, 255, 255, 255,
885                        255, 255, 255, 255, 255, 255, 255, 255,
886                        255, 247, 231, 215, 199, 183, 167, 151,
887                        135, 119, 103, 87, 71, 55, 39, 23,
888                        7, 0, 0, 0, 0, 0, 0, 0};
889        final int[] blue =
890                {0, 143, 159, 175, 191, 207, 223, 239,
891                        255, 255, 255, 255, 255, 255, 255, 255,
892                        255, 255, 255, 255, 255, 255, 255, 255,
893                        255, 247, 231, 215, 199, 183, 167, 151,
894                        135, 119, 103, 87, 71, 55, 39, 23,
895                        7, 0, 0, 0, 0, 0, 0, 0,
896                        0, 0, 0, 0, 0, 0, 0, 0,
897                        0, 0, 0, 0, 0, 0, 0, 0};
898
899        final IndexedColorMap cmap = new IndexedColorMap( red, green, blue );
900        cmap.setTransform( new LinearTransform( 0.0, (double) red.length, datar.start, datar.end ) );
901        return cmap;
902    }
903
904    /**
905     * Change the Pane layout to StackedLayout.
906     *
907     * @param event
908     */
909
910    void stacked_itemStateChanged( java.awt.event.ItemEvent event )
911    {
912        /*
913        * Get the component list for mainPane_ and change
914        * the layout to StackedLayout.
915        */
916//        Component[] comps = mainPane_.getComponents();
917//        mainPane_.setBatch( true );
918//        mainPane_.setLayout( new StackedLayout() );
919//        /*
920//        * Remove any axes that have been associated with
921//        * the second graph.  With the layers overlayed it
922//        * is not necessary to have duplicate axes.
923//        */
924//        Graph gr2 = ( (Layer) comps[1] ).getGraph();
925//        ( (CartesianGraph) gr2 ).removeAllXAxes();
926//        ( (CartesianGraph) gr2 ).removeAllYAxes();
927//        /*
928//        * Tell the Applet that the mainPane_ needs to
929//        * be layed out and re-draw the mainPane_.
930//        */
931//        if( isApplet_ )
932//        {
933//            validate();
934//        }
935//        else
936//        {
937//            frame.validate();
938//        }
939//        mainPane_.setBatch( false );
940//        if( tree_ != null )
941//        {
942//            if( tree_.isVisible() )
943//            {
944//                tree_.setJPane( mainPane_ );
945//                tree_.expandTree();
946//            }
947//        }
948    }
949
950    /**
951     * Change the Pane layout to GridLayout.
952     *
953     * @param event
954     */
955
956    void grid_itemStateChanged( java.awt.event.ItemEvent event )
957    {
958        /*
959        * Get the component list for mainPane_ and change
960        * the layout to GridLayout.
961        */
962//        Component[] comps = mainPane_.getComponents();
963//        mainPane_.setBatch( true );
964//        mainPane_.setLayout( new GridLayout( 2, 0 ) );
965//        /*
966//        * Get the first and second graphs from the first
967//        * and second layers, respectively.
968//        */
969//        Graph gr = ( (Layer) comps[0] ).getGraph();
970//        Graph gr2 = ( (Layer) comps[1] ).getGraph();
971//        /*
972//        * Create copies of all X-Axes associated with the first
973//        * graph for the second graph. If the axes are not copied then
974//        * the second graph will have the second line plotted, but without
975//        * any axes.
976//        */
977//        for( Enumeration xa = ( (CartesianGraph) gr ).xAxisElements(); xa.hasMoreElements(); )
978//        {
979//            ( (CartesianGraph) gr2 ).addXAxis( ( (Axis) xa.nextElement() ).copy() );
980//        }
981//        /*
982//        * Create copies of all Y-Axes associated with the first
983//        * graph for the second graph.
984//        */
985//        for( Enumeration ya = ( (CartesianGraph) gr ).yAxisElements(); ya.hasMoreElements(); )
986//        {
987//            ( (CartesianGraph) gr2 ).addYAxis( ( (Axis) ya.nextElement() ).copy() );
988//        }
989//        /*
990//        * Tell the Applet that the mainPane_ needs to
991//        * be layed out and re-draw the mainPane_.
992//        */
993//        if( isApplet_ )
994//        {
995//            validate();
996//        }
997//        else
998//        {
999//            frame.validate();
1000//        }
1001//        //    mainPane_.draw();
1002//        mainPane_.setBatch( false );
1003//        if( tree_ != null )
1004//        {
1005//            if( tree_.isVisible() )
1006//            {
1007//                tree_.setJPane( mainPane_ );
1008//                tree_.expandTree();
1009//            }
1010//        }
1011    }
1012
1013    void showTree_ActionPerformed( java.awt.event.ActionEvent event )
1014    {
1015//        /*
1016//        * Create the ClassTree dialog to display the classes used
1017//        * in the mainPane_ and allow editing.
1018//        */
1019//        if( tree_ == null )
1020//        {
1021//            tree_ = new JClassTree( "Classes for LayoutDemo" );
1022//        }
1023//        tree_.setJPane( mainPane_ );
1024//        tree_.show();
1025    }
1026
1027    private static final Log LOGGER = LogFactory.getLog( EtherPlotServiceImpl.class );
1028
1029    // Dimensions of the jPanes
1030    private static final int MAX_WIDTH = 800;
1031    private static final int MAX_HEIGHT = 700;
1032    private static final int MARGIN_LEFT_RIGHT = 50;
1033
1034    private static final int TITLE_FONT_SIZE = 20;
1035    private static final int FONT_SIZE = 12;
1036    private static final int ERROR_FONT_SIZE = 10;
1037}
Note: See TracBrowser for help on using the repository browser.