1 /*
   2  * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
   3  * 
   4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   5  *
   6  * The contents of this file are subject to the terms of either the Universal Permissive License
   7  * v 1.0 as shown at http://oss.oracle.com/licenses/upl
   8  *
   9  * or the following license:
  10  *
  11  * Redistribution and use in source and binary forms, with or without modification, are permitted
  12  * provided that the following conditions are met:
  13  * 
  14  * 1. Redistributions of source code must retain the above copyright notice, this list of conditions
  15  * and the following disclaimer.
  16  * 
  17  * 2. Redistributions in binary form must reproduce the above copyright notice, this list of
  18  * conditions and the following disclaimer in the documentation and/or other materials provided with
  19  * the distribution.
  20  * 
  21  * 3. Neither the name of the copyright holder nor the names of its contributors may be used to
  22  * endorse or promote products derived from this software without specific prior written permission.
  23  * 
  24  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
  25  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  26  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
  27  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  29  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  30  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY
  31  * WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32  */
  33 package org.openjdk.jmc.greychart.testutil;
  34 
  35 import java.awt.Color;
  36 import java.awt.event.MouseAdapter;
  37 import java.awt.event.MouseEvent;
  38 import java.util.Date;
  39 import java.util.HashSet;
  40 import java.util.Set;
  41 
  42 import org.openjdk.jmc.ui.common.xydata.DefaultTimestampedData;
  43 import org.openjdk.jmc.ui.common.xydata.ITimestampedData;
  44 
  45 import org.openjdk.jmc.greychart.DefaultMetadataProvider;
  46 import org.openjdk.jmc.greychart.GreyChartPanel;
  47 import org.openjdk.jmc.greychart.YAxis.Position;
  48 import org.openjdk.jmc.greychart.data.SeriesProviderSet;
  49 import org.openjdk.jmc.greychart.impl.DateXAxis;
  50 import org.openjdk.jmc.greychart.impl.DefaultVerticalIndexRenderer;
  51 import org.openjdk.jmc.greychart.impl.DefaultXYGreyChart;
  52 import org.openjdk.jmc.greychart.impl.DefaultYAxis;
  53 import org.openjdk.jmc.greychart.util.ChartRenderingToolkit;
  54 
  55 /**
  56  * Tests adding samples out of order.
  57  */
  58 public class RenderingOutOfOrderTester {
  59         private final static long PERIOD = 1000;
  60 
  61         private final DefaultSignalGenerator m_saw = new DefaultSignalGenerator(DefaultSignalGenerator.SAW, PERIOD, MIN_Y,
  62                         MAX_Y, 0, 0);
  63         private final DefaultSignalGenerator m_sine = new DefaultSignalGenerator(DefaultSignalGenerator.SINUS, PERIOD,
  64                         MIN_Y, MAX_Y, 0, 0);
  65 
  66         private final static double MAX_Y = 5;
  67         private final static double MIN_Y = -5;
  68 
  69         private static DateXAxis xaxis;
  70         private static TimestampDataSeries SINUS_DATA;
  71         private static TimestampDataSeries SAW_DATA;
  72 
  73         private final static Date START_TIME = new Date();
  74         private static GreyChartPanel panel;
  75 
  76         private long m_time = 0;
  77 
  78         private class MyMouseListener extends MouseAdapter {
  79                 /**
  80                  * @see java.awt.event.MouseListener#mouseClicked(java.awt.event.MouseEvent)
  81                  */
  82                 @Override
  83                 public void mouseClicked(MouseEvent e) {
  84                         System.out.println("Adding value!");
  85                         doStep();
  86                 }
  87 
  88                 /**
  89                  * @see java.awt.event.MouseListener#mouseEntered(java.awt.event.MouseEvent)
  90                  */
  91                 @Override
  92                 public void mouseEntered(MouseEvent e) {
  93                         System.out.println("Entered graph!");
  94                 }
  95 
  96                 /**
  97                  * @see java.awt.event.MouseListener#mouseExited(java.awt.event.MouseEvent)
  98                  */
  99                 @Override
 100                 public void mouseExited(MouseEvent e) {
 101                         System.out.println("Exited graph!");
 102                 }
 103 
 104         }
 105 
 106         /**
 107          * Entry point.
 108          *
 109          * @param args
 110          */
 111         public static void main(String[] args) {
 112                 Set<String> options = new HashSet<>();
 113                 for (String s : args) {
 114                         options.add(s);
 115                 }
 116                 RenderingOutOfOrderTester tester = new RenderingOutOfOrderTester();
 117                 tester.setup(options);
 118         }
 119 
 120         private void setup(Set<String> options) {
 121                 SeriesProviderSet<ITimestampedData> provider = new SeriesProviderSet<>();
 122                 SINUS_DATA = new TimestampDataSeries();
 123                 SAW_DATA = new TimestampDataSeries();
 124                 provider.addDataSeries(SAW_DATA);
 125                 provider.addDataSeries(SINUS_DATA);
 126                 String title = "Out of order rendering test. Click for next point!";
 127                 DefaultXYGreyChart<ITimestampedData> graph = new DefaultXYGreyChart<>();
 128 
 129                 DefaultYAxis yaxis_left = new DefaultYAxis(graph);
 130                 DefaultYAxis yaxis_right = new DefaultYAxis(graph);
 131                 yaxis_left.setMin(MIN_Y);
 132                 yaxis_left.setMax(MAX_Y);
 133                 yaxis_left.setTickSize(4);
 134                 yaxis_left.setNumberOfTicks(20);
 135                 yaxis_left.setAlwaysShowZero(false);
 136                 yaxis_left.setAutoPadding(0.05);
 137                 yaxis_left.setTitle("Saw value");
 138                 yaxis_left.setPosition(Position.LEFT);
 139                 yaxis_right.setMin(MIN_Y);
 140                 yaxis_right.setMax(MAX_Y);
 141                 yaxis_right.setTickSize(4);
 142                 yaxis_right.setNumberOfTicks(20);
 143                 yaxis_right.setAlwaysShowZero(false);
 144                 yaxis_right.setAutoPadding(0.05);
 145                 yaxis_right.setTitle("Sine value");
 146                 yaxis_right.setPosition(Position.RIGHT);
 147 
 148                 graph.setMetadataProvider(new DefaultMetadataProvider());
 149                 graph.setAutoUpdateOnAxisChange(true);
 150                 xaxis = new DateXAxis(graph);
 151                 xaxis.setRange(START_TIME.getTime(), START_TIME.getTime() + 60000);
 152                 xaxis.setPaintGridLinesEnabled(true);
 153                 xaxis.setNumberOfTicks(20);
 154 
 155                 graph.setDataProvider(provider);
 156 
 157                 graph.setXAxis(xaxis);
 158                 graph.addYAxis(yaxis_left);
 159                 graph.addYAxis(yaxis_right);
 160 
 161                 graph.getXAxis().setTitle("Time");
 162 
 163                 panel = new GreyChartPanel(graph);
 164                 graph.setIndexRenderer(new DefaultVerticalIndexRenderer(graph));
 165                 panel.setBackground(new Color(240, 240, 240));
 166 
 167                 panel.addMouseListener(new MyMouseListener());
 168                 panel.setName("rendering performance");
 169                 panel.setBackground(Color.WHITE);
 170                 panel.getChart().getPlotRenderer().setBackground(Color.LIGHT_GRAY);
 171                 // Range chosen to provoke interpolation.
 172                 xaxis.setRange(105, 1015);
 173                 graph.setAntialiasingEnabled(false);
 174                 ChartRenderingToolkit.testComponent(panel, 1200, 800);
 175                 graph.setTitle(title);
 176                 for (int i = 0; i < 9; i++) {
 177                         doStep();
 178                 }
 179         }
 180 
 181         /**
 182          * Adds two points of data, out of order, to the series.
 183          */
 184         public void doStep() {
 185                 DefaultTimestampedData tsd1Sin = new DefaultTimestampedData(m_time, Double.valueOf(m_sine.getValue(m_time)));
 186                 DefaultTimestampedData tsd1Saw = new DefaultTimestampedData(m_time, Double.valueOf(m_saw.getValue(m_time)));
 187                 m_time += 10;
 188                 DefaultTimestampedData tsd2Sin = new DefaultTimestampedData(m_time, Double.valueOf(m_sine.getValue(m_time)));
 189                 DefaultTimestampedData tsd2Saw = new DefaultTimestampedData(m_time, Double.valueOf(m_saw.getValue(m_time)));
 190 
 191                 SINUS_DATA.addData(tsd2Sin);
 192                 SAW_DATA.addData(tsd2Saw);
 193                 SINUS_DATA.addData(tsd1Sin);
 194                 SAW_DATA.addData(tsd1Saw);
 195                 panel.repaint();
 196                 m_time += 20;
 197         }
 198 }