我用下面的代码创建了一个 JFreeChart,但是 Y 轴标记被截断。即使数据点在 Y 轴上重叠,我应该如何显示图表?基本上,我希望从我的文件中生成 Y 轴点,填充适当的范围并显示在图表中。
private static JFreeChart buildChart(TimeSeriesCollection dataset,
String title, boolean endPoints) throws IOException {
// Create the chart
JFreeChart chart0 = ChartFactory.createTimeSeriesChart(
title, "Hour", "Count", dataset, true, true, false);
// Setup the appearance of the chart
chart0.setBackgroundPaint(Color.white);
XYPlot plot = (XYPlot) chart0.getXYPlot();
plot.setBackgroundPaint(Color.lightGray);
plot.setDomainGridlinePaint(Color.white);
plot.setRangeGridlinePaint(Color.white);
plot.setAxisOffset(new RectangleInsets(10.0, 10.0, 10.0, 10.0));
plot.setDomainCrosshairVisible(true);
plot.setRangeCrosshairVisible(true);
// Display data points or just the lines?
if (endPoints) {
XYItemRenderer renderer = plot.getRenderer();
if (renderer instanceof StandardXYItemRenderer) {
StandardXYItemRenderer rr = (StandardXYItemRenderer) renderer;
rr.setBaseShapesVisible(true);
rr.setBaseShapesFilled(true);
rr.setDrawSeriesLineAsPath(true);
rr.setSeriesPaint(0, Color.blue.brighter());
rr.setSeriesVisible(0, true); // default
rr.setSeriesVisibleInLegend(0, true); // default
NumberAxis domainAxis = new NumberAxis();
domainAxis.setUpperMargin(0.15);
domainAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
domainAxis = (NumberAxis) plot.getDomainAxis();
domainAxis = (NumberAxis) plot.getRangeAxis();
domainAxis.setAutoRangeIncludesZero(false);
}
}
// Tell the chart how we would like dates to read
DateAxis axis = (DateAxis) plot.getDomainAxis();
axis.setAutoRange(true);
//axis.getDefaultAutoRange();
axis.setDateFormatOverride(new SimpleDateFormat("HH:mm"));
try {
ChartUtilities.saveChartAsJPEG(new File("suc.jpg"), 1.0f, chart0, 990, 700);
} catch (IOException e) {
e.printStackTrace();
}
return chart0;
}
下面是创建的图像,您可以清楚地看到 Y 轴有重叠显示。