作成したサークルの周りに数字を追加しようとしています。問題は、テキストを追加するとすぐにサークルの一部が削除され、理由はわかりません。 私は角度に基づいてテキストを描画することと上の位置を計算することにより、出力を作ることができた円でテキストを描画するには?
public class PieChartView extends View {
private Paint slicePaint;
private int[] sliceClrs = { Color.RED, Color.WHITE,
Color.CYAN, Color.WHITE,
Color.CYAN, Color.WHITE,
Color.CYAN, Color.WHITE,
Color.CYAN, Color.WHITE};
private RectF rectf; // Our box
private float[] datapoints; // Our values
Canvas canvas;
Paint textColor;
Path path = new Path();
int colorCounterIndex = 0;
public PieChartView(Context context, AttributeSet attrs) {
super(context, attrs);
slicePaint = new Paint();
slicePaint.setAntiAlias(true);
slicePaint.setDither(true);
slicePaint.setStyle(Paint.Style.FILL);
textColor = new Paint();
textColor.setColor(Color.BLACK);
textColor.setTextSize(50);
}
@Override
protected void onDraw(Canvas canvas) {
this.canvas = canvas;
if (this.datapoints != null) {
int startTop = 0;
int startLeft = 0;
int endBottom = getWidth();
int endRight = endBottom; // To make this an equal square
// Create the box
rectf = new RectF(startLeft, startTop, endRight, endBottom); // Creating the box
float[] scaledValues = scale(); // Get the scaled values
float sliceStartPoint = 0;
for (int i = 0; i < scaledValues.length; i++) {
slicePaint.setColor(sliceClrs[i]);
canvas.drawArc(rectf, sliceStartPoint, scaledValues[i], true, slicePaint); // Draw slice
canvas.rotate(36, getWidth()/2, getHeight()/2);
path.addCircle(getWidth()/2, getHeight()/2, 180, Path.Direction.CW);
canvas.drawTextOnPath("(" + i + ")", path, 0, 5, textColor);
sliceStartPoint += scaledValues[i]; // Update starting point of the next slice
}
setLayerType(View.LAYER_TYPE_SOFTWARE, null);
}
}
public Canvas getCanvas() {
return canvas;
}
public void setDataPoints(float[] datapoints) {
this.datapoints = datapoints;
invalidate(); // Tells the chart to redraw itself
}
private float[] scale() {
float[] scaledValues = new float[this.datapoints.length];
float total = getTotal(); // Total all values supplied to the chart
for (int i = 0; i < this.datapoints.length; i++) {
scaledValues[i] = (this.datapoints[i]/total) * 360; // Scale each value
}
return scaledValues;
}
private float getTotal() {
float total = 0;
for (float val : this.datapoints)
total += val;
return total;
}
}
前後に感謝
であるだけでなく、あなたの質問に出力スクリーンショットを追加してください。 – Shubhank
@Shubhank look above – Anna
私はコードをテストできるようにdataPointsを渡す方法を教えてください。現在はnullで失敗しています – Shubhank