enter image description here我在项目中使用MP条形图。我复制粘贴一个教程中的代码。该教程中的代码工作正常但在我的项目中出错。
BarChart barChart = (BarChart) findViewById(R.id.barchart);
ArrayList<BarEntry> entries = new ArrayList<>();
entries.add(new BarEntry(8f, 0));
entries.add(new BarEntry(2f, 1));
entries.add(new BarEntry(5f, 2));
entries.add(new BarEntry(20f, 3));
entries.add(new BarEntry(15f, 4));
entries.add(new BarEntry(19f, 5));
BarDataSet bardataset = new BarDataSet(entries, "Cells");
ArrayList<String> labels = new ArrayList<String>();
labels.add("2016");
labels.add("2015");
labels.add("2014");
labels.add("2013");
labels.add("2012");
labels.add("2011");
//error on this line because of string arraylist
//BarData data = new BarData(labels, bardataset);
barChart.setData(data); // set the data and list of lables into chart
barChart.setDescription("Set Bar Chart Description"); // set the description
bardataset.setColors(ColorTemplate.COLORFUL_COLORS);
barChart.animateY(5000);
// XML文件
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<com.github.mikephil.charting.charts.BarChart
android:id="@+id/barchart"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<!--com.jjoe64.graphview.GraphView
android:id="@+id/graph"
android:layout_width="match_parent"
android:layout_height="200dip" /-->
</LinearLayout>
答案 0 :(得分:0)
试试这个简单的教程。你从中得到了一些想法。
ArrayList<BarEntry> bargroup1 = new ArrayList<>();
bargroup1.add(new BarEntry(8f,0));
bargroup1.add(new BarEntry(2f,1));
bargroup1.add(new BarEntry(5f,2));
bargroup1.add(new BarEntry(20f,3));
bargroup1.add(new BarEntry(15f,4));
bargroup1.add(new BarEntry(19f,5));
BarDataSet barDataSet1 = new BarDataSet(bargroup1, "Group 1");
barDataSet1.setColors(ColorTemplate.COLORFUL_COLORS);
ArrayList<String> labels = new ArrayList<String>();
labels.add("2016");
labels.add("2015");
labels.add("2014");
labels.add("2013");
labels.add("2012");
labels.add("2011");
ArrayList<BarDataSet> dataSets = new ArrayList<>();
dataSets.add(barDataSet1);
BarData data = new BarData(labels, dataSets);
barChart.setData(data);