package com.main.designtest;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import java.util.ArrayList;
import java.util.List;
import lecho.lib.hellocharts.gesture.ZoomType;
import lecho.lib.hellocharts.model.Axis;
import lecho.lib.hellocharts.model.AxisValue;
import lecho.lib.hellocharts.model.Line;
import lecho.lib.hellocharts.model.LineChartData;
import lecho.lib.hellocharts.model.PointValue;
import lecho.lib.hellocharts.model.ValueShape;
import lecho.lib.hellocharts.view.LineChartView;
/**
* Created by 明月清泉 on 2016/9/8.
* 图表
*/
public class ChartFragment extends Fragment{
private View fragRootView;
private LineChartView lineChartView;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
fragRootView=inflater.inflate(R.layout.fragment_chart,null);
return fragRootView;
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
lineChartView= (LineChartView) fragRootView.findViewById(R.id.lineChart);
initLineChart();
super.onActivityCreated(savedInstanceState);
}
private void initLineChart(){
//橫、纵坐标值
List values=new ArrayList<>();
values.add(new PointValue(0,25));
values.add(new PointValue(1,27));
values.add(new PointValue(2,32));
values.add(new PointValue(3,23));
values.add(new PointValue(4,38));
//橫轴值集合
List axisXs=new ArrayList<>();
for(int i=0;i<6;i++){
//集合添加值
axisXs.add(new AxisValue(i).setLabel("kk"+i));
}
//线
Line line = new Line(values)
.setColor(Color.BLUE)//线颜色
.setCubic(true)//曲线是否平滑
.setStrokeWidth(1)//线粗细
.setPointRadius(2)//坐标点大小
.setHasLabels(true)//是否显示坐标文本备注(纵坐标数值)
//.setHasLabelsOnlyForSelected(true)//点击数据坐标提示数值(设置了这个line.setHasLabels(true);就无效)
.setHasLines(true)//是否有线(默认true)
.setHasPoints(true)//是否有点(默认true)
.setShape(ValueShape.CIRCLE)//点形状ValueShape.CIRCLE(圆形)、ValueShape.DIAMOND(棱形)、ValueShape.SQUARE(正方形)《默认圆形》
.setFilled(true);//是否填充曲线的面积(默认为false)
//线集合
List lines = new ArrayList<>();
//线集合添加线(添加几条线,一张表中就有几条)
lines.add(line);
//橫轴
Axis axisX=new Axis()
.setHasLines(true)//是否显示轴网格线
.setName("时间")//轴名称
.setTextColor(Color.RED)//坐标轴文字颜色
.setTextSize(14)//坐标轴文字大小
.setLineColor(Color.GRAY)//线颜色(横轴为网格竖线,纵轴为网格横线)
.setHasTiltedLabels(true)//坐标轴文字是否倾斜(默认为false,不倾斜)
.setInside(false)//坐标值文字在图标内部还是在轴下面(默认为flase,在轴下面)
.setMaxLabelChars(3)//最大间隔
.setValues(axisXs);//轴数值
//纵轴
Axis axisY=new Axis()
.setHasLines(true)
.setName("温度(℃)");
//线图表数据
LineChartData data = new LineChartData();
//设置坐标点旁边的文字背景
data.setValueLabelBackgroundColor(Color.parseColor("#1ca0aa"));
//设置坐标点旁文字背景此方法必须设置为false
data.setValueLabelBackgroundAuto(false);
//设置坐标点旁文字背景是否可见(在line.setHasLines为true的情况下,默认为true)
data.setValueLabelBackgroundEnabled(true);
//设置坐标点旁边的文字颜色
data.setValueLabelsTextColor(Color.BLACK);
//设置坐标点旁边的文字大小
data.setValueLabelTextSize(10);
//设置左侧轴
data.setAxisYLeft(axisY);
//设置底部轴
data.setAxisXBottom(axisX);
//线图表数据设置线集合
data.setLines(lines);
//视图设置图表
lineChartView.setLineChartData(data);
//是否可以拉伸,默认为true
lineChartView.setInteractive(true);
//是否可以放大,默认为true(设置为true的前提是setInteractive为true)
lineChartView.setZoomEnabled(true);
//设置放大类型
lineChartView.setZoomType(ZoomType.HORIZONTAL);
//点值是否可以点击
lineChartView.setValueTouchEnabled(false);
}
}