eclipse / swtchart

Eclipse Public License 2.0
43 stars 41 forks source link

Added support for labels in pie and doughnut charts #311

Open buchen opened 1 year ago

buchen commented 1 year ago

Issue: #286

This is draft to enables labels on pie and doughnut charts.

I propose to extend the ISeriesLabel to add options for positioning and a label provider. I happy to discuss alternatives how to integrate it into the existing API.

There are three things that I would love to hear your thoughts:

First, I would love to enable multiple label placements (say the percentage inside the pie slice, the label placed outside). But that does not really fit to the existing ISeriesLabel interface with the get/setter for the ISeriesLabel. One could add a createAdditionalLabel method.

Second, in PP I am rendering the label color - if placed inside the pie slice - based on the color of the slice (dark slice = white; bright slice = black). Again, I could ignore the Foreground method in the interface, but it is not intuitive as the interface has the method

Third, I am using Function<Node, String> labelProvider to not introduce a dependency to JFace. Any better idea?

Examples for the use of the new API:

circularSeries.getLabel().setVisible(true);
circularSeries.getLabel().setPosition(ICircularSeriesLabel.Position.Inside);
circularSeries.getLabel().setForeground(Display.getDefault().getSystemColor(SWT.COLOR_DARK_RED));
circularSeries.getLabel().setLabelProvider(node -> String.format("%s (%.1f%%)", node.getId(), node.getValue() / node.getParent().getValue() * 100));

Example charts:

Bildschirm­foto 2022-12-04 um 19 39 40 Bildschirm­foto 2022-12-04 um 19 40 00 Bildschirm­foto 2022-12-04 um 19 50 44 Bildschirm­foto 2022-12-04 um 19 52 09
buchen commented 1 year ago

(I will fix the copyright headers once we agreed on the API and I polished the pull request up)

eselmeister commented 1 year ago

That looks really promising. Let me inspect it in detail and give you a feedback soon.

eselmeister commented 1 year ago

@buchen I have refactored the Pie charts handling, see https://github.com/eclipse/swtchart/issues/319.

I will start a release of SWTChart by Monday, 16th of January. If you'd like to merge your improvements for the next release, please let me know.