80,351
社区成员
发帖
与我相关
我的任务
分享
package com.example.a12438.mytubiao;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import java.util.ArrayList;
import java.util.List;
import lecho.lib.hellocharts.gesture.ZoomType;
import lecho.lib.hellocharts.listener.ColumnChartOnValueSelectListener;
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.util.ChartUtils;
import lecho.lib.hellocharts.view.ColumnChartView;
public class Zhuzhuangtu extends AppCompatActivity {
//声明所需变量
public final static String[] months = new String[] { "Jan", "Feb", "Mar",
"Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec", };
private Button fanhui2;
ColumnChartView columnChart;
ColumnChartData columnData;
List<Column> lsColumn = new ArrayList<Column>();
List<SubcolumnValue> lsValue;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.zhuzhuang);
columnChart=findViewById(R.id.columnChart);
fanhui2=findViewById(R.id.fanhui2);
dataInit();
fanhui2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent=new Intent(Zhuzhuangtu.this, MainActivity.class);
startActivity(intent);
}
});
}
//初始化数据并显示在图表上
private void dataInit() {
int numSubcolumns = 1;
int numColumns = months.length;
List<AxisValue> axisValues = new ArrayList<AxisValue>();
List<Column> columns = new ArrayList<Column>();
List<SubcolumnValue> values;
for (int i = 0; i < numColumns; ++i) {
values = new ArrayList<SubcolumnValue>();
for (int j = 0; j < numSubcolumns; ++j) {
values.add(new SubcolumnValue((float) Math.random() * 50f + 5,
ChartUtils.pickColor()));
}
// 点击柱状图就展示数据量
axisValues.add(new AxisValue(i).setLabel(months[i]));
columns.add(new Column(values).setHasLabelsOnlyForSelected(true));
}
columnData = new ColumnChartData(columns);
columnData.setAxisXBottom(new Axis(axisValues).setHasLines(true)
.setTextColor(Color.BLACK));
columnData.setAxisYLeft(new Axis().setHasLines(true)
.setTextColor(Color.BLACK).setMaxLabelChars(2));
columnChart.setColumnChartData(columnData);
// Set value touch listener that will trigger changes for chartTop.
columnChart.setOnValueTouchListener((ColumnChartOnValueSelectListener) new ValueTouchListener());
// Set selection mode to keep selected month column highlighted.
columnChart.setValueSelectionEnabled(true);
columnChart.setZoomType(ZoomType.HORIZONTAL);
}
/**
* 柱状图监听器
*
* @author 1017
*
*/
private class ValueTouchListener implements
ColumnChartOnValueSelectListener {
@Override
public void onValueSelected(int columnIndex, int subcolumnIndex,
SubcolumnValue value) {
// generateLineData(value.getColor(), 100);
}
@Override
public void onValueDeselected() {
// generateLineData(ChartUtils.COLOR_GREEN, 0);
}
}
}
layout布局:
<?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:orientation="vertical"
android:scrollbars="none">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<lecho.lib.hellocharts.view.ColumnChartView
android:id="@+id/columnChart"
android:layout_width="match_parent"
android:layout_height="400dp"
/>
<Button
android:id="@+id/fanhui2"
android:layout_width="100dp"
android:layout_height="50dp"
android:layout_gravity="center"
android:text="返回"/>
</LinearLayout>
</LinearLayout>