版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_34536167/article/details/78751966
效果图:
1、上一篇已经介绍怎么引入hellocharts了,这里就不重复了,直接上代码
1.1、还是一个工具类:Tools (关键代码)
import android.graphics.Color;
import java.util.ArrayList;
import java.util.List;
import lecho.lib.hellocharts.model.Axis;
import lecho.lib.hellocharts.model.AxisValue;
import lecho.lib.hellocharts.model.Column;
import lecho.lib.hellocharts.model.ColumnChartData;
import lecho.lib.hellocharts.model.SubcolumnValue;
import lecho.lib.hellocharts.view.ColumnChartView;
public class Tools {
public static void setChartViewData(ColumnChartView chart) {
//底部标题
List<String> title = new ArrayList<>();
//颜色值
List<Integer> color = new ArrayList<>();
//X、Y轴值list
List<AxisValue> axisXValues = new ArrayList<>();
List<SubcolumnValue> mPointValues = new ArrayList<>();
//所有的柱子
List<Column> columns = new ArrayList<>();
//单个柱子
Column column = new Column();
title.add("星期一");
title.add("星期二");
title.add("星期三");
title.add("星期四");
title.add("星期五");
title.add("星期六");
title.add("星期日");
//颜色值
color.add(Color.parseColor("#009BDB"));
color.add(Color.parseColor("#85B74F"));
color.add(Color.parseColor("#ff0000"));
//显示几个小柱子 这里为3
for (int i = 0; i < 3; i++) {
//值的大小、颜色
mPointValues.add(new SubcolumnValue((float) (100 * Math.random()), color.get(i)));
}
//对每个集合的柱子进行遍历
for (int i = 0; i < title.size(); i++) {
//设置X轴的柱子所对应的属性名称(底部文字)
axisXValues.add(new AxisValue(i).setLabel(title.get(i)));
//将每个属性得列全部添加到List中
//添加了7个大柱子Column,单个大柱子里面mPointValues大小为3(自行调整)
columns.add(column);
}
//是否显示每个柱子的标签
column.setHasLabels(true);
//设置每个柱子的Lable是否选中,为false,表示不用选中,一直显示在柱子上
column.setHasLabelsOnlyForSelected(false);
//设置Columns添加到Data中
ColumnChartData columnData = new ColumnChartData(columns);
column.setValues(mPointValues);
//底部
Axis axisBottom = new Axis(axisXValues);
//是否显示X轴的网格线
axisBottom.setHasLines(false);
//分割线颜色
axisBottom.setLineColor(Color.parseColor("#ff0000"));
//字体颜色
axisBottom.setTextColor(Color.parseColor("#666666"));
//字体大小
axisBottom.setTextSize(10);
//底部文字
axisBottom.setName("底部标题");
//每个柱子的便签是否倾斜着显示
axisBottom.setHasTiltedLabels(true);
//距离各标签之间的距离,包括离Y轴间距 (0-32之间)
axisBottom.setMaxLabelChars(10);
//设置是否自动生成轴对象,自动适应表格的范围(设置之后底部标题变成0-5)
//axisBottom.setAutoGenerated(true);
axisBottom.setHasSeparationLine(true);
//设置x轴在底部显示
columnData.setAxisXBottom(axisBottom);
//左边 属性与上面一致
Axis axisLeft = new Axis();
axisLeft.setHasLines(false);
axisLeft.setName("左边标题");
axisLeft.setHasTiltedLabels(true);
axisLeft.setTextColor(Color.parseColor("#666666"));
columnData.setAxisYLeft(axisLeft);
//设置数据标签的字体大小
//data.setValueLabelTextSize(12);
//设置数据标签的字体颜色
//data.setValueLabelsTextColor(Color.WHITE);
//设置数据背景是否跟随节点颜色
//data.setValueLabelBackgroundAuto(true);
//设置是否有数据背景 是否跟随columvalue的颜色变化
// data.setValueLabelBackgroundEnabled(true);
//设置坐标点旁边的文字背景(...)
//data.setValueLabelBackgroundColor(Color.YELLOW);
//axisBottom.setMaxLabelChars(5);
//设置组与组之间的间隔比率,取值范围0-1,1表示组与组之间不留任何间隔
columnData.setFillRatio(0.7f);
chart.setInteractive(false);
//最后将所有值显示在View中
chart.setColumnChartData(columnData);
}
}
1.2、MainActivity 主布局
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import lecho.lib.hellocharts.view.ColumnChartView;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ColumnChartView chart = (ColumnChartView) this.findViewById(R.id.chart);
Tools.setChartViewData(chart);
}
/**
* 强制设置为横屏(这里只是为了更好的展示数据可删除)
*/
@Override
protected void onResume() {
if (getRequestedOrientation() != ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE) {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}
super.onResume();
}
}
1.3、 activity_main.xml 布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#fff"
android:orientation="vertical">
<lecho.lib.hellocharts.view.ColumnChartView
android:id="@+id/chart"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="30dp" />
</LinearLayout>