|
|
@@ -66,6 +66,8 @@ public class PieChartView extends View {
|
|
|
|
|
|
private List<Integer> colors;
|
|
|
|
|
|
+ private int textColor;
|
|
|
+
|
|
|
private TextPaint titlePaint;
|
|
|
private TextPaint valuePaint;
|
|
|
private TextPaint legendDescriptionPaint;
|
|
|
@@ -100,30 +102,49 @@ public class PieChartView extends View {
|
|
|
try {
|
|
|
title = a.getString(R.styleable.PieChartView_title);
|
|
|
showLegend = a.getBoolean(R.styleable.PieChartView_showLegend, true);
|
|
|
+ textColor = a.getColor(R.styleable.PieChartView_textColor, Color.BLACK);
|
|
|
} finally {
|
|
|
a.recycle();
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ private float getLegendHeight() {
|
|
|
+ if (!showLegend) {
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+
|
|
|
+ return legendSquareSize * 1.5f * items.size();
|
|
|
+ }
|
|
|
+
|
|
|
@Nullable
|
|
|
private RectF getLegendRect() {
|
|
|
if (showLegend) {
|
|
|
- float left = titleRect.right - legendWidth;
|
|
|
- float right = titleRect.right;
|
|
|
- float top = titleRect.bottom;
|
|
|
- float bottom = getHeight();
|
|
|
+ float legendHeight = getLegendHeight();
|
|
|
+
|
|
|
+ float left = getRight() - legendWidth;
|
|
|
+ float right = getRight(); // titleRect.right;
|
|
|
+ float top = titleRect != null ? titleRect.bottom : 0;
|
|
|
+ float bottom = getHeight() - top;
|
|
|
|
|
|
- return new RectF(left, top, right, bottom);
|
|
|
+ RectF rect = new RectF(left, top, right, bottom);
|
|
|
+ rect.top = rect.centerY() - legendHeight / 2;
|
|
|
+ if (rect.top <= LEGEND_PADDING_TOP)
|
|
|
+ rect.top = LEGEND_PADDING_TOP;
|
|
|
+
|
|
|
+ // TODO: fix bottom
|
|
|
+ // rect.bottom = rect.centerY() + legendHeight / 2;
|
|
|
+
|
|
|
+ return rect;
|
|
|
}
|
|
|
return null;
|
|
|
}
|
|
|
|
|
|
private RectF getPieChartRect() {
|
|
|
// Pie chart max rectangle
|
|
|
- float left = titleRect.left + chartPaddingPx;
|
|
|
- float top = titleRect.bottom + chartPaddingPx;
|
|
|
- float right = titleRect.right - chartPaddingPx;
|
|
|
+ float left = getPaddingLeft() + chartPaddingPx;
|
|
|
+ float top = (titleRect != null ? titleRect.bottom : 0) + chartPaddingPx;
|
|
|
+ float right = getWidth() - getPaddingRight() - chartPaddingPx;
|
|
|
if (showLegend)
|
|
|
right -= legendRect.width();
|
|
|
float bottom = getHeight() - getPaddingBottom() - chartPaddingPx;
|
|
|
@@ -143,14 +164,21 @@ public class PieChartView extends View {
|
|
|
}
|
|
|
|
|
|
private RectF getTitleRect() {
|
|
|
- return new RectF(getPaddingLeft(), getPaddingTop(),
|
|
|
- getWidth() - getPaddingLeft() - getPaddingRight(), 32.f);
|
|
|
+ if (title != null)
|
|
|
+ return new RectF(getPaddingLeft(), getPaddingTop(),
|
|
|
+ getWidth() - getPaddingRight(), 32.f);
|
|
|
+ return null;
|
|
|
}
|
|
|
|
|
|
private void initTitleText() {
|
|
|
// int width = (int) titlePaint.measureText(title);
|
|
|
- titleTextLayout = new StaticLayout(title, titlePaint, (int) titleRect.width(),
|
|
|
- Layout.Alignment.ALIGN_CENTER, 1.0f, 0, false);
|
|
|
+ if (titleRect != null) {
|
|
|
+ titleTextLayout = new StaticLayout(title, titlePaint, (int) titleRect.width(),
|
|
|
+ Layout.Alignment.ALIGN_CENTER, 1.0f, 0, false);
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ titleTextLayout = null;
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
private void init(AttributeSet attributeSet) {
|
|
|
@@ -162,16 +190,17 @@ public class PieChartView extends View {
|
|
|
titlePaint = new TextPaint();
|
|
|
titlePaint.setAntiAlias(true);
|
|
|
titlePaint.setTextSize(Utils.spsToPixels(titleTextSizeSp, getResources()));
|
|
|
- titlePaint.setColor(Color.BLACK);
|
|
|
+ titlePaint.setColor(textColor);
|
|
|
|
|
|
valuePaint = new TextPaint();
|
|
|
valuePaint.setAntiAlias(true);
|
|
|
valuePaint.setTextSize(Utils.spsToPixels(chartTextSizeSp, getResources()));
|
|
|
- valuePaint.setColor(Color.BLACK);
|
|
|
+ valuePaint.setColor(textColor);
|
|
|
|
|
|
legendDescriptionPaint = new TextPaint();
|
|
|
legendDescriptionPaint.setAntiAlias(true);
|
|
|
legendDescriptionPaint.setTextSize(Utils.spsToPixels(legendTextSizeSp, getResources()));
|
|
|
+ legendDescriptionPaint.setColor(textColor);
|
|
|
}
|
|
|
|
|
|
private void initRectangles() {
|
|
|
@@ -194,11 +223,13 @@ public class PieChartView extends View {
|
|
|
// canvas.drawRect(pieChartRect, p);
|
|
|
|
|
|
if (items.size() == 0) {
|
|
|
- StaticLayout staticLayout = new StaticLayout("No data available", valuePaint, (int)titleRect.width(), Layout.Alignment.ALIGN_CENTER,
|
|
|
+ int width = titleRect != null ? (int)titleRect.width() : getWidth();
|
|
|
+
|
|
|
+ StaticLayout staticLayout = new StaticLayout("No data available", valuePaint, width, Layout.Alignment.ALIGN_CENTER,
|
|
|
0, 0, false);
|
|
|
|
|
|
canvas.save();
|
|
|
- canvas.translate(0, titleRect.top + chartPaddingPx);
|
|
|
+ canvas.translate(0, pieChartRect.top);
|
|
|
|
|
|
staticLayout.draw(canvas);
|
|
|
|
|
|
@@ -378,9 +409,21 @@ public class PieChartView extends View {
|
|
|
if (adapter != null) {
|
|
|
initColors();
|
|
|
createItems();
|
|
|
- initRectangles();
|
|
|
}
|
|
|
|
|
|
+ initRectangles();
|
|
|
+ initTitleText();
|
|
|
+
|
|
|
invalidate();
|
|
|
}
|
|
|
+
|
|
|
+ public String getTitle() {
|
|
|
+ return title;
|
|
|
+ }
|
|
|
+
|
|
|
+ public void setTitle(String title) {
|
|
|
+ this.title = title;
|
|
|
+
|
|
|
+ adapterChanged();
|
|
|
+ }
|
|
|
}
|