|
|
@@ -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() {
|