JFree Chart3d how to get data points displayed on XYZ Line Chart

62 Views Asked by At

I drew a 3D line chart using jfree.chart3d in the Orson Charts library using java. How can I get the data between two points selected with the mouse on the line chart I drew?

I searched deeply but get no getDataset() or similar methods in the library. I try to get the line plot at the clicked point with the mouse but found no such method. Any help would be appreciated. There are solutions on 2D charts but I have not come across such a solution for jfree.chart3d library

I try to read the dataSet among the line charts selected by mouse. First mouse click defines the start point of the sub-line, and second mouse click defines the end point of the sub-line of the line selected by mouse. I tried to read the dataSet as follows. But I get "getRenderer() is undefined for type Plot3D" error. Here are my questions:

1.How can I highlight the selected line plot, so that to create awareness to the user that he/she has chosen the right plot among other plots?

2.How can I get the selected line plot's dataSet?

3.How Can I obtain the sub-dataSet between two mouse clicks on the selected line plot?

Here is the complete code:

/* ===================
 * Orson Charts - Demo
 * ===================
 *
 * Copyright 2013-2022, by David Gilbert. All rights reserved.
 *
 * https://github.com/jfree/jfree-demos
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *   - Redistributions of source code must retain the above copyright
 *     notice, this list of conditions and the following disclaimer.
 *   - Redistributions in binary form must reproduce the above copyright
 *     notice, this list of conditions and the following disclaimer in the
 *     documentation and/or other materials provided with the distribution.
 *   - Neither the name of the JFree organisation nor the
 *     names of its contributors may be used to endorse or promote products
 *     derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
 * ARE DISCLAIMED. IN NO EVENT SHALL OBJECT REFINERY LIMITED BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 * 
 * Note that the above terms apply to the demo source only, and not the 
 * Orson Charts library.
 * 
 */

 package com.orsoncharts.demo;

 import java.awt.Color;
 import java.awt.BorderLayout;
 import javax.swing.JFrame;
 import javax.swing.JPanel;
 import org.jfree.chart3d.Chart3D;
 import org.jfree.chart3d.Chart3DFactory;
 import org.jfree.chart3d.Chart3DPanel;
 import org.jfree.chart3d.axis.IntegerTickSelector;
 import org.jfree.chart3d.axis.NumberAxis3D;
 import org.jfree.chart3d.data.xyz.XYZDataset;
 import org.jfree.chart3d.data.xyz.XYZSeries;
 import org.jfree.chart3d.data.xyz.XYZSeriesCollection;
 import org.jfree.chart3d.graphics3d.Dimension3D;
 import org.jfree.chart3d.graphics3d.swing.DisplayPanel3D;
 import org.jfree.chart3d.plot.XYZPlot;
 
 /**
  * A demo of a 3D line chart.
  */
 @SuppressWarnings("serial")
 public class XYZLineChart3DDemo1 extends JFrame {
 
     /**
      * Creates a new test app.
      *
      * @param title  the frame title.
      */
     public XYZLineChart3DDemo1(String title) {
         super(title);
         addWindowListener(new ExitOnClose());
         getContentPane().add(createDemoPanel());
     }
 
     /**
      * Returns a panel containing the content for the demo.  This method is
      * used across all the individual demo applications to allow aggregation 
      * into a single "umbrella" demo (OrsonChartsDemo).
      * 
      * @return A panel containing the content for the demo.
      */
     public static JPanel createDemoPanel() {
         DemoPanel content = new DemoPanel(new BorderLayout());
         content.setPreferredSize(OrsonChartsDemo.DEFAULT_CONTENT_SIZE);
         XYZDataset dataset = createDataset();
         Chart3D chart = createChart(dataset);
         Chart3DPanel chart3DPanel = new Chart3DPanel(chart);
         content.setChartPanel(chart3DPanel);
         chart3DPanel.zoomToFit(OrsonChartsDemo.DEFAULT_CONTENT_SIZE);
         content.add(new DisplayPanel3D(chart3DPanel));

         chart3DPanel.addChartMouseListener(new Chart3DMouseListener() {
        
            @Override
            public void chartMouseMoved(Chart3DMouseEvent arg0) {
            }
            @Override
            public void chartMouseClicked(Chart3DMouseEvent arg0) {
                  // Retrieve the chart from the event
                Chart3D chart = arg0.getChart();
                // Retrieve the plot from the chart
                Plot3D plot = chart.getPlot();
                // Retrieve the renderer from the plot
                XYZRenderer renderer = (XYZRenderer)plot.getRenderer();//Error
                // Retrieve the dataset from the plot
                XYZDataset dataset = renderer.getPlot().getDataset();
            }
        });
         return content;
     }
     
     private static Chart3D createChart(XYZDataset dataset) {
         Chart3D chart = Chart3DFactory.createXYZLineChart("XYZ Line Chart Demo", 
                 "Orson Charts", dataset, "Day", "Index", "Station");
         chart.setChartBoxColor(new Color(255, 255, 255, 128));
         XYZPlot plot = (XYZPlot) chart.getPlot();
         plot.setDimensions(new Dimension3D(15, 3, 8));
         NumberAxis3D zAxis = (NumberAxis3D) plot.getZAxis();
         zAxis.setTickSelector(new IntegerTickSelector());
         return chart;    
     }
     
     /**
      * Creates a sample dataset (hard-coded for the purpose of keeping the
      * demo self-contained - in practice you would normally read your data
      * from a file, database or other source).
      * 
      * @return A sample dataset.
      */
     public static XYZDataset<String> createDataset() {    
         XYZSeriesCollection<String> dataset = new XYZSeriesCollection<>();
         
         for (int s = 1; s < 24; s++) {
             XYZSeries<String> series = new XYZSeries<>("Series " + s);
             double y = 1.0;
             for (int i = 0; i < 3000; i++) {
                 y = y * (1.0 + (Math.random() - 0.499) / 10.0);
                 series.add(i, y, s);
             }
             dataset.add(series);
         }
         
         return dataset;
     }
 
 
     /**
      * Starting point for the app.
      *
      * @param args  command line arguments (ignored).
      */
     public static void main(String[] args) {
         XYZLineChart3DDemo1 app = new XYZLineChart3DDemo1(
                 "OrsonCharts: XYZLineChart3DDemo1.java");
         app.pack();
         app.setVisible(true);
     }
 }
0

There are 0 best solutions below