一、添加依赖或者添加jar包
我添加依赖怎么都失败 所以我就下了个jar包,放进lib目录 然后添加进library就OK
二、布局文件中添加图表控件
随便添加在哪
<com.github.mikephil.charting.charts.BarChart
android:id="@+id/bar_chart2"
android:layout_width="match_parent"
android:layout_height="300dp" />
三、添加代码
将下面的各个方法函数添加进对应的Activcity的代码中
private void initBarChart() {
barChart = findViewById(R.id.bar_chart2);
barChart.getDescription().setEnabled(false);
barChart.setExtraOffsets(20, 20, 20, 20);
setAxis();
setLegend();
setData();
}
private void setData() {
List<IBarDataSet> sets = new ArrayList<>();
List<BarEntry> barEntries1 = new ArrayList<>();
barEntries1.add(new BarEntry(0, 1100f));
barEntries1.add(new BarEntry(1, 1000f));
barEntries1.add(new BarEntry(2, 900f));
barEntries1.add(new BarEntry(3, 800f));
barEntries1.add(new BarEntry(4, 700f));
barEntries1.add(new BarEntry(5, 600f));
barEntries1.add(new BarEntry(6, 500f));
barEntries1.add(new BarEntry(7, 400f));
barEntries1.add(new BarEntry(8, 300f));
barEntries1.add(new BarEntry(9, 200f));
BarDataSet barDataSet1 = new BarDataSet(barEntries1, "");
barDataSet1.setValueTextColor(Color.RED);
barDataSet1.setValueTextSize(15f);
barDataSet1.setColor(Color.parseColor("#1AE61A"));
barDataSet1.setLabel("蔬菜");
barDataSet1.setValueFormatter(new IValueFormatter() {
@Override
public String getFormattedValue(float value, Entry entry, int dataSetIndex, ViewPortHandler viewPortHandler) {
return value + "斤";
}
});
sets.add(barDataSet1);
List<BarEntry> barEntries2 = new ArrayList<>();
barEntries2.add(new BarEntry(0.3f, 210f));
barEntries2.add(new BarEntry(1.3f, 450f));
barEntries2.add(new BarEntry(2.3f, 430f));
barEntries2.add(new BarEntry(3.3f, 440f));
barEntries2.add(new BarEntry(4.3f, 180f));
barEntries2.add(new BarEntry(5.3f, 180f));
barEntries2.add(new BarEntry(6.3f, 180f));
barEntries2.add(new BarEntry(7.3f, 180f));
barEntries2.add(new BarEntry(8.3f, 180f));
barEntries2.add(new BarEntry(9.3f, 180f));
BarDataSet barDataSet2 = new BarDataSet(barEntries2, "");
barDataSet2.setDrawValues(true);
barDataSet2.setColor(Color.parseColor("#F7F709"));
barDataSet2.setValueTextColor(Color.RED);
barDataSet2.setValueTextSize(15f);
barDataSet2.setLabel("水果");
sets.add(barDataSet2);
BarData barData = new BarData(sets);
barData.setBarWidth(0.3f);
barChart.setData(barData);
}
private void setLegend() {
Legend legend = barChart.getLegend();
legend.setFormSize(12f);
legend.setTextSize(15f);
legend.setDrawInside(true);
legend.setOrientation(Legend.LegendOrientation.VERTICAL);
legend.setHorizontalAlignment(Legend.LegendHorizontalAlignment.RIGHT);
legend.setVerticalAlignment(Legend.LegendVerticalAlignment.TOP);
legend.setYOffset(55f);
legend.setXOffset(30f);
}
private void setAxis() {
XAxis xAxis = barChart.getXAxis();
xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
xAxis.setDrawGridLines(false);
xAxis.setLabelCount(10);
xAxis.setTextSize(15f);
final String labelName[] = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "10"};
xAxis.setValueFormatter(new IAxisValueFormatter() {
@Override
public String getFormattedValue(float value, AxisBase axis) {
if ((int) value < labelName.length) {
return labelName[(int) value];
} else {
return "";
}
}
});
xAxis.setYOffset(15);
YAxis yAxis_right = barChart.getAxisRight();
yAxis_right.setAxisMaximum(1200f);
yAxis_right.setAxisMinimum(0f);
yAxis_right.setEnabled(false);
YAxis yAxis_left = barChart.getAxisLeft();
yAxis_left.setAxisMaximum(1200f);
yAxis_left.setAxisMinimum(0f);
yAxis_left.setTextSize(15f);
}
|