gpt4 book ai didi

java - CombinedDomainXYPlot 不重新缩放域轴

转载 作者:塔克拉玛干 更新时间:2023-11-01 22:51:35 24 4
gpt4 key购买 nike

当我从使用 CombinedDomainXYPlot 的图表中隐藏系列数时,所有范围轴都会很好地自动重新缩放。但是,域轴不会重新缩放。有什么方法可以手动刷新缩放比例,或者我可能缺少一个设置来启用此设置中域轴的自动缩放?

最佳答案

CombinedDomainXYPlotgetDataRange() 中为其共享域轴建立组合最大Range。这是允许共享轴所必需的。更改系列的可见性对共享域轴没有影响;更改 Dataset 通过其 configure() 方法更新共享域轴。在任何一种情况下,子图的范围轴都可以独立更新。

下面的示例允许您独立更新子图或更改系列的可见性。在 configure() 上中断以查看效果。您对切换 setAutoRange () 的建议可以替换为对 configure() 的一次调用;但效果应该为零,因为数据及其组合的最大 Range 没有变化。

mainPlot.getDomainAxis().configure();

要自动更新共享域轴,请使用 addSeries()removeSeries() 而不是 setSeriesVisible()

顺便说一句,不要忽视Initial Threadspack()

combined plot image

import java.awt.*;
import java.awt.event.ActionEvent;
import java.util.Random;
import javax.swing.*;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.NumberAxis;
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 static org.jfree.chart.renderer.xy.StandardXYItemRenderer.*;
import org.jfree.chart.renderer.xy.XYItemRenderer;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;

/**
* @see https://stackoverflow.com/a/64508866/230513
* @see https://stackoverflow.com/q/11870416/230513
*/
public class CombinedPlot {

private static final int MAX = 3;
private static final Random RND = new Random();

public static void main(String[] args) {
EventQueue.invokeLater(CombinedPlot::init);
}

private static void init() {
XYItemRenderer renderer = new StandardXYItemRenderer(SHAPES_AND_LINES);
XYPlot plot1 = new XYPlot(
generateData(), null, new NumberAxis("Range 1"), renderer);
XYPlot plot2 = new XYPlot(
generateData(), null, new NumberAxis("Range 2"), renderer);
final CombinedDomainXYPlot plot =
new CombinedDomainXYPlot(new NumberAxis("Domain"));
plot.setDomainPannable(true);
plot.setRangePannable(true);
plot.add(plot1);
plot.add(plot2);
plot.setOrientation(PlotOrientation.VERTICAL);
JFreeChart chart = new JFreeChart(
"Combined Plots", JFreeChart.DEFAULT_TITLE_FONT, plot, true);
ChartPanel chartPanel = new ChartPanel(chart);
chartPanel.setPreferredSize(new Dimension(800, 500));

JPanel controlPanel = new JPanel();
controlPanel.add(new JButton(new UpdateAction(plot, 0)));
controlPanel.add(new JButton(new UpdateAction(plot, 1)));
for (int i = 0; i < MAX; i++) {
JCheckBox jcb = new JCheckBox(new VisibleAction(renderer, i));
jcb.setSelected(true);
renderer.setSeriesVisible(i, true);
controlPanel.add(jcb);
}

JFrame frame = new JFrame("Combined Plot Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(chartPanel, BorderLayout.CENTER);
frame.add(controlPanel, BorderLayout.SOUTH);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}

private static class UpdateAction extends AbstractAction {

private final XYPlot plot;

public UpdateAction(CombinedDomainXYPlot plot, int i) {
super("Update plot " + (i + 1));
this.plot = (XYPlot) plot.getSubplots().get(i);
}

@Override
public void actionPerformed(ActionEvent e) {
plot.setDataset(CombinedPlot.generateData());
}
}

private static class VisibleAction extends AbstractAction {

private XYItemRenderer renderer;
private int i;

public VisibleAction(XYItemRenderer renderer, int i) {
super("Series " + (i + 1));
this.renderer = renderer;
this.i = i;
}

@Override
public void actionPerformed(ActionEvent e) {
renderer.setSeriesVisible(i, !renderer.getSeriesVisible(i));
}
}

private static XYSeriesCollection generateData() {
XYSeriesCollection data = new XYSeriesCollection();
for (int i = 0; i < MAX; i++) {
data.addSeries(generateSeries("Series " + (i + 1)));
}
return data;
}

private static XYSeries generateSeries(String key) {
XYSeries series = new XYSeries(key);
for (int i = 0; i < 16; i++) {
series.add(RND.nextGaussian(), RND.nextGaussian());
}
return series;
}
}

关于java - CombinedDomainXYPlot 不重新缩放域轴,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11870416/

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