/*****/Package COM. huaxia. bank. test; import Java. AWT. color; import Java. AWT. dimension; import Java. AWT. font; import Java. AWT. toolkit; import Java. AWT. event. windowadapter; import Java. AWT. event. using wevent; import Java. text. dateformat; import Java. text. decimalformat; import Java. text. numberformat; import Java. text. simpledateformat; import Org. jfree. chart. chartfactory; import Org. jfree. chart. chartframe; import Org. jfree. chart. jfreechart; import Org. jfree. chart. axis. dateaxis; import Org. jfree. chart. axis. datetickunit; import Org. jfree. chart. axis. valueaxis; import Org. jfree. chart. labels. standardcategorytooltipgenerator; import Org. jfree. chart. labels. standardxytooltipgenerator; import Org. jfree. chart. labels. xytooltipgenerator; import Org. jfree. chart. plot. plotorientation; import Org. jfree. chart. plot. xyplot; import Org. jfree. chart. renderer. XY. standardxyitemrenderer; import Org. jfree. chart. renderer. XY. xyitemrenderer; import Org. jfree. chart. title. texttitle; import Org. jfree. data. time. day; import Org. jfree. data. time. hour; import Org. jfree. data. time. month; import Org. jfree. data. time. timeseries; import Org. jfree. data. time. timeseriescollection; import Org. jfree. data. XY. xydataset; import Org. jfree. UI. applicationframe; import Org. jfree. UI. rectangleinsets;/*** @ author cuiran **/public class timeseriestest {public final static string month = "month"; public final static string day = "day "; public final static string hour = "Hour"; private jfreechart rchart = NULL; // The Chart object Public String charttitle = ""; // The chart title Public String chartxdesc = ""; // X axis title Public String chartydesc = ""; // y axis title Public String chartseriesdesc = ""; // curve description Public String chartseriesdesc1 = ""; // curve description public int graphwidth = 600; // default width public int graphhigh = 400; // default height Public String timeformat = "MM/YYYY"; // daily: mm-dd, by hour: hh: Mm // indicates the statistical cycle type (year, month, day, And hour) selected by the user ). // year: year, month: month, day: day, hour: hour Public String periodtype = ""; // used to determine the time interval public int dateinterval = 1; // statistical result dataset timeseriescollection statdataset = new timeseriescollection (); Timeseries monthseries = NULL; // Timeseries monthseries1 = NULL; // Timeseries dayseries = NULL; // day Statistical Chart Data Set Timeseries hourseries = NULL; // hour statistical chart data set public void createtread () {settimeseriesstattype ();} /*** create a trend chart ** @ return jfreechart chart object jfreechart */private jfreechart createtrendchart () {jfreechart _ freechart = chartfactory. createtimeserieschart (charttitle, chartxdesc, chartydesc, gettimeseriesstatdataset (), true); _ freechart. setbackgroundpaint (color. white); xyplot _ xyplot = _ freechart. getxyplot (); _ xyplot. setorientation (plotorientation. vertical); _ xyplot. setbackgroundpaint (color. lightgray); _ xyplot. setdomaingridlinepaint (color. white); _ xyplot. setrangegridlinepaint (color. white); _ xyplot. setaxisoffset (New rectangleinsets (1.0, 2.0, 2.0, 10.0); xyitemrenderer Renderer = _ xyplot. getrenderer (); dateformat df = new simpledateformat ("yyyy Year mm Month"); numberformat NF = numberformat. getnumberinstance (); standardxytooltipgenerator toolg = new standardxytooltipgenerator (standardxytooltipgenerator. default_tool_tip_format, DF, NF); Renderer. settooltipgenerator (toolg); // dateaxis = (dateaxis) _ xyplot. getdomainaxis (); If (periodtype. equalsignorecase ("month") {If (dateinterval> 0) {dateaxis. settickunit (New datetickunit (datetickunit. month, dateinterval) ;}} else if (periodtype. specified signorecase ("day") {If (dateinterval> 0) {dateaxis. settickunit (New datetickunit (datetickunit. day, dateinterval) ;}} else if (periodtype. equalsignorecase ("Hour") {If (dateinterval> 0) {dateaxis. settickunit (New datetickunit (datetickunit. hour, dateinterval);} dateaxis. setdateformatoverride (New simpledateformat (timeformat); Return _ freechart ;} /*** add trend chart data * @ Param periodtype Interval Type * @ Param year * @ Param month * @ Param day date * @ Param hour time * @ Param statdata Statistics */ public void addtimeseriesunitdata (INT year, int month, int statdata) {If (periodtype. repeated signorecase ("month") {If (monthseries = NULL) {monthseries = new Timeseries (chartseriesdesc, month. class);} monthseries. add (new month (month, year), statdata); system. out. println ("month");} else if (periodtype. specified signorecase ("day") {If (dayseries = NULL) {dayseries = new Timeseries (chartseriesdesc, day. class);} // dayseries. add (New Day (day, month, year), statdata);} else if (periodtype. equalsignorecase ("Hour") {If (hourseries = NULL) {hourseries = new Timeseries (chartseriesdesc, hour. class);} // hourseries. add (New hour (hour, day, month, year), statdata );}} /*** add trend chart data * @ Param periodtype Interval Type * @ Param year * @ Param month * @ Param day date * @ Param hour time * @ Param statdata Statistics */ public void addtimeseriesunitdataall (INT year, int month, int statdata) {If (periodtype. repeated signorecase ("month") {If (monthseries1 = NULL) {monthseries1 = new Timeseries (chartseriesdesc1, month. class);} monthseries1.add (new month (month, year), statdata); system. out. println ("month");} else if (periodtype. specified signorecase ("day") {If (dayseries = NULL) {dayseries = new Timeseries (chartseriesdesc1, day. class);} // dayseries. add (New Day (day, month, year), statdata);} else if (periodtype. equalsignorecase ("Hour") {If (hourseries = NULL) {hourseries = new Timeseries (chartseriesdesc1, hour. class);} // hourseries. add (New hour (hour, day, month, year), statdata );}} /*** set the range type of trend chart Statistics * @ Param periodtype */private void settimeseriesstattype () {If (periodtype. specified signorecase ("month") {statdataset. addseries (monthseries); statdataset. addseries (monthseries1);} else if (periodtype. specified signorecase ("day") {statdataset. addseries (dayseries);} else if (periodtype. equalsignorecase ("Hour") {statdataset. addseries (hourseries) ;}}/*** get the statistical data of the time sequence chart * @ return xydataset Statistical Data */private xydataset gettimeseriesstatdataset () {statdataset. setdomainispointsintime (true); Return statdataset;} public int getdateinterval () {return dateinterval;} // font configuration method (solving Chinese problems) Private Static void configfont (jfreechart) {// configure font xfont = new font ("", Font. plain, 12); // X axis font yfont = new font ("", Font. plain, 12); // y axis font kfont = new font ("", Font. plain, 12); // The bottom font titlefont = new font ("", Font. bold, 25); // image title xyplot plot = chart. getxyplot (); // Drawing Structure object of the image // picture title chart. settitle (New texttitle (chart. gettitle (). gettext (), titlefont); // bottom // chart. getlegend (). setitemfont (kfont); // The title Font chart in the horizontal axis. getlegend (). setitemfont (kfont); // horizontal axis list font plot. getdomainaxis (). setticklabelfont (kfont); // The title Font of the horizontal axis. getdomainaxis (). setlabelfont (kfont); // y axis valueaxis rangeaxis = plot. getrangeaxis (); rangeaxis. setlabelfont (yfont); rangeaxis. setlabelpaint (color. blue); // font color rangeaxis. setticklabelfont (yfont);} public void setdateinterval (INT dateinterval) {This. dateinterval = dateinterval;} public static void main (string arhs []) {timeseriestest trendchart = new timeseriestest (); trendchart. charttitle = "trend chart of one year"; trendchart. chartseriesdesc = "Confirm quantity"; trendchart. chartseriesdesc1 = "risk quantity"; trendchart. chartxdesc = "month"; trendchart. chartydesc = "quantity"; trendchart. graphhigh = 400; trendchart. graphwidth = 600; trendchart. timeformat = "yyyy Year mm Month"; trendchart. periodtype = timeseriestest. month; double basedata = 100.0; double RDATA = basedata; trendchart. addtimeseriesunitdata (2011,); trendchart. addtimeseriesunitdata (2011,); trendchart. addtimeseriesunitdata (2012, 1, 50); trendchart. addtimeseriesunitdata (2012, 2, 80); trendchart. addtimeseriesunitdata (2012, 3, 30); trendchart. addtimeseriesunitdata (2012, 4, 10); trendchart. addtimeseriesunitdataall (2011,); trendchart. addtimeseriesunitdataall (2011,); trendchart. addtimesseriesunitdataall (2012, 1, 70); trendchart. addtimeseriesunitdataall (2012, 2, 90); trendchart. addtimeseriesunitdataall (2012, 3, 50); trendchart. addtimeseriesunitdataall (2012, 4, 40); trendchart. createtread (); jfreechart chart = trendchart. createtrendchart (); trendchart. configfont (Chart); Final chartframe Preview = new chartframe ("one year Chart", chart); preview. addwindowlistener (New windowadapter () {public void windowclosing (final protected wevent event) {preview. dispose () ;}}); preview. pack (); // adjust the size and position of the preview window, which is suitable for the screen and centered. // dimension screensize = toolkit. getdefatooltoolkit (). getscreensize (); // preview. setsize (screensize. width, screensize. height-50); // applicable to the screen. 50 indicates taking the toolbar into account. // dimension framesize = preview. getsize (); // If (framesize. height> screensize. height) {/// framesize. height = screensize. height; //} // If (framesize. width> screensize. width) {// framesize. width = screensize. width; //} // preview. setlocation (screensize. width-framesize. width)/2, // (screensize. height-framesize. height-50)/2); // display the report preview window preview. setvisible (true );}}
Running Effect
Some code:
Dateformat df = new simpledateformat ("YYYY mm"); numberformat NF = numberformat. getnumberinstance (); standardxytooltipgenerator toolg = new standardxytooltipgenerator (standardxytooltipgenerator. default_tool_tip_format, DF, NF); Renderer. settooltipgenerator (toolg );
Add the code to display the corresponding graph points.
Xylineandshaperenderer = (xylineandshaperenderer) _ xyplot. getrenderer (); // sets the grid background color _ xyplot. setbackgroundpaint (color. white); // set the grid vertical line color _ xyplot. setdomaingridlinepaint (color. pink); // set the grid horizontal line color _ xyplot. setrangegridlinepaint (color. pink); // set the distance between the graph and the XY axis _ xyplot. setaxisoffset (New rectangleinsets (0d, 0d, 0d, 10d); // sets whether the curve shows the data point xylineandshaperenderer. setbaseshapesvisible (true); // set the value of each data point in the curve.