赞
踩
现在项目中又用到了柱状图来统计最近一周的设备操作信息数量,还是边学习边记录个DEMO。
public class ColumnChartActivity extends AppCompatActivity {
//柱状图控件
private ColumnChartView column_chart_view;
//统计图数据
private ColumnChartData data;
//数据标志
private List<String> week;
//模拟数据
private List<Float> testData;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_column_chart);
column_chart_view = (ColumnChartView) findViewById(R.id.column_chart_view);
testData = new ArrayList<>();
for (int i = 0; i < 7; i++) {
testData.add(i * 2 + 5f);
}
setHistoryChart(testData, Color.parseColor("#F1C704"), column_chart_view);
}
/**
* @param columnDatas :每一条柱子上代表的数量,columnColor:柱子颜色,charView:柱形图控件
* @description 历史记录柱形图数据填充
* @author ldm
* @time 2017/5/23 11:03
*/
private void setHistoryChart(List<Float> columnDatas, int columnColor, ColumnChartView charView) {
week = new ArrayList<>();
for (int i = 6; i >= 0; i--) {
week.add(getWeekDays(i));
}
// 使用的 7列,每列1个subcolumn。
int numSubcolumns = 1;
int numColumns = 7;
//定义一个圆柱对象集合
List<Column> columns = new ArrayList<Column>();
//子列数据集合
List<SubcolumnValue> values;
List<AxisValue> axisValues = new ArrayList<AxisValue>();
//遍历列数numColumns
for (int i = 0; i < numColumns; ++i) {
values = new ArrayList<SubcolumnValue>();
//遍历每一列的每一个子列
for (int j = 0; j < numSubcolumns; ++j) {
//为每一柱图添加颜色和数值
float f = columnDatas.get(i);
values.add(new SubcolumnValue(f, columnColor));
}
//创建Column对象
Column column = new Column(values);
ColumnChartValueFormatter chartValueFormatter = new SimpleColumnChartValueFormatter(0);
column.setFormatter(chartValueFormatter);
//是否有数据标注
column.setHasLabels(true);
//是否是点击圆柱才显示数据标注
column.setHasLabelsOnlyForSelected(false);
columns.add(column);
//给x轴坐标设置描述
axisValues.add(new AxisValue(i).setLabel(week.get(i)));
}
//创建一个带有之前圆柱对象column集合的ColumnChartData
data = new ColumnChartData(columns);
data.setValueLabelTextSize(8);
data.setValueLabelBackgroundColor(Color.parseColor("#00000000"));
// data.setValueLabelTypeface(Typeface.DEFAULT);// 设置数据文字样式
data.setValueLabelBackgroundEnabled(true);
data.setValueLabelBackgroundAuto(false);
//定义x轴y轴相应参数
Axis axisX = new Axis();
Axis axisY = new Axis().setHasLines(true);
axisY.setName("数量");//轴名称
axisY.hasLines();//是否显示网格线
axisY.setTextColor(Color.parseColor("#ffffff"));//颜色
axisX.hasLines();
axisX.setTextColor(Color.parseColor("#C9C9C9"));
axisX.setValues(axisValues);
axisX.setTextSize(10);
axisX.setHasSeparationLine(false);
//把X轴Y轴数据设置到ColumnChartData 对象中
data.setAxisXBottom(axisX);
data.setAxisYLeft(axisY);
//给表填充数据,显示出来
charView.setInteractive(false);
charView.setColumnChartData(data);
}
/**
* @description 获取最近七天的星期
* @author ldm
* @time 2017/5/23 10:30
*/
private String getWeekDays(int days) {
if (days == 0) {
return getString(R.string.today);
}
String[] weekDays = getResources().getStringArray(R.array.cms_week);
Calendar cal = Calendar.getInstance();
cal.add(Calendar.DATE, -days);
Date monday = cal.getTime();
cal.setTime(monday);
int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
if (w < 0)
w = 0;
return weekDays[w];
}
}
布局文件:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="300dp"
android:padding="10dp"
android:layout_marginTop="150dp"
android:background="#5e677f"
>
<TextView
android:id="@+id/column_title"
android:layout_width="72dp"
android:layout_alignParentLeft="true"
android:layout_centerInParent="true"
android:layout_height="wrap_content"
android:text="柱状统计图示例"
android:textSize="15sp"
android:textColor="#FFFFFF"
/>
<lecho.lib.hellocharts.view.ColumnChartView
android:id="@+id/column_chart_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_toRightOf="@id/column_title"
android:layout_margin="8dp"
></lecho.lib.hellocharts.view.ColumnChartView>
</RelativeLayout>
res/values/strings.xml:
<resources>
<string name="monday">星期一</string>
<string name="tuesday">星期二</string>
<string name="wednesday">星期三</string>
<string name="thursday">星期四</string>
<string name="friday">星期五</string>
<string name="saturday">星期六</string>
<string name="sunday">星期日</string>
<string name="today">今天</string>
</resources>
res/values/arrays.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="cms_week">
<item>@string/sunday</item>
<item>@string/monday</item>
<item>@string/tuesday</item>
<item>@string/wednesday</item>
<item>@string/thursday</item>
<item>@string/friday</item>
<item>@string/saturday</item>
</string-array>
</resources>
效果图:
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。