我需要绘制饼图(动态值)。如何在不使用第三方API的情况下创建图表。
答案 0 :(得分:22)
下面显示了简单的饼图,您必须扩展以获得更多功能... values []和colors数组应该相等....
public class Demo extends Activity {
/** Called when the activity is first created. */
float values[]={300,400,100,500};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
LinearLayout linear=(LinearLayout) findViewById(R.id.linear);
values=calculateData(values);
linear.addView(new MyGraphview(this,values));
}
private float[] calculateData(float[] data) {
// TODO Auto-generated method stub
float total=0;
for(int i=0;i<data.length;i++)
{
total+=data[i];
}
for(int i=0;i<data.length;i++)
{
data[i]=360*(data[i]/total);
}
return data;
}
public class MyGraphview extends View
{
private Paint paint=new Paint(Paint.ANTI_ALIAS_FLAG);
private float[] value_degree;
private int[] COLORS={Color.BLUE,Color.GREEN,Color.GRAY,Color.CYAN,Color.RED};
RectF rectf = new RectF (10, 10, 200, 200);
int temp=0;
public MyGraphview(Context context, float[] values) {
super(context);
value_degree=new float[values.length];
for(int i=0;i<values.length;i++)
{
value_degree[i]=values[i];
}
}
@Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
super.onDraw(canvas);
for (int i = 0; i < value_degree.length; i++) {//values2.length; i++) {
if (i == 0) {
paint.setColor(COLORS[i]);
canvas.drawArc(rectf, 0, value_degree[i], true, paint);
}
else
{
temp += (int) value_degree[i - 1];
paint.setColor(COLORS[i]);
canvas.drawArc(rectf, temp, value_degree[i], true, paint);
}
}
}
}
}
答案 1 :(得分:4)
绘制饼图的基本功能,输入是颜色数组和值数组。它们的数组必须具有相同的大小。根据每张幻灯片的值和所有值的总和来调整切片。当然,您也可以更改为浮点值。 此解决方案作为轻量级辅助函数提供。它易于使用,您无需为其定义类。
public static void drawPieChart(Bitmap bmp, int[] colors, int[] slices){
//canvas to draw on it
Canvas canvas = new Canvas(bmp);
RectF box = new RectF(2, 2,bmp.getWidth()-2 , bmp.getHeight()-2);
//get value for 100%
int sum = 0;
for (int slice : slices) {
sum += slice;
}
//initalize painter
Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeWidth(1f);
paint.setStyle(Style.FILL_AND_STROKE);
float start = 0;
//draw slices
for(int i =0; i < slices.length; i++){
paint.setColor(colors[i]);
float angle;
angle = ((360.0f / sum) * slices[i]);
canvas.drawArc(box, start, angle, true, paint);
start += angle;
}
}
答案 2 :(得分:3)
Ramesh的答案几乎没有问题。然而,最后的白色故障是由从浮动到int的舍入错误引起的。只需将“temp”的类型更改为float,并在“temp + = value_degree [i-1]”时将结果删除到(int),并且它是完美的。
答案 3 :(得分:2)
我有一个相当简单的开源软件库,您很可能会用它来实现您的目的(https://github.com/saulpower/ExpandablePieChart)。
答案 4 :(得分:0)
您可以使用扩展org.achartengine.chart.PieChart
的aChartEngine roundChart
有关详细信息,请转到achartengine-0.7.0-javadocs/org/achartengine/chart/PieChart.html
从aChartEngine.org
答案 5 :(得分:-1)
如果你正在寻找一些只绘制漂亮图表的东西,那么我建议你使用MPAndroidChart。如果你正在寻找更轻量级的东西,你可以使用它。
public class PieDataSet {
private List<PieEntry> mPieEntries;
private Paint mCurrentPaint;
public PieDataSet(List<PieEntry> entries) {
mPieEntries = entries;
mCurrentPaint = new Paint();
}
public void draw(Canvas canvas, float x, float y, float radius){
RectF target = new RectF(x - radius, y - radius, x + radius, y + radius);
float startAngle = 0;
for(PieEntry entry : mPieEntries){
int arc = (int)(entry.getValue() * 360);
mCurrentPaint.setColor(entry.getColor());
canvas.drawArc(target, startAngle, arc, true, mCurrentPaint);
startAngle = startAngle + arc;
}
}
public static class PieEntry {
private float mValue;
private int mColor;
public PieEntry(float value, int color){
mValue = value;
mColor = color;
}
//region Getters {}
public float getValue() {
return mValue;
}
public int getColor() {
return mColor;
}
//endregion
//region Setters {}
public void setValue(float mValue) {
this.mValue = mValue;
}
public void setColor(int mColor) {
this.mColor = mColor;
}
//endregion
}
}
您只需创建PieDataSet.PieEntry
个对象的列表,然后创建PieDataSet
。创建PieDataSet
对象后,只需调用pieDataSet.draw(canvas, x, y, radius)
即可在画布上绘制饼图。
要记住的一点是确保您的数据集值都加起来为1.0f(100%),否则您将无法获得所需的结果。