写点什么

Android 开发中如何实现折线图的方法 (1)

发布于: 2021 年 11 月 07 日

折线图布局部分:

折线图的布局是很简单的:


<lecho.lib.hellocharts.view.LineChartView


android:id="@+id/line_chart"


android:padding="25dp"


android:layout_width="fill_parent"


android:layout_height="fill_parent"


a


《Android学习笔记总结+最新移动架构视频+大厂安卓面试真题+项目实战源码讲义》
浏览器打开:qq.cn.hn/FTe 免费领取
复制代码


ndroid:background="#ffffff"/>


<ListView


android:visibility="gone"


android:divider="@null"


android:scrollbars="none"


android:layout_marginTop="15dp"


android:id="@+id/listview"


android:layout_width="match_parent"


android:layout_height="match_parent"></ListView>


</LinearLayout>

折线图部分对应 java 代码

public class ListActivity extends Activity {


private ListView listview;


private List<StepEntity> stepEntityList = new ArrayList<>();


private StepDataDao stepDataDao;


private LineChartView lineChart;


private List<StepEntity> list;


String[] date ;


float [] score;


private List<PointValue> mPointValues = new ArrayList<PointValue>();


private List<AxisValue> mAxisXValues = new ArrayList<AxisValue>();


@Override


protected void onCreate(Bundle savedInstanceState) {


super.onCreate(savedInstanceState);


setContentView(R.layout.activity_list);


stepDataDao = new StepDataDao(this);


initView();


list = stepDataDao.getAllDatas( );


date=new String[list.size()];


score=new float[list.size()];


for (int i=0;i<list.size();i++){


date[i]=list.get(i).getCurDate();


score[i]=Float.parseFloat(list.get(i).getSteps());


}


getAxisXLables();


getAxisPoints();


initLineChart();


}


private void initLineChart(){


Line line = new Line(mPointValues).setColor(Color.parseColor("#FFCD41"));


List<Line> lines = new ArrayList<Line>();


line.setShape(ValueShape.CIRCLE); //折线图上每个数据点的形状,这里是圆形


line.setCubic(false);


line.setFilled(false);


line.setHasLabels(true);


line.setHasLines(true);


line.setHasPoints(true);


lines.add(line);


LineChartData data = new LineChartData();


data.setLines(lines);


//坐标轴


Axis axisX = new Axis();


axisX.setHasTiltedLabels(true);


axisX.setTextColor(Color.parseColor("#D6D6D9"));//设置字体颜色


axisX.setTextSize(8);//设置字体大小


axisX.setMaxLabelChars(8);//最多几个 X 轴坐标


axisX.setValues(mAxisXValues);


data.setAxisXBottom(axisX);


axisX.setHasLines(true);


Axis axisY = new Axis();


axisY.setName("");


axisY.setTextSize(8);


data.setAxisYLeft(axisY);


//设置行为属性,缩放、滑动、平移


lineChart.setInteractive(true);


lineChart.setZoomType(ZoomType.HORIZONTAL);


lineChart.setMaxZoom((float) 3);


lineChart.setLineChartData(data);


lineChart.setVisibility(View.VISIBLE);


//设置 X 轴数据的显示个数(x 轴 0-7 个数据)


Viewport v = new Viewport(lineChart.getMaximumViewport());


v.left = 0;


v.right= 7;


lineChart.setCurrentViewport(v);


}


private void initView() {


listview=(ListView) findViewById(R.id.listview);


lineChart = (LineChartView)findViewById(R.id.line_chart);


}


private void getAxisXLables(){


for (int i = 0; i < date.length; i++) {


mAxisXValues.add(new AxisValue(i).setLabel(date[i]));


}


}


private void getAxisPoints(){


for (int i = 0; i < score.length; i++) {


mPointValues.add(new PointValue(i, score[i]));

评论

发布
暂无评论
Android开发中如何实现折线图的方法 (1)