柱图:JFreeChart chart = ChartFactory.createBarChart3D(
"XXX统计图", //图表名称
"类型", //目录轴的显示标签(X轴标题)
"数据额", //数值轴的显示标签(Y轴标题)
dataset, //数据集
PlotOrientation.VERTICAL, //图表方向:水平、垂直
true, //是否显示图例
false, // 是否显示 tooltip
false); // 是否指定 url
chart.removeLegend();//移除图例
chart.getRenderingHints().put(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_OFF); //设置消除字体的锯齿渲染
chart.addSubtitle(new TextTitle("")); //设置副标题
chart.setBorderVisible(true); //设置边框是否可见
chart.setBorderStroke(new BasicStroke(1));//设置边框宽度(整个最外层的边框)
chart.setBorderPaint(Color.BLACK); //设置边框着色
chart.setBackgroundPaint(bcParam.getBgColor());//设置背景颜色
/***** plot 设置 (坐标区)*****/
CategoryPlot plot = (CategoryPlot) chart.getPlot();
//设置显示位置
plot.setDomainAxisLocation(AxisLocation.TOP_OR_RIGHT);
plot.setRangeAxisLocation(AxisLocation.BOTTOM_OR_RIGHT);
plot.setDomainGridlinePaint(Color.blue); //设置网格竖线颜色
plot.setDomainGridlinesVisible(true); //网格竖线是否显示
plot.setRangeGridlinePaint(Color.blue); //设置网格横线颜色
plot.setRangeGridlinesVisible(true); //网格横线是否显示
plot.setRangeGridlineStroke(new BasicStroke(0.0f));//数据轴网格线条笔触
plot.setBackgroundPaint(Color.LIGHT_GRAY); //坐标区背景颜色
plot.setOutlineVisible(true); //坐标区表框是否显示
plot.setOutlinePaint(Color.WHITE); //坐标区边框颜色 但是要在 plot.setOutlineVisible(true); 的前提下
plot.setForegroundAlpha(1.0f); //设置柱的透明度
plot.setAxisOffset(); //坐标轴到数据区的间距(具体参数不知道怎么设置)
plot.setDomainGridlineStroke(new BasicStroke());
plot.setRangeGridlineStroke(new BasicStroke());
/***** renderer设置 柱子相关属性设置 *****/
BarRenderer renderer = new BarRenderer(); 或 BarRenderer3D renderer = new BarRenderer3D();
plot.setRenderer(renderer); //将修改后的属性值保存到图中
renderer.setDrawBarOutline(true); //柱子边框颜色是否显示
renderer.setBaseOutlinePaint(Color.ORANGE);//柱子边框颜色 但是要在 renderer.setDrawBarOutline(true); 的前提下
renderer.setWallPaint(Color.gray); //设置柱子墙体颜色
renderer.setMaximumBarWidth(0.08); //设置柱子宽度 这里的参数param是一个(0-1)之间的小数,表示柱子宽度占整个图表宽度的百分比。(比如你图表宽度设置的是600pt,想要柱子的宽度为6PT,设置成0.01即可。)
renderer.setMinimumBarLength(0.1); //设置柱子高度
renderer.setSeriesPaint(0,Color.ORANGE); //设置柱的颜色
renderer.setItemMargin(0); //设置每个地区所包含的平行柱的之间距离
renderer.setBaseItemLabelsVisible(true); //在柱子上显示相应信息 是否显示
renderer.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator()); //在柱子上显示相应信息
renderer.setBaseItemLabelPaint(Color.BLACK);//设置数值颜色,默认黑色
renderer.setBasePositiveItemLabelPosition(new ItemLabelPosition(ItemLabelAnchor.OUTSIDE12, TextAnchor.CENTER_LEFT)); //搭配ItemLabelAnchor TextAnchor 这两项能达到不同的效果,但是ItemLabelAnchor最好选OUTSIDE,因为INSIDE显示不出来
renderer.setItemLabelAnchorOffset(10); //可以进一步调整数值的位置,但是得根据ItemLabelAnchor选择情况.
renderer.setBarPainter(new StandardBarPainter()); //去掉立体感(柱子显示平面效果)
renderer.setShadowVisible(false); //不显示柱子阴影
setTickMarkStroke(Stroke stroke) 坐标轴标尺笔触
setTickMarksVisible(boolean flag) 坐标轴标尺是否显示
renderer.setSeriesStroke(0, new BasicStroke(2.0F)); 其中“0”这里是指第几条线,0为第一条;“2.0F”这里是指线的宽度
/***** domainAxis设置 X轴设置 *****/
CategoryAxis domainAxis = plot.getDomainAxis(); //对X轴做操作
domainAxis.setLabel(""); //X轴的标题内容
domainAxis.setTickLabelsVisible(true); //X轴的标题文字是否显示
domainAxis.setTickLabelPaint(Color.red); //X轴的标题文字颜色
domainAxis.setAxisLinePaint(Color.red); //X轴横线颜色
domainAxis.setTickMarksVisible(true); //标记线是否显示
domainAxis.setTickMarkOutsideLength(3); //标记线向外长度
domainAxis.setTickMarkInsideLength(3); //标记线向内长度
domainAxis.setTickMarkPaint(Color.red); //标记线颜色
domainAxis.setUpperMargin(0.2); //设置距离图片左端距离
domainAxis.setLowerMargin(0.2); //设置距离图片右端距离
domainAxis.setMaximumCategoryLabelWidthRatio(0.6f); //横轴上的 Lable 是否完整显示
domainAxis.setCategoryLabelPositions(CategoryLabelPositions.DOWN_45);//横轴上的 Lable 45度倾斜
domainAxis.setTickLabelFont(new Font("sans-serif", Font.PLAIN, 11)); //设置X轴坐标上的文字(防止乱码)
domainAxis.setLabelFont(new Font("宋体", Font.PLAIN, 12)); //设置X轴的标题文字(防止乱码)
domainAxis.setTickLabelsVisible(true); //X轴的标题文字是否显示
domainAxis.setTickMarksVisible(true); //标记线是否显示
domainAxis.setTickMarkInsideLength(3); //标记线向内长度
domainAxis.setUpperMargin(0.2); //设置距离图片左端距离
domainAxis.setLowerMargin(0.2); //设置距离图片右端距离
/***** rAxis设置 Y轴设置 *****/
ValueAxis rAxis = plot.getRangeAxis(); //对Y轴做操作
rAxis.setLabel("时间"); //Y轴内容
rAxis.setLabelPaint(Color.red); //标题内容颜色
rAxis.setLabelAngle(1.6); //标题内容显示角度(1.6时候水平)
rAxis.setAxisLinePaint(Color.red); //Y轴竖线颜色
rAxis.setAxisLineVisible(true); //Y轴竖线是否显示
rAxis.setVisible(true); //Y轴内容是否显示
rAxis.setAutoRange(true); //是否自动设置数据轴数据范围
rAxis.setTickLabelsVisible(true); //刻度数值是否显示
rAxis.setMinorTickMarksVisible(true); //标记线是否显示
rAxis.setMinorTickCount(7); //节段中的刻度数
rAxis.setMinorTickMarkInsideLength(3); //内刻度线向内长度
rAxis.setMinorTickMarkOutsideLength(3); //内刻度记线向外长度
rAxis.setTickMarkInsideLength(3); //外刻度线向内长度
rAxis.setTickMarkPaint(Color.red); //刻度线颜色
rAxis.setTickMarksVisible(true); //所有Y标记线是否显示 (如果前面设置rAxis.setMinorTickMarksVisible(true); 则其照样显示)
rAxis.setTickLabelFont(new Font("sans-serif", Font.PLAIN, 12)); //设置Y轴坐标上的文字
rAxis.setLabelFont(new Font("黑体", Font.PLAIN, 12)); //设置Y轴的标题文字
rAxis.setRange(100, 600); //Y轴取值范围(下面不能出现 rAxis.setAutoRange(true) 否则不起作用)
rAxis.setLowerBound(100); //Y轴以开始的最小值
rAxis.setUpperBound(600); //Y轴的最大值
rAxis.setUpperMargin(0.15); //设置最高的一个 Item 与图片顶端的距离 (在设置rAxis.setRange(100, 600);情况下不起作用)
rAxis.setLowerMargin(0.15); //设置最低的一个 Item 与图片底端的距离
rangeAxis.setPositiveArrowVisible(true); //是否显示正向箭头
/***** 数据精度设置 *****/
NumberAxis numberaxis = (NumberAxis) plot.getRangeAxis();
numberaxis.setTickLabelFont(new Font("sans-serif", Font.PLAIN, 12)); //设置Y轴坐标上的文字(防止乱码)
numberaxis.setLabelFont(new Font("黑体", Font.PLAIN, 12)); //设置Y轴的标题文字(防止乱码)
numberaxis.setAutoRangeIncludesZero(true); //设置刻度必须从0开始
numberaxis.setNumberFormatOverride(new DecimalFormat("#0.000")); //设置纵坐标数据精度
XY轴标题可以用:
TextTitle textTitle = chart.getTitle();
textTitle.setFont(new Font("黑体", Font.PLAIN, 20)); //设置标题字体
textTitle.setBackgroundPaint(Color.LIGHT_GRAY); //标题背景色
textTitle.setPaint(Color.cyan); //标题字体颜色
textTitle.setText("类型统计图"); //标题内容
/***** 图例 (LegendTitle) *****/
jfreechart.getLegend().setItemFont(new Font("宋体", Font.PLAIN, 12)); //设置图例的字体(防止乱码)
LegendTitle legend = new LegendTitle(chart.getPlot()); //创建图例
legend.setBorder(0,0,0,0); //设置四周的边距,带线框. (不显示边框)
legend.setPosition(RectangleEdge.RIGHT); //设置图例的位置
legend.setMargin(0, 0, 0, 5); //这样就只是距离右边有距离 margin 5
chart.addLegend(); //图表中添加图例
//图例所拥有的一些其他方法
legend.setBackgroundPaint(Paint paint) //可设置图例的背景色
legend.setTitle(String title) //图示标题内容
legend.setTitleFont(Font font) //图示标题字体
legend.setBoundingBoxArcWidth(int arcWidth) //图示边界圆角宽
legend.setBoundingBoxArcHeight(int arcHeight) //图示边界圆角高
legend.setOutlinePaint(Paint paint) //图示边界线条颜色
legend.setOutlineStroke(Stroke stroke) //图示边界线条笔触
legend.setDisplaySeriesLines(boolean flag) //图示项是否显示横线(折线图有效)
legend.setDisplaySeriesShapes(boolean flag) //图示项是否显示形状(折线图有效)
legend.setItemFont(Font font) //图示项字体
legend.setItemPaint(Paint paint) //图示项字体颜色
legend.setAnchor(int anchor) //图示在图表中的显示位置(参数常量在Legend类中定义)
/***** 数据格式 *****/
饼图:JFreeChart chart = ChartFactory.createPieChart(
pieName, //图表名称
dataset, //数据集
true, //是否显示图例
false, // 是否显示 tooltip
false); // 是否指定 url
chart.removeLegend();//移除图例
plot.setLabelLinkStyle(PieLabelLinkStyle.STANDARD); //饼图的文字连接线设置成直线 可选参数CUBIC_CURVE, QUAD_CURVE, STANDARD (STANDARD为直线 )
线图:JFreeChart chart = ChartFactory. createLineChart(
"", // 图标题
"", // x轴标题
"", // y轴标题
dataset, // 数据源
PlotOrientation.VERTICAL,// 显示方向
false, // 是否显示图例
false, // 是否显示 tooltip
false); // 是否指定 url
chart.setBorderStroke(new BasicStroke(0.3f)); //设置边框宽度
chart.setBorderVisible(true); //设置边框是否可见
chart.setBorderPaint(Color.BLACK); //设置边框着色
CategoryPlot plot = (CategoryPlot) chart.getPlot();
NumberAxis numberaxis = (NumberAxis) plot.getRangeAxis();
String numAxis = "#0";
if(numberAxis) numAxis = "#0.00";
numberaxis.setNumberFormatOverride(new DecimalFormat(numAxis));//地址Y轴的显示精度
LineAndShapeRenderer lineandshaperenderer = (LineAndShapeRenderer)plot.getRenderer();
plot.setOutlinePaint(Color.WHITE); //数据区的边界线条颜色
lineandshaperenderer.setSeriesStroke( //定义series为"First"的(即series1)点之间的连线
0, //第几个数据集
new BasicStroke(
3.0F, //线的宽度
1,
1,
1.0F,
new float[] {10F,//控制虚线直线
0F },
1F));
lineandshaperenderer.setSeriesPaint(0, new Color(74,126,187)); //设置每个分组的线的颜色
if(isGroup){
lineandshaperenderer.setSeriesPaint(1, new Color(190,75,72));
lineandshaperenderer.setSeriesPaint(2, new Color(152,185,84));
lineandshaperenderer.setSeriesStroke(1,new BasicStroke(3.0F,1,1,1.0F,new float[] {10F, 0F },1F));
lineandshaperenderer.setSeriesStroke(2,new BasicStroke(3.0F,1,1,1.0F,new float[] {10F, 0F },1F));
}
//设置网格背景颜色
plot.setBackgroundPaint(Color.WHITE);
//设置网格竖线颜色
plot.setDomainGridlinePaint(Color.WHITE);
//设置网格横线颜色
plot.setRangeGridlinePaint(Color.BLACK);
//设置曲线图与xy轴的距离
plot.setAxisOffset(new RectangleInsets(0D, 0D, 0D, 0D));
//设置曲线显示各数据点的值
CategoryAxis domainAxis = plot.getDomainAxis();
domainAxis.setCategoryLabelPositions(CategoryLabelPositions.UP_45); //横轴上的label斜显示
//图表中添加图例
Font fontIn = new Font("", 8, 10);
LegendTitle legendTitle = new LegendTitle(chart.getPlot());//创建图例
legendTitle.setPosition(RectangleEdge.RIGHT); //设置图例的位置
legendTitle.setMargin(0, 0, 0, 5);
legendTitle.setItemFont(fontIn);
chart.addLegend(legendTitle);
FileOutputStream fos_jpg = null;
try {
fos_jpg = new FileOutputStream(url);
ChartUtilities.writeChartAsJPEG(fos_jpg,0.99f,chart,400,250,null);
fos_jpg.close();
} catch (Exception e) {
e.printStackTrace();
}
线图(分组)数据集:
DefaultCategoryDataset dataset = new DefaultCategoryDataset();
dataset.addValue(5, "PM2.5", "2015-10-10");
dataset.addValue(7, "PM2.5", "2015-11-10");
dataset.addValue(1, "PM2.5", "2015-10-8");
dataset.addValue(12, "PM2.5", "2015-10-9");
dataset.addValue(8, "wendu", "2015-10-10");
dataset.addValue(9, "wendu", "2015-11-10");
dataset.addValue(12, "wendu", "2015-10-8");
dataset.addValue(5, "wendu", "2015-10-9");
dataset.addValue(3, "电流", "2015-10-10");
dataset.addValue(3, "电流", "2015-11-10");
dataset.addValue(2, "电流", "2015-10-8");
dataset.addValue(8, "电流", "2015-10-9");
//写出去
FileOutputStream fos_jpg = null;
try {
fos_jpg = new FileOutputStream(bcParam.getUrl());
ChartUtilities.writeChartAsJPEG(fos_jpg,0.99f,chart,bcParam.getWidth(),bcParam.getHeight(),null);
fos_jpg.close();
} catch (Exception e) {
e.printStackTrace();
}