我正在尝试实现一个"子画面高度调整按钮";在char面板中的组合域图
所以我有一个像这样的控制器:
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();
}
}
现在我已经实现了一个";Plot Bigger";按钮,应在每次单击时增加每个子图形的高度。但它不会调整框的大小。。。怎么了
知道吗?
我解决了这个问题,切换到ChartViewer而不是ChartPanel,然后
myChartViewer.setPrefHeight(..,..)
为我工作。