效果如图所示:
1.首先libs添加:
hellocharts-library-1.5.8.jar
给出地址:http://download.csdn.net/detail/u012534831/9531494
2.xml布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.lotus.columnchartdemo.MainActivity">
<TextView
android:id="@+id/tv_result"
android:gravity="right"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:textSize="16sp"
android:textColor="@color/colorPrimary"/>
<lecho.lib.hellocharts.view.ColumnChartView
android:layout_width="368dp"
android:layout_height="300dp"
android:id="@+id/columnchart"
tools:layout_editor_absoluteY="0dp"
tools:layout_editor_absoluteX="8dp" />
</LinearLayout>
3.activity中直接使用:
public class MainActivity extends AppCompatActivity implements ColumnChartOnValueSelectListener {
private ColumnChartView columnchart;
private TextView tv_result;
private ArrayList<String> arrayListX = new ArrayList<>();
private ArrayList<Float> arrayListY = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
columnchart = (ColumnChartView) this.findViewById(R.id.columnchart);
tv_result = (TextView) this.findViewById(R.id.tv_result);
for( int i = 10; i < 20; i++){
arrayListX.add( String.valueOf( i ) );
}
for( float i = 5.0f; i < 15.0f; i++){
arrayListY.add( i );
}
//设置表属性
columnchart.setZoomEnabled(true);
columnchart.setOnValueTouchListener(this);
columnchart.setInteractive(true);
columnchart.setZoomType(ZoomType.HORIZONTAL);
//设置x,y轴
Axis aY = new Axis();
Axis aX = new Axis();
aY.setLineColor(getResources().getColor(R.color.colorblack));//设置后并看不出效果...
aY.setTextSize(12);
aY.setTypeface(Typeface.DEFAULT);// 设置文字样式,此处为默认
aY.setTextColor(getResources().getColor(R.color.colorRed));//Y轴的文字颜色,Y轴的竖线颜色是文字的颜色变淡一点
aX.setLineColor(getResources().getColor(R.color.colorBlue1));//设置后并看不出效果...
aX.setTextSize(12);
aX.setTypeface(Typeface.DEFAULT);// 设置文字样式,此处为默认
aX.setTextColor(getResources().getColor(R.color.colorPurple));//X轴的文字颜色,X轴的竖线颜色是文字的颜色变淡一点
//给存储树状图数据的数组赋值
ArrayList<AxisValue> aXValues = new ArrayList<>();
for (int i = 0; i < arrayListX.size(); i++) {
aXValues.add(new AxisValue(i).setLabel( arrayListX.get(i) ));
}
aX.setValues(aXValues);//给X轴设置数据
//设置column属性
ArrayList<Column> cs = new ArrayList<>();
ArrayList<SubcolumnValue> subValues;
for (int i = 0; i < arrayListY.size(); i++) {
subValues = new ArrayList<>();
//树状图的矩形颜色(自动设置:矩形上面标识的文字为白色,底色背景为矩形颜色加深一点)
subValues.add(new SubcolumnValue( arrayListY.get(i), getResources().getColor(R.color.colorGreen)));
Column column = new Column(subValues);//给Y轴设置数据
column.setHasLabels(true);
// column.setHasLabelsOnlyForSelected(true);//点击此矩形后才显示它的标签
cs.add(column);
}
ColumnChartData data = new ColumnChartData(cs);
data.setAxisXBottom(aX);
data.setAxisYLeft(aY);
columnchart.setColumnChartData(data);
}
//柱状图的矩形的点击事件: i 为从X轴第一个开始数的第几个矩形
@Override
public void onValueSelected(int i, int i1, SubcolumnValue subcolumnValue) {
tv_result.setText( subcolumnValue.getValue() + "卡" + Integer.toString( i ));
}
@Override
public void onValueDeselected() {
}
}
注:
1.发现双击柱状图中的矩形会放大
2.之前要添加依赖
//引入hellocharts图表插件
compile 'com.github.lecho:hellocharts-android:v1.5.8'
才行,但这次并不需要,反而添加依赖后反而报错,结果是添加jar包后就可以直接使用了,不太懂。。。