gpt4 book ai didi

java - 更新 JFreeChart 时保留用户缩放状态

转载 作者:行者123 更新时间:2023-12-01 15:15:44 29 4
gpt4 key购买 nike

我正在使用 JFreeChart 1.0.14 绘制 XYSeries。我确实可以访问所有元素(ChartPanelJFreeChartXYSeriesCollectionXYSeries)。有时会有新的数据要绘制,所以我更新我的系列(为简单起见,我们假设只有一个系列):

SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
XYSeries xyData = new XYSeries("Trace");
for (int i = 0; i < n; i++)
xyData.add(data[0][i], data[1][i], false);

collection.removeAllSeries();
collection.addSeries(xyData);

chart.getXYPlot().setRangeAxis(createNewAxis());
}
});

这一切都很好。但我有一个问题。用户可以通过在绘图中拖动来缩放。每次数据更新时,此缩放都会重置。这是有道理的,因为我创建了新的轴。如何保留用户缩放状态?由于用户还可以选择自动范围/范围,因此我不想对这些进行某种修改(Axis#setRange/setAutoRange)。我发现了一些关于 ChartPanel#getScaleX() 的内容。但在上述 invokeLater 的开头始终是 1.0。我也找不到任何 ChartPanel.setScaleX

我做错了什么,还是有另一种方法可以保留用户缩放状态而不改变轴的范围/自动范围行为?

顺便说一句:我必须重新创建轴,因为它们还可以在对数刻度和线性刻度之间切换。

最佳答案

我认为每次添加新点或调用 removeAllSeries() 时都不需要创建新的 XYSeries xyData

此示例演示如何将动态数据添加到 XYSeriesCollection

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JPanel;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.LogAxis;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.data.time.Millisecond;
import org.jfree.data.xy.XYDataset;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;

/**
* A demonstration application showing a XYseries chart where you can
* dynamically add (random) data by clicking on a button.
*/
public class DynamicDataDemo1 extends ApplicationFrame {
/**
* Constructs a new demonstration application.
*
* @param title
* the frame title.
*/
public DynamicDataDemo1(String title) {
super(title);
MyDemoPanel demoPanel = new MyDemoPanel();
setContentPane(demoPanel);
}



static class MyDemoPanel extends DemoPanel implements ActionListener {
/** The time series data. */

/** The most recent value added. */
private double lastValue1 = 200.0;
private double lastValue2 = 200.0;
private XYPlot plot;
private boolean logAxis = false;
private String lastSeries = "";
private final XYSeriesCollection dataset;
private static String SERIES_NAME = "Random Data ";
private static int seriesNumber = 0;
/**
* Creates a new instance.
*/
public MyDemoPanel() {
super(new BorderLayout());
lastSeries = SERIES_NAME + (seriesNumber++);
XYSeries series = new XYSeries(lastSeries);
dataset = new XYSeriesCollection(series);
ChartPanel chartPanel = new ChartPanel(createChart(dataset));
chartPanel.setPreferredSize(new java.awt.Dimension(500, 270));
addChart(chartPanel.getChart());
JPanel buttonPanel = new JPanel();
buttonPanel.setBorder(BorderFactory.createEmptyBorder(4, 4, 4, 4));
JButton button = new JButton("Add New Data Item");
button.setActionCommand("ADD_DATA");
button.addActionListener(this);
buttonPanel.add(button);
{
JButton button2 = new JButton("Switch Axis");
button2.setActionCommand("SWITCH_AXIS");
button2.addActionListener(this);
buttonPanel.add(button2);
}
{
JButton button2 = new JButton("Add Series");
button2.setActionCommand("ADD_SERIES");
button2.addActionListener(this);
buttonPanel.add(button2);
}
add(chartPanel);
add(buttonPanel, BorderLayout.SOUTH);
}

private JFreeChart createChart(XYDataset dataset) {
JFreeChart result = ChartFactory.createXYLineChart("Dynamic Data Demo", "Time", "Value", dataset, PlotOrientation.VERTICAL, true, true, false);
plot = (XYPlot) result.getPlot();
ValueAxis domainAxis = plot.getDomainAxis();
domainAxis.setAutoRange(true);
final ValueAxis rangeAxis = plot.getRangeAxis();
rangeAxis.setAutoRange(true);
return result;
}

@Override
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().equals("ADD_DATA")) {
double factor1 = 0.90 + 0.2 * Math.random();
double factor2 = 0.90 + 0.2 * Math.random();
this.lastValue1 = this.lastValue1 * factor1;
this.lastValue2 = this.lastValue2 * factor2;
Millisecond now = new Millisecond();
System.out.println("Now = " + now.toString());
XYSeries series = dataset.getSeries(lastSeries);
series.add(this.lastValue2, this.lastValue1);
} else if (e.getActionCommand().equals("SWITCH_AXIS")) {
if (!logAxis) {
LogAxis xAxis = new LogAxis("X");
LogAxis yAxis = new LogAxis("Y");
updateZoom(xAxis,yAxis);
logAxis = true;
} else {
NumberAxis xAxis = new NumberAxis("X");
NumberAxis yAxis = new NumberAxis("Y");
updateZoom(xAxis,yAxis);
logAxis = false;
}
} else if (e.getActionCommand().equals("ADD_SERIES")) {
lastSeries = SERIES_NAME + (seriesNumber++);
XYSeries series = new XYSeries(lastSeries);
dataset.addSeries(series);
}
}

private void (ValueAxis xAxis, ValueAxis yAxis) {
double domainMin = plot.getDomainAxis().getRange().getLowerBound();
double domainMax = plot.getDomainAxis().getRange().getUpperBound();
System.out.println(domainMin + "," + domainMax);

double rangeMin = plot.getRangeAxis().getRange().getLowerBound();
double rangeMax = plot.getRangeAxis().getRange().getUpperBound();
System.out.println(rangeMin + "," + rangeMax);

xAxis.setRange(domainMin, domainMax);
plot.setDomainAxis(xAxis);
yAxis.setRange(rangeMin, rangeMax);
plot.setRangeAxis(yAxis);

}
}

public static JPanel createDemoPanel() {
return new DynamicDataDemo1.MyDemoPanel();
}

public static void main(String[] args) {
DynamicDataDemo1 demo = new DynamicDataDemo1("JFreeChart: Dynamic XYSeries");
demo.pack();
RefineryUtilities.centerFrameOnScreen(demo);
demo.setVisible(true);
}
}

此示例基于 JFreeChart:DynamicDataDemo1.java。每次单击“添加新数据项”时,都会添加一个新点。在此示例中,当您添加数据点时,缩放将被保留,但当您更改轴时则不会。

要在将轴从正常刻度更改为对数刻度时保持缩放,反之亦然,请使用两个轴的当前上限和下限,并设置新轴的范围,如中所示更新缩放

关于java - 更新 JFreeChart 时保留用户缩放状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11632045/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com