|
| 1 | +package org.eclipse.swtchart.internal.series; |
| 2 | + |
| 3 | +import java.util.function.Function; |
| 4 | + |
| 5 | +import org.eclipse.swt.graphics.Font; |
| 6 | +import org.eclipse.swt.graphics.GC; |
| 7 | +import org.eclipse.swt.graphics.Point; |
| 8 | +import org.eclipse.swt.graphics.Rectangle; |
| 9 | +import org.eclipse.swt.graphics.Transform; |
| 10 | +import org.eclipse.swtchart.IAxis; |
| 11 | +import org.eclipse.swtchart.ICircularSeriesLabel; |
| 12 | +import org.eclipse.swtchart.internal.axis.Axis; |
| 13 | +import org.eclipse.swtchart.model.Node; |
| 14 | + |
| 15 | +public class CircularSeriesLabel extends SeriesLabel implements ICircularSeriesLabel { |
| 16 | + |
| 17 | + private Position position = Position.Inside; |
| 18 | + private Function<Node, String> labelProvider = Node::getId; |
| 19 | + |
| 20 | + public CircularSeriesLabel() { |
| 21 | + |
| 22 | + } |
| 23 | + |
| 24 | + @Override |
| 25 | + public void setPosition(Position position) { |
| 26 | + |
| 27 | + this.position = position; |
| 28 | + } |
| 29 | + |
| 30 | + @Override |
| 31 | + public Position getPosition() { |
| 32 | + |
| 33 | + return position; |
| 34 | + } |
| 35 | + |
| 36 | + @Override |
| 37 | + public void setLabelProvider(Function<Node, String> labelProvider) { |
| 38 | + |
| 39 | + this.labelProvider = labelProvider; |
| 40 | + } |
| 41 | + |
| 42 | + /* package */ void draw(GC gc, Node node, int level, Axis xAxis, Axis yAxis) { |
| 43 | + |
| 44 | + if(!isVisible()) |
| 45 | + return; |
| 46 | + switch(position) { |
| 47 | + case Inside: |
| 48 | + drawInside(gc, node, level, xAxis, yAxis); |
| 49 | + break; |
| 50 | + case Outside: |
| 51 | + drawOutside(gc, node, level, xAxis, yAxis); |
| 52 | + break; |
| 53 | + default: |
| 54 | + throw new IllegalArgumentException(); |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + private void drawInside(GC gc, Node node, int level, Axis xAxis, Axis yAxis) { |
| 59 | + |
| 60 | + String label = labelProvider.apply(node); |
| 61 | + if(label == null) |
| 62 | + return; |
| 63 | + |
| 64 | + Point angleBounds = node.getAngleBounds(); |
| 65 | + // check if pie chart (inner bound) is big enough to draw label |
| 66 | + gc.setFont(getFont()); |
| 67 | + Point textSize = gc.textExtent(label); |
| 68 | + Point start = getPixelCoordinate(xAxis, yAxis, (level - 1), angleBounds.x); |
| 69 | + Point end = getPixelCoordinate(xAxis, yAxis, (level - 1), angleBounds.x + angleBounds.y); |
| 70 | + int size = (int)Math.sqrt(Math.pow((end.x - start.x), 2) + Math.pow((end.y - start.y), 2)); |
| 71 | + if(size < textSize.y - 4) |
| 72 | + return; |
| 73 | + // calculate text angle |
| 74 | + int angle = angleBounds.x + angleBounds.y / 2; |
| 75 | + Point innerBound = getPixelCoordinate(xAxis, yAxis, (level - 1), angle); |
| 76 | + Point outerBound = getPixelCoordinate(xAxis, yAxis, level, angle); |
| 77 | + Transform t = new Transform(gc.getDevice()); |
| 78 | + Point textPosition; |
| 79 | + if(angle >= 90 && angle <= 270) { |
| 80 | + textPosition = outerBound; |
| 81 | + t.translate(textPosition.x, textPosition.y); |
| 82 | + t.rotate((-angle + 180)); |
| 83 | + } else { |
| 84 | + textPosition = innerBound; |
| 85 | + t.translate(textPosition.x, textPosition.y); |
| 86 | + t.rotate(-angle); |
| 87 | + } |
| 88 | + gc.setTransform(t); |
| 89 | + gc.setForeground(getForeground()); |
| 90 | + int length = (int)Math.sqrt(Math.pow((outerBound.x - innerBound.x), 2) + Math.pow((outerBound.y - innerBound.y), 2)); |
| 91 | + gc.setClipping(0, 0 - (textSize.y / 2), length - 3, textSize.y); |
| 92 | + gc.drawString(label, 2, 0 - (textSize.y / 2), true); |
| 93 | + gc.setClipping((Rectangle)null); |
| 94 | + gc.setTransform(null); |
| 95 | + } |
| 96 | + |
| 97 | + private void drawOutside(GC gc, Node node, int level, Axis xAxis, Axis yAxis) { |
| 98 | + |
| 99 | + String label = labelProvider.apply(node); |
| 100 | + if(label == null) |
| 101 | + return; |
| 102 | + |
| 103 | + Point angleBounds = node.getAngleBounds(); |
| 104 | + int angle = angleBounds.x + angleBounds.y / 2; |
| 105 | + Point textSize = gc.textExtent(label); |
| 106 | + // some heuristic to check if there is enough space to render a |
| 107 | + // label |
| 108 | + Point start = getPixelCoordinate(xAxis, yAxis, (level - 1), angleBounds.x); |
| 109 | + Point end = getPixelCoordinate(xAxis, yAxis, (level - 1), angleBounds.x + angleBounds.y); |
| 110 | + if(Math.abs(start.y - end.y) < 4) |
| 111 | + return; |
| 112 | + Point point = getPixelCoordinate(xAxis, yAxis, level * 1.03, angle); |
| 113 | + int x = angle >= 90 && angle <= 270 ? point.x - textSize.x : point.x; |
| 114 | + Font oldFont = gc.getFont(); |
| 115 | + gc.setForeground(getForeground()); |
| 116 | + gc.setFont(getFont()); |
| 117 | + gc.drawString(label, x, point.y - (textSize.y / 2), true); |
| 118 | + gc.setFont(oldFont); |
| 119 | + } |
| 120 | + |
| 121 | + protected final Point getPixelCoordinate(IAxis xAxis, IAxis yAxis, double pieLevel, int angle) { |
| 122 | + |
| 123 | + double xCoordinate = pieLevel * Math.cos(Math.toRadians(angle)); |
| 124 | + double yCoordinate = pieLevel * Math.sin(Math.toRadians(angle)); |
| 125 | + return new Point(xAxis.getPixelCoordinate(xCoordinate), yAxis.getPixelCoordinate(yCoordinate)); |
| 126 | + } |
| 127 | +} |
0 commit comments