0

I'm trying to implement a "subplot height resize button" for a combined domain plot in a char panel.
So I have a controller like this one:

package org.my.jfxtestplots;

import java.io.IOException;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.util.Date;
import java.util.Random;
import javafx.application.Platform;
import javafx.embed.swing.SwingNode;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.ScrollPane;
import javax.swing.JComponent;
import javax.swing.SwingUtilities;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.annotations.XYTitleAnnotation;
import org.jfree.chart.axis.DateAxis;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.block.BlockBorder;
import org.jfree.chart.plot.CombinedDomainXYPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.StandardXYItemRenderer;
import org.jfree.chart.renderer.xy.XYItemRenderer;

import org.jfree.chart.title.LegendTitle;
import org.jfree.chart.ui.RectangleAnchor;
import org.jfree.chart.ui.RectangleEdge;
import org.jfree.data.time.Millisecond;
import org.jfree.data.time.TimeSeries;
import org.jfree.data.time.TimeSeriesCollection;
import org.jfree.data.time.TimeSeriesDataItem;
    
public class myController {
    private CombinedDomainXYPlot plot_combinato;
    private ChartPanel chart_panel_preview;
    
    private int wgt=1;
    
    @FXML
    private Button btnPlot;
    @FXML
    private ScrollPane scroll_waves;
    @FXML
    private Button btnPlotBigger;
   
    @FXML
    private void btnPlot_click(ActionEvent event) {
        NumberAxis rangeAxis1;
        rangeAxis1 = new NumberAxis();
        String panel_title = "Titolo";
        ZoneId zoneId = ZoneId.systemDefault();
        int wCounter=0;
        TimeSeries series;
        TimeSeriesCollection dataset;
        LocalDateTime dateStart;
        LocalDateTime new_date;
        long timestamp;
        float time_sample_value;
        long current_time;
        Millisecond ms;
        TimeSeriesDataItem item;
        float numero; 
        LegendTitle legend_title;      
        XYPlot subplot;
        XYItemRenderer renderer;
//        
        Random rand =new Random();
        int upper_bound = 10000;
        
        SwingNode chart_panel_preview_container_swingNode; 
        
        scroll_waves.setFitToWidth(true);
        scroll_waves.setFitToHeight(false);
        
//  
        plot_combinato = new CombinedDomainXYPlot(new DateAxis("Time"));
        plot_combinato.setOrientation(PlotOrientation.VERTICAL);
        plot_combinato.setRangeAxis(rangeAxis1);
        
        chart_panel_preview = new ChartPanel(new JFreeChart(panel_title, 
                                                JFreeChart.DEFAULT_TITLE_FONT, 
                                                plot_combinato, 
                                                true));  
//
        for (int idStation=0; idStation < 10; idStation++) {
            wCounter+=1;
                
            series = new TimeSeries("STAZ" + String.valueOf(wCounter) + " CHAN");
            dateStart = LocalDateTime.now(); 
            timestamp = dateStart.atZone(zoneId).toInstant().toEpochMilli();

            for (Integer i =0; i< 1000; i++) {  
                    
                time_sample_value = i.floatValue()/(float)100.0; //tmpWave.getSamplingRate(); //the time value of the sample
                current_time= timestamp + Float.valueOf(time_sample_value*1000).intValue();
                new_date = millsToLocalDateTime(current_time);
                ms= new Millisecond(Date.from(new_date.atZone(zoneId).toInstant()));
                numero = rand.nextFloat();
                item = new TimeSeriesDataItem(ms, numero); 
                    
                series.add(item);                   
            }     
//  
            dataset = new TimeSeriesCollection();
            dataset.addSeries(series);
//        
            rangeAxis1 = new NumberAxis();
            rangeAxis1.setAutoRange(true);          
            renderer = new StandardXYItemRenderer();             
            renderer.setSeriesPaint(0, new java.awt.Color(70,240,8));           // Colore del segnale               
//         
            subplot = new XYPlot(dataset, null, rangeAxis1, renderer);
            subplot.setBackgroundPaint(new java.awt.Color(68,68,68));           // Colore dello sfondo

            // Mostra le linee crosshair per pickare tempo-ampiezza
            subplot.setDomainCrosshairVisible(false);
            subplot.setRangeCrosshairVisible(false);                     
//           
            legend_title = new LegendTitle(subplot);
            legend_title.setItemFont(new java.awt.Font("Dialog", java.awt.Font.BOLD, 9));
            legend_title.setBackgroundPaint(new java.awt.Color(200, 200, 255, 100));
            legend_title.setItemPaint(java.awt.Color.WHITE);

            legend_title.setFrame(new BlockBorder(java.awt.Color.GRAY));
            legend_title.setPosition(RectangleEdge.BOTTOM);
            XYTitleAnnotation ta = new XYTitleAnnotation(0.98, 0.02, legend_title,RectangleAnchor.BOTTOM_RIGHT);
//
            ta.setMaxWidth(0.48);
            subplot.addAnnotation(ta);                   
////                
//subplot.setWeight(1);
            plot_combinato.add(subplot, wgt); //, 1000 + wCounter+1); 
            System.out.println("PESO: " + subplot.getWeight());
                 
        }   
        
        chart_panel_preview_container_swingNode= new SwingNode();
      
        this.createSwingContent(chart_panel_preview_container_swingNode, chart_panel_preview);
        
        scroll_waves.setContent(chart_panel_preview_container_swingNode);
                  
    }
//--------------------------------------------------------------------------------          
    public static LocalDateTime millsToLocalDateTime(long millis) {
      Instant instant = Instant.ofEpochMilli(millis);
      LocalDateTime date = instant.atZone(ZoneOffset.UTC).toLocalDateTime();
      return date;
  }    
//--------------------------------------------------------------------------------      
    private void createSwingContent(SwingNode swingNode, JComponent t) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                Platform.setImplicitExit(true);
                swingNode.setContent(t);          
            }
        });
    }    
