jfreechart 生成图说明以及demo

demo:

XYSeriesCollection xYSeriesCollection = new XYSeriesCollection();
XYSeries xYSeries = new XYSeries("每条曲线图名字");
xYSeries.add(time, (Number) oneResultMap.get(time));//x,y的值
xYSeriesCollection.addSeries(xYSeries);//加入一条线
XYDataset dataSet = xYSeriesCollection;
JFreeChart chart = ChartFactory.createXYStepChart(”图标题“, "x含义", "y含义", dataSet);
//字体
Font titleFont = new Font("宋体", Font.BOLD, 25);
Font font = new Font("宋体", Font.CENTER_BASELINE, 12);
chart.getTitle().setFont(titleFont); // 设置标题字体
chart.getLegend().setItemFont(font);// 设置图例类别字体
chart.setBackgroundPaint(Color.WHITE);// 设置背景色
//获取绘图区对象
XYPlot plot = chart.getXYPlot();
plot.setBackgroundPaint(Color.LIGHT_GRAY); // 设置绘图区背景色
plot.setRangeGridlinePaint(Color.WHITE); // 设置水平方向背景线颜色
plot.setRangeGridlinesVisible(true);// 设置是否显示水平方向背景线,默认值为true
plot.setDomainGridlinePaint(Color.WHITE); // 设置垂直方向背景线颜色
plot.setDomainGridlinesVisible(true); // 设置是否显示垂直方向背景线,默认值为false
DateAxis domainAxis = (DateAxis) plot.getDomainAxis();
domainAxis.setLabelFont(font); // 设置横轴字体
domainAxis.setTickLabelFont(font);// 设置坐标轴标尺值字体
domainAxis.setLowerMargin(0.01);// 左边距 边框距离
domainAxis.setUpperMargin(0.06);// 右边距 边框距离,防止最后边的一个数据靠近了坐标轴。
domainAxis.setMaximumDate(upDate);// 日期轴上的最小日期
domainAxis.setMinimumDate(lowDate);// 日期轴上的最大日期
domainAxis.setRange(lowDate, upDate); //日期轴范围
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
domainAxis.setDateFormatOverride(sdf);// 日期轴日期标签的显示格式
//计算得到起止时间差
long dateSub = upDate.getTime() - lowDate.getTime();
long dateSubHours = dateSub/(1000 * 60 * 60);
int subEveryHoursCount = (int) (dateSubHours/8);
domainAxis.setTickUnit(new DateTickUnit(DateTickUnit.HOUR, subEveryHoursCount, sdf));
//设置是否垂直或者水平
domainAxis.setVerticalTickLabels(true);
domainAxis.setTickMarkPosition(DateTickMarkPosition.MIDDLE);
ValueAxis rangeAxis  = plot.getRangeAxis();
rangeAxis.setLabelFont(font);
rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());//Y轴显示整数
rangeAxis.setAutoRangeMinimumSize(1);   //最小跨度
rangeAxis.setUpperMargin(0.18);//上边距,防止最大的一个数据靠近了坐标轴。
rangeAxis.setLowerBound(minData);   //最小值显示0
rangeAxis.setAutoRange(false);   //不自动分配Y轴数据
rangeAxis.setTickMarkStroke(new BasicStroke(1.6f));     // 设置坐标标记大小
rangeAxis.setTickMarkPaint(Color.BLACK);     // 设置坐标标记颜色
XYStepRenderer xYStepRenderer = new XYStepRenderer();
xYStepRenderer.setSeriesStroke(0, new BasicStroke(2.0F));
xYStepRenderer.setSeriesStroke(1, new BasicStroke(2.0F));
xYStepRenderer.setBaseToolTipGenerator((XYToolTipGenerator)new StandardXYToolTipGenerator());
xYStepRenderer.setDefaultEntityRadius(6);
xYStepRenderer.setBaseItemLabelGenerator((XYItemLabelGenerator)new StandardXYItemLabelGenerator());
xYStepRenderer.setBaseItemLabelsVisible(true);
xYStepRenderer.setBaseItemLabelFont(new Font("Dialog", 1, 14));
plot.setNoDataMessageFont(titleFont);//字体的大小
plot.setNoDataMessagePaint(Color.RED);//字体颜色
FileOutputStream fos_jpg = null;
String chartName = "";
try {
    isChartPathExist(getImgpath());
    chartName = getImgpath() + charName;
    fos_jpg = new FileOutputStream(chartName);
    // 将报表保存为jpeg文件
    ChartUtilities.writeChartAsJPEG(fos_jpg, chart, 600, 400);
    return chartName;
} catch (FileNotFoundException e) {
    log.error("===createTimeXYStepChar error chartName:" + chartName, e);
} catch (IOException e) {
    log.error("===createTimeXYStepChar error===", e);
} finally {
    try {
        fos_jpg.close();
    } catch (IOException e) {
        log.error("===createTimeXYStepChar error===", e);
    }
}
}

//以上代码已经生成步进图图片

//一下生成折线图图片’

