java - Choose the series of data that you want to display -
i have plot multiple series of data:
i want able pick series want display. example, 0°
, 20°
ones. there simple way manipulating chart without using jcheckbox
? want able this, example, clicking on legend of series.
as shown here, jcheckbox
more flexible, clicking directly on chart may more convenient. example below adds chartmouselistener
makes series invisible when clicking on either xyitementity
in series or legenditementity
. of course, once series invisible, cannot clicked on again; you'll need way restore visibility. among alternatives, first illustrated below:
restore visibility of series when clicking elsewhere on chart.
combine approach cited above, toggling
jcheckbox
accordingly in implementation ofchartmouseclicked()
.loop through series in button handler, restoring 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(); (int = 0; < 3; i++) { xyseries series = new xyseries("value" + i); (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 { (int = 0; < 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); } }
Comments
Post a Comment