Przeglądaj źródła

Centering legend

Berislav 9 lat temu
rodzic
commit
deec1fd95b

+ 1 - 1
app/build.gradle

@@ -4,7 +4,7 @@ apply plugin: 'maven-publish'
 
 def libraryGroupId = 'hr.tacitus.graphs'
 def libraryArtifactId = 'android-graphs'
-def libraryVersion = '0.0.6'
+def libraryVersion = '0.0.8'
 
 android {
     compileSdkVersion 25

+ 60 - 17
app/src/main/java/hr/tacitus/graphs/PieChartView.java

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

+ 1 - 0
app/src/main/res/values/attrs.xml

@@ -13,6 +13,7 @@
 
     <declare-styleable name="PieChartView">
         <attr name="title" format="string" />
+        <attr name="textColor" format="color" />
         <attr name="showLegend" format="boolean" />
     </declare-styleable>
 </resources>

+ 3 - 1
graphsapp/src/main/res/layout/activity_main.xml

@@ -23,8 +23,10 @@
         android:layout_below="@id/toolbar"
         android:layout_width="match_parent"
         android:layout_height="200dp"
-        app:title="Expenses share in total costs"
+        android:background="#123521"
         android:id="@+id/pieChart"
+        app:title="Some testing"
+        app:textColor="#ffffff"
         />
 
 

+ 1 - 1
graphsapp/src/main/res/values/styles.xml

@@ -1,7 +1,7 @@
 <resources>
 
     <!-- Base application theme. -->
-    <style name="AppTheme" parent="Theme.AppCompat.DayNight.DarkActionBar">
+    <style name="AppTheme" parent="Theme.AppCompat.NoActionBar">
         <!-- Customize your theme here. -->
         <item name="colorPrimary">@color/colorPrimary</item>
         <item name="colorPrimaryDark">@color/colorPrimaryDark</item>