我正在尝试实施饼图,并且我一直没有获得图表数据。我正在使用MPAndroidChart库,我喜欢它的设计和演示部分。我正在复制此代码,它无法正常工作。我不想在我的应用中使用任何搜索栏,请帮助我与这个库相处。谢谢。
当然,请考虑我的活动实现OnChartValueSelectedListener
private PieChart mChart;
private Typeface tf;
protected String[] mMonths = new String[] {
"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Okt", "Nov", "Dec"
};
protected String[] mParties = new String[] {
"Party A", "Party B", "Party C", "Party D", "Party E", "Party F", "Party G", "Party H",
"Party I", "Party J", "Party K", "Party L", "Party M", "Party N", "Party O", "Party P",
"Party Q", "Party R", "Party S", "Party T", "Party U", "Party V", "Party W", "Party X",
"Party Y", "Party Z"
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.piechart);
PieChart mChart = (PieChart) findViewById(R.id.chart1);
mChart.setUsePercentValues(true);
mChart.setDescription("");
mChart.setExtraOffsets(5, 10, 5, 5);
mChart.setDragDecelerationFrictionCoef(0.95f);
tf = Typeface.createFromAsset(getAssets(), "OpenSans-Regular.ttf"); mChart.setCenterTextTypeface(Typeface.createFromAsset(getAssets(), "OpenSans-Light.ttf"));
mChart.setCenterText(generateCenterSpannableText());
mChart.setDrawHoleEnabled(true);
mChart.setHoleColor(Color.WHITE);
mChart.setTransparentCircleColor(Color.WHITE);
mChart.setTransparentCircleAlpha(110);
mChart.setHoleRadius(58f);
mChart.setTransparentCircleRadius(61f);
mChart.setDrawCenterText(true);
mChart.setRotationAngle(0);
// enable rotation of the chart by touch
mChart.setRotationEnabled(true);
mChart.setHighlightPerTapEnabled(true);
// mChart.setUnit(" €");
// mChart.setDrawUnitsInChart(true);
// add a selection listener
mChart.setOnChartValueSelectedListener(this);
setData(3, 100);
mChart.animateY(1400, Easing.EasingOption.EaseInOutQuad);
// mChart.spin(2000, 0, 360);
Legend l = mChart.getLegend();
l.setPosition(Legend.LegendPosition.RIGHT_OF_CHART);
l.setXEntrySpace(7f);
l.setYEntrySpace(0f);
l.setYOffset(0f);
}
private void setData(int count, float range) {
float mult = range;
ArrayList<Entry> yVals1 = new ArrayList<Entry>();
// IMPORTANT: In a PieChart, no values (Entry) should have the same
// xIndex (even if from different DataSets), since no values can be
// drawn above each other.
for (int i = 0; i < count + 1; i++) {
yVals1.add(new Entry((float) (Math.random() * mult) + mult / 5, i));
}
ArrayList<String> xVals = new ArrayList<String>();
for (int i = 0; i < count + 1; i++)
xVals.add(mParties[i % mParties.length]);
PieDataSet dataSet = new PieDataSet(yVals1, "Election Results");
dataSet.setSliceSpace(3f);
dataSet.setSelectionShift(5f);
// add a lot of 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);
//dataSet.setSelectionShift(0f);
PieData data = new PieData(xVals, dataSet);
data.setValueFormatter(new PercentFormatter());
data.setValueTextSize(11f);
data.setValueTextColor(Color.WHITE);
data.setValueTypeface(tf);
mChart.setData(data);
// undo all highlights
mChart.highlightValues(null);
mChart.invalidate();
}
private SpannableString generateCenterSpannableText() {
SpannableString s = new SpannableString("MPAndroidChart\ndeveloped by Philipp Jahoda");
s.setSpan(new RelativeSizeSpan(1.7f), 0, 14, 0);
s.setSpan(new StyleSpan(Typeface.NORMAL), 14, s.length() - 15, 0);
s.setSpan(new ForegroundColorSpan(Color.GRAY), 14, s.length() - 15, 0);
s.setSpan(new RelativeSizeSpan(.8f), 14, s.length() - 15, 0);
s.setSpan(new StyleSpan(Typeface.ITALIC), s.length() - 14, s.length(), 0);
s.setSpan(new ForegroundColorSpan(ColorTemplate.getHoloBlue()), s.length() - 14, s.length(), 0);
return s;
}
@Override
public void onValueSelected(Entry e, int dataSetIndex, Highlight h) {
if (e == null)
return;
Log.i("VAL SELECTED",
"Value: " + e.getVal() + ", xIndex: " + e.getXIndex()
+ ", DataSet index: " + dataSetIndex);
}
@Override
public void onNothingSelected() {
Log.i("PieChart", "nothing selected");
}
编辑:我的问题是使用此行Typeface.createFromAsset(getAssets(),&#34; OpenSans-Regular.ttf&#34;);它给我addFontFromAsset无法创建字体OpenSans-Regular.ttf 尽管当我使用这个InputStream时它对我有用am = getAssets()。open(&#34; cert.pem&#34;);