Pārlūkot izejas kodu

Added pie chart adapter and color provider

Berislav 9 gadi atpakaļ
vecāks
revīzija
092501b6d7

+ 13 - 0
app/src/main/java/hr/tacitus/graphs/PieChartAdapter.java

@@ -0,0 +1,13 @@
+package hr.tacitus.graphs;
+
+/**
+ * Created by Bero on 9.7.2017..
+ */
+
+public interface PieChartAdapter {
+    int getCount();
+
+    String getDescription(int position);
+    float getValue(int position);
+    String getValueDescription(int position);
+}

+ 9 - 0
app/src/main/java/hr/tacitus/graphs/PieChartColorProvider.java

@@ -0,0 +1,9 @@
+package hr.tacitus.graphs;
+
+/**
+ * Created by Bero on 9.7.2017..
+ */
+
+public interface PieChartColorProvider {
+    int getColor(int position);
+}

+ 92 - 40
app/src/main/java/hr/tacitus/graphs/PieChartView.java

@@ -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();
     }
-
 }

+ 42 - 3
graphsapp/src/main/java/hr/tacitus/graphsapp/MainActivity.java

@@ -1,11 +1,14 @@
 package hr.tacitus.graphsapp;
 
+import android.graphics.Color;
 import android.support.v7.app.AppCompatActivity;
 import android.os.Bundle;
 import android.view.View;
 
 import hr.tacitus.graphs.BarGraphAdapter;
 import hr.tacitus.graphs.BarGraphView;
+import hr.tacitus.graphs.PieChartAdapter;
+import hr.tacitus.graphs.PieChartColorProvider;
 import hr.tacitus.graphs.PieChartView;
 
 import java.text.DecimalFormat;
@@ -24,10 +27,15 @@ public class MainActivity extends AppCompatActivity {
 //        updateAdapter();
 
         // float[] values = {12.5f, 50.0f, 25f, 12.5f};
-        float[] values = {10f, 5f, 15f, 10f, 10f, 5f, 15f, 5f, 5f, 5f, 5f, 5f, 5f, };
         pieChartView = (PieChartView) findViewById(R.id.pieChart);
-        pieChartView.setValues(values);
-
+        pieChartView.setColorProvider(new PieChartColorProvider() {
+            int[] colors = {Color.RED, Color.YELLOW, Color.parseColor("#f47142")};
+            @Override
+            public int getColor(int position) {
+                return colors[position];
+            }
+        });
+        updateAdapter();
     }
 
     public void toggleAdapter(View view) {
@@ -68,8 +76,39 @@ public class MainActivity extends AppCompatActivity {
         };
     }
 
+    private PieChartAdapter createPieChartAdapter() {
+        return new PieChartAdapter() {
+            // float[] values = {10f, 5f, 15f, 10f, 10f, 5f, 15f, 5f, 5f, 5f, 5f, 5f, 5f, };
+            float[] values = {23.5f, 27.5f, 49f};
+            String[] descriptions = {"Fuel", "Regular Expenses", "Irregular Expenses"};
+
+            @Override
+            public int getCount() {
+                return values.length;
+            }
+
+            @Override
+            public String getDescription(int position) {
+                return descriptions[position];
+            }
+
+            @Override
+            public float getValue(int position) {
+                return values[position];
+            }
+
+            @Override
+            public String getValueDescription(int position) {
+                return Float.toString(values[position]) + " %";
+            }
+        };
+    }
+
     private void updateAdapter() {
         if (bgv != null)
             bgv.setAdapter(bgv.getAdapter() == null ? createSampleAdapter() : null);
+        if (pieChartView != null) {
+            pieChartView.setAdapter(pieChartView.getAdapter() == null ? createPieChartAdapter() : null);
+        }
     }
 }