我正在尝试创建一个用户过滤器,根据用户点击启用/禁用系列。我添加了一个复选框按钮,可以启用/禁用该系列。
单击按钮时,只有图例项目消失。地块上的区域仍然可见。我的方法有误吗?或者这在StackedXYAreaChart中是不可能的?我做错了什么?
package test;
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import org.jfree.chart.*;
import org.jfree.chart.labels.StandardXYToolTipGenerator;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.StackedXYAreaRenderer2;
import org.jfree.data.xy.*;
public class Test2
{
private static final long serialVersionUID = 1L;
public Test2(String s)
{
JFreeChart jfreechart = createChart(createDataset());
JPanel jpanel = new ChartPanel(jfreechart);
JPanel masterPanel = new JPanel(new BorderLayout());
masterPanel.add(jpanel, BorderLayout.CENTER);
masterPanel.add(addUserControls(jfreechart), BorderLayout.EAST);
JFrame frame = new JFrame();
frame.add(masterPanel);
frame.pack();
frame.setVisible(true);
}
private static TableXYDataset createDataset()
{
DefaultTableXYDataset defaulttablexydataset = new DefaultTableXYDataset();
XYSeries xyseries = new XYSeries("Series 1", true, false);
xyseries.add(5D, 5D);
xyseries.add(10D, 15.5D);
xyseries.add(15D, 9.5D);
xyseries.add(20D, 7.5D);
defaulttablexydataset.addSeries(xyseries);
XYSeries xyseries1 = new XYSeries("Series 2", true, false);
xyseries1.add(5D, 5D);
xyseries1.add(10D, 15.5D);
xyseries1.add(15D, 9.5D);
xyseries1.add(20D, 3.5D);
defaulttablexydataset.addSeries(xyseries1);
return defaulttablexydataset;
}
private static JFreeChart createChart(TableXYDataset tablexydataset)
{
JFreeChart jfreechart = ChartFactory.createStackedXYAreaChart("Stacked XY Area Chart Demo 1", "X Value", "Y Value", tablexydataset, PlotOrientation.VERTICAL, true, true, false);
XYPlot xyplot = (XYPlot)jfreechart.getPlot();
StackedXYAreaRenderer2 stackedxyarearenderer2 = new StackedXYAreaRenderer2();
stackedxyarearenderer2.setToolTipGenerator(new StandardXYToolTipGenerator());
xyplot.setRenderer(0, stackedxyarearenderer2);
return jfreechart;
}
public static JPanel createDemoPanel()
{
JFreeChart jfreechart = createChart(createDataset());
return new ChartPanel(jfreechart);
}
public static void main(String args[])
{
Test2 stackedxyareachartdemo1 = new Test2("Stacked XY Area Chart Demo 1");
}
public JPanel addUserControls(JFreeChart chartPlot) {
JPanel userPanel = new JPanel(new GridLayout(2,2));
JCheckBox dataButton = new JCheckBox("Data");
dataButton.setSelected(true);
dataButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
if (dataButton.isSelected()) {
StackedXYAreaRenderer2 renderer = (StackedXYAreaRenderer2) chartPlot.getXYPlot().getRenderer();
renderer.setSeriesVisible(0, Boolean.TRUE);
} else {
StackedXYAreaRenderer2 renderer = (StackedXYAreaRenderer2) chartPlot.getXYPlot().getRenderer();
renderer.setSeriesVisible(0, Boolean.FALSE);
}
}
});
userPanel.add(dataButton);
return userPanel;
}
}
答案 0 :(得分:1)
StackedXYBarRenderer
对here描述了问题。
最简单的解决方案是覆盖默认渲染器的drawItem()
方法以强制实现可见性:
public void drawItem(Graphics2D g2,
XYItemRendererState state,
Rectangle2D dataArea,
PlotRenderingInfo info,
XYPlot plot,
ValueAxis domainAxis,
ValueAxis rangeAxis,
XYDataset dataset,
int series,
int item,
CrosshairState crosshairState,
int pass) {
if (!getItemVisible(series, item)) {
return;
}
super.drawItem(g2, state, dataArea, info, plot, domainAxis, rangeAxis, dataset, series, item, crosshairState, pass);
}
然而,输出可能不是预期的输出:
上面的链接后面有更复杂的提案。
答案 1 :(得分:0)
根据上述答案,这是我修改过的代码:
package test;
import java.awt.BorderLayout;
import java.awt.Graphics2D;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.Rectangle2D;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import org.jfree.chart.*;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.labels.StandardXYToolTipGenerator;
import org.jfree.chart.plot.CrosshairState;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.PlotRenderingInfo;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.StackedXYAreaRenderer2;
import org.jfree.chart.renderer.xy.XYItemRendererState;
import org.jfree.data.time.Millisecond;
import org.jfree.data.time.TimeTableXYDataset;
import org.jfree.data.xy.*;
public class Test2
{
private static final long serialVersionUID = 1L;
public Test2(String s)
{
JFreeChart jfreechart = createChart(createDataset());
JPanel jpanel = new ChartPanel(jfreechart);
JPanel masterPanel = new JPanel(new BorderLayout());
masterPanel.add(jpanel, BorderLayout.CENTER);
masterPanel.add(addUserControls(jfreechart), BorderLayout.EAST);
JFrame frame = new JFrame();
frame.add(masterPanel);
frame.pack();
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
private static TimeTableXYDataset createDataset()
{
TimeTableXYDataset xyseries = new TimeTableXYDataset();
xyseries.add(new Millisecond(), 5D, "Series 1");
xyseries.add(new Millisecond(), 10D, "Series 1");
xyseries.add(new Millisecond(), 15D, "Series 1");
xyseries.add(new Millisecond(), 20D, "Series 1");
xyseries.add(new Millisecond(), 15D, "Series 2");
xyseries.add(new Millisecond(), 20D, "Series 2");
xyseries.add(new Millisecond(), 25D, "Series 2");
xyseries.add(new Millisecond(), 30D, "Series 2");
return xyseries;
}
private static JFreeChart createChart(TableXYDataset tablexydataset)
{
JFreeChart jfreechart = ChartFactory.createStackedXYAreaChart("Stacked XY Area Chart Demo 1", "X Value", "Y Value", tablexydataset, PlotOrientation.VERTICAL, true, true, false);
XYPlot xyplot = (XYPlot)jfreechart.getPlot();
StackedXYAreaRenderer2 stackedxyarearenderer2 = new StackedXYAreaRenderer2() {
public void drawItem(Graphics2D g2,
XYItemRendererState state,
Rectangle2D dataArea,
PlotRenderingInfo info,
XYPlot plot,
ValueAxis domainAxis,
ValueAxis rangeAxis,
XYDataset dataset,
int series,
int item,
CrosshairState crosshairState,
int pass) {
if (!getItemVisible(series, item)) {
return;
}
super.drawItem(g2, state, dataArea, info, plot, domainAxis, rangeAxis, dataset, series, item, crosshairState, pass);
}
};
stackedxyarearenderer2.setToolTipGenerator(new StandardXYToolTipGenerator());
xyplot.setRenderer(0, stackedxyarearenderer2);
return jfreechart;
}
public static JPanel createDemoPanel()
{
JFreeChart jfreechart = createChart(createDataset());
return new ChartPanel(jfreechart);
}
public static void main(String args[])
{
Test2 stackedxyareachartdemo1 = new Test2("Stacked XY Area Chart Demo 1");
}
public JPanel addUserControls(JFreeChart chartPlot) {
JPanel userPanel = new JPanel(new GridLayout(2,2));
JCheckBox Series0Button = new JCheckBox("Series 1");
Series0Button.setSelected(true);
Series0Button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
System.out.println("Series count" + chartPlot.getXYPlot().getSeriesCount());
if (Series0Button.isSelected()) {
StackedXYAreaRenderer2 renderer = (StackedXYAreaRenderer2) chartPlot.getXYPlot().getRenderer();
renderer.setSeriesVisible(0, Boolean.TRUE, true);
} else {
StackedXYAreaRenderer2 renderer = (StackedXYAreaRenderer2) chartPlot.getXYPlot().getRenderer();
renderer.setSeriesVisible(0, Boolean.FALSE, true);
}
}
});
JCheckBox series1Button = new JCheckBox("Series 2");
series1Button.setSelected(true);
series1Button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
if (series1Button.isSelected()) {
StackedXYAreaRenderer2 renderer = (StackedXYAreaRenderer2) chartPlot.getXYPlot().getRenderer();
renderer.setSeriesVisible(1, Boolean.TRUE, true);
} else {
StackedXYAreaRenderer2 renderer = (StackedXYAreaRenderer2) chartPlot.getXYPlot().getRenderer();
renderer.setSeriesVisible(1, Boolean.FALSE, true);
}
}
});
userPanel.add(Series0Button);
userPanel.add(series1Button);
return userPanel;
}
}