Android统计图表绘制:基于新版MPAndroidChart绘制数理统计柱状图
github上的开源项目MPAndroidChart在最新的3.0.3中,绘制图表的机制有所改变,从数据到图形,需要以新的方式绘制。现在以3.0.3为例,绘制一个简单的柱状图。
使用之前要添加引用:
implementation 'com.github.PhilJay:MPAndroidChart:v3.0.3'
写一个布局:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <com.github.mikephil.charting.charts.BarChart android:id="@+id/bar_chart" android:layout_width="match_parent" android:layout_height="300dp" android:layout_centerInParent="true" /> </RelativeLayout>
然后在上层Java代码处理:
package zhangphil.demo; import android.graphics.Color; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import com.github.mikephil.charting.animation.Easing; import com.github.mikephil.charting.charts.BarChart; import com.github.mikephil.charting.components.AxisBase; import com.github.mikephil.charting.components.Legend; import com.github.mikephil.charting.components.XAxis; import com.github.mikephil.charting.components.YAxis; import com.github.mikephil.charting.data.BarData; import com.github.mikephil.charting.data.BarDataSet; import com.github.mikephil.charting.data.BarEntry; import com.github.mikephil.charting.data.Entry; import com.github.mikephil.charting.formatter.IAxisValueFormatter; import com.github.mikephil.charting.formatter.IValueFormatter; import com.github.mikephil.charting.interfaces.datasets.IBarDataSet; import com.github.mikephil.charting.utils.ViewPortHandler; import java.util.ArrayList; public class MainActivity extends AppCompatActivity { private String[] types = {"银行", "非银金融", "建筑材料", "化工", "医药生物", "电子"}; private float[] changes = {27.91f, 5.9f, -0.4f, -17.79f, -21.85f, -39.58f}; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); BarChart mChart = findViewById(R.id.bar_chart); initBarChart(mChart); setBarChartData(types.length, mChart); } private void initBarChart(BarChart mBarChart) { mBarChart.setBackgroundColor(Color.WHITE); mBarChart.setDrawGridBackground(false); //网格 mBarChart.getDescription().setEnabled(false);//描述 //背景阴影 mBarChart.setDrawBarShadow(false); //显示边界 mBarChart.setDrawBorders(false); //设置动画效果 mBarChart.animateY(1000, Easing.EasingOption.Linear); mBarChart.animateX(1000, Easing.EasingOption.Linear); //折线图例 标签 设置 Legend l = mBarChart.getLegend(); l.setEnabled(false); YAxis leftAxis = mBarChart.getAxisLeft(); YAxis rightAxis = mBarChart.getAxisRight(); leftAxis.setAxisMinimum(0f); rightAxis.setAxisMinimum(0f); leftAxis.setEnabled(false); rightAxis.setEnabled(false); XAxis xAxis = mBarChart.getXAxis(); //XY轴的设置 //X轴设置显示位置在底部 xAxis.setPosition(XAxis.XAxisPosition.BOTTOM); //xAxis.setGranularity(1f); //xAxis.setDrawAxisLine(true); xAxis.setDrawGridLines(false); xAxis.setTextColor(0xff74828F); xAxis.setTextSize(10f); xAxis.setAxisLineColor(0xffe0e0e0); xAxis.setValueFormatter(new IAxisValueFormatter() { @Override public String getFormattedValue(float value, AxisBase axis) { int idx = (int) value; return types[idx]; } }); } private void setBarChartData(int count, BarChart mChart) { ArrayList<BarEntry> yVals = new ArrayList<>(); int[] colors = new int[count]; for (int i = 0; i < count; i++) { float val = changes[i]; if (val > 0) { colors[i] = 0xffF04933; } if (val < 0) { colors[i] = 0xff2BBE53; } yVals.add(new BarEntry(i, Math.abs(val))); } BarDataSet mBarDataSet = new BarDataSet(yVals, "股票数据"); mBarDataSet.setDrawIcons(false); mBarDataSet.setColors(colors); mBarDataSet.setValueTextSize(12f); mBarDataSet.setValueTextColor(0xff74828F); ArrayList<IBarDataSet> dataSets = new ArrayList<>(); dataSets.add(mBarDataSet); BarData mBarData = new BarData(dataSets); mBarData.setBarWidth(0.6f); mBarData.setValueFormatter(new IValueFormatter() { @Override public String getFormattedValue(float value, Entry entry, int dataSetIndex, ViewPortHandler viewPortHandler) { int idx = (int) entry.getX(); return String.valueOf(changes[idx]); } }); mChart.setData(mBarData); } }
代码运行后的结果如图所示: