Selaa lähdekoodia

Support for multiline legend

Berislav 9 vuotta sitten
vanhempi
sitoutus
ab517c3b1e

+ 41 - 15
app/src/main/java/hr/tacitus/graphs/PieChartView.java

@@ -12,6 +12,7 @@ import android.text.StaticLayout;
 import android.text.TextPaint;
 import android.text.TextUtils;
 import android.util.AttributeSet;
+import android.util.Log;
 import android.view.View;
 
 import java.util.ArrayList;
@@ -35,6 +36,7 @@ public class PieChartView extends View {
 
         // cached value
         int descriptionWidth;
+        StaticLayout descriptionLayout;
 
         String valueDescription;
         int valueDescriptionWidth;
@@ -117,13 +119,18 @@ public class PieChartView extends View {
             return 0;
         }
 
-        return legendSquareSize * 1.5f * items.size();
+        float height = 0;
+        for (PieChartItem item : items) {
+            height += item.descriptionLayout.getHeight();
+            Log.d("SL-HEIGHT-2", String.format("%d", item.descriptionLayout.getHeight()));
+        }
+        return height;
     }
 
     @Nullable
     private RectF getLegendRect() {
         if (showLegend) {
-            float legendHeight = getLegendHeight();
+            float legendHeight = getLegendHeight() + 2 * LEGEND_PADDING_TOP;
 
             float left = getRight() - legendWidth;
             float right = getRight(); // titleRect.right;
@@ -131,12 +138,17 @@ public class PieChartView extends View {
             float bottom = getHeight() - top;
 
             RectF rect = new RectF(left, top, right, bottom);
-            rect.top = rect.centerY() - legendHeight / 2;
-            if (rect.top <= LEGEND_PADDING_TOP)
+            float centerY = rect.centerY();
+
+            rect.top = centerY - legendHeight / 2;
+            if (rect.top <= LEGEND_PADDING_TOP) {
                 rect.top = LEGEND_PADDING_TOP;
+            }
 
-            // TODO: fix bottom
-            // rect.bottom = rect.centerY() + legendHeight / 2;
+            rect.bottom = centerY + legendHeight / 2;
+            if (rect.bottom >= getHeight() - top - LEGEND_PADDING_TOP) {
+                rect.bottom = getHeight() - top - LEGEND_PADDING_TOP;
+            }
 
             return rect;
         }
@@ -290,18 +302,25 @@ public class PieChartView extends View {
 
             canvas.drawRect(new RectF(left, top, left + legendSquareSize, top + legendSquareSize), item.paint);
 
-            StaticLayout staticLayout = new StaticLayout(item.description,
-                    0, item.description.length(),
-                    legendDescriptionPaint, item.descriptionWidth,
-                    Layout.Alignment.ALIGN_NORMAL, 0, 0, false, TextUtils.TruncateAt.END, (int)legendMaxTextWidth);
+//            StaticLayout staticLayout = new StaticLayout(item.description,
+//                    0, item.description.length(),
+//                    legendDescriptionPaint, item.descriptionWidth,
+//                    Layout.Alignment.ALIGN_NORMAL, 0, 0, false, TextUtils.TruncateAt.END, (int)legendMaxTextWidth);
+
+//            StaticLayout staticLayout = new StaticLayout(item.description, legendDescriptionPaint,
+//                    (int)legendMaxTextWidth, Layout.Alignment.ALIGN_NORMAL, 1f, 0f, false);
+//
+//            Log.d("SL-HEIGHT", String.format("%d", staticLayout.getHeight()));
 
             canvas.save();
             canvas.translate(left + (legendSquareSize * 1.5f), top);
-            staticLayout.draw(canvas);
+            item.descriptionLayout.draw(canvas);
+
+            // staticLayout.draw(canvas);
 
             canvas.restore();
 
-            top += legendSquareSize * 1.5f;
+            top += item.descriptionLayout.getHeight();
         }
     }
 
@@ -320,8 +339,9 @@ public class PieChartView extends View {
     public void onSizeChanged(int xNew, int yNew, int xOld, int yOld) {
         super.onSizeChanged(xNew, yNew, xOld, yOld);
 
-        initRectangles();
-        initTitleText();
+        adapterChanged();
+//        initRectangles();
+//        initTitleText();
 
         readyToDraw = true;
     }
@@ -368,8 +388,9 @@ public class PieChartView extends View {
             pieChartItem.description = adapter.getDescription(i);
             pieChartItem.valueDescription = adapter.getValueDescription(i);
 
-            if (pieChartItem.description != null)
+            if (pieChartItem.description != null) {
                 pieChartItem.descriptionWidth = Utils.textWidth(pieChartItem.description, legendDescriptionPaint, true);
+            }
             if (pieChartItem.valueDescription != null) {
                 pieChartItem.valueDescriptionWidth = Utils.textWidth(pieChartItem.valueDescription, valuePaint, true);
                 pieChartItem.valueDescriptionHeight = Utils.textHeight(pieChartItem.valueDescription, valuePaint);
@@ -406,6 +427,11 @@ public class PieChartView extends View {
         if (legendWidth > 0) {
             legendMaxTextWidth = legendWidth - 2 * LEGEND_PADDING_HORIZONTAL - legendSquareSize * 1.5f;
         }
+
+        for (PieChartItem item : items) {
+            item.descriptionLayout = new StaticLayout(item.description, legendDescriptionPaint,
+                    (int)legendMaxTextWidth, Layout.Alignment.ALIGN_NORMAL, 1f, 0f, false);
+        }
     }
 
     public PieChartAdapter getAdapter() {

+ 1 - 1
graphsapp/src/main/java/hr/tacitus/graphsapp/MainActivity.java

@@ -80,7 +80,7 @@ public class MainActivity extends AppCompatActivity {
         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 110234 HRK", "Regular Expenses 53200 HRK", "Irregular Expenses"};
+            String[] descriptions = {"Fuel 110234 HRK but promising!", "Regular Expenses 53200 HRK", "Irregular Expenses 12500 HRK"};
 
             @Override
             public int getCount() {