Tuesday, 15 May 2012

java - How to get diamond shape for points in JFreechart -



java - How to get diamond shape for points in JFreechart -

i need diamond shapes on timeseries in jfreechart, unable it. can please guide code should added code below accomplish diamond shape points , how alter colours of lines?

(the programme uses rs , stmt , other things derived db , defined somewhere else. programme works right now, problem looks super boring.)

timeseries s1 = new timeseries("technology", day.class); timeseries s2 = new timeseries("entertainment", day.class); timeseries s3 = new timeseries("soap", day.class); timeseries s4 = new timeseries("music", day.class); timeseries s5 = new timeseries("native", day.class); timeseries s6 = new timeseries("speciality", day.class); timeseries s7 = new timeseries("science", day.class); simpledateformat sdf = new simpledateformat("yyyy-mm-dd"); date plotdate; if (!(combo_individualid.getmodel().getsize() == 0)) { string sql = "" + "select * " + "from `customerbasetag` " + "where `individual_idindividual` =? "; seek { stmt = conn.preparestatement(sql); stmt.setstring(1, combo_individualid.getselecteditem().tostring()); rs = stmt.executequery(); while (rs.next()) { seek { plotdate = sdf.parse(rs.getstring("session_date")); s1.add(new day(plotdate), new integer(integer.parseint(rs.getstring("technology")))); s2.add(new day(plotdate), new integer(integer.parseint(rs.getstring("entertainment")))); s3.add(new day(plotdate), new integer(integer.parseint(rs.getstring("soap")))); s4.add(new day(plotdate), new integer(integer.parseint(rs.getstring("music")))); s5.add(new day(plotdate), new integer(integer.parseint(rs.getstring("native")))); s6.add(new day(plotdate), new integer(integer.parseint(rs.getstring("speciality")))); s7.add(new day(plotdate), new integer(integer.parseint(rs.getstring("science")))); } grab (parseexception ex) { joptionpane.showmessagedialog(null, "parse exception" + ex.getmessage()); } } } grab (sqlexception ex) { joptionpane.showmessagedialog(null, "error during session select" + ex.getmessage()); } /*note: chart plotting here*/ timeseriescollection dataset = new timeseriescollection(); dataset.addseries(s1); dataset.addseries(s2); dataset.addseries(s3); dataset.addseries(s4); dataset.addseries(s5); dataset.addseries(s6); dataset.addseries(s7); jfreechart chart = chartfactory.createtimeserieschart( "ts chart", "date", "value", dataset, true, true, false); xyplot plot = (xyplot) chart.getplot(); plot.setbackgroundpaint(color.lightgray); plot.setdomaingridlinepaint(color.white); plot.setrangegridlinepaint(color.white); plot.setaxisoffset(new rectangleinsets(5.0, 5.0, 5.0, 5.0)); plot.setdomaincrosshairvisible(true); plot.setrangecrosshairvisible(true); chartframe f = new chartframe("individual selection evaluation", chart); f.setvisible(true); f.setsize(800, 600); f.setlocationrelativeto(null); } else { joptionpane.showmessagedialog(null, "please select individual"); }

i have updated code not work still, , maintain getting old chart back. heres code.

timeseriescollection dataset = new timeseriescollection(); dataset.addseries(s1); dataset.addseries(s2); dataset.addseries(s3); dataset.addseries(s4); dataset.addseries(s5); dataset.addseries(s6); dataset.addseries(s7); jfreechart chart = chartfactory.createtimeserieschart("time series chart individual id: "+combo_individualid.getselecteditem().tostring() , "date", "value", dataset, true, true, false); shape theshape = shapeutilities.creatediamond(1); xyplot plot = (xyplot) chart.getplot(); plot.setbackgroundpaint(color.lightgray); plot.setdomaingridlinepaint(color.white); plot.setrangegridlinepaint(color.white); plot.setaxisoffset(new rectangleinsets(5.0, 5.0, 5.0, 5.0)); plot.setdomaincrosshairvisible(true); plot.setrangecrosshairvisible(true); xyitemrenderer renderer = plot.getrenderer(); renderer.setseriesshape(0, theshape); renderer.setseriesshape(1, theshape); renderer.setseriesshape(2, theshape); renderer.setseriesshape(3, theshape); renderer.setseriesshape(4, theshape); renderer.setseriesshape(5, theshape); renderer.setseriesshape(6, theshape); chartframe f = new chartframe("individual selection evaluation", chart); f.setvisible(true); f.setsize(800, 600); f.setlocationrelativeto(null);

shapeutilities.creatediamond() can create diamond shape; can apply shown in example of different shape.

addendum: default, createtimeserieschart() creates xylineandshaperenderer can modify shown below , here.

xyplot plot = (xyplot) chart.getplot(); xylineandshaperenderer r = (xylineandshaperenderer) plot.getrenderer(); r.setseriesshape(0, shapeutilities.creatediamond(5)); r.setseriesshapesvisible(0, true);

sscce:

import java.awt.dimension; import java.util.random; import javax.swing.jframe; import org.jfree.chart.*; import org.jfree.chart.plot.xyplot; import org.jfree.chart.renderer.xy.xylineandshaperenderer; import org.jfree.data.time.day; import org.jfree.data.time.timeseries; import org.jfree.data.time.timeseriescollection; import org.jfree.data.xy.xydataset; import org.jfree.util.shapeutilities; /** @see http://stackoverflow.com/a/14822991/230513 */ public class test { private static final int n = 16; private static final random random = new random(); private static xydataset createdataset() { final timeseries series = new timeseries("data"); day current = new day(); (int = 0; < n; i++) { series.add(current, math.abs(random.nextgaussian())); current = (day) current.next(); } homecoming new timeseriescollection(series); } private static jfreechart createchart(final xydataset dataset) { jfreechart chart = chartfactory.createtimeserieschart( "test", "day", "value", dataset, false, false, false); xyplot plot = (xyplot) chart.getplot(); xylineandshaperenderer r = (xylineandshaperenderer) plot.getrenderer(); r.setseriesshape(0, shapeutilities.creatediamond(5)); r.setseriesshapesvisible(0, true); homecoming chart; } public static void main(string[] args) { jframe f = new jframe(); f.setdefaultcloseoperation(jframe.exit_on_close); xydataset dataset = createdataset(); jfreechart chart = createchart(dataset); chartpanel chartpanel = new chartpanel(chart) { @override public dimension getpreferredsize() { homecoming new dimension(630, 480); } }; f.add(chartpanel); f.pack(); f.setlocationrelativeto(null); f.setvisible(true); } }

java swing jfreechart

No comments:

Post a Comment