|
|
@@ -10,12 +10,15 @@ import android.support.annotation.Nullable;
|
|
|
import android.text.Layout;
|
|
|
import android.text.StaticLayout;
|
|
|
import android.text.TextPaint;
|
|
|
+import android.text.TextUtils;
|
|
|
import android.util.AttributeSet;
|
|
|
import android.view.View;
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
import java.util.List;
|
|
|
|
|
|
+import static android.R.attr.ellipsize;
|
|
|
+
|
|
|
/**
|
|
|
* Created by Bero on 7.7.2017..
|
|
|
*/
|
|
|
@@ -32,6 +35,8 @@ public class PieChartView extends View {
|
|
|
// cached value
|
|
|
int descriptionWidth;
|
|
|
|
|
|
+ String valueDescription;
|
|
|
+
|
|
|
float getAngle() {
|
|
|
return value * 3.6f;
|
|
|
}
|
|
|
@@ -48,13 +53,16 @@ public class PieChartView extends View {
|
|
|
private float legendTextSizeSp = 10.f;
|
|
|
private float legendSquareSize = 20f;
|
|
|
private float legendWidth;
|
|
|
+ private float legendMaxTextWidth;
|
|
|
|
|
|
private float CHART_PADDING_DP = 24.f;
|
|
|
private float chartTextSizeSp = 10.f;
|
|
|
private float chartPaddingPx;
|
|
|
|
|
|
+ private PieChartAdapter adapter;
|
|
|
+ private PieChartColorProvider colorProvider;
|
|
|
|
|
|
- private String title = "This is a title";
|
|
|
+ private String title;
|
|
|
|
|
|
private List<Integer> colors;
|
|
|
|
|
|
@@ -181,9 +189,22 @@ public class PieChartView extends View {
|
|
|
}
|
|
|
|
|
|
private void drawChart(Canvas canvas) {
|
|
|
- Paint p = new Paint();
|
|
|
- p.setColor(Color.CYAN);
|
|
|
- canvas.drawRect(pieChartRect, p);
|
|
|
+// Paint p = new Paint();
|
|
|
+// p.setColor(Color.CYAN);
|
|
|
+// canvas.drawRect(pieChartRect, p);
|
|
|
+
|
|
|
+ if (items.size() == 0) {
|
|
|
+ StaticLayout staticLayout = new StaticLayout("No data available", valuePaint, (int)titleRect.width(), Layout.Alignment.ALIGN_CENTER,
|
|
|
+ 0, 0, false);
|
|
|
+
|
|
|
+ canvas.save();
|
|
|
+ canvas.translate(0, titleRect.top + chartPaddingPx);
|
|
|
+
|
|
|
+ staticLayout.draw(canvas);
|
|
|
+
|
|
|
+ canvas.restore();
|
|
|
+ return;
|
|
|
+ }
|
|
|
|
|
|
float startAngle = -90.f;
|
|
|
for (PieChartItem item : items) {
|
|
|
@@ -202,16 +223,16 @@ public class PieChartView extends View {
|
|
|
float top = pieChartRect.centerY() - y;
|
|
|
|
|
|
// RectF textRect = new RectF(left, top, left + textWidth, top + valuePaint.getTextSize());
|
|
|
- canvas.drawText(Integer.toHexString(item.paint.getColor()), left, top, valuePaint);
|
|
|
+ canvas.drawText(item.valueDescription, left, top, valuePaint);
|
|
|
|
|
|
startAngle -= item.getAngle();
|
|
|
}
|
|
|
}
|
|
|
|
|
|
private void drawLegend(Canvas canvas) {
|
|
|
- Paint p = new Paint();
|
|
|
- p.setColor(Color.RED);
|
|
|
- canvas.drawRect(legendRect, p);
|
|
|
+// Paint p = new Paint();
|
|
|
+// p.setColor(Color.RED);
|
|
|
+// canvas.drawRect(legendRect, p);
|
|
|
|
|
|
float left = legendRect.left + LEGEND_PADDING_HORIZONTAL;
|
|
|
float top = legendRect.top + LEGEND_PADDING_TOP;
|
|
|
@@ -221,10 +242,11 @@ public class PieChartView extends View {
|
|
|
PieChartItem item = items.get(i);
|
|
|
|
|
|
canvas.drawRect(new RectF(left, top, left + legendSquareSize, top + legendSquareSize), item.paint);
|
|
|
- // canvas.drawText(Double.toString(item.value), left + 30f, top + 20f, valuePaint);
|
|
|
+
|
|
|
StaticLayout staticLayout = new StaticLayout(item.description,
|
|
|
- valuePaint, Utils.textWidth(item.description, valuePaint, true),
|
|
|
- Layout.Alignment.ALIGN_NORMAL, 0, 0, false);
|
|
|
+ 0, item.description.length(),
|
|
|
+ legendDescriptionPaint, item.descriptionWidth,
|
|
|
+ Layout.Alignment.ALIGN_NORMAL, 0, 0, false, TextUtils.TruncateAt.END, (int)legendMaxTextWidth);
|
|
|
|
|
|
canvas.save();
|
|
|
canvas.translate(left + (legendSquareSize * 1.5f), top);
|
|
|
@@ -257,39 +279,47 @@ public class PieChartView extends View {
|
|
|
readyToDraw = true;
|
|
|
}
|
|
|
|
|
|
- private void initColors(int count) {
|
|
|
+ private void initColors() {
|
|
|
colors = new ArrayList<>();
|
|
|
-
|
|
|
- // 3 sets of 2 main colors - rb, rg, bg
|
|
|
- int maxValue = 220;
|
|
|
- int perSetGain = (int) (maxValue / (Math.ceil(count / 6.) + 1));
|
|
|
-
|
|
|
- float r = maxValue, g = 0, b = 0;
|
|
|
- for (int i = 0; i < count; ++i) {
|
|
|
- int color = Color.rgb((int) r, (int) g, (int) b);
|
|
|
- colors.add(color);
|
|
|
-
|
|
|
- r = Color.green(color);
|
|
|
- g = Color.blue(color);
|
|
|
- b = Color.red(color);
|
|
|
-
|
|
|
- if (i != 0 && (i + 1) % 3 == 0) {
|
|
|
- if (i == 2) {
|
|
|
- g = maxValue;
|
|
|
- } else {
|
|
|
- r -= perSetGain;
|
|
|
+ int count = adapter.getCount();
|
|
|
+ if (colorProvider != null) {
|
|
|
+ for (int i = 0; i < adapter.getCount(); ++i) {
|
|
|
+ colors.add(colorProvider.getColor(i));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ // 3 sets of 2 main colors - rb, rg, bg
|
|
|
+ int maxValue = 220;
|
|
|
+ int perSetGain = (int) (maxValue / (Math.ceil(count / 6.) + 1));
|
|
|
+
|
|
|
+ float r = maxValue, g = 0, b = 0;
|
|
|
+ for (int i = 0; i < count; ++i) {
|
|
|
+ int color = Color.rgb((int) r, (int) g, (int) b);
|
|
|
+ colors.add(color);
|
|
|
+
|
|
|
+ r = Color.green(color);
|
|
|
+ g = Color.blue(color);
|
|
|
+ b = Color.red(color);
|
|
|
+
|
|
|
+ if (i != 0 && (i + 1) % 3 == 0) {
|
|
|
+ if (i == 2) {
|
|
|
+ g = maxValue;
|
|
|
+ } else {
|
|
|
+ r -= perSetGain;
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- private void createItems(float[] values) {
|
|
|
- for (int i = 0; i < values.length; ++i) {
|
|
|
+ private void createItems() {
|
|
|
+ for (int i = 0; i < adapter.getCount(); ++i) {
|
|
|
PieChartItem pieChartItem = new PieChartItem();
|
|
|
- pieChartItem.value = values[i];
|
|
|
+ pieChartItem.value = adapter.getValue(i);
|
|
|
pieChartItem.paint = new Paint(Paint.ANTI_ALIAS_FLAG);
|
|
|
pieChartItem.paint.setColor(colors.get(i));
|
|
|
- pieChartItem.description = Double.toString(values[i]);
|
|
|
+ pieChartItem.description = adapter.getDescription(i);
|
|
|
+ pieChartItem.valueDescription = adapter.getValueDescription(i);
|
|
|
|
|
|
pieChartItem.descriptionWidth = Utils.textWidth(pieChartItem.description, legendDescriptionPaint, true);
|
|
|
|
|
|
@@ -320,15 +350,37 @@ public class PieChartView extends View {
|
|
|
break;
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+ if (legendWidth > 0) {
|
|
|
+ legendMaxTextWidth = legendWidth - 2 * LEGEND_PADDING_HORIZONTAL - legendSquareSize * 1.5f;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public PieChartAdapter getAdapter() {
|
|
|
+ return adapter;
|
|
|
+ }
|
|
|
+
|
|
|
+ public void setAdapter(PieChartAdapter adapter) {
|
|
|
+ this.adapter = adapter;
|
|
|
+
|
|
|
+ adapterChanged();
|
|
|
+ }
|
|
|
+
|
|
|
+ public void setColorProvider(PieChartColorProvider colorProvider) {
|
|
|
+ this.colorProvider = colorProvider;
|
|
|
+
|
|
|
+ adapterChanged();
|
|
|
}
|
|
|
|
|
|
- public void setValues(float[] values) {
|
|
|
+ private void adapterChanged() {
|
|
|
items.clear();
|
|
|
|
|
|
- initColors(values.length);
|
|
|
- createItems(values);
|
|
|
- initRectangles();
|
|
|
+ if (adapter != null) {
|
|
|
+ initColors();
|
|
|
+ createItems();
|
|
|
+ initRectangles();
|
|
|
+ }
|
|
|
+
|
|
|
invalidate();
|
|
|
}
|
|
|
-
|
|
|
}
|