java - How do I draw a short line over a bar on an XYPlot using XYBarRenderer? -
i'm using jfreechart, xyplot , xybarrenderer plot horizontal bar graph. need draw line on each bar in graph that's width of bar, (the red lines):
those red lines aren't same size blue bars, fine, can't cover entire graph. i've been toying around answers posted this question , i've been trying use xybarrenderer.draw... , xybarrenderer.add... no avail.
any appreciated!
ideally in jfreechart create chart using dataset implements intervalcategorydataset interface , renderer can render both bars , median markers directly data. however, main implementation of dataset interface bit cumbersome use , there not renderer draws visual representation looking for.
the next best thing (that can think of) use overlaid chart, allows median markers overlaid on top of bars - requires 2 datasets , 2 renderers. here result (the source code follows):
/* ======================= * overlaidchartdemo1.java * ======================= * * copyright (c) 2014, object refinery limited. * rights reserved. * * http://www.jfree.org/jfreechart/index.html * * redistribution , use in source , binary forms, or without * modification, permitted provided following conditions met: * - redistributions of source code must retain above copyright * notice, list of conditions , following disclaimer. * - redistributions in binary form must reproduce above copyright * notice, list of conditions , following disclaimer in * documentation and/or other materials provided distribution. * - neither name of object refinery limited nor * names of contributors may used endorse or promote products * derived software without specific prior written permission. * * software provided copyright holders , contributors "as is" * , express or implied warranties, including, not limited to, * implied warranties of merchantability , fitness particular purpose * disclaimed. in no event shall object refinery limited liable * direct, indirect, incidental, special, exemplary, or consequential damages * (including, not limited to, procurement of substitute goods or services; * loss of use, data, or profits; or business interruption) caused , * on theory of liability, whether in contract, strict liability, or tort * (including negligence or otherwise) arising in way out of use of * software, if advised of possibility of such damage. * * original author: david gilbert (for object refinery limited); * contributor(s): -; * * changes * ------- * 10-sep-2014 : version 1 (dg); * */ package org.jfree.chart.demo; import java.awt.basicstroke; import java.awt.color; import java.awt.dimension; import org.jfree.chart.chartfactory; import org.jfree.chart.chartpanel; import org.jfree.chart.jfreechart; import org.jfree.chart.standardcharttheme; import org.jfree.chart.axis.axislocation; import org.jfree.chart.axis.categoryaxis; import org.jfree.chart.axis.numberaxis; import org.jfree.chart.axis.valueaxis; import org.jfree.chart.plot.categoryplot; import org.jfree.chart.plot.datasetrenderingorder; import org.jfree.chart.plot.plotorientation; import org.jfree.chart.renderer.category.intervalbarrenderer; import org.jfree.chart.renderer.category.levelrenderer; import org.jfree.chart.renderer.category.standardbarpainter; import org.jfree.data.category.categorydataset; import org.jfree.data.category.defaultcategorydataset; import org.jfree.data.category.defaultintervalcategorydataset; import org.jfree.data.category.intervalcategorydataset; import org.jfree.ui.applicationframe; import org.jfree.ui.refineryutilities; /** * sample application stack overflow question * http://stackoverflow.com/questions/25772655/how-do-i-draw-a-short-line-over-a-bar-on-an-xyplot-using-xybarrenderer. */ public class overlaidchartdemo1 extends applicationframe { /** * creates new demo instance. * * @param title frame title. */ public overlaidchartdemo1(string title) { super(title); categorydataset dataset = createdataset1(); jfreechart chart = createchart(dataset); chartpanel chartpanel = new chartpanel(chart, false); chartpanel.setfillzoomrectangle(true); chartpanel.setmousewheelenabled(true); chartpanel.setpreferredsize(new dimension(600, 480)); setcontentpane(chartpanel); } private static intervalcategorydataset createdataset1() { double[] start = new double[] { -4.1, -2.2, -2.1, -1.2, -0.4, -0.2, -0.1, -0.15, 0, -0.1, -0.1, 0, 0, 0, 0 }; double[] end = new double[] { 2.9, 3.6, 3.3, 2.5, 2.8, 2, 2.1, 2, 2, 1.8, 1.7, 1.8, 1.6, 1.2, 1.2 }; defaultintervalcategorydataset dataset = new defaultintervalcategorydataset(new double[][]{ start }, new double[][]{ end }); dataset.setcategorykeys(new string[] { "dedicated short bias", "managed futures", "s&p 500", "long/short equity", "emerging markets", "convertible arbitrage", "event driven - multi strategy", "event driven", "dj/credit suisse hedge fund index", "event driven - distressed", "multi-strategy", "global macro", "fixed income arbitrage", "event driven - risk arbitrage", "equity market neutral"}); return dataset; } private static categorydataset createdataset2() { defaultcategorydataset dataset = new defaultcategorydataset(); dataset.addvalue(-1.3, "s1", "dedicated short bias"); dataset.addvalue(0.7, "s1", "managed futures"); dataset.addvalue(1.2, "s1", "s&p 500"); dataset.addvalue(1.1, "s1", "long/short equity"); dataset.addvalue(1.4, "s1", "emerging markets"); dataset.addvalue(1.05, "s1", "convertible arbitrage"); dataset.addvalue(1.2, "s1", "event driven - multi strategy"); dataset.addvalue(1.1, "s1", "event driven"); dataset.addvalue(0.7, "s1", "dj/credit suisse hedge fund index"); dataset.addvalue(1, "s1", "event driven - distressed"); dataset.addvalue(0.8, "s1", "multi-strategy"); dataset.addvalue(1.2, "s1", "global macro"); dataset.addvalue(0.8, "s1", "fixed income arbitrage"); dataset.addvalue(0.5, "s1", "event driven - risk arbitrage"); dataset.addvalue(0.6, "s1", "equity market neutral"); return dataset; } /** * creates sample chart. * * @param dataset dataset. * * @return chart. */ private static jfreechart createchart(categorydataset dataset) { categoryaxis xaxis = new categoryaxis(null); valueaxis yaxis = new numberaxis("monthly return (%)"); intervalbarrenderer renderer = new intervalbarrenderer(); renderer.setbarpainter(new standardbarpainter()); renderer.setseriespaint(0, new color(10, 12, 118)); renderer.setshadowvisible(false); categoryplot plot = new categoryplot(dataset, xaxis, yaxis, renderer); plot.setorientation(plotorientation.horizontal); plot.setrangeaxislocation(axislocation.bottom_or_left); plot.setbackgroundpaint(new color(230, 230, 230)); plot.setrangegridlinepaint(color.white); plot.setrangegridlinestroke(new basicstroke(0.5f)); // overlay second dataset plot.setdataset(1, createdataset2()); levelrenderer renderer2 = new levelrenderer(); renderer2.setseriesstroke(0, new basicstroke(4.0f, basicstroke.cap_round, basicstroke.join_round)); renderer2.setseriespaint(0, color.red); plot.setrenderer(1, renderer2); plot.setdatasetrenderingorder(datasetrenderingorder.forward); jfreechart chart = new jfreechart( "interquartile range , median: monthly returns", plot); chart.removelegend(); chart.setbackgroundpaint(color.white); return chart; } /** * starting point demonstration application. * * @param args ignored. */ public static void main(string[] args) { overlaidchartdemo1 demo = new overlaidchartdemo1( "jfreechart: overlaidchartdemo1.java"); demo.pack(); refineryutilities.centerframeonscreen(demo); demo.setvisible(true); } }
Comments
Post a Comment