Skip to content

Commit 898c334

Browse files
authored
1.2.1 changes: (halfhp#7)
* Lots of new documentation * New functionality for pie charts * Added FastLineAndPointRenderer * Project update to SDK 24 * Removed sdkmanager * Replaced jmockit with mockito * Added circleci config
1 parent 36933d2 commit 898c334

29 files changed

Lines changed: 782 additions & 142 deletions

androidplot-core/build.gradle

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
* limitations under the License.
1515
*/
1616

17-
apply plugin: 'android-sdk-manager'
1817
apply plugin: 'com.android.library'
1918
apply plugin: 'com.github.dcendents.android-maven'
2019
apply plugin: 'com.jfrog.bintray'
@@ -46,9 +45,13 @@ def siteUrl = 'http://androidplot.com'
4645
def gitUrl = 'https://bitbucket.org/androidplot/androidplot.git'
4746

4847
dependencies {
49-
testCompile group: 'org.jmockit', name: 'jmockit', version: '1.20'
50-
testCompile group: 'junit', name: 'junit', version: '4.8.1'
51-
testCompile "org.robolectric:robolectric:3.0"
48+
testCompile "org.mockito:mockito-core:1.10.19"
49+
testCompile group: 'junit', name: 'junit', version: '4.9'
50+
testCompile "org.robolectric:robolectric:3.1"
51+
52+
// temp fix for:
53+
// https://github.com/robolectric/robolectric/issues/1932
54+
testCompile 'org.khronos:opengl-api:gl1.1-android-2.1_r1'
5255
}
5356

5457
task javadoc(type: Javadoc) {

androidplot-core/src/main/java/com/androidplot/Plot.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -548,6 +548,10 @@ public synchronized boolean removeListener(PlotListener listener) {
548548
return listeners.remove(listener);
549549
}
550550

551+
protected ArrayList<PlotListener> getListeners() {
552+
return listeners;
553+
}
554+
551555
protected void notifyListenersBeforeDraw(Canvas canvas) {
552556
for (PlotListener listener : listeners) {
553557
listener.onBeforeDraw(this, canvas);

androidplot-core/src/main/java/com/androidplot/pie/PieChart.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@
2929
import com.androidplot.ui.HorizontalPositioning;
3030
import com.androidplot.ui.VerticalPositioning;
3131

32+
/**
33+
* Basic representation of a Pie Chart that displays a title and pie widget.
34+
*/
3235
public class PieChart extends Plot<Segment, SegmentFormatter, PieRenderer> {
3336

3437
private static final int DEFAULT_PIE_WIDGET_H_DP = 18;
@@ -37,6 +40,8 @@ public class PieChart extends Plot<Segment, SegmentFormatter, PieRenderer> {
3740
private static final int DEFAULT_PIE_WIDGET_Y_OFFSET_DP = 0;
3841
private static final int DEFAULT_PIE_WIDGET_X_OFFSET_DP = 0;
3942

43+
private static final int DEFAULT_PADDING_DP = 5;
44+
4045
public void setPie(PieWidget pie) {
4146
this.pie = pie;
4247
}
@@ -74,7 +79,8 @@ protected void onPreInit() {
7479
VerticalPositioning.ABSOLUTE_FROM_CENTER,
7580
Anchor.CENTER);
7681

77-
pie.setPadding(10, 10, 10, 10);
82+
final float padding = PixelUtils.dpToPix(DEFAULT_PADDING_DP);
83+
pie.setPadding(padding, padding, padding, padding);
7884
}
7985

8086
@Override

androidplot-core/src/main/java/com/androidplot/pie/PieRenderer.java

Lines changed: 45 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@
2525

2626
import java.util.List;
2727

28+
/**
29+
* Basic renderer for drawing pie charts.
30+
*/
2831
public class PieRenderer extends SeriesRenderer<PieChart, Segment, SegmentFormatter> {
2932

3033
// starting angle to use when drawing the first radial line of the first segment.
@@ -79,9 +82,17 @@ public void onRender(Canvas canvas, RectF plotArea, Segment series, SegmentForma
7982
protected void drawSegment(Canvas canvas, RectF bounds, Segment seg, SegmentFormatter f,
8083
float rad, float startAngle, float sweep) {
8184
canvas.save();
85+
startAngle = startAngle + f.getRadialInset();
86+
sweep = sweep - (f.getRadialInset() * 2);
87+
88+
// midpoint angle between startAngle and endAngle
89+
final float halfSweepEndAngle = startAngle + (sweep/2);
90+
91+
PointF translated = calculateLineEnd(
92+
bounds.centerX(), bounds.centerY(), f.getOffset(), halfSweepEndAngle);
8293

83-
float cx = bounds.centerX();
84-
float cy = bounds.centerY();
94+
final float cx = translated.x;
95+
final float cy = translated.y;
8596

8697
float donutSizePx;
8798
switch(donutMode) {
@@ -95,41 +106,55 @@ protected void drawSegment(Canvas canvas, RectF bounds, Segment seg, SegmentForm
95106
throw new UnsupportedOperationException("Not yet implemented.");
96107
}
97108

109+
final float outerRad = rad - f.getOuterInset();
110+
final float innerRad = donutSizePx == 0 ? 0 : donutSizePx + f.getInnerInset();
111+
//final float outerRad = rad;
112+
//final float innerRad = donutSizePx;
113+
98114
// do we have a pie chart of less than 100%
99115
if(Math.abs(sweep - 360f) > Float.MIN_VALUE) {
100116
// vertices of the first radial:
101-
PointF r1Outer = calculateLineEnd(cx, cy, rad, startAngle);
102-
PointF r1Inner = calculateLineEnd(cx, cy, donutSizePx, startAngle);
117+
PointF r1Outer = calculateLineEnd(cx, cy, outerRad, startAngle);
118+
PointF r1Inner = calculateLineEnd(cx, cy, innerRad, startAngle);
103119

104120
// vertices of the second radial:
105-
PointF r2Outer = calculateLineEnd(cx, cy, rad, startAngle + sweep);
106-
PointF r2Inner = calculateLineEnd(cx, cy, donutSizePx, startAngle + sweep);
121+
PointF r2Outer = calculateLineEnd(cx, cy, outerRad, startAngle + sweep);
122+
PointF r2Inner = calculateLineEnd(cx, cy, innerRad, startAngle + sweep);
107123

108124
Path clip = new Path();
109125

126+
// outer arc:
110127
// leave plenty of room on the outside for stroked borders;
111128
// necessary because the clipping border is ugly
112129
// and cannot be easily anti aliased. Really we only care about masking off the
113130
// radial edges.
114-
clip.arcTo(new RectF(bounds.left - rad,
115-
bounds.top - rad,
116-
bounds.right + rad,
117-
bounds.bottom + rad),
131+
clip.arcTo(new RectF(bounds.left - outerRad,
132+
bounds.top - outerRad,
133+
bounds.right + outerRad,
134+
bounds.bottom + outerRad),
118135
startAngle, sweep);
119136
clip.lineTo(cx, cy);
120137
clip.close();
121138
canvas.clipPath(clip);
122139

123140
Path p = new Path();
124-
p.arcTo(bounds, startAngle, sweep);
141+
142+
// outer arc:
143+
p.arcTo(new RectF(
144+
cx - outerRad,
145+
cy - outerRad,
146+
cx + outerRad,
147+
cy + outerRad)
148+
, startAngle, sweep);
125149
p.lineTo(r2Inner.x, r2Inner.y);
126150

151+
// inner arc:
127152
// sweep back to original angle:
128153
p.arcTo(new RectF(
129-
cx - donutSizePx,
130-
cy - donutSizePx,
131-
cx + donutSizePx,
132-
cy + donutSizePx),
154+
cx - innerRad,
155+
cy - innerRad,
156+
cx + innerRad,
157+
cy + innerRad),
133158
startAngle + sweep, -sweep);
134159

135160
p.close();
@@ -144,23 +169,23 @@ protected void drawSegment(Canvas canvas, RectF bounds, Segment seg, SegmentForm
144169
else {
145170
canvas.save(Canvas.CLIP_SAVE_FLAG);
146171
Path chart = new Path();
147-
chart.addCircle(cx, cy, rad, Path.Direction.CW);
172+
chart.addCircle(cx, cy, outerRad, Path.Direction.CW);
148173
Path inside = new Path();
149-
inside.addCircle(cx, cy, donutSizePx, Path.Direction.CW);
174+
inside.addCircle(cx, cy, innerRad, Path.Direction.CW);
150175
canvas.clipPath(inside, Region.Op.DIFFERENCE);
151176
canvas.drawPath(chart, f.getFillPaint());
152177
canvas.restore();
153178
}
154179

155180
// draw inner line:
156-
canvas.drawCircle(cx, cy, donutSizePx, f.getInnerEdgePaint());
181+
canvas.drawCircle(cx, cy, innerRad, f.getInnerEdgePaint());
157182

158183
// draw outer line:
159-
canvas.drawCircle(cx, cy, rad, f.getOuterEdgePaint());
184+
canvas.drawCircle(cx, cy, outerRad, f.getOuterEdgePaint());
160185
canvas.restore();
161186

162187
PointF labelOrigin = calculateLineEnd(cx, cy,
163-
(rad-((rad- donutSizePx)/2)), startAngle + (sweep/2));
188+
(outerRad-((outerRad- innerRad)/2)), halfSweepEndAngle);
164189

165190
// TODO: move segment labelling outside the segment drawing loop
166191
// TODO: so that the labels will not be clipped by the edge of the next

androidplot-core/src/main/java/com/androidplot/pie/SegmentFormatter.java

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,11 @@ public class SegmentFormatter extends Formatter<PieChart> {
3838
private Paint labelPaint;
3939
private Paint labelMarkerPaint;
4040

41+
private float offset;
42+
private float radialInset;
43+
private float innerInset;
44+
private float outerInset;
45+
4146
{
4247
setFillPaint(new Paint());
4348
// outer edge:
@@ -64,7 +69,6 @@ public class SegmentFormatter extends Formatter<PieChart> {
6469
getLabelPaint().setTextSize(DEFAULT_LABEL_FONT_SIZE);
6570
getLabelPaint().setAntiAlias(true);
6671
getLabelPaint().setTextAlign(Paint.Align.CENTER);
67-
//getLabelPaint().setShadowLayer(5, 4, 4, Color.BLACK);
6872

6973
// label marker paint:
7074
setLabelMarkerPaint(new Paint());
@@ -176,4 +180,46 @@ public Paint getLabelMarkerPaint() {
176180
public void setLabelMarkerPaint(Paint labelMarkerPaint) {
177181
this.labelMarkerPaint = labelMarkerPaint;
178182
}
183+
184+
public float getOffset() {
185+
return offset;
186+
}
187+
188+
/**
189+
* Set an offset relative to the center of the pie chart at which this segment should be drawn;
190+
* generally used to highlight specific segments.
191+
* @param offset
192+
*/
193+
public void setOffset(float offset) {
194+
this.offset = offset;
195+
}
196+
197+
public float getRadialInset() {
198+
return radialInset;
199+
}
200+
201+
/**
202+
* Set an inset in degrees for the radial edges of this segment.
203+
* generally used to highlight specific segments.
204+
* @param radialInset
205+
*/
206+
public void setRadialInset(float radialInset) {
207+
this.radialInset = radialInset;
208+
}
209+
210+
public float getInnerInset() {
211+
return innerInset;
212+
}
213+
214+
public void setInnerInset(float innerInset) {
215+
this.innerInset = innerInset;
216+
}
217+
218+
public float getOuterInset() {
219+
return outerInset;
220+
}
221+
222+
public void setOuterInset(float outerInset) {
223+
this.outerInset = outerInset;
224+
}
179225
}

0 commit comments

Comments
 (0)