gpt4 book ai didi

java - 禁用 StackedXYAreaChart 中的系列 - jfreechart

转载 作者:行者123 更新时间:2023-12-01 11:19:23 26 4
gpt4 key购买 nike

我正在尝试创建一个用户过滤器,该过滤器将根据用户点击启用/禁用一系列。我添加了一个复选框按钮,它将启用/禁用该系列。

当我单击按钮时,只有图例项消失。地 block 上的区域仍然可见。我的做法有错吗?或者这在 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;
}
}

最佳答案

问题描述here对于StackedXYBarRenderer

最简单的解决方案是重写默认渲染器的 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);
}

但是,输出可能不是预期的:

enter image description here

上面的链接有更复杂的建议。

关于java - 禁用 StackedXYAreaChart 中的系列 - jfreechart,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31438489/

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