选择要显示的数据系列 [英] Choose the series of data that you want to display
问题描述
我有一个包含多个数据系列的图:
我希望能够选择我想显示的系列.例如,只有 0°
和 20°
的.是否有一种简单的方法可以通过在不使用 JCheckBox
的情况下操作图表来做到这一点?我希望能够做到这一点,例如,通过点击系列的图例.
如图
import java.awt.Dimension;导入 java.awt.EventQueue;导入 javax.swing.JFrame;导入 org.jfree.chart.ChartMouseEvent;导入 org.jfree.chart.ChartMouseListener;导入 org.jfree.chart.ChartPanel;导入 org.jfree.chart.JFreeChart;导入 org.jfree.chart.axis.NumberAxis;导入 org.jfree.chart.entity.ChartEntity;导入 org.jfree.chart.entity.LegendItemEntity;导入 org.jfree.chart.entity.XYItemEntity;导入 org.jfree.chart.labels.StandardXYToolTipGenerator;导入 org.jfree.chart.plot.PlotOrientation;导入 org.jfree.chart.plot.XYPlot;导入 org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;导入 org.jfree.data.xy.XYSeries;导入 org.jfree.data.xy.XYSeriesCollection;/** @see https://stackoverflow.com/a/43286042/230513 */公共类 VisibleTest {私人无效显示(){JFrame f = new JFrame("Test");f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);XYSeriesCollection 数据集 = new XYSeriesCollection();for (int i = 0; i <3; i++) {XYSeries 系列 = new XYSeries("value" + i);for (double t = 0; t <2 * Math.PI; t += 0.5) {series.add(t, Math.sin(t) + i);}数据集.addSeries(series);}NumberAxis xAxis = new NumberAxis("域");NumberAxis yAxis = new NumberAxis("range");XYLineAndShapeRenderer 渲染器 = new XYLineAndShapeRenderer(true, true);renderer.setBaseToolTipGenerator(new StandardXYToolTipGenerator());XYPlot plot = new XYPlot(dataset, xAxis, yAxis, renderer);JFreeChart chart = new JFreeChart("Test", plot);ChartPanel chartPanel = new ChartPanel(chart) {@覆盖公共维度 getPreferredSize() {返回新维度(640, 480);}};chartPanel.addChartMouseListener(new ChartMouseListener() {@覆盖public void chartMouseClicked(ChartMouseEvent e) {ChartEntity ce = e.getEntity();if (ce instanceof XYItemEntity) {XYItemEntity item = (XYItemEntity) ce;renderer.setSeriesVisible(item.getSeriesIndex(), false);} else if (ce instanceof LegendItemEntity) {LegendItemEntity item = (LegendItemEntity) ce;比较键 = item.getSeriesKey();renderer.setSeriesVisible(dataset.getSeriesIndex(key), false);} 别的 {for (int i = 0; i < dataset.getSeriesCount(); i++) {renderer.setSeriesVisible(i, true);}}}@覆盖public void chartMouseMoved(ChartMouseEvent e) {}});f.add(chartPanel);f.pack();f.setLocationRelativeTo(null);f.setVisible(true);}公共静态无效主(字符串 [] args){EventQueue.invokeLater(new VisibleTest()::display);}}
I have a plot with multiple series of data:
I want to be able to pick the series that I want to display. For example, only the 0°
and 20°
ones. Is there a simple way to do this by manipulating the chart without using JCheckBox
? I want to be able to do this, for example, by clicking on the legend of the series.
As shown here, JCheckBox
is more flexible, but clicking directly on the chart may be more convenient. The example below adds a ChartMouseListener
that makes a series invisible when clicking on either an XYItemEntity
in the series or its LegendItemEntity
. Of course, once a series is invisible, it cannot be clicked on again; you'll need a way to restore visibility. Among some alternatives, the first is illustrated below:
Restore the visibility of all series when clicking elsewhere on the chart.
Combine this with the approach cited above, toggling the
JCheckBox
accordingly in your implementation ofchartMouseClicked()
.Loop through the series in a button handler, restoring the visibility of each.
import java.awt.Dimension;
import java.awt.EventQueue;
import javax.swing.JFrame;
import org.jfree.chart.ChartMouseEvent;
import org.jfree.chart.ChartMouseListener;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.entity.ChartEntity;
import org.jfree.chart.entity.LegendItemEntity;
import org.jfree.chart.entity.XYItemEntity;
import org.jfree.chart.labels.StandardXYToolTipGenerator;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;
/** @see https://stackoverflow.com/a/43286042/230513 */
public class VisibleTest {
private void display() {
JFrame f = new JFrame("Test");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
XYSeriesCollection dataset = new XYSeriesCollection();
for (int i = 0; i < 3; i++) {
XYSeries series = new XYSeries("value" + i);
for (double t = 0; t < 2 * Math.PI; t += 0.5) {
series.add(t, Math.sin(t) + i);
}
dataset.addSeries(series);
}
NumberAxis xAxis = new NumberAxis("domain");
NumberAxis yAxis = new NumberAxis("range");
XYLineAndShapeRenderer renderer = new XYLineAndShapeRenderer(true, true);
renderer.setBaseToolTipGenerator(new StandardXYToolTipGenerator());
XYPlot plot = new XYPlot(dataset, xAxis, yAxis, renderer);
JFreeChart chart = new JFreeChart("Test", plot);
ChartPanel chartPanel = new ChartPanel(chart) {
@Override
public Dimension getPreferredSize() {
return new Dimension(640, 480);
}
};
chartPanel.addChartMouseListener(new ChartMouseListener() {
@Override
public void chartMouseClicked(ChartMouseEvent e) {
ChartEntity ce = e.getEntity();
if (ce instanceof XYItemEntity) {
XYItemEntity item = (XYItemEntity) ce;
renderer.setSeriesVisible(item.getSeriesIndex(), false);
} else if (ce instanceof LegendItemEntity) {
LegendItemEntity item = (LegendItemEntity) ce;
Comparable key = item.getSeriesKey();
renderer.setSeriesVisible(dataset.getSeriesIndex(key), false);
} else {
for (int i = 0; i < dataset.getSeriesCount(); i++) {
renderer.setSeriesVisible(i, true);
}
}
}
@Override
public void chartMouseMoved(ChartMouseEvent e) {}
});
f.add(chartPanel);
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
}
public static void main(String[] args) {
EventQueue.invokeLater(new VisibleTest()::display);
}
}
这篇关于选择要显示的数据系列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!