Vous pouvez créer une classe personnalisée, qui étend PieChart
et de surcharger sa fonction drawLabel()
(et peut être draw()
) :
public class AdvancedPieChart extends PieChart {
@Override
protected void drawLabel(Canvas canvas, String labelText, DefaultRenderer renderer, List<RectF> prevLabelsBounds, int centerX, int centerY, float shortRadius, float longRadius, float currentAngle, float angle, int left, int right, int color, Paint paint, boolean line, boolean display) {
// do what You want with Label text, e.g. just
canvas.drawText(labelText, xLabel, yLabel, paint);
}
}
et utilisez votre AdvancedPieChart
au lieu de "standard" PieChart
.
Mise à jour :
Par exemple, cette mise en œuvre :
public class AdvancedPieChart extends PieChart {
public AdvancedPieChart(CategorySeries dataset, DefaultRenderer renderer) {
super(dataset, renderer);
}
@Override
protected void drawLabel(Canvas canvas, String labelText, DefaultRenderer renderer, List<RectF> prevLabelsBounds, int centerX, int centerY, float shortRadius, float longRadius, float currentAngle, float angle, int left, int right, int color, Paint paint, boolean line, boolean display) {
super.drawLabel(canvas, labelText, renderer, prevLabelsBounds, centerX, centerY, shortRadius, longRadius, currentAngle, angle, left, right, color, paint, line, display);
String additionalText = "Hello";
Paint textPaint = new Paint();
textPaint.setColor(Color.WHITE);
textPaint.setTextSize(18 * Resources.getSystem().getDisplayMetrics().densityDpi / 160f);
textPaint.setTypeface(Typeface.create(Typeface.DEFAULT, Typeface.BOLD));
Rect bounds = new Rect();
textPaint.getTextBounds(additionalText, 0, additionalText.length(), bounds);
canvas.drawText(additionalText, this.mCenterX - bounds.width() / 2, this.mCenterY - bounds.height() / 3, textPaint);
}
}
dessiner le texte "Hello" au centre du graphique :
Mise en page ( activity_main.xml
) :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:id="@+id/activity_main"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="ua.com.omelchenko.chartapplication.MainActivity">
<RelativeLayout
android:id="@+id/chart_holder"
android:layout_width="match_parent"
android:layout_height="match_parent">
</RelativeLayout>
</RelativeLayout>
(bien sûr, vous n'avez pas besoin de mises en page imbriquées)
et MainActivity.java
est :
public class MainActivity extends AppCompatActivity {
private RelativeLayout mPieChartHolder;
private GraphicalView mChartView;
private PieChart mPieChart;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String[] names = new String[] { "Category 1", "Category 2", "Category 3" };
double[] values = { 20, 30, 50 };
int[] colors = { Color.RED, Color.GREEN, Color.BLUE };
CategorySeries categorySeries = new CategorySeries("Pie Chart");
for (int i = 0; i < names.length; i++) {
categorySeries.add(names[i], values[i]);
}
DefaultRenderer defaultRenderer = new DefaultRenderer();
for (int i = 0; i < values.length; i++) {
SimpleSeriesRenderer seriesRenderer = new SimpleSeriesRenderer();
seriesRenderer.setColor(colors[i]);
defaultRenderer.setBackgroundColor(Color.BLACK);
defaultRenderer.setLabelsTextSize(20f);
defaultRenderer.setLegendTextSize(20f);
defaultRenderer.setApplyBackgroundColor(true);
defaultRenderer.addSeriesRenderer(seriesRenderer);
}
mPieChart = new AdvancedPieChart(categorySeries, defaultRenderer);
mChartView = new GraphicalView(this, mPieChart);
mPieChartHolder = (RelativeLayout) findViewById(R.id.chart_holder);
mPieChartHolder.addView(mChartView, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT));
}
}
(bien sûr, il ne s'agit que d'une démo).
Et si vous appuyez sur Ctrl+LeftMouseButton
lorsque le pointeur de la souris est sur PieChart
word dans AndroidStudio 2.0 et plus Vous pouvez trouver beaucoup de choses intéressantes sur l'organisation interne des PieChart
par exemple les coordonnées des centres de tarte, etc.