0

I am using SwingNode to insert JFreeChart into a JavaFX application. I am able to plot the Line graph using arrays. I want to update the Chart with the slider values. I mean the chart should be movable in the graphical area.

Here are my Controller code and the code to plot graph

I don't have any exception everything executes properly but I am not able to connect the slider with graph. Please help me in finding the mistake I have done.

public class FXMLController implements Initializable 
{

 @FXML
 private void open_btn_action(ActionEvent event) 
 {
    xArray = new double[]{0.0};
    yArray = new double[]{0.0};
    fc=new FileChooser();
    fc.getExtensionFilters().addAll();
    fc.getExtensionFilters().addAll(new ExtensionFilter("Text Files","*.txt"));
    fc.getExtensionFilters().addAll(new ExtensionFilter("CSV Files","*.csv"));

    file=fc.showOpenDialog(null);

    if(file!=null)
    { //System.out.println(file.getAbsolutePath());
        try 
        {
            count = (int) Files.lines(file.toPath(), Charset.defaultCharset()).count();
        } 
        catch (IOException ex)  
         {
            Logger.getLogger(Data.class.getName()).log(Level.SEVERE, null, ex);
         }
       data = Data.getData(file,count);
       xArray = new double[data.length];
       yArray = new double[data.length];

       xArray = Data.array_x_value(data);
       yArray = Data.array_y_value(data);

       chart = Chart.chart_plot(xArray,yArray);
       final SwingNode chartSwingNode = new SwingNode();
       final SwingNode sliderNode = new SwingNode();

       chartSwingNode.setContent(new ChartPanel(chart));

       final JSlider slider = new JSlider(0,xArray.length);
      slider.addChangeListener(new ChangeListener() 
      {
        @Override
        public void stateChanged(ChangeEvent e) 
        {
            final XYPlot plot = (XYPlot) chart.getPlot();
            plot.setDataset(Chart.list.get(slider.getValue()));
            System.out.println(slider.getValue());
        }});

       sliderNode.setContent(slider);
       //Adding Chart to the splitPane and slider on the display
       bPane.setCenter(chartSwingNode);
       bPane.setBottom(sliderNode);


        upperCount = xArray.length;
        mainSplit.getItems().set(1,bPane);

       }              
      }

     }



    //Chart Class to plot chart

public class Chart 
{
static JFreeChart chart;
static double[] xValues,yValues;
static XYPlot plot;
    static List<XYDataset> list = new ArrayList<XYDataset>();

public static JFreeChart chart_plot(double[] x,double[] y)
{
        try {
          UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());  // for look and feel like a desktop application

      } catch (ClassNotFoundException | IllegalAccessException | InstantiationException | UnsupportedLookAndFeelException e) { }
    xValues=new double[x.length];  
    xValues=x;
    yValues=new double[y.length];
    yValues=y;
    XYDataset dataset=createDataset(xValues,yValues);
    chart = ChartFactory.createXYLineChart("","Wave number cm"+'\u2212'+'\u00B9',"Intensity",
             dataset, PlotOrientation.VERTICAL,true, true, false);
     chart.getPlot().setBackgroundPaint(new Color(235, 244, 250));
     plot = (XYPlot)chart.getPlot();
     final NumberAxis axis2 = (NumberAxis) plot.getRangeAxis();
             //axis2.setLabelPaint(Color.magenta);  
             //axis2.setTickLabelPaint(Color.darkGray);
     axis2.setAutoRange(true);
     axis2.setAutoRangeIncludesZero(false);
     plot.getRenderer().setSeriesPaint(0, new Color(10,11,45));
             plot.getRenderer().setSeriesStroke(0,new BasicStroke(1.25f));
             plot.setDomainPannable(false);
     plot.setDomainGridlinePaint(new Color(0xC0,0xC0,0xC0));
     plot.setRangeGridlinePaint(new Color(0xC0,0xC0,0xC0));
     plot.setRangeZeroBaselineVisible(false);
     plot.setDomainZeroBaselineVisible(false);
             plot.setOutlineVisible(false);
     ValueAxis domain = plot.getDomainAxis();
         domain.setAutoRange(true);
        //range.setRange(-MINMAX, MINMAX);
     plot.configureRangeAxes(); 
     plot.setDomainCrosshairVisible(true);   
         plot.setDomainCrosshairLockedOnData(true);   
         plot.setRangeCrosshairVisible(true);
           //plot.setOutlinePaint(Color.gray);      
      //System.out.println(plot.isRangeZeroBaselineVisible());
     XYLineAndShapeRenderer renderer =
                (XYLineAndShapeRenderer) plot.getRenderer();     
    ChartListener.chartListner(chart);
            //System.out.println("X length="+ xValues.length);  
             list = new ArrayList<XYDataset>();
    for (int i = 0; i <= xValues.length; i++) {
        list.add(getDataset(i));
    }

    return chart;       
}

private static XYDataset getDataset(int n) {

    final XYSeries series = new XYSeries("series1");
    double values;
    for (int length = 0; length < xValues.length; length++) {
        values = xValues[length];
        series.add(values, yValues[length]);
      }
       return new XYSeriesCollection(series);
   }
   public static XYDataset createDataset(double[] array_x,double[] array_y)
  {
    DefaultXYDataset ds = new DefaultXYDataset();
    double[][] data={array_x,array_y};
            ds.addSeries("series1", data);
            return ds;
   }

 }  

Thanks in advance

  • https://stackoverflow.com/questions/15190048/update-graph-with-jfreechart-and-slider?rq=1 I referred this. – DivyaShrungarJ Jul 16 '18 at 11:38
  • Are you looking for a [`SlidingXYDataset`](https://stackoverflow.com/search?tab=votes&q=user%3a230513%20%5bjfreechart%5d%20slidingxydataset)? – trashgod Jul 16 '18 at 16:17
  • yes... thank you, can I get some examples of that? – DivyaShrungarJ Jul 17 '18 at 04:12
  • I have created my own SlidingXYDataset that implements XYDataset in a way similar to how SlidingCategoryDataset implements CategoryDataset. But it doesn't work. Please suggest me what can I do? – DivyaShrungarJ Jul 17 '18 at 07:35
  • Did you try the implementation cited [here](https://stackoverflow.com/a/44105790/230513)? – trashgod Jul 17 '18 at 15:24

0 Answers0