效果图
传送门
https://github.com/AAChartModel/AAChartCore
参数文档
https://www.highcharts.com.cn/docs/column-and-bar-charts
安装
1. 下载Demo AAChartCoreDemo
2. 将Demo中的名为 AAChartCoreLib 和 assets 的文件夹拖入项目中
使用
1. 在xml文件中使用 AAChartView 布局
<com.example.anan.AAChartCore.AAChartCoreLib.AAChartCreator.AAChartView
android:id="@+id/chart"
android:layout_width="match_parent"
android:layout_marginHorizontal="@dimen/dp_10"
android:layout_height="wrap_content"/>
2. 在java文件中使用
AAChartView chart = getViewById(R.id.chart);
//需要先设置AAChartView的宽高才能往里面填充数据
if(item.getList().size() < 4){
//如果数据小于4,将高度固定500px
chart.getLayoutParams().height = 500;
}else {
//如果数据大于等于4,根据数据量动态设置高度
chart.getLayoutParams().height = item.getList().size() * 50 + 140;
}
chart.aa_drawChartWithChartModel(getChartModel(item));
3. 配置 ChartModel 参数
private AAChartModel getChartModel(RegionalCompareBean.TitleList item){
String[] title = new String[item.getList().size()];//x轴的显示的数据
Object[] value = new Object[item.getList().size()];//y轴的显示的数据
if(item != null){
for(int i = 0; i < item.getList().size(); i++){
title[i] = item.getList().get(i).getTitle();
value[i] = Float.parseFloat(item.getList().get(i).getItem());
}
}
AAChartModel aaChartModel = new AAChartModel()
.chartType(AAChartType.Column) //设置显示类型是柱状图
.inverted(true) //X轴Y轴互换
.dataLabelsEnabled(true) //柱状图上数据是否显示
.yAxisVisible(true) //Y轴是否显示
.tooltipEnabled(false) //点击柱状图是否提示
.xAxisReversed(true) //x轴数据是否反转
.xTickLength(10f) //x 轴刻度线长度
.xTickWidth(1f) //x 轴刻度线宽度
.xTickColor("#333333") //x 轴刻度线颜色
.xLineColor("#333333") //x 轴线颜色
.yAxisLineWidth(1f) //y 轴轴线的宽度
.yRotation(-30f) //y 轴标签旋转角度
.yAxisReversed(false) //y 轴是否翻转
.yAxisMin(0f)
.yTickLength(10f) //y 轴刻度线长度
.yTickWidth(1f) //y 轴刻度线宽度
.yTickAmount(6) //y 轴刻度线总数
.yTickColor("#333333") //y 轴刻度线颜色
.yLineColor("#333333") //y 轴线颜色
.yAxisGridLineWidth(0.5f) //y 轴网格线的宽度
.yAxisAllowDecimals(true) //y 轴是否允许显示小数
.legendEnabled(false) //是否显示图例
.axesTextColor("#333333") //x 轴和 y 轴文字颜色
.colorsTheme(new String[]{"#1a82f5"}) //图表主题颜色数组,多个颜色值按顺序显示
.yAxisTitle("") //y 轴标题
.title("") //主标题内容
.subtitle("") //副标题内容
.categories(title) //x轴上显示的数据
.series(new AASeriesElement[]{
new AASeriesElement()
.data(value) //y 轴上显示的数据
.colorByPoint(true) //y 轴上是否根据colorsTheme显示多个不同颜色
});
return aaChartModel;
}
下面是我根据github上面下载并根据自己需求修改的包