当我从使用CombinedDomainXYPlot的图表中隐藏系列数时,所有范围轴都会自动重新调整。但是,域轴不会重新缩放。有没有办法手动刷新缩放,或者可能有一个我缺少的设置,以便在此设置中启用域轴的自动缩放?
答案 0 :(得分:4)
CombinedDomainXYPlot
在Range
中为其共享域轴建立合并的最大getDataRange()
。这是允许共享轴所必需的。更改系列的可见性对共享域轴没有影响;更改Dataset
通过其configure()
方法更新共享域轴。在任何一种情况下,子图的范围轴都可以独立更新。
以下示例允许您更新子图或独立更改系列的可见性。休息configure()
以查看效果。切换setAutoRange ()
的建议可以通过对configure()
的一次调用来替换;但效果应为零,因为数据及其组合的最大Range
不变。
mainPlot.getDomainAxis().configure();
要自动更新共享域轴,请使用addSeries()
或removeSeries()
代替setSeriesVisible()
。
顺便说一句,不要忽视Initial Threads和pack()
。
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 org.jfree.chart.renderer.xy.XYItemRenderer;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;
/**
* @see http://stackoverflow.com/q/11870416/230513
*/
public class CombinedPlot {
private static final int MAX = 3;
private static final Random rand = new Random();
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
init();
}
});
}
private static void init() {
XYItemRenderer renderer = new StandardXYItemRenderer();
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.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);
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(rand.nextGaussian(), rand.nextGaussian());
}
return series;
}
}
答案 1 :(得分:2)
回答我自己的问题:
我设法使用一点点黑客刷新轴:
mainPlot.getDomainAxis().setAutoRange(false);
mainPlot.getDomainAxis().setAutoRange(true);
它不是很好但它可以解决问题。不过,我希望有人能发布一个更好的解决方案......
以下是使用非自定义数据集的代码。请运行它,然后单击一个名为click的大按钮多次隐藏几个系列。域轴未重新调整。
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;
import java.util.Random;
import javax.swing.*;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.DateAxis;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.event.ChartChangeEvent;
import org.jfree.chart.event.ChartChangeListener;
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.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;
import org.jfree.ui.RectangleEdge;
public class Runner {
private static Random rand = new Random();
public static void main(String[] args) {
XYSeriesCollection data = new XYSeriesCollection();
int max = rand.nextInt(2) + 2;
for (int i = 0; i < max; i++) {
data.addSeries(generateSeries("Series" + (i + 1)));
}
final XYItemRenderer renderer1 = new StandardXYItemRenderer();
final XYPlot plot1 = new XYPlot(data, null, new DateAxis("Dates"), renderer1);
data = new XYSeriesCollection();
for (int i = 0; i < max; i++) {
data.addSeries(generateSeries("Series" + (i + 1)));
}
final XYPlot plot2 = new XYPlot(data, null, new NumberAxis("Numbers"), renderer1);
final CombinedDomainXYPlot plot = new CombinedDomainXYPlot(new NumberAxis("Domain"));
plot.setGap(10.0);
// add the subplots...
plot.add(plot1, 1);
plot.add(plot2, 1);
plot.setOrientation(PlotOrientation.VERTICAL);
// return a new chart containing the overlaid plot...
final JFreeChart chart = new JFreeChart("CombinedDomainXYPlot Demo",
JFreeChart.DEFAULT_TITLE_FONT, plot, true);
chart.getLegend().setPosition(RectangleEdge.RIGHT);
chart.addChangeListener(new ChartChangeListener() {
boolean changed = false;
@Override
public void chartChanged(ChartChangeEvent event) {
if (!changed) {
} else {
changed = false;
}
}
});
ChartPanel panel = new ChartPanel(chart);
JPanel panel2 = new JPanel(new BorderLayout(0, 10));
panel2.add(panel, BorderLayout.CENTER);
JButton b = new JButton("Click");
b.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
CombinedDomainXYPlot plot = (CombinedDomainXYPlot) chart.getXYPlot();
List l = plot.getSubplots();
int index = rand.nextInt(plot1.getSeriesCount() + plot2.getSeriesCount());
boolean b = renderer1.isSeriesVisible(index);
renderer1.setSeriesVisible(index, false);
}
});
panel2.add(b, BorderLayout.NORTH);
panel2.setVisible(true);
JFrame frame = new JFrame("dsadsa");
frame.add(panel2);
frame.setSize(800, 600);
frame.setVisible(true);
}
private static XYSeries generateSeries(String key) {
XYSeries series = new XYSeries(key);
int points = 15;
double val = 0.0;
double x = 0.0;
for (int i = 0; i < points; i++) {
val += rand.nextDouble() * 6 - 3;
x += rand.nextDouble() * 4;
series.add(x, val);
}
return series;
}
}