java如何在jfree折线图中添加注释
我在应用程序中使用jfree图表。我需要一个在x轴标签上方显示笔划点值的图表
预期的
我需要去掉数值之间的记号(54%和2008)。 我尝试了下面的代码来获取注释
final DefaultCategoryDataset dataset = new DefaultCategoryDataset();
dataset.addValue(23, "Line", "2008");
dataset.addValue(145, "Line", "2009");
dataset.addValue(245, "Line", "2010");
dataset.addValue(322, "Line", "2011");
final JFreeChart chart = ChartFactory.createLineChart(
"", // chart title
"", // domain axis label
"", // range axis label
dataset, // data
PlotOrientation.VERTICAL, // orientation
false, // include legend
false, // tooltips
false // urls
);
chart.setBackgroundPaint(Color.WHITE);
chart.setBorderVisible(true);
chart.setBorderPaint(Color.decode("#EEEEEE"));
chart.setPadding(new RectangleInsets(10, 10, 5, 5));
final CategoryPlot plot = (CategoryPlot) chart.getPlot();
plot.setBackgroundPaint(Color.WHITE);
plot.setRangeZeroBaselinePaint(Color.RED);
plot.setOutlineVisible(false);
plot.setRangeGridlinePaint(Color.white);
plot.setDomainGridlinePaint(Color.BLUE);
final CategoryAxis categoryAxis = (CategoryAxis) plot.getDomainAxis();
categoryAxis.setAxisLineVisible(false);
categoryAxis.setTickMarksVisible(false);
categoryAxis.setTickLabelFont(new java.awt.Font("Arial", java.awt.Font.PLAIN, 8));
final NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
rangeAxis.setVisible(false);
rangeAxis.setLabelPaint(Color.BLUE);
rangeAxis.setRangeWithMargins(21, 600);
DecimalFormat format = new DecimalFormat("###,###");
StandardCategoryItemLabelGenerator labelGenerator = new StandardCategoryItemLabelGenerator("{2}", format);
final LineAndShapeRenderer renderer = (LineAndShapeRenderer) plot.getRenderer();
renderer.setBaseShapesVisible(true);
renderer.setBaseShapesFilled(true);
renderer.setSeriesStroke(0, new BasicStroke(1.5f));
renderer.setSeriesItemLabelsVisible(0, true);
renderer.setBaseItemLabelGenerator(labelGenerator);
renderer.setBaseItemLabelFont(new java.awt.Font("Arial", java.awt.Font.PLAIN, 9));
Shape circle = new Ellipse2D.Double(-2, -2, 4, 4);
renderer.setSeriesShape(0, circle);
plot.getRenderer().setSeriesPaint(0, Color.decode("#0066CC"));
// CategoryAnnotation categoryAnnotation=new CategoryLineAnnotation(Male1, 248, Male2, 216, null, null);
// XYLineAnnotation annotation=new XYLineAnnotation(8, 0, 8, 24, new BasicStroke(2.0f), Color.blue);
// plot.addAnnotation( (CategoryAnnotation) annotation);
try {
ChartUtilities.saveChartAsPNG(new File("E:\\jfreeLinechart.png"), chart, 290, 95);
System.out.println("=====chart=====");
} catch (Exception e) {
e.printStackTrace();
}
从上面的代码我得到了下面的图表
实际值
请帮助我获得jfree折线图中的预期图像
# 1 楼答案
使用方法
setBasePositiveItemLabelPosition
可以设置基本阳性项目标签位置。试试这个:使用
OUTSIDE
、INSIDE
或CENTER
可以指定标签将放置在项目的位置# 2 楼答案
我找到了解决办法。对于预期图表,必须创建虚拟系列值。并使用此代码
您将得到相应的图表