选择要显示的一系列数据 [英] Choose the series of data that you want to display
问题描述
我有一系列包含多个数据系列的图表:
我希望能够选择我想要显示的系列。例如,只有 0°
和 20°
。有没有一种简单的方法可以通过操作图表而不使用 JCheckBox
来实现这一点?我希望能够这样做,例如,点击系列的图例。
如图所示
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 * /
公共类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(系列);
}
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(图表){
@Override
public Dimension getPreferredSize(){
return new Dimension(640,480);
}
};
chartPanel.addChartMouseListener(new ChartMouseListener(){
@Override
public void chartMouseClicked(ChartMouseEvent e){
ChartEntity ce = e.getEntity();
if(b) 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);
}
}
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屋!