我正在使用以下代码将总标签定位在条形图的栏上。据推测,该位置位于每个渲染条的顶部。但是标签渲染的一些原因搞乱了整个图表!
更新定位总标签问题每个栏的总数应位于每个栏的顶部中心!相反,它位于靠近顶部的位置,而不是与条形对齐。
以下行有渲染图表标签的麻烦:
ItemLabelAnchor labelAnchor = "O".equals(direction) ? ItemLabelAnchor.OUTSIDE12 : ItemLabelAnchor.INSIDE8;
TextAnchor textAnchor = "O".equals(direction) ? TextAnchor.TOP_CENTER : TextAnchor.CENTER;
TextAnchor rotationAnchor = "O".equals(direction) ? TextAnchor.TOP_CENTER : TextAnchor.BOTTOM_CENTER;
renderer.setSeriesPositiveItemLabelPosition(0,
new ItemLabelPosition(labelAnchor, textAnchor, rotationAnchor, ((angle * Math.PI) / 180)));
我正在使用jdk 8和jFreeChart-1.0.19。完整代码清单后:
CategoryPlot plot = chart.getCategoryPlot();
if (plot != null) {
if (log.isDebugEnabled()) {
log.debug("Total Renderers : " + plot.getRendererCount());
}
try {
JRPropertiesMap propMap = jasperChart.getPropertiesMap();
if (propMap.containsProperty(APPLY_LABEL_ROTATION)) {
DecimalFormat dfKey = new DecimalFormat("###,###");
/* {0} - label would be equal to Series expression,
* {1} - label would be equal to Category expression,
* {2} - label would be equal to Value expression
*/
StandardCategoryItemLabelGenerator labelGenerator = new StandardCategoryItemLabelGenerator("{2}", dfKey);
LegendItemCollection lic = new LegendItemCollection();
List<LegendItem> legendList = new ArrayList<LegendItem>();
LegendItem totalOpenLegend = null;
for (int i = 0; i <= (plot.getRendererCount() - 1); i++) {
String prop = propMap.getProperty("Chart_" + (i + 1));
Double angle = null == prop ? 0D : Double.valueOf(prop.substring(0, prop.length() - 1));
String direction = null == prop ? "O" : "" + prop.charAt(prop.length() - 1);
if (log.isDebugEnabled()) {
log.debug("Property value for renerer : " + i + " Chart_" + (i + 1) + " angle : " + angle + ", Direction : " + direction);
}
CategoryItemRenderer renderer = plot.getRenderer(i);
renderer.setBaseItemLabelsVisible(true);
renderer.setBaseItemLabelGenerator(labelGenerator);
if (i == 0) {
Shape shape = ShapeUtilities.createLineRegion(new Line2D.Double(-6, 0, 6, 0), 1);
((LineAndShapeRenderer) renderer).setSeriesShape(0, shape);
((LineAndShapeRenderer) renderer).setBaseItemLabelsVisible(false);
}
if (i == 1) {
//shape = ShapeUtilities.createLineRegion(new Line2D.Double(0, 0, 1, 1), 2);
((LineAndShapeRenderer) renderer).setBaseShapesFilled(false);
((LineAndShapeRenderer) renderer).setBaseItemLabelPaint(Color.RED);
}
if (i == 2) {
((LineAndShapeRenderer) renderer).setBaseItemLabelsVisible(false);
}
if (i == 3) {
((LineAndShapeRenderer) renderer).setBaseItemLabelPaint(new Color(139, 90, 43));
}
LegendItem item = renderer.getLegendItem(i, 0);
if ((i >= 0) && (i < 4)) {
legendList.add(item);
} else {
totalOpenLegend = item;
BarRenderer barRenderer = (BarRenderer) renderer;
barRenderer.setMaximumBarWidth(0.3);
barRenderer.setItemMargin(0.1);
}
ItemLabelAnchor labelAnchor = "O".equals(direction) ? ItemLabelAnchor.OUTSIDE12 : ItemLabelAnchor.INSIDE8;
TextAnchor textAnchor = "O".equals(direction) ? TextAnchor.TOP_CENTER : TextAnchor.CENTER;
TextAnchor rotationAnchor = "O".equals(direction) ? TextAnchor.TOP_CENTER : TextAnchor.BOTTOM_CENTER;
renderer.setSeriesPositiveItemLabelPosition(0,
new ItemLabelPosition(labelAnchor, textAnchor, rotationAnchor, ((angle * Math.PI) / 180)));
//--
plot.setRenderer(i, renderer);
}
lic.add(totalOpenLegend);
for (LegendItem li : legendList) {
lic.add(li);
}
System.out.println("Setting Legend Items");
plot.setFixedLegendItems(lic);
plot.getDomainAxis().setLowerMargin(0);