Android MPAndroidChart BarChart
继续上篇
https://blog.csdn.net/weixin_44889138/article/details/103498294
导入依赖,使用方式,可以参考上上一篇
柱状图
xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<com.github.mikephil.charting.charts.BarChart
android:id="@+id/bar_chart"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
方法 | 解释 |
---|---|
setTouchEnabled | 设置是否可以触摸 |
setDragEnabled | 是否可以拖拽 |
setScaleEnabled | 是否可以缩放 |
setPinchZoom | y轴的值是否跟随图表变换缩放;如果禁止,y轴的值会跟随图表变换缩放 |
setDrawBarShadow | 柱状图没有数据的部分是否显示阴影效果 |
使用
- setChartData
偷个懒,没有重构,写在一起了
同样还是要设置对应的Entry
//设置两条数据
final List<BarEntry> value1 = new ArrayList<>();
BarEntry[] entries = {new BarEntry(0f, 396), new BarEntry(1f, 1089),
new BarEntry(2f, 963), new BarEntry(3f, 756), new BarEntry(4f, 287)};
for (int i = 0; i < entries.length; i++) {
value1.add(entries[i]);
}
List<BarEntry> value2 = new ArrayList<>();
value2.add(new BarEntry(0f, 245));
value2.add(new BarEntry(1f, 520));
value2.add(new BarEntry(2f, 504));
value2.add(new BarEntry(3f, 517));
value2.add(new BarEntry(4f, 186));
- BarDataSet
BarDataSet set1 = new BarDataSet(value1, "有违章");
set1.setColor(Color.GREEN);
set1.setValueFormatter(new ValueFormatter() {
@Override
public String getFormattedValue(float value) {
if ((int) (value % 5) == 0) {
return new DecimalFormat("##.0").format((value / (value + 245)) * 100) + "%";
} else if ((int) (value % 5) == 1) {
return new DecimalFormat("##.0").format((value / (value + 520)) * 100) + "%";
} else if ((int) (value % 5) == 2) {
return new DecimalFormat("##.0").format((value / (value + 504)) * 100) + "%";
} else if ((int) (value % 5) == 3) {
return new DecimalFormat("##.0").format((value / (value + 517)) * 100) + "%";
} else if ((int) (value % 5) == 4) {
return new DecimalFormat("##.0").format((value / (value + 186)) * 100) + "%";
}
return "";
}
});
BarDataSet set2 = new BarDataSet(value2, "无违章");
set2.setColor(Color.BLUE);
set2.setDrawValues(false);
setValueFormatter格式值
- setData
List<IBarDataSet> barDataSets = new ArrayList<>();
barDataSets.add(set1);
barDataSets.add(set2);
BarData barData = new BarData(barDataSets);
barData.setBarWidth(0.5f);
mBarChart.setDrawValueAboveBar(false);//柱状图上面的数值是否在柱子上面
mBarChart.setData(barData);
- setXAxis
private void setXAxis() {
XAxis xAxis = mBarChart.getXAxis();
xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
final String[] strings = {"90后", "80后", "70后", "60后", "50后"};
xAxis.setValueFormatter(new ValueFormatter() {
@Override
public String getFormattedValue(float value) {
return strings[(int) (value % 5)];
}
});
xAxis.setLabelCount(5);
xAxis.setDrawGridLines(false);
xAxis.setTextSize(15f);
xAxis.setTypeface(Typeface.DEFAULT_BOLD); // 加粗字体
}
- setYAxis
private void setYAxis() {
YAxis axisRight = mBarChart.getAxisRight();
axisRight.setEnabled(false);
YAxis axisLeft = mBarChart.getAxisLeft();
axisLeft.setAxisMinimum(0); // 最小值为0
axisLeft.setAxisMaximum(1200); // 最大值为1200
axisLeft.setValueFormatter(new ValueFormatter() {
@Override
public String getFormattedValue(float value) {
return (int) value + "";
}
});
}
- setLegend
private void setLegend() {
Legend legend = mBarChart.getLegend();
legend.setHorizontalAlignment(Legend.LegendHorizontalAlignment.RIGHT);
legend.setVerticalAlignment(Legend.LegendVerticalAlignment.TOP);
legend.setOrientation(Legend.LegendOrientation.VERTICAL);
legend.setDrawInside(true);
}
整体代码
public class MainActivity extends AppCompatActivity {
private BarChart mBarChart;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mBarChart = findViewById(R.id.bar_chart);
setXAxis();
setYAxis();
setLegend();
initData();
}
private void initData() {
mBarChart.setExtraOffsets(24f, 48f, 24f, 24f);
setChartData();
}
private void setChartData() {
final List<BarEntry> value1 = new ArrayList<>();
BarEntry[] entries = {new BarEntry(0f, 396), new BarEntry(1f, 1089),
new BarEntry(2f, 963), new BarEntry(3f, 756), new BarEntry(4f, 287)};
for (int i = 0; i < entries.length; i++) {
value1.add(entries[i]);
}
List<BarEntry> value2 = new ArrayList<>();
value2.add(new BarEntry(0f, 245));
value2.add(new BarEntry(1f, 520));
value2.add(new BarEntry(2f, 504));
value2.add(new BarEntry(3f, 517));
value2.add(new BarEntry(4f, 186));
BarDataSet set1 = new BarDataSet(value1, "有违章");
set1.setColor(Color.GREEN);
set1.setValueFormatter(new ValueFormatter() {
@Override
public String getFormattedValue(float value) {
if ((int) (value % 5) == 0) {
return new DecimalFormat("##.0").format((value / (value + 245)) * 100) + "%";
} else if ((int) (value % 5) == 1) {
return new DecimalFormat("##.0").format((value / (value + 520)) * 100) + "%";
} else if ((int) (value % 5) == 2) {
return new DecimalFormat("##.0").format((value / (value + 504)) * 100) + "%";
} else if ((int) (value % 5) == 3) {
return new DecimalFormat("##.0").format((value / (value + 517)) * 100) + "%";
} else if ((int) (value % 5) == 4) {
return new DecimalFormat("##.0").format((value / (value + 186)) * 100) + "%";
}
return "";
}
});
BarDataSet set2 = new BarDataSet(value2, "无违章");
set2.setColor(Color.BLUE);
set2.setDrawValues(false);
List<IBarDataSet> barDataSets = new ArrayList<>();
barDataSets.add(set1);
barDataSets.add(set2);
BarData barData = new BarData(barDataSets);
barData.setBarWidth(0.5f);
mBarChart.setDrawValueAboveBar(false);//柱状图上面的数值是否在柱子上面
mBarChart.setData(barData);
}
private void setXAxis() {
XAxis xAxis = mBarChart.getXAxis();
xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
final String[] strings = {"90后", "80后", "70后", "60后", "50后"};
xAxis.setValueFormatter(new ValueFormatter() {
@Override
public String getFormattedValue(float value) {
return strings[(int) (value % 5)];
}
});
xAxis.setLabelCount(5);
xAxis.setDrawGridLines(false);
xAxis.setTextSize(15f);
xAxis.setTypeface(Typeface.DEFAULT_BOLD); // 加粗字体
}
private void setYAxis() {
YAxis axisRight = mBarChart.getAxisRight();
axisRight.setEnabled(false);
YAxis axisLeft = mBarChart.getAxisLeft();
axisLeft.setAxisMinimum(0); // 最小值为0
axisLeft.setAxisMaximum(1200); // 最大值为1200
axisLeft.setValueFormatter(new ValueFormatter() {
@Override
public String getFormattedValue(float value) {
return (int) value + "";
}
});
}
private void setLegend() {
Legend legend = mBarChart.getLegend();
legend.setHorizontalAlignment(Legend.LegendHorizontalAlignment.RIGHT);
legend.setVerticalAlignment(Legend.LegendVerticalAlignment.TOP);
legend.setOrientation(Legend.LegendOrientation.VERTICAL);
legend.setDrawInside(true);
}
}
完成