package org.gbicc.sys.util; import java.awt.BasicStroke; import java.awt.Color; import java.awt.RenderingHints; import java.io.FileOutputStream; import java.text.DecimalFormat; import java.text.NumberFormat; import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.Map.Entry; import java.util.Random; import javax.servlet.ServletOutputStream; import org.jfree.chart.ChartFactory; import org.jfree.chart.ChartUtilities; import org.jfree.chart.JFreeChart; import org.jfree.chart.axis.CategoryAxis; import org.jfree.chart.axis.CategoryLabelPositions; import org.jfree.chart.axis.NumberAxis; import org.jfree.chart.axis.ValueAxis; import org.jfree.chart.labels.StandardCategoryItemLabelGenerator; import org.jfree.chart.labels.StandardCategoryToolTipGenerator; import org.jfree.chart.labels.StandardPieSectionLabelGenerator; import org.jfree.chart.labels.StandardPieToolTipGenerator; import org.jfree.chart.plot.CategoryPlot; import org.jfree.chart.plot.DatasetRenderingOrder; import org.jfree.chart.plot.PiePlot; import org.jfree.chart.plot.PiePlot3D; import org.jfree.chart.plot.PlotOrientation; import org.jfree.chart.renderer.category.BarRenderer; import org.jfree.chart.renderer.category.BarRenderer3D; import org.jfree.chart.renderer.category.LineAndShapeRenderer; import org.jfree.chart.renderer.category.StackedBarRenderer; import org.jfree.chart.renderer.category.StackedBarRenderer3D; import org.jfree.chart.title.TextTitle; import org.jfree.data.category.CategoryDataset; import org.jfree.data.category.DefaultCategoryDataset; import org.jfree.data.general.DefaultPieDataset; import org.jfree.ui.RectangleInsets; import org.jfree.util.Rotation; /** * jfreeChart工具类 * * @project SClientSpring * @author tangdu * @time 2012-3-28 * @see * [email protected] */ public class JfreeChartUtil { /* * java.awt 字体 */ private static java.awt.Font heiFont = new java.awt.Font("黑体", java.awt.Font.PLAIN, 15);// 黑体 private static java.awt.Font heiFontX = new java.awt.Font("黑体", java.awt.Font.PLAIN, 12);// 黑体 private static java.awt.Font songFont = new java.awt.Font( "STSongStd-Light", java.awt.Font.PLAIN, 12);// 宋体 //拆线图折点形式 private static float dashes[] = { 8.0f }; // 定义虚线数组 private static BasicStroke brokenLine = new BasicStroke(1.6f,// 线条粗细 BasicStroke.JOIN_ROUND, // 端点风格 BasicStroke.JOIN_ROUND, // 折点风格 8.f, // 折点处理办法 dashes, // 虚线数组 0.0f); // 虚线偏移量 //图表默认宽度 private static int WIDTH=500; private static int HEIGHT=300; /** * 数据对象--(柱形,折线数据 格式) * * @param data * data 二维数据格式 * @param rowKeys * 行 * @param columnKeys * 列 * @return * @throws Exception */ public static DefaultCategoryDataset getBarLineData( List<ArrayList<Double>> data, List<String> rowKeys, List<String> columnKeys) throws Exception { DefaultCategoryDataset defaultpiedataset = null; if (data != null && rowKeys != null && columnKeys != null) { if (data.size() == rowKeys.size()) { defaultpiedataset = new DefaultCategoryDataset(); for (int i = 0; i < rowKeys.size(); i++) { List<Double> list = data.get(i); for (int j = 0; j < columnKeys.size(); j++) { defaultpiedataset.addValue(list.get(j), rowKeys.get(i), columnKeys.get(j)); } } } } return defaultpiedataset; } /** * 数据对象--(饼图数据 格式) * * @param map * 数据格式 * @return * @throws Exception */ public static DefaultPieDataset getPieData(Map<String, Double> map) throws Exception { DefaultPieDataset defaultpiedataset = null; if (map != null) { defaultpiedataset = new DefaultPieDataset(); for (Entry<String, Double> entry : map.entrySet()) { defaultpiedataset.setValue(entry.getKey(), entry.getValue()); } } return defaultpiedataset; } /** * 3d 或是2d 柱形图 * @param title 标题 * @param titlex x轴 * @param titley y轴 * @param defaultpiedataset 数据集 * @param is3D 是否3d * @param colorBar 颜色集 * @param out 输出流 * @throws Exception */ public static void createBarChart(String title, String titlex, String titley, DefaultCategoryDataset defaultpiedataset, boolean is3D,String colorBar[], ServletOutputStream out) throws Exception { JFreeChart chart = null; CategoryPlot categoryplot = null; BarRenderer customBarRenderer = null; if (is3D) { chart = ChartFactory.createBarChart3D(title, titlex, titley, defaultpiedataset, PlotOrientation.VERTICAL, true, true, false); categoryplot = (CategoryPlot) chart.getPlot(); // 设置柱子颜色 customBarRenderer = (BarRenderer3D) categoryplot.getRenderer(); } else { chart = ChartFactory.createBarChart(title, titlex, titley, defaultpiedataset, PlotOrientation.VERTICAL, true, true, false); categoryplot = (CategoryPlot) chart.getPlot(); // 设置柱子颜色 customBarRenderer = (BarRenderer) categoryplot.getRenderer(); } // 设置柱子颜色 if (colorBar != null) { for (int i = 0; i < colorBar.length; i++) { customBarRenderer.setSeriesPaint(i, Color.decode(colorBar[i])); } } // 设置标题字体样式 TextTitle textTitle = chart.getTitle(); textTitle.setFont(heiFont); // 设置柱状体颜色 categoryplot.getRenderer().setSeriesPaint(0, new Color(228, 109, 10)); NumberAxis numberaxis = (NumberAxis) categoryplot.getRangeAxis(); CategoryAxis domainAxis = categoryplot.getDomainAxis(); // y轴 -数据轴网格是否可见 categoryplot.setRangeGridlinesVisible(true); // x轴 -数据轴网格是否可见 categoryplot.setDomainGridlinesVisible(false); // 没有 数据 categoryplot.setNoDataMessage("没有数据可以显示"); categoryplot.setNoDataMessageFont(heiFont); categoryplot.setRangeGridlinePaint(Color.GRAY);// y轴虚线色彩 categoryplot.setDomainGridlinePaint(Color.WHITE);// x轴虚线色彩 categoryplot.setBackgroundPaint(Color.WHITE);// 面板颜色 // 设置标题倾斜度-x轴 domainAxis.setCategoryLabelPositions(CategoryLabelPositions.STANDARD); // 设置X轴坐标上的字体样式 domainAxis.setTickLabelFont(heiFontX); // 设置X轴的标题字体样式 domainAxis.setLabelFont(heiFont); // 设置Y轴坐标上的字体样式 numberaxis.setTickLabelFont(heiFontX); // 设置Y轴的标题字体样式 numberaxis.setLabelFont(heiFont); // 设置图片最底部字体样式 if (chart.getLegend() != null) { chart.getLegend().setItemFont(heiFontX); } // 设置颜色 /* * BarRenderer renderer = (BarRenderer) categoryplot.getRenderer(); * renderer.setDrawBarOutline(false); */ // 输出 if (out == null) { ChartUtilities.writeChartAsPNG(new FileOutputStream( "D:\\barChart.jpg"), chart, WIDTH, HEIGHT); } else { ChartUtilities.writeChartAsPNG(out, chart, WIDTH, HEIGHT); out.close(); } } /** * 3d 或是2d 柱形堆积图 * @param title 标题 * @param titlex x轴 * @param titley y轴 * @param defaultpiedataset 数据集 * @param is3D 是否3d * @param colorBar 颜色集 * @param out 输出流 * @throws Exception */ public static void createStackBarChart(String title, String titlex, String titley, DefaultCategoryDataset defaultpiedataset, boolean is3D,String colorBar[], ServletOutputStream out) throws Exception { JFreeChart chart = null; CategoryPlot categoryplot = null; BarRenderer customBarRenderer = null; if (is3D) { chart = ChartFactory.createStackedBarChart3D(title, titlex, titley, defaultpiedataset, PlotOrientation.VERTICAL, true, true, false); categoryplot = (CategoryPlot) chart.getPlot(); // 设置柱子颜色 customBarRenderer = (StackedBarRenderer3D) categoryplot.getRenderer(); } else { chart = ChartFactory.createStackedBarChart(title, titlex, titley, defaultpiedataset, PlotOrientation.VERTICAL, true, true, false); categoryplot = (CategoryPlot) chart.getPlot(); // 设置柱子颜色 customBarRenderer = (StackedBarRenderer) categoryplot.getRenderer(); } // 设置柱子颜色 if (colorBar != null) { for (int i = 0; i < colorBar.length; i++) { customBarRenderer.setSeriesPaint(i, Color.decode(colorBar[i])); } } // 设置标题字体样式 TextTitle textTitle = chart.getTitle(); textTitle.setFont(heiFont); // 设置柱状体颜色 categoryplot.getRenderer().setSeriesPaint(0, new Color(228, 109, 10)); NumberAxis numberaxis = (NumberAxis) categoryplot.getRangeAxis(); CategoryAxis domainAxis = categoryplot.getDomainAxis(); // y轴 -数据轴网格是否可见 categoryplot.setRangeGridlinesVisible(true); // x轴 -数据轴网格是否可见 categoryplot.setDomainGridlinesVisible(false); // 没有 数据 categoryplot.setNoDataMessage("没有数据可以显示"); categoryplot.setNoDataMessageFont(heiFont); categoryplot.setRangeGridlinePaint(Color.GRAY);// y轴虚线色彩 categoryplot.setDomainGridlinePaint(Color.WHITE);// x轴虚线色彩 categoryplot.setBackgroundPaint(Color.WHITE);// 面板颜色 // 设置标题倾斜度-x轴 domainAxis.setCategoryLabelPositions(CategoryLabelPositions.STANDARD); // 设置X轴坐标上的字体样式 domainAxis.setTickLabelFont(heiFontX); // 设置X轴的标题字体样式 domainAxis.setLabelFont(heiFont); // 设置Y轴坐标上的字体样式 numberaxis.setTickLabelFont(heiFontX); // 设置Y轴的标题字体样式 numberaxis.setLabelFont(heiFont); // 设置图片最底部字体样式 if (chart.getLegend() != null) { chart.getLegend().setItemFont(heiFontX); } // 设置颜色 /* * BarRenderer renderer = (BarRenderer) categoryplot.getRenderer(); * renderer.setDrawBarOutline(false); */ // 输出 if (out == null) { ChartUtilities.writeChartAsPNG(new FileOutputStream( "D:\\stackbarChart.jpg"), chart, WIDTH, HEIGHT); } else { ChartUtilities.writeChartAsPNG(out, chart, WIDTH, HEIGHT); out.close(); } } /** * 折线图 * * @param title * 标题 * @param titlex * x标题 * @param titley * y标题 * @param defaultpiedataset * 数据 * @param colorBar * 数据集 * @param out * servlet 输出流 * @throws Exception */ public static void createLineChart(String title, String titlex, String titley, DefaultCategoryDataset defaultpiedataset,String colorLine[], ServletOutputStream out) throws Exception { JFreeChart chart = ChartFactory.createLineChart(title, titlex, titley, defaultpiedataset, PlotOrientation.VERTICAL, true, true, false); // 设置标题字体样式 TextTitle textTitle = chart.getTitle(); textTitle.setFont(heiFont); // 设置柱状体颜色 CategoryPlot categoryplot = (CategoryPlot) chart.getPlot(); categoryplot.getRenderer().setSeriesPaint(0, new Color(85, 139, 185)); categoryplot.setDomainGridlinesVisible(true); // y轴 -数据轴网格是否可见 categoryplot.setRangeGridlinesVisible(true); // x轴 -数据轴网格是否可见 categoryplot.setDomainGridlinesVisible(false); // 没有 数据 categoryplot.setNoDataMessage("没有数据可以显示"); categoryplot.setRangeGridlinePaint(Color.GRAY);// y轴虚线色彩 categoryplot.setDomainGridlinePaint(Color.WHITE);// x轴虚线色彩 categoryplot.setBackgroundPaint(Color.WHITE);// 面板颜色 // 设置轴和面板之间的距离 categoryplot.setAxisOffset(new RectangleInsets(5D, 5D, 5D, 5D)); NumberAxis numberaxis = (NumberAxis) categoryplot.getRangeAxis(); CategoryAxis domainAxis = categoryplot.getDomainAxis(); // domainAxis.setVisible(false); // x轴是否斜体角度 domainAxis.setCategoryLabelPositions(CategoryLabelPositions.STANDARD); // 设置X轴坐标上的字体样式 domainAxis.setTickLabelFont(heiFontX); // 设置X轴的标题字体样式 domainAxis.setLabelFont(heiFont); // 设置Y轴坐标上的字体样式 numberaxis.setTickLabelFont(heiFontX); // 设置Y轴的标题字体样式 numberaxis.setLabelFont(heiFont); // 设置图片最底部字体样式 if (chart.getLegend() != null) { chart.getLegend().setItemFont(heiFontX); } numberaxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits()); numberaxis.setAutoRangeIncludesZero(true); numberaxis.setUpperMargin(0.20); numberaxis.setLabelAngle(Math.PI / 2.0); LineAndShapeRenderer lineandshaperenderer = (LineAndShapeRenderer) categoryplot .getRenderer(); //设置折线颜色 if (colorLine != null) { for (int i = 0; i < colorLine.length; i++) { lineandshaperenderer.setSeriesPaint(i, Color.decode(colorLine[i])); //lineandshaperenderer.setSeriesStroke(i, brokenLine); } } //lineandshaperenderer.setSeriesVisibleInLegend(false);//不显示x轴标题栏 lineandshaperenderer.setBaseShapesVisible(true); // series 点(即数据点)可见 lineandshaperenderer.setBaseLinesVisible(true); // series // 点(即数据点)间有连线可见 // 输出 if (out == null) { ChartUtilities.writeChartAsPNG(new FileOutputStream( "D:\\lineChart.jpg"), chart, WIDTH, HEIGHT); } else { ChartUtilities.writeChartAsPNG(out, chart, WIDTH, HEIGHT); out.close(); } } /** * 饼图2d,或 是 3D * @param dataset 数据集 * @param chartTitle 标题 * @param is3D 是否是3d * @param isLeng 是否显示标题栏 x轴 * @param out 输出流 * @throws Exception */ public static void create3DPieChart(DefaultPieDataset dataset, String chartTitle, boolean is3D,boolean isLeng,String colorPie[], ServletOutputStream out) throws Exception { // 利用工厂类来创建3D饼图 JFreeChart chart = null; PiePlot plot = null; if (is3D) { chart = ChartFactory.createPieChart3D(chartTitle, dataset, false, true, false); plot = (PiePlot3D) chart.getPlot(); } else { chart = ChartFactory.createPieChart(chartTitle, dataset, false, true, false); plot = (PiePlot) chart.getPlot(); } // 使下说明标签字体清晰,去锯齿类似于 chart.getRenderingHints().put(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_OFF); chart.setTextAntiAlias(false); // 图片背景色 chart.setBackgroundPaint(Color.white); chart.setBorderPaint(Color.WHITE); chart.setBorderVisible(false); chart.setTextAntiAlias(false); // 设置图标题的字体重新设置title(否组有些版本Title会出现乱码) chart.getTitle().setFont(songFont); // 设置图例(Legend)上的文字(//底部的字体) if(!isLeng){chart.getLegend().setItemFont(songFont);} // 指定饼图轮廓线的颜色 plot.setBaseSectionOutlinePaint(Color.WHITE); plot.setBaseSectionPaint(Color.WHITE); // 设置无数据时的信息 plot.setNoDataMessage("没有可用数据"); plot.setNoDataMessageFont(heiFont); plot.setOutlineVisible(false); // 设置无数据时的信息显示颜色 plot.setNoDataMessagePaint(Color.red); // 图片中显示百分比:自定义方式,{0} 表示选项, {1} 表示数值, {2} 表示所占比例 ,小数点后两位 /*plot.setLabelGenerator(new StandardPieSectionLabelGenerator( "{0}={1}({2})", NumberFormat.getNumberInstance(), new DecimalFormat("0.00%")));*/ plot.setLabelGenerator(new StandardPieSectionLabelGenerator( "{0}({2})", NumberFormat.getNumberInstance(), new DecimalFormat("0.00%"))); plot.setLabelFont(songFont); //设置颜色 if(colorPie!=null){ for(int i=0;i<colorPie.length;i++){ plot.setSectionPaint(i,Color.decode(colorPie[i])); } } // 指定图片的透明度(0.0-1.0) plot.setForegroundAlpha(0.65f); plot.setBackgroundPaint(Color.WHITE); // 设置第一个 饼块section 的开始位置,默认是12点钟方向 plot.setStartAngle(90); plot.setCircular(true);//圆形 plot.setLabelGap(0.01D);//间距 plot.setToolTipGenerator(new StandardPieToolTipGenerator()); //设置饼状图的绘制方向,可以按顺时针方向绘制,也可以按逆时针方向绘制 plot.setDirection(Rotation.ANTICLOCKWISE); //设置突出显示的数据块 //plot.setExplodePercent("One", 0.1D); // 输出 if (out == null) { ChartUtilities.writeChartAsPNG(new FileOutputStream( "D:\\pie3DChart.jpg"), chart, WIDTH, HEIGHT); } else { ChartUtilities.writeChartAsPNG(out, chart, WIDTH, HEIGHT); out.close(); } } /** * 创建复合图 bar_line * @param chartTitle * @param Xname x轴title * @param Yname y轴tilte * @param dataset1 bar数据集 * @param dataset2 line数据集 * @param is3D 是否是3D * @param colorBar bar颜色 * @param colorLine line颜色 * @param out 输出流 * @throws Exception */ public static void createAndChart(String chartTitle, String Xname, String Yname, CategoryDataset dataset1, CategoryDataset dataset2, boolean is3D,String colorBar[],String colorLine[], ServletOutputStream out) throws Exception { JFreeChart chart ; BarRenderer renderer; CategoryPlot plot; if(is3D){ chart=ChartFactory.createBarChart3D(chartTitle, Xname, Yname, dataset1, PlotOrientation.VERTICAL, true, true, false); plot = chart.getCategoryPlot();// 获得图表区域对象 renderer=new BarRenderer3D(); }else{ chart=ChartFactory.createBarChart(chartTitle, Xname, Yname, dataset1, PlotOrientation.VERTICAL, true, true, false); plot = chart.getCategoryPlot();// 获得图表区域对象 renderer=new BarRenderer(); } renderer.setShadowVisible(false);//是否有阴影 // 设置标题字体样式 TextTitle textTitle = chart.getTitle(); textTitle.setFont(heiFont); chart.getLegend().setItemFont(songFont); // y軸設置 ValueAxis rangeAxis = plot.getRangeAxis(); rangeAxis.setLabelFont(songFont); rangeAxis.setTickLabelFont(songFont); //rangeAxis.setUpperBound(10);// 設置Y軸數值 最大 sumCountErrorNum //rangeAxis.setLowerMargin(0.05); // 設置最低的一個Item與圖片底端的距離 //rangeAxis.setUpperMargin(0.05); plot.setRangeAxis(rangeAxis); // 顯示柱體的數值 //renderer.setItemLabelGenerator(new StandardCategoryItemLabelGenerator()); //renderer.setItemLabelsVisible(true); // renderer.setMinimumBarLength(0.5);//設置柱體高度 plot.setRenderer(renderer); // 设置柱子颜色 if (colorBar != null) { for (int i = 0; i < colorBar.length; i++) { renderer.setSeriesPaint(i, Color.decode(colorBar[i])); } } //x轴 CategoryAxis domainAxis = plot.getDomainAxis(); domainAxis.setLabelFont(songFont); domainAxis.setTickLabelFont(songFont);// domainAxis.setCategoryMargin(0.1);// 橫軸標簽之間的距離10% domainAxis.setMaximumCategoryLabelWidthRatio(4f);// 橫軸上的 Lable 是否完整顯示 domainAxis.setUpperMargin(0.05); domainAxis.setLowerMargin(0.05); //domainAxis.setCategoryLabelPositions(CategoryLabelPositions // .createUpRotationLabelPositions(Math.PI / 7.0)); //chart.setAntiAlias(true); chart.setBackgroundPaint(Color.WHITE); chart.setBorderPaint(Color.BLACK); //chart.setBorderVisible(true); // 创建折线图 plot.setDataset(1, dataset2); plot.mapDatasetToRangeAxis(1, 1); NumberAxis numberaxis = new NumberAxis();// 折線Y軸名稱(右邊) numberaxis.setLabelFont(songFont); plot.setRangeAxis(1, numberaxis); // 定義折線顯示樣式 LineAndShapeRenderer lineandshaperenderer = new LineAndShapeRenderer(); lineandshaperenderer .setToolTipGenerator(new StandardCategoryToolTipGenerator()); // 设置柱子颜色 if (colorLine != null) { for (int i = 0; i < colorLine.length; i++) { lineandshaperenderer.setSeriesPaint(i, Color.decode(colorLine[i])); } } // 设置数据点显示 lineandshaperenderer .setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator()); //lineandshaperenderer.setBaseItemLabelsVisible(true); plot.setRenderer(1, lineandshaperenderer); plot.setDatasetRenderingOrder(DatasetRenderingOrder.FORWARD); /*LegendTitle legendtitle = new LegendTitle(plot.getRenderer(0)); legendtitle.setMargin(new RectangleInsets(2D, 2D, 2D, 2D)); legendtitle.setBorder(new BlockBorder()); LegendTitle legendtitle1 = new LegendTitle(plot.getRenderer(1)); legendtitle1.setMargin(new RectangleInsets(2D, 2D, 2D, 2D)); legendtitle1.setBorder(new BlockBorder()); BlockContainer blockcontainer = new BlockContainer( new BorderArrangement()); blockcontainer.add(legendtitle, RectangleEdge.LEFT); blockcontainer.add(legendtitle1, RectangleEdge.RIGHT); blockcontainer.add(new EmptyBlock(2000D, 0.0D)); CompositeTitle compositetitle = new CompositeTitle(blockcontainer); compositetitle.setPosition(RectangleEdge.BOTTOM); chart.addSubtitle(compositetitle);*/ //设置plot plot.setNoDataMessage("没有数据可以显示"); plot.setNoDataMessageFont(heiFont); plot.setRangeGridlinePaint(Color.GRAY);// y轴虚线色彩 plot.setDomainGridlinePaint(Color.WHITE);// x轴虚线色彩 plot.setBackgroundPaint(Color.WHITE);// 面板颜色 // 输出 if (out == null) { ChartUtilities.writeChartAsPNG(new FileOutputStream( "D:\\fuheChar2t.jpg"), chart, WIDTH, HEIGHT); } else { ChartUtilities.writeChartAsPNG(out, chart, WIDTH, HEIGHT); out.close(); } } /** * 创建叠加图 bar_stacked_line * @param chartTitle * @param Xname x轴title * @param Yname y轴tilte * @param dataset1 bar数据集 * @param dataset2 line数据集 * @param is3D 是否是3D * @param colorBar bar颜色 * @param colorLine line颜色 * @param out 输出流 * @throws Exception */ public static void createAndStackChart(String chartTitle, String Xname, String Yname, CategoryDataset dataset1, CategoryDataset dataset2, boolean is3D,String colorBar[],String colorLine[], ServletOutputStream out) throws Exception { JFreeChart chart ; BarRenderer renderer; CategoryPlot plot; if(is3D){ chart=ChartFactory.createStackedBarChart3D(chartTitle, Xname, Yname, dataset1, PlotOrientation.VERTICAL, true, true, false); plot = chart.getCategoryPlot();// 获得图表区域对象 renderer=new StackedBarRenderer3D(); }else{ chart=ChartFactory.createStackedBarChart(chartTitle, Xname, Yname, dataset1, PlotOrientation.VERTICAL, true, true, false); plot = chart.getCategoryPlot();// 获得图表区域对象 renderer=new StackedBarRenderer(); } renderer.setShadowVisible(false);//是否有阴影 // 设置标题字体样式 TextTitle textTitle = chart.getTitle(); textTitle.setFont(heiFont); chart.getLegend().setItemFont(songFont); // y軸設置 ValueAxis rangeAxis = plot.getRangeAxis(); //设置折点格式 //rangeAxis.setAxisLineStroke(brokenLine); rangeAxis.setLabelFont(songFont); rangeAxis.setTickLabelFont(songFont); //rangeAxis.setUpperBound(10);// 設置Y軸數值 最大 sumCountErrorNum //rangeAxis.setLowerMargin(0.05); // 設置最低的一個Item與圖片底端的距離 //rangeAxis.setUpperMargin(0.05); plot.setRangeAxis(rangeAxis); // 顯示柱體的數值 //renderer.setItemLabelGenerator(new StandardCategoryItemLabelGenerator()); //renderer.setItemLabelsVisible(true); // renderer.setMinimumBarLength(0.5);//設置柱體高度 plot.setRenderer(renderer); // 设置柱子颜色 if (dataset1 != null) { for (int i = 0; i < colorBar.length; i++) { //设置渐变 //GradientPaint gp = new GradientPaint(); //renderer.setSeriesPaint(i, gp); renderer.setSeriesPaint(i, Color.decode(colorBar[i])); } } //x轴 CategoryAxis domainAxis = plot.getDomainAxis(); domainAxis.setLabelFont(songFont); domainAxis.setTickLabelFont(songFont);// domainAxis.setCategoryMargin(0.1);// 橫軸標簽之間的距離10% domainAxis.setMaximumCategoryLabelWidthRatio(4f);// 橫軸上的 Lable 是否完整顯示 domainAxis.setUpperMargin(0.05); domainAxis.setLowerMargin(0.05); chart.setBackgroundPaint(Color.WHITE); chart.setBorderPaint(Color.BLACK); // 创建折线图 plot.setDataset(1, dataset2); plot.mapDatasetToRangeAxis(1, 1); NumberAxis numberaxis = new NumberAxis();// 折線Y軸名稱(右邊) numberaxis.setLabelFont(songFont); plot.setRangeAxis(1, numberaxis); // 定義折線顯示樣式 LineAndShapeRenderer lineandshaperenderer = new LineAndShapeRenderer(); lineandshaperenderer .setToolTipGenerator(new StandardCategoryToolTipGenerator()); // 设置柱子颜色 if (dataset2 != null) { for (int i = 0; i < colorLine.length; i++) { lineandshaperenderer.setSeriesPaint(i, Color.decode(colorLine[i])); //lineandshaperenderer.setSeriesStroke(i, brokenLine); } } // 设置数据点显示 lineandshaperenderer .setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator()); plot.setRenderer(1, lineandshaperenderer); plot.setDatasetRenderingOrder(DatasetRenderingOrder.FORWARD); //设置plot plot.setNoDataMessage("没有数据可以显示"); plot.setNoDataMessageFont(heiFont); plot.setRangeGridlinePaint(Color.GRAY);// y轴虚线色彩 plot.setDomainGridlinePaint(Color.WHITE);// x轴虚线色彩 plot.setBackgroundPaint(Color.WHITE);// 面板颜色 // 输出 if (out == null) { ChartUtilities.writeChartAsPNG(new FileOutputStream( "D:\\stackedChart.jpg"), chart, WIDTH, HEIGHT); } else { ChartUtilities.writeChartAsPNG(out, chart, WIDTH, HEIGHT); out.close(); } } /** * 随机生成16制颜色 * * @return */ public static String getRandColorCode() { String r, g, b; Random random = new Random(); r = Integer.toHexString(random.nextInt(256)).toUpperCase(); g = Integer.toHexString(random.nextInt(256)).toUpperCase(); b = Integer.toHexString(random.nextInt(256)).toUpperCase(); r = r.length() == 1 ? "0" + r : r; g = g.length() == 1 ? "0" + g : g; b = b.length() == 1 ? "0" + b : b; return "#" + r + g + b; } }