第一步添加依赖
根据GItHub上项目的提示,在project的build.gradle中的allprojects代码块中添加如下代码
repositories {
maven { url 'https://jitpack.io' }
}
在app的build.gradle中添加如下代码
dependencies {
implementation 'com.github.PhilJay:MPAndroidChart:v3.1.0'
}
第二步添加Layout布局
柱形图
在需要柱形图的地方添加BarChart
<com.github.mikephil.charting.charts.BarChart
android:id="@+id/bar_chart"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
在Activity或者Fragment中设置柱形图样式
barChart = findViewById(R.id.bar_chart);
barChart.setOnChartValueSelectedListener(this);//传入this,让当前Activity实现监听回调接口
barChart.setDrawBarShadow(false);//设置柱形图阴影
barChart.setDrawValueAboveBar(ture);//设置值是否在矩形上方显示
barChart.getDescription().setEnabled(false);//取消左下角描述,可用barChart.getDescription().setText("yuqingsen")设置显示内容
barChart.setMaxVisibleValueCount(60);//如果图表的条目超过60则不显示柱形图上的值
barChart.setPinchZoom(true);//true表示图表可以在任意方向双指放大缩小,false表示只能根据XY两个方向分别放大缩小
barChart.setDrawGridBackground(false);//是否显示图表的背景色
XAxis xAxis = barChart.getXAxis();//获取代表X轴标签的对象
YAxis leftAxis = chart.getAxisLeft();//获取代表左边Y轴的标签的对象
YAxis rightAxis = chart.getAxisRight();//获取代表右边Y轴的标签的对象
//坐标轴样式设定
ValueFormatter xAxisFormatter = new DayAxisValueFormatter(barChart);//X轴月份样式
xAxis.setPosition(XAxisPosition.BOTTOM);//设置X轴标签显示位置
xAxis.setTypeface(tfLight);
xAxis.setDrawGridLines(false);//不绘制网格线
xAxis.setGranularity(1f); // 设置最小间隔,防止当放大时,出现重复标签
xAxis.setLabelCount(8);//设置显示的标签个数,Y轴的这个方法需要多传一个Boolean参数,默认都传false
xAxis.setValueFormatter(xAxisFormatter);
//省略一些重复的方法
leftAxis.setSpaceTop(15f);//设置柱子离图表顶部最小距离
leftAxis.setAxisMinimum(0f); //为这个轴设置一个自定义的最小值。如果设置,这个值不会自动根据所提供的数据计算。
设置示例图
Legend l = chart.getLegend();
l.setVerticalAlignment(Legend.LegendVerticalAlignment.BOTTOM);//位于顶部还是顶部
l.setHorizontalAlignment(Legend.LegendHorizontalAlignment.LEFT);//位于左边还是右边
l.setOrientation(Legend.LegendOrientation.HORIZONTAL);//跟图表的排列关系
l.setDrawInside(false);//是否设置到图表里面
l.setForm(LegendForm.CIRCLE);//设置形状
l.setFormSize(9f);//大小
l.setTextSize(11f);//示例的字体大小
l.setXEntrySpace(4f);
设置数据(需要再看一下)
f (barChart.getData() != null &&
barChart.getData().getDataSetCount() > 0) {
set1 = (BarDataSet) barChart.getData().getDataSetByIndex(0);
set1.setValues(values);
barChart.getData().notifyDataChanged();
barChart.notifyDataSetChanged();
} else {
set1 = new BarDataSet(values, "The year 2017");
set1.setDrawIcons(false);
int startColor1 = ContextCompat.getColor(this, android.R.color.holo_orange_light);
int startColor2 = ContextCompat.getColor(this, android.R.color.holo_blue_light);
int startColor3 = ContextCompat.getColor(this, android.R.color.holo_orange_light);
int startColor4 = ContextCompat.getColor(this, android.R.color.holo_green_light);
int startColor5 = ContextCompat.getColor(this, android.R.color.holo_red_light);
int endColor1 = ContextCompat.getColor(this, android.R.color.holo_blue_dark);
int endColor2 = ContextCompat.getColor(this, android.R.color.holo_purple);
int endColor3 = ContextCompat.getColor(this, android.R.color.holo_green_dark);
int endColor4 = ContextCompat.getColor(this, android.R.color.holo_red_dark);
int endColor5 = ContextCompat.getColor(this, android.R.color.holo_orange_dark);
List<GradientColor> gradientColors = new ArrayList<>();
gradientColors.add(new GradientColor(startColor1, endColor1));
gradientColors.add(new GradientColor(startColor2, endColor2));
gradientColors.add(new GradientColor(startColor3, endColor3));
gradientColors.add(new GradientColor(startColor4, endColor4));
gradientColors.add(new GradientColor(startColor5, endColor5));
set1.setGradientColors(gradientColors);
ArrayList<IBarDataSet> dataSets = new ArrayList<>();
dataSets.add(set1);
BarData data = new BarData(dataSets);
data.setValueTextSize(10f);
data.setValueTypeface(tfLight);
data.setBarWidth(0.9f);
barChart.setData(data);
}