//-------------------------------------------------------------------------------- 

    @FXML
    private void btnPlotBigger_click(ActionEvent event) {
        wgt+=1;
        btnPlot.fire();
    }
}

Now I have implemented a "Plot Bigger" button that should increase each subplot height every time it's clicked. But it does not resize the boxes... What's wrong?
Any idea?

Andrea
  • 13
  • 3
  • Have you tried adjusting the weight when you add the subplot? – trashgod Oct 13 '20 at 16:59
  • The subplot is an XYPlot(...). I assume you meant "height" instead of "weight"... But I can't find any method like "setSize" or "setDimension" and so on... – Andrea Oct 14 '20 at 06:08
  • `XYPlot::setWeight`; you can also specify the weight when you `add()` the subplot. Please [edit] your question to include a [mcve] that shows your current approach. – trashgod Oct 14 '20 at 13:07
  • I'm posting some code... – Andrea Oct 19 '20 at 12:01
  • Why not use [tag:jfreechart-fx], cited [here](https://stackoverflow.com/tags/jfreechart-fx/info)? – trashgod Oct 19 '20 at 15:08
  • I'm actually using jfreechart-fx. I use a swing container just to add the chart to the scroll panel – Andrea Oct 19 '20 at 15:53
  • I see that `setWeight()` fires a change event that you may have to handle. Instead of scrolling, I typically use the zoom and pan features mentioned [here](https://stackoverflow.com/a/44151032/230513) and illustrated [here](https://stackoverflow.com/a/57702029/230513) with JavaFX. – trashgod Oct 19 '20 at 16:01

1 Answers1

0

I solved this issue switching to ChartViewer instead of ChartPanel and then

myChartViewer.setPrefHeight(..,..)

worked for me.

Andrea
  • 13
  • 3