/**
* PeriodAxisChart 图表
* @param ChartTitle
* @param viceTitle
* @param Xname
* @param YName
* @param xydataset
* @return
*/
public static JFreeChart createPeriodAxisChart(String ChartTitle,String viceTitle, String Xname,String YName,XYDataset xydataset)
{
Font font = new Font("宋体", Font.BOLD, 24);// 必须要指定,否则乱码
Font Lablefont = new Font("宋体", Font.BOLD, 12);
JFreeChart jfreechart = ChartFactory.createTimeSeriesChart(ChartTitle, Xname, YName, xydataset, true, true, false);
XYPlot xyplot = (XYPlot)jfreechart.getPlot();
xyplot.setDomainCrosshairVisible(true);
xyplot.setRangeCrosshairVisible(true);
XYItemRenderer xyitemrenderer = xyplot.getRenderer();
if(xyitemrenderer instanceof XYLineAndShapeRenderer)
{
XYLineAndShapeRenderer xylineandshaperenderer = (XYLineAndShapeRenderer)xyitemrenderer;
xylineandshaperenderer.setBaseShapesVisible(false);
xylineandshaperenderer.setBaseShapesFilled(true);
xylineandshaperenderer.setBaseItemLabelsVisible(true);
}
PeriodAxis periodaxis = new PeriodAxis(Xname);
periodaxis.setTimeZone(TimeZone.getTimeZone("Pacific/Auckland"));
periodaxis.setAutoRangeTimePeriodClass(org.jfree.data.time.Day.class);
PeriodAxisLabelInfo aperiodaxislabelinfo[] = new PeriodAxisLabelInfo[3];
aperiodaxislabelinfo[0] = new PeriodAxisLabelInfo(org.jfree.data.time.Day.class, new SimpleDateFormat("d"));
aperiodaxislabelinfo[1] = new PeriodAxisLabelInfo(org.jfree.data.time.Month.class, new SimpleDateFormat("MMM"), new RectangleInsets(2D, 2D, 2D, 2D), Lablefont, Color.blue, false, new BasicStroke(0.0F), Color.lightGray);
aperiodaxislabelinfo[2] = new PeriodAxisLabelInfo(org.jfree.data.time.Year.class, new SimpleDateFormat("yyyy"));
periodaxis.setLabelInfo(aperiodaxislabelinfo);
xyplot.setDomainAxis(periodaxis);
ChartUtilities.applyCurrentTheme(jfreechart);
jfreechart.getTitle().setFont(font);
jfreechart.getLegend().setItemFont(Lablefont);
xyplot.getDomainAxis().setLabelFont(Lablefont);
xyplot.getRangeAxis().setLabelFont(Lablefont);
// 设置副标题
jfreechart.addSubtitle(new TextTitle("------" + viceTitle, new Font(
"Dialog", Font.ITALIC, 12)));
periodaxis.setLabelFont(Lablefont);
aperiodaxislabelinfo[1]= new PeriodAxisLabelInfo(org.jfree.data.time.Month.class, new SimpleDateFormat("MMM"), new RectangleInsets(2D, 2D, 2D, 2D), Lablefont, Color.blue, false, new BasicStroke(0.0F), Color.lightGray);
return jfreechart;
}
效果图:
转载于:https://blog.51cto.com/skykill/413511