我正在尝试创建一个用户过滤器,该过滤器将基于用户单击来启用/禁用一系列操作。我添加了一个复选框按钮,它将启用/禁用该系列。
当我单击按钮时,只有图例项消失。图上的区域仍然可见。我的方法错了吗?还是在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;
}
}
最佳答案
该问题在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);
}
但是,输出可能不是预期的:
在上面的链接之后,还有更复杂的建议。
关于java - 在StackedXYAreaChart中禁用系列-jfreechart,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/31438489/