使用滑块更新SwingNode中的Jfreechart

时间:2018-07-16 11:35:44

标签: javafx jfreechart jslider

我正在使用SwingNode将JFreeChart插入JavaFX应用程序。我能够使用数组绘制折线图。我想用滑块值更新图表。我的意思是图表应该可以在图形区域中移动。

这是我的Controller代码和绘制图形的代码

我没有任何例外,一切都可以正常执行,但是我无法将滑块与图形连接。请帮助我找出我犯的错误。

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;
   }

 }  

预先感谢

0 个答案:

没有答案