主要jar:jfreechart.jar
private JFreeChart chart; public JFreeChart getFoldlineGraph(double[][] data, String[] rowKeys, String[] columnKeys, boolean isline) { CategoryDataset dataset = getBarData(data, rowKeys, columnKeys); createTimeXYChar("单板测试通过率", "图例", "成功率", dataset, "", isline); return chart; } private CategoryDataset getBarData(double[][] data, String[] rowKeys, String[] columnKeys) { return DatasetUtilities.createCategoryDataset(rowKeys, columnKeys, data); }
private void createTimeXYChar(String chartTitle, String x, String y, CategoryDataset xyDataset, String charName, boolean isline) { chart = ChartFactory.createLineChart(chartTitle, x, y, xyDataset, PlotOrientation.VERTICAL, true, true, false); Font font00 = new Font("微软雅黑", Font.LAYOUT_NO_LIMIT_CONTEXT, 13); LegendTitle legend = chart.getLegend(); legend.setItemFont(font00);// 设置注释字体 chart.setTextAntiAlias(false); // 设置图标题的字体重新设置title Font font = new Font("微软雅黑", Font.LAYOUT_NO_LIMIT_CONTEXT, 15); TextTitle title = new TextTitle(chartTitle); title.setFont(font); chart.setTitle(title); CategoryPlot categoryplot = (CategoryPlot) chart.getPlot(); // x轴分类轴网格是否可见 categoryplot.setDomainGridlinesVisible(true); // y轴数据轴网格是否可见 categoryplot.setRangeGridlinesVisible(true); categoryplot.setRangeGridlinePaint(Color.pink);// 虚线色彩 categoryplot.setDomainGridlinePaint(Color.pink);// 虚线色彩 categoryplot.setBackgroundPaint(Color.white); // 设置轴和面板之间的距离 categoryplot.setAxisOffset(new RectangleInsets(0D, 0D, 0D, 0D)); CategoryAxis domainAxis = categoryplot.getDomainAxis(); domainAxis.setLabelFont(new Font("微软雅黑", Font.LAYOUT_NO_LIMIT_CONTEXT, 13));// 轴标题 domainAxis.setTickLabelFont(new Font("微软雅黑", Font.LAYOUT_NO_LIMIT_CONTEXT, 13));// 轴数值 domainAxis.setCategoryLabelPositions(CategoryLabelPositions.UP_45); // 横轴上的 // 设置距离图片左端距离 domainAxis.setLowerMargin(0); // 设置距离图片右端距离 domainAxis.setUpperMargin(0); NumberAxis numberaxis = (NumberAxis) categoryplot.getRangeAxis(); numberaxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits()); numberaxis.setAutoRangeIncludesZero(true); numberaxis.setLabelFont(new Font("微软雅黑", Font.LAYOUT_NO_LIMIT_CONTEXT, 13)); // 设置最高的一个值与图片顶端的距离 numberaxis.setUpperMargin(0.15); // 设置最低的一个值与图片底端的距离 // numberaxis.setLowerMargin(0.15); // 获得renderer LineAndShapeRenderer lineandshaperenderer = (LineAndShapeRenderer) categoryplot .getRenderer(); lineandshaperenderer.setBaseShapesVisible(true); // series 点(即数据点)可见 lineandshaperenderer.setBaseLinesVisible(isline); // series 点(即数据点)间有连线可见 // 显示折点数据 lineandshaperenderer.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator()); lineandshaperenderer.setBaseItemLabelsVisible(true); }