Skip to content

Instantly share code, notes, and snippets.

@jecyhw
Last active May 13, 2017 07:07
Show Gist options
  • Save jecyhw/8ddf1b96f2e14464258f498b96b02227 to your computer and use it in GitHub Desktop.
Save jecyhw/8ddf1b96f2e14464258f498b96b02227 to your computer and use it in GitHub Desktop.
JFreeChart时间序列图绘制
//关于乱码解决,将相应的字体文件拷贝到$JAVA_HOME/jre/lib/fonts/fallback文件夹下即可
static private Font font = new Font("宋体", Font.PLAIN, 12);
public byte[] jFreeChartExample(String day) {
try {
Date end = DateUtils.parseDate(day, TableHelper.YYYY_MM_DD);
Date start = DateUtils.addDays(end, -29);
TimeSeriesCollection collTransfer = new TimeSeriesCollection();
TimeSeriesCollection collWaitTime = new TimeSeriesCollection();
TimeSeries seriesTransfer = new TimeSeries("平均转接次数");
TimeSeries seriesWaitTime = new TimeSeries("平均等待时间(s)");
//数据来源省略
collTransfer.addSeries(seriesTransfer);
collWaitTime.addSeries(seriesWaitTime);
JFreeChart jFreeChart = ChartFactory.createTimeSeriesChart(
cityMap.get(cityId) + DateFormatUtils.format(start, "yyyy.MM.dd") + "-" + DateFormatUtils.format(end, "yyyy.MM.dd") + " 生态400服务质量趋势图",// 图表标题
"", // 主轴标签(x轴)
"",// 范围轴标签(y轴)
collWaitTime, // 数据集
true, // 是否包含图例
true, // 提示信息是否显示
false);// 是否使用urls
setCharts(jFreeChart);
XYPlot xyPlot = jFreeChart.getXYPlot();
setXYPlot(xyPlot);
//设置第二个y轴
NumberAxis numberAxis1 = new NumberAxis("");
numberAxis1.setNumberFormatOverride(new DecimalFormat("#.###"));
xyPlot.setRangeAxis(1, numberAxis1);
xyPlot.setDataset(1, collTransfer);
xyPlot.mapDatasetToRangeAxis(1, 1);
//第一个y轴的renderer
XYItemRenderer renderer = xyPlot.getRenderer();
renderer.setBaseToolTipGenerator(StandardXYToolTipGenerator.getTimeSeriesInstance());
//第二个y轴的renderer
xyPlot.setRenderer(1, new StandardXYItemRenderer());
//第一个y轴精度
NumberAxis numberAxis = (NumberAxis) xyPlot.getRangeAxis();
numberAxis.setNumberFormatOverride(new DecimalFormat("#.#"));
logger.info("title:{}, legend:{}", jFreeChart.getTitle().getFont(), jFreeChart.getLegend().getItemFont());
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
ChartUtilities.writeChartAsJPEG(outputStream, 1, jFreeChart, 1000, 400);
return outputStream.toByteArray();
} catch (ParseException | IOException e) {
e.printStackTrace();
}
return new byte[0];
}
private void setCharts(JFreeChart jFreeChart) {
jFreeChart.getTitle().setFont(font);
RectangleInsets insets = new RectangleInsets(5, 5, 5, 5);
LegendTitle legend = jFreeChart.getLegend();
if (legend != null) {
legend.setMargin(insets);
legend.setItemFont(font);
}
jFreeChart.setPadding(insets);
jFreeChart.setBackgroundPaint(Color.WHITE);
}
private void setXYPlot(XYPlot xyPlot) {
xyPlot.setBackgroundPaint(Color.WHITE);
xyPlot.setRangeGridlinesVisible(true);
xyPlot.setDomainGridlinesVisible(true);
xyPlot.setDomainZeroBaselineVisible(false);
xyPlot.setRangeZeroBaselineVisible(false);
xyPlot.setRangeGridlinePaint(Color.GRAY);
xyPlot.setDomainGridlinePaint(Color.GRAY);
DateAxis dateaxis = (DateAxis) xyPlot.getDomainAxis();
dateaxis.setDateFormatOverride(new SimpleDateFormat(TableHelper.YYYY_MM_DD));
dateaxis.setTickUnit(new DateTickUnit(DateTickUnitType.DAY, 3));//x 轴刻度间隔为 3
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment