gpt4 book ai didi

android - 无法使用 MPAndroidChart 像饼图那样绘制 donut

转载 作者:太空宇宙 更新时间:2023-11-03 12:51:46 25 4
gpt4 key购买 nike

我正在使用 MPAndroidChart我正在尝试使用 mpandroidchart lib 创建一个看起来像 donut 的饼图,但不知何故我无法做到这一点。下面是我的代码-

ArrayList<Entry> entries = new ArrayList<>();
entries.add(new Entry((float) 20.0, 0));
entries.add(new Entry((float) 30.0, 1));

int colors[] = {Color.parseColor("#DCDEE0"),Color.parseColor("#466A80"),Color.parseColor("#0078CA"),Color.parseColor("#5BC2E7"),Color.parseColor("#99E4FF")};
PieDataSet dataset = new PieDataSet(entries, "# of Calls");
dataset.setColors(colors);
dataset.setSliceSpace(3f);
ArrayList<String> labels = new ArrayList<String>();
labels.add("January");
labels.add("February");
/* labels.add("March");
labels.add("April");
labels.add("May");
labels.add("June");*/

PieChart chart1 = new PieChart(this);
chart1.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 0.5f));
chart1.setHoleRadius(60f);
chart1.setHoleColorTransparent(false);
chart1.setDrawHoleEnabled(true);
chart1.setUsePercentValues(true);
chart1.setHoleColor(Color.WHITE);
PieChart chart2 = new PieChart(this);
chart2.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 0.5f));
LinearLayout chart = (LinearLayout)findViewById(R.id.chart);
chart.addView(chart1);
chart.addView(chart2);

PieData data = new PieData(labels, dataset);
chart1.setData(data);
chart2.setData(data);

我可以画饼图,但不能让它看起来像一个 donut 。请引用随附的屏幕截图。有人可以帮忙吗。

enter image description here

最佳答案

是的,试试这个代码:=

public class piechart extends ActionBarActivity {

RelativeLayout mainLayout;
PieChart mChart;
// we're going to display pie chart for smartphones martket shares
float[] yData = { 15, 10, 15, 8, 4,10 };
String[] xData = { "Technical excellence", "Nimble", "Innovation", "Integrity", "Colloboration","Passion" };

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_piechart);
mainLayout = (RelativeLayout) findViewById(R.id.mainLayout);
mChart = new PieChart(this);

mainLayout.addView(mChart);
mainLayout.setBackgroundColor(Color.parseColor("#ffffff"));

// configure pie chart
mChart.setUsePercentValues(true);
mChart.setDescription("Employee Analysis");

// enable hole and configure
mChart.setDrawHoleEnabled(true);
mChart.setHoleColorTransparent(true);
mChart.setHoleRadius(7);
mChart.setTransparentCircleRadius(10);

// enable rotation of the chart by touch
mChart.setRotationAngle(0);
mChart.setRotationEnabled(true);

// set a chart value selected listener
mChart.setOnChartValueSelectedListener(new OnChartValueSelectedListener() {

@Override
public void onValueSelected(Entry e, int dataSetIndex, Highlight h) {
// display msg when value selected
if (e == null)
return;

Intent i=new Intent(piechart.this,Nimble.class);
startActivity(i);
}

@Override
public void onNothingSelected() {

}
});

// add data
addData();

// customize legends
Legend l = mChart.getLegend();
l.setPosition(Legend.LegendPosition.RIGHT_OF_CHART);
l.setXEntrySpace(7);
l.setYEntrySpace(5);

}

private void addData() {
ArrayList<Entry> yVals1 = new ArrayList<Entry>();

for (int i = 0; i < yData.length; i++)
yVals1.add(new Entry(yData[i], i));

ArrayList<String> xVals = new ArrayList<String>();

for (int i = 0; i < xData.length; i++)
xVals.add(xData[i]);

// create pie data set
PieDataSet dataSet = new PieDataSet(yVals1, "Market Share");
dataSet.setSliceSpace(3);
dataSet.setSelectionShift(5);

// add many colors
ArrayList<Integer> colors = new ArrayList<Integer>();

for (int c : ColorTemplate.VORDIPLOM_COLORS)
colors.add(c);

for (int c : ColorTemplate.JOYFUL_COLORS)
colors.add(c);

for (int c : ColorTemplate.COLORFUL_COLORS)
colors.add(c);

for (int c : ColorTemplate.LIBERTY_COLORS)
colors.add(c);

for (int c : ColorTemplate.PASTEL_COLORS)
colors.add(c);

colors.add(ColorTemplate.getHoloBlue());
dataSet.setColors(colors);

// instantiate pie data object now
PieData data = new PieData(xVals, dataSet);
data.setValueFormatter(new PercentFormatter());
data.setValueTextSize(11f);
data.setValueTextColor(Color.GRAY);

mChart.setData(data);

// undo all highlights
mChart.highlightValues(null);

// update pie chart
mChart.invalidate();
mChart.getLegend().setEnabled(false);
}

关于android - 无法使用 MPAndroidChart 像饼图那样绘制 donut ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29555691/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com