I'm trying to figure out how to control the labeling of 2 different XYSeries for a XYLineChart. For example, I have a line series show the real market value of a home and another line series showing the maximum assessed value. However, some of the labels on the lines overlap. I was able to control the overall labeling of the lines by accessing the XYItemRenderer and ItemLabelPosition but this works as a whole. I need to adjust the labeling on each individual line. Any suggestions? Thanks.
2 Answers
You can control the visibility of the labels for each series using the renderer's method, setSeriesItemLabelsVisible()
. Use a JCheckBox
, seen here, or a JComboBox
, seen here, to allow the user to adjust the visibility as desired. Persist the user's choice using Preferences
.
Thanks a bunch for your assistance. You pointed me in the right direction. Here is the process I used to control the item label position.
- Get AbstractXYItemRenderer
- Get ItemLabelPosition and customize
- Set postion using setSeriesPositionItemLabelPosition method of AbstractXYItemRenderer
final AbstractXYItemRenderer xyRenderer = (AbstractXYItemRenderer) this.chart.getXYPlot().getRenderer();
final ItemLabelPosition p1 = new ItemLabelPosition(ItemLabelAnchor.OUTSIDE12, TextAnchor.TOP_CENTER, TextAnchor.TOP_CENTER, - Math.PI / 4); xyRenderer.setItemLabelAnchorOffset(6.5); xyRenderer.setSeriesItemLabelPaint(1, Color.BLUE); xyRenderer.setSeriesPositiveItemLabelPosition(1, p1);
final ItemLabelPosition p2 = new ItemLabelPosition(ItemLabelAnchor.OUTSIDE6, TextAnchor.BOTTOM_CENTER, TextAnchor.BOTTOM_CENTER, - Math.PI / 4); xyRenderer.setItemLabelAnchorOffset(-8.5); xyRenderer.setSeriesItemLabelPaint(0, Color.RED); xyRenderer.setSeriesPositiveItemLabelPosition(0, p2);

- 11
- 2