项目需要用到数据曲线,而且曲线是实时变化的,所以选择了第三方图表库Charts。选择Charts的最主要原因还是它是作者按照Android的常用曲线库MPAndroidChart写的,功能UI基本相近。
曲线的基本用法很简单,在GitHub上可以下载到demo,有各种示例,K线图、折线图和饼状图等都有。以下主要讲我自己碰到的特定问题和解决方案。
-
自定义的坐标文字和间隔
项目曲线是根据时间不停变化的,而坐标轴是一个点一个数据,也就是对应的一秒一个数据。但是项目需求坐标单位是分钟,所以每个坐标间就有60个数据。
ChartXAxis有两个属性:axisMaximum、labelCount
/// The maximum value for this axis.
/// If set, this value will not be calculated automatically depending on the provided data.
/// Use `resetCustomAxisMax()` to undo this.
/// the number of label entries the axis should have
/// max = 25,
/// min = 2,
/// default = 6,
/// be aware that this number is not fixed and can only be approximated
按照注释的意思,我只需要设置一个最大数值,然后再设置坐标显示个数,比如我设置