List<Map<String, String>> datas = new ArrayList<>();
Map<String, String> dataMap = new HashMap<>();
dataMap.put(JFreeChartUtil.DATA_COUNT, oneResultMap.get(time).toString());
dataMap.put(JFreeChartUtil.X_NAME, key);
dataMap.put(JFreeChartUtil.Y_NAME, new SimpleDateFormat(dateFormateStr).format(new Date(time)));
datas.add(dataMap);
DefaultCategoryDataset dataSet = new DefaultCategoryDataset();
for (Map<String,String> tempData: data) {
    dataSet.setValue(Double.parseDouble(tempData.get(DATA_COUNT)),tempData.get(X_NAME), tempData.get(Y_NAME));
}
///如果把createLineChart改为createLineChart3D就变为了3D效果的折线图
JFreeChart  chart = ChartFactory.createLineChart(chartTitle, "", "", dataSet,
        PlotOrientation.VERTICAL, // 绘制方向
        true, // 显示图例
        true, // 采用标准生成器
        false // 是否生成超链接
);
//字体
Font titleFont = new Font("宋体", Font.BOLD, 25);
Font font = new Font("宋体", Font.CENTER_BASELINE, 12);
chart.getTitle().setFont(titleFont); // 设置标题字体
chart.getLegend().setItemFont(font);// 设置图例类别字体
chart.setBackgroundPaint(Color.WHITE);// 设置背景色
//获取绘图区对象
CategoryPlot plot = chart.getCategoryPlot();
plot.setBackgroundPaint(Color.LIGHT_GRAY); // 设置绘图区背景色
plot.setRangeGridlinePaint(Color.WHITE); // 设置水平方向背景线颜色
plot.setRangeGridlinesVisible(true);// 设置是否显示水平方向背景线,默认值为true
plot.setDomainGridlinePaint(Color.WHITE); // 设置垂直方向背景线颜色
plot.setDomainGridlinesVisible(true); // 设置是否显示垂直方向背景线,默认值为false
CategoryAxis domainAxis = plot.getDomainAxis();
domainAxis.setLabelFont(font); // 设置横轴字体
domainAxis.setTickLabelFont(font);// 设置坐标轴标尺值字体
domainAxis.setLowerMargin(0.06);// 左边距 边框距离
domainAxis.setUpperMargin(0.06);// 右边距 边框距离,防止最后边的一个数据靠近了坐标轴。
domainAxis.setMaximumCategoryLabelLines(2);
domainAxis.setCategoryLabelPositions(CategoryLabelPositions.UP_45);
ValueAxis rangeAxis = plot.getRangeAxis();
rangeAxis.setLabelFont(font);
rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());//Y轴显示整数
rangeAxis.setAutoRangeMinimumSize(1);   //最小跨度
rangeAxis.setUpperMargin(0.18);//上边距,防止最大的一个数据靠近了坐标轴。
rangeAxis.setLowerBound(minData);   //最小值显示0
rangeAxis.setAutoRange(false);   //不自动分配Y轴数据
rangeAxis.setTickMarkStroke(new BasicStroke(1.6f));     // 设置坐标标记大小
rangeAxis.setTickMarkPaint(Color.BLACK);     // 设置坐标标记颜色
// 获取折线对象
LineAndShapeRenderer renderer = (LineAndShapeRenderer) plot.getRenderer();
BasicStroke realLine = new BasicStroke(1.8f); // 设置实线
// 设置虚线
float dashes[] = { 5.0f };
BasicStroke brokenLine = new BasicStroke(2.2f, // 线条粗细
        BasicStroke.CAP_ROUND, // 端点风格
        BasicStroke.JOIN_ROUND, // 折点风格
        8f, dashes, 0.6f);
for (int i = 0; i < dataSet.getRowCount(); i++) {
    renderer.setSeriesStroke(i, realLine); // 利用实线绘制
    renderer.setBaseShapesFilled(false);
}
plot.setNoDataMessage("无对应的数据,请重新查询。");
plot.setNoDataMessageFont(titleFont);//字体的大小
plot.setNoDataMessagePaint(Color.RED);//字体颜色
FileOutputStream fos_jpg = null;
String chartName = "";
try {
    isChartPathExist(getImgpath());
    chartName = getImgpath() + charName;
    fos_jpg = new FileOutputStream(chartName);
    // 将报表保存为jpeg文件
    ChartUtilities.writeChartAsJPEG(fos_jpg, chart, 700, 400);
    return chartName;
} catch (FileNotFoundException e) {
    log.error("===createTimeXYStepChar error chartName:" + chartName, e);
} catch (IOException e) {
    log.error("===createTimeXYStepChar error===", e);
} finally {
    try {
        fos_jpg.close();
    } catch (IOException e) {
        log.error("===createTimeXYStepChar error===", e);
    }
}

 

 

 

