java - Change JFreeChart Histogram colors dynamically? -
i'm trying show histogram of image , show colors. i've done jfreechart , createxylinechart , getting data iterating on pixels.
to speed i'm trying "createhistogram". i've followed this code.
to update chart new values i've used 2 methods:
xyplot plot = (xyplot) chart.getplot(); plot.setdataset(sethistogram(red, green, blue));
being sethistogram method returns histogramdataset depending on checkbox activated (boolean red, green , blue).
that works intended.
the second thing must when update colors of each series, otherwise default value. did following more or less same procedure values:
private void sethistogramcolors(boolean red, boolean green, boolean blue) { xyplot plot = (xyplot) chart.getplot(); xybarrenderer renderer = (xybarrenderer) plot.getrenderer(); renderer.setbarpainter(new standardxybarpainter()); paint[] paintarray = null; if (red) { if (green) { if (blue) { paintarray = new paint[3]; paintarray[0] = new color(0x80ff0000, true);// translucent red, green & blue paintarray[1] = new color(0x8000ff00, true); paintarray[2] = new color(0x800000ff, true); } else { paintarray = new paint[2]; paintarray[0] = new color(0x80ff0000, true); paintarray[1] = new color(0x8000ff00, true); } } else { paintarray = new paint[1]; paintarray[0] = new color(0x80ff0000, true); } } else if (green) { if (blue) { paintarray = new paint[2]; paintarray[0] = new color(0x8000ff00, true); paintarray[1] = new color(0x800000ff, true); } else { paintarray = new paint[1]; paintarray[0] = new color(0x8000ff00, true); } } else if (blue){ paintarray = new paint[1]; paintarray[0] = new color(0x800000ff, true); } else { return; } plot.setdrawingsupplier(new defaultdrawingsupplier( paintarray, defaultdrawingsupplier.default_fill_paint_sequence, defaultdrawingsupplier.default_outline_paint_sequence, defaultdrawingsupplier.default_stroke_sequence, defaultdrawingsupplier.default_outline_stroke_sequence, defaultdrawingsupplier.default_shape_sequence)); }
but can see works when plot code first time, , next plots of different series take sames colors. here example, rgb color supposed be: [ when update red color goes green histogram , green's blue one:
is there anyway fix this?
based on fragment , comment, looks trying change display adding , removing series dataset in checkbox handlers. instead of altering model, change visibility of series in view. in case can give each checkbox suitable action
, shown here , below. note in particular visibleaction
uses series index matches corresponding color in custom drawingsupplier
.
import java.awt.borderlayout; import java.awt.color; import java.awt.eventqueue; import java.awt.paint; import java.awt.event.actionevent; import java.awt.image.bufferedimage; import java.awt.image.raster; import java.io.ioexception; import java.net.url; import javax.imageio.imageio; import javax.swing.abstractaction; import javax.swing.imageicon; import javax.swing.jcheckbox; import javax.swing.jframe; import javax.swing.jlabel; import javax.swing.jpanel; import org.jfree.chart.chartfactory; import org.jfree.chart.chartpanel; import org.jfree.chart.jfreechart; import org.jfree.chart.plot.defaultdrawingsupplier; import org.jfree.chart.plot.plotorientation; import org.jfree.chart.plot.xyplot; import org.jfree.chart.renderer.xy.standardxybarpainter; import org.jfree.chart.renderer.xy.xybarrenderer; import org.jfree.data.statistics.histogramdataset; /** * @see https://stackoverflow.com/q/40537278/230513 * @see https://stackoverflow.com/q/11870416/230513 * @see https://stackoverflow.com/a/28519356/230513 */ public class histogram { private static final int bins = 256; private final bufferedimage image = getimage(); private histogramdataset dataset; private xybarrenderer renderer; private bufferedimage getimage() { try { return imageio.read(new url( "http://i.imgur.com/kxxhih1.jpg")); } catch (ioexception e) { e.printstacktrace(system.err); } return null; } private chartpanel createchartpanel() { // dataset dataset = new histogramdataset(); raster raster = image.getraster(); final int w = image.getwidth(); final int h = image.getheight(); double[] r = new double[w * h]; r = raster.getsamples(0, 0, w, h, 0, r); dataset.addseries("red", r, bins); r = raster.getsamples(0, 0, w, h, 1, r); dataset.addseries("green", r, bins); r = raster.getsamples(0, 0, w, h, 2, r); dataset.addseries("blue", r, bins); // chart jfreechart chart = chartfactory.createhistogram("histogram", "value", "count", dataset, plotorientation.vertical, true, true, false); xyplot plot = (xyplot) chart.getplot(); renderer = (xybarrenderer) plot.getrenderer(); renderer.setbarpainter(new standardxybarpainter()); // translucent red, green & blue paint[] paintarray = { new color(0x80ff0000, true), new color(0x8000ff00, true), new color(0x800000ff, true) }; plot.setdrawingsupplier(new defaultdrawingsupplier( paintarray, defaultdrawingsupplier.default_fill_paint_sequence, defaultdrawingsupplier.default_outline_paint_sequence, defaultdrawingsupplier.default_stroke_sequence, defaultdrawingsupplier.default_outline_stroke_sequence, defaultdrawingsupplier.default_shape_sequence)); chartpanel panel = new chartpanel(chart); panel.setmousewheelenabled(true); return panel; } private jpanel createcontrolpanel() { jpanel panel = new jpanel(); panel.add(new jcheckbox(new visibleaction(0))); panel.add(new jcheckbox(new visibleaction(1))); panel.add(new jcheckbox(new visibleaction(2))); return panel; } private class visibleaction extends abstractaction { private final int i; public visibleaction(int i) { this.i = i; this.putvalue(name, (string) dataset.getserieskey(i)); this.putvalue(selected_key, true); renderer.setseriesvisible(i, true); } @override public void actionperformed(actionevent e) { renderer.setseriesvisible(i, !renderer.getseriesvisible(i)); } } private void display() { jframe f = new jframe("histogram"); f.setdefaultcloseoperation(jframe.exit_on_close); f.add(createchartpanel()); f.add(createcontrolpanel(), borderlayout.south); f.add(new jlabel(new imageicon(image)), borderlayout.west); f.pack(); f.setlocationrelativeto(null); f.setvisible(true); } public static void main(string[] args) { eventqueue.invokelater(() -> { new histogram().display(); }); } }
Comments
Post a Comment