// 1.导入包 jfreechart-1.0.9.jar,jcommon-1.0.16.jar
// 2.java 代码
public class Tests { private static final String CHART_PATH = "E:/test/"; public static void main(String[] args){ Tests t = new Tests(); t.makeLineAndShapeChart(); } /** * 柱状图数据 */ public void makeBarChart() { double[][] data = new double[][] { { 672, 766, 223, 540, 126 }, { 325, 521, 210, 340, 106 }, { 332, 256, 523, 241, 521 } }; String[] rowKeys = { "苹果", "梨子", "葡萄" }; String[] columnKeys = { "北京", "上海", "广州", "成都", "深圳" }; CategoryDataset dataset = getBarData(data, rowKeys, columnKeys); createBarChart(dataset, "x坐标", "y坐标", "柱状图", "barGroup.png"); } /** * 折线图数据 */ public void makeLineAndShapeChart() { double[][] data = new double[][] { { 672, 766, 223, 540, 126 }, { 325, 521, 210, 340, 106 }, { 332, 256, 523, 240, 526 }, { 331, 251, 521, 240, 526 } }; String[] rowKeys = { "苹果", "梨子", "葡萄" , "葡萄2"}; String[] columnKeys = { "北京", "上海", "广州", "成都", "深圳" }; CategoryDataset dataset = getBarData(data, rowKeys, columnKeys); createTimeXYChar("折线图", "x轴", "y轴", dataset, "lineAndShap.jpg"); } //柱状图,折线图数据集 public CategoryDataset getBarData(double[][] data, String[] rowKeys,String[] columnKeys) { return DatasetUtilities.createCategoryDataset(rowKeys, columnKeys, data); } /** * 柱状图 * @param dataset * @param xName * @param yName * @param chartTitle * @param charName * @return */ public String createBarChart(CategoryDataset dataset, String xName,String yName, String chartTitle, String charName) { JFreeChart chart = ChartFactory.createBarChart(chartTitle, // 图表标题 xName, // 目录轴的显示标签 yName, // 数值轴的显示标签 dataset, // 数据集 PlotOrientation.VERTICAL, // 图表方向:水平、垂直 true, // 是否显示图例(对于简单的柱状图必须是false) false, // 是否生成工具 false // 是否生成URL链接 ); Font labelFont = new Font("SansSerif", Font.TRUETYPE_FONT, 12); /* * VALUE_TEXT_ANTIALIAS_OFF表示将文字的抗锯齿关闭, * 使用的关闭抗锯齿后,字体尽量选择12到14号的宋体字,这样文字最清晰好看 */ chart.getRenderingHints().put(RenderingHints.KEY_TEXT_ANTIALIASING,RenderingHints.VALUE_TEXT_ANTIALIAS_OFF); chart.setTextAntiAlias(false); chart.setBackgroundPaint(Color.white); // create plot CategoryPlot plot = chart.getCategoryPlot(); // 设置横虚线可见 plot.setRangeGridlinesVisible(true); // 虚线色彩 plot.setRangeGridlinePaint(Color.gray); // 数据轴精度 NumberAxis vn = (NumberAxis) plot.getRangeAxis(); // vn.setAutoRangeIncludesZero(true); DecimalFormat df = new DecimalFormat("#0.00"); vn.setNumberFormatOverride(df); // 数据轴数据标签的显示格式 // x轴设置 CategoryAxis domainAxis = plot.getDomainAxis(); domainAxis.setLabelFont(labelFont);// 轴标题 domainAxis.setTickLabelFont(labelFont);// 轴数值 // Lable(Math.PI/3.0)度倾斜 // domainAxis.setCategoryLabelPositions(CategoryLabelPositions // .createUpRotationLabelPositions(Math.PI / 3.0)); domainAxis.setMaximumCategoryLabelWidthRatio(0.6f);// 横轴上的Lable是否完整显示 // 设置距离图片左端距离 domainAxis.setLowerMargin(0.1); // 设置距离图片右端距离 domainAxis.setUpperMargin(0.1); // 设置columnKey 是否间隔显示 // domainAxis.setSkipCategoryLabelsToFit(true); plot.setDomainAxis(domainAxis); // 设置柱图背景色(注意,系统取色的时候要使用16位的模式来查看颜色编码,这样比较准确) plot.setBackgroundPaint(new Color(255, 255, 204)); // y轴设置 ValueAxis rangeAxis = plot.getRangeAxis(); rangeAxis.setLabelFont(labelFont); rangeAxis.setTickLabelFont(labelFont); // 设置最高的一个Item 与图片顶端的距离 rangeAxis.setUpperMargin(0.15); // 设置最低的一个Item 与图片底端的距离 rangeAxis.setLowerMargin(0.15); plot.setRangeAxis(rangeAxis); BarRenderer renderer = new BarRenderer(); // 设置柱子宽度 renderer.setMaximumBarWidth(0.05); // 设置柱子高度 renderer.setMinimumBarLength(0.2); // 设置柱子边框颜色 renderer.setBaseOutlinePaint(Color.BLACK); // 设置柱子边框可见 renderer.setDrawBarOutline(true); // 设置柱的颜色 renderer.setSeriesPaint(0, new Color(204, 255, 255)); renderer.setSeriesPaint(1, new Color(153, 204, 255)); renderer.setSeriesPaint(2, new Color(51, 204, 204)); // 设置每个地区所包含的平行柱的之间距离 renderer.setItemMargin(0.0); // 显示每个柱的数值,并修改该数值的字体属性 renderer.setIncludeBaseInRange(true); renderer .setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator()); renderer.setBaseItemLabelsVisible(true); plot.setRenderer(renderer); // 设置柱的透明度 plot.setForegroundAlpha(1.0f); FileOutputStream fos_jpg = null; try { isChartPathExist(CHART_PATH); String chartName = CHART_PATH + charName; fos_jpg = new FileOutputStream(chartName); ChartUtilities.writeChartAsPNG(fos_jpg, chart, 500, 500, true, 10); return chartName; } catch (Exception e) { e.printStackTrace(); return null; } finally { try { fos_jpg.close(); } catch (Exception e) { e.printStackTrace(); } } } private void isChartPathExist(String chartPath) { File file = new File(chartPath); if (!file.exists()) { file.mkdirs(); // log.info("CHART_PATH="+CHART_PATH+"create."); } } /** * 折线图 * * @param chartTitle * @param x * @param y * @param xyDataset * @param charName * @return */ public String createTimeXYChar(String chartTitle, String x, String y,CategoryDataset xyDataset, String charName) { JFreeChart chart = ChartFactory.createLineChart(chartTitle, x, y, xyDataset, PlotOrientation.VERTICAL, true, true, false); chart.setTextAntiAlias(false); chart.setBackgroundPaint(Color.WHITE); // 设置图标题的字体重新设置title Font font = new Font("隶书", Font.BOLD, 25); TextTitle title = new TextTitle(chartTitle); title.setFont(font); chart.setTitle(title); // 设置面板字体 Font labelFont = new Font("SansSerif", Font.TRUETYPE_FONT, 12); chart.setBackgroundPaint(Color.WHITE); CategoryPlot categoryplot = (CategoryPlot) chart.getPlot(); // x轴 // 分类轴网格是否可见 categoryplot.setDomainGridlinesVisible(false); // y 轴 // 数据轴网格是否可见 categoryplot.setRangeGridlinesVisible(true); categoryplot.setRangeGridlinePaint(Color.black);// 虚线色彩 categoryplot.setDomainGridlinePaint(Color.black);// 虚线色彩 categoryplot.setBackgroundPaint(Color.white); // 设置轴和面板之间的距离 // categoryplot.setAxisOffset(new RectangleInsets(5D, 5D, 5D, 5D)); CategoryAxis domainAxis = categoryplot.getDomainAxis(); domainAxis.setLabelFont(labelFont);// 轴标题 domainAxis.setTickLabelFont(labelFont);// 轴数值 // domainAxis.setCategoryLabelPositions(CategoryLabelPositions.UP_45); // 横轴上的// Lable// 45度倾斜 // 设置距离图片左端距离 domainAxis.setLowerMargin(0.0); // 设置距离图片右端距离 domainAxis.setUpperMargin(0.0); NumberAxis numberaxis = (NumberAxis) categoryplot.getRangeAxis(); numberaxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits()); numberaxis.setAutoRangeIncludesZero(true); // 获得renderer 注意这里是下嗍造型到lineandshaperenderer LineAndShapeRenderer lineandshaperenderer = (LineAndShapeRenderer) categoryplot.getRenderer(); lineandshaperenderer.setBaseShapesVisible(true); // series 点(即数据点)可见 lineandshaperenderer.setBaseLinesVisible(true); // series 点(即数据点)间有连线可见 // 显示折点数据 // lineandshaperenderer.setBaseItemLabelGenerator(new // StandardCategoryItemLabelGenerator()); // lineandshaperenderer.setBaseItemLabelsVisible(true); FileOutputStream fos_jpg = null; try { isChartPathExist(CHART_PATH); String chartName = CHART_PATH + charName; fos_jpg = new FileOutputStream(chartName); // 将报表保存为png文件 ChartUtilities.writeChartAsPNG(fos_jpg, chart, 500, 510); // 图表大小 System.out.println(charName); return chartName; } catch (Exception e) { e.printStackTrace(); return null; } finally { try { fos_jpg.close(); } catch (Exception e) { e.printStackTrace(); } } } }