效果图:
开始时间和结束时间的间隔
Long timeLength = (simpleDateFormat.parse(endtime).getTime() - simpleDateFormat.parse(starttime).getTime())/(60*60*1000);
//为生成柱状图添加数据 TimeTableXYDataset localTimeTableXYDataset = new TimeTableXYDataset();
localTimeTableXYDataset.add((new Hour(小时,日,月,年), 参数值, 标题名称);//根据自己需要的格式添加
/** * 柱状图 间隔显示 * * @param path 保存路径 * @param titleName 标题名称 * @param paramTableXYDataset 参数 * @param width * @param height * @param title * @param imageHeight * @throws IOException */ public static Jfreechart createChart(String titleName, TableXYDataset paramTableXYDataset, int width, int height, Long timeLength) throws IOException { //x轴标题 DateAxis localDateAxis = new DateAxis(null); SimpleDateFormat sdf = new SimpleDateFormat("d日H时", Locale.CHINESE); //格式化时间轴显示 //隔多少单位显示 localDateAxis.setTickUnit(new DateTickUnit(DateTickUnit.HOUR, (int) (timeLength / 6.0), sdf));//6.0可变参数 根据大小调整//y轴标题 NumberAxis localNumberAxis = new NumberAxis(null); localNumberAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());// localNumberAxis.setUpperMargin(0.1D); localNumberAxis.setTickUnit(new NumberTickUnit(8));// localNumberAxis.setLowerBound(0.0); StackedXYBarRenderer localStackedXYBarRenderer = new StackedXYBarRenderer(0.1D); //阴影 localStackedXYBarRenderer.setShadowVisible(false); localStackedXYBarRenderer.setBaseItemLabelsVisible(true); localStackedXYBarRenderer.setItemLabelAnchorOffset(0); // 设置柱状图的顶端显示数字 //柱状上不显示数据 localStackedXYBarRenderer.setBaseItemLabelGenerator(new StandardXYItemLabelGenerator());// localStackedXYBarRenderer.setBasePositiveItemLabelPosition((new ItemLabelPosition(ItemLabelAnchor.INSIDE8, TextAnchor.BOTTOM_CENTER))); localStackedXYBarRenderer.setBasePositiveItemLabelPosition(new ItemLabelPosition(ItemLabelAnchor.OUTSIDE12, TextAnchor.BOTTOM_CENTER)); //设置每个平行柱的之间距离 localStackedXYBarRenderer.setMargin(0.5D); XYPlot localXYPlot = new XYPlot(paramTableXYDataset, localDateAxis, localNumberAxis, localStackedXYBarRenderer); JFreeChart localJFreeChart = new JFreeChart(titleName, localXYPlot); TextTitle textTitle = localJFreeChart.getTitle(); textTitle.setFont(new Font("微软雅黑", Font.PLAIN, 12)); localJFreeChart.setTitle(textTitle); // 设置总的背景颜色 localJFreeChart.setBackgroundPaint(ChartColor.white); // 获得图表对象 XYPlot p = localJFreeChart.getXYPlot(); // 设置图的背景颜色 p.setBackgroundPaint(ChartColor.white); //设置图的边框 p.setOutlinePaint(ChartColor.white); //设置x轴网格线 p.setDomainGridlinePaint(ChartColor.white); //设置y轴网格线 p.setRangeGridlinePaint(ChartColor.white); NumberAxis numberaxis = (NumberAxis) p.getRangeAxis(); numberaxis.setUpperMargin(0.14999999999999999D); numberaxis.setLowerBound(0.0);// numberaxis.setTickUnit(new NumberTickUnit(0.5));//设置Y轴间隔 numberaxis.setAutoTickUnitSelection(true);//取消柱子上的渐变色 XYBarRenderer customBarRenderer = (XYBarRenderer) p.getRenderer(); customBarRenderer.setBaseItemLabelGenerator(new StandardXYItemLabelGenerator()); customBarRenderer.setSeriesItemLabelsVisible(0, true); customBarRenderer.setBarPainter(new StandardXYBarPainter());//设置柱子颜色 customBarRenderer.setSeriesPaint(0, new Color(0, 97, 183)); localJFreeChart.getLegend().setVisible(false); // saveIoImage(path, width, height, cellsValue, title, imageHeight, localJFreeChart); return localJFreeChart; }