1. 一些关于AXIS类的方法: 
   2. Axis 类: 
   3. void setVisible(boolean flag)坐标轴是否可见 
   4. void setAxisLinePaint(Paint paint)坐标轴线条颜色(3D轴无效) 
   5. void setAxisLineStroke(Stroke stroke) 坐标轴线条笔触(3D轴无效) 
   6. void setAxisLineVisible(boolean visible)坐标轴线条是否可见(3D轴无效) 
   7. void setFixedDimension(double dimension)(用于复合表中对多坐标轴的设置) 
   8. void setLabel(String label)坐标轴标题 
   9. void setLabelFont(Font font)坐标轴标题字体 
  10. void setLabelPaint(Paint paint)坐标轴标题颜色 
  11. void setLabelAngle(double angle)`坐标轴标题旋转角度(纵坐标可以旋转) 
  12. void setTickLabelFont(Font font)坐标轴标尺值字体 
  13. void setTickLabelPaint(Paint paint)坐标轴标尺值颜色 
  14. void setTickLabelsVisible(boolean flag)坐标轴标尺值是否显示 
  15. void setTickMarkPaint(Paint paint)坐标轴标尺颜色 
  16. void setTickMarkStroke(Stroke stroke) 坐标轴标尺笔触 
  17. void setTickMarksVisible(boolean flag)坐标轴标尺是否显示 
  18. ValueAxis(Axis) 类: 
  19. void setAutoRange(boolean auto)自动设置数据轴数据范围 
  20. void setAutoRangeMinimumSize(double size)自动设置数据轴数据范围时数据范围的最小跨度 
  21. void setAutoTickUnitSelection(boolean flag)数据轴的数据标签是否自动确定(默认为true) 
  22. void setFixedAutoRange(double length)数据轴固定数据范围(设置100的话就是显示MAXVALUE到MAXVALUE-100那段数据范围) 
  23. void setInverted(boolean flag)数据轴是否反向(默认为false) 
  24. void setLowerMargin(double margin)数据轴下(左)边距 
  25. void setUpperMargin(double margin)数据轴上(右)边距 
  26. void setLowerBound(double min)数据轴上的显示最小值 
  27. void setUpperBound(double max)数据轴上的显示最大值 
  28. void setPositiveArrowVisible(boolean visible)是否显示正向箭头(3D轴无效) 
  29. void setNegativeArrowVisible(boolean visible)是否显示反向箭头(3D轴无效) 
  30. void setVerticalTickLabels(boolean flag)数据轴数据标签是否旋转到垂直 
  31. void setStandardTickUnits(TickUnitSource source) 数据轴的数据标签(可以只显示整数标签,需要将AutoTickUnitSelection设false) 
  32. NumberAxis(ValueAxis) 类: 
  33. void setAutoRangeIncludesZero(boolean flag)是否强制在自动选择的数据范围中包含0 
  34. void setAutoRangeStickyZero(boolean flag)是否强制在整个数据轴中包含0,即使0不在数据范围中 
  35. void setNumberFormatOverride(NumberFormat formatter) 数据轴数据标签的显示格式 
  36. void setTickUnit(NumberTickUnit unit) 数据轴的数据标签(需要将AutoTickUnitSelection设false) 
  37. DateAxis(ValueAxis) 类: 
  38. void setMaximumDate(Date maximumDate) 日期轴上的最小日期 
  39. void setMinimumDate(Date minimumDate) 日期轴上的最大日期 
  40. void setRange(Date lower,Date upper) 日期轴范围 
  41. void setDateFormatOverride(DateFormat formatter) 日期轴日期标签的显示格式 
  42. void setTickUnit(DateTickUnit unit) 日期轴的日期标签(需要将AutoTickUnitSelection设false) 
  43. void setTickMarkPosition(DateTickMarkPosition position) 日期标签位置(参数常量在org.jfree.chart.axis.DateTickMarkPosition类中定义) 
  44. CategoryAxis(Axis) 类: 
  45. void setCategoryMargin(double margin)分类轴边距 
  46. void setLowerMargin(double margin)分类轴下(左)边距 
  47. void setUpperMargin(double margin)分类轴上(右)边距 
  48. void setVerticalCategoryLabels(boolean flag)分类轴标题是否旋转到垂直 
  49. void setMaxCategoryLabelWidthRatio(float ratio)分类轴分类标签的最大宽度 
  50. jfreechart 设置技巧 
  51.  
  52. 1. 横坐标内容竖立   
  53.       XYPlot xyplot = jfreechart.getXYPlot(); 
  54.         DateAxis dateaxis = (DateAxis)xyplot.getDomainAxis(); 
  55.         dateaxis.setTickUnit(new DateTickUnit(1, 1, new SimpleDateFormat("MMM-yyyy"))); 
  56.         dateaxis.setVerticalTickLabels(true); 
  57.  
  58. 2.设置最大坐标范围 
  59.  1)ValueAxis axis = xyplot.getRangeAxis() ; 
  60.        axis.setRange(0,100) ; 
  61.        xyplot.setRangeAxis(axis);  
  62.  
  63.   2)numberaxis1.setUpperBound(6500D);//最大值 
  64.      numberaxis1.setLowerBound(5500D);//最小值 
  65. 2.设置时间轴的间隔时间 
  66.     dateaxis.setTickUnit(new DateTickUnit(DateTickUnit.DAY,1));//设置时间间隔为一天 
————————————————
版权声明:本文为CSDN博主「WWChu」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/wwchu/article/details/6151872

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值