我正在使用Swift Charts API绘制使用iOS加速度计拍摄的数据。我只需要在y轴上绘制加速度值,在x轴上绘制时间值。
我设法通过提供x和y值绘制折线图:
lineChartView.delegate = self
lineChartView.descriptionText = "Acceleration/Time"
lineChartView.descriptionTextColor = UIColor.blackColor()
lineChartView.gridBackgroundColor = UIColor.greenColor()
let allData = ActivityData.getByActivitiesId(activity.id)
var xVals = [String]()
var yVals = [ChartDataEntry]()
let formatter = NSNumberFormatter()
formatter.numberStyle = .DecimalStyle
formatter.maximumFractionDigits = 2
formatter.maximumIntegerDigits = 3
var i = 0;
for data in allData
{
let entry = ChartDataEntry(value: data.xUserAcc, xIndex: i)
yVals.append(entry)
xVals.append(formatter.stringFromNumber(NSNumber(double:data.time))!)
print(data.time)
i += 1
}
let set1 = LineChartDataSet(yVals: yVals, label: "xAcc")
set1.axisDependency = .Left
set1.setColor(UIColor.redColor())
set1.setCircleColor(UIColor.redColor()) // our circle will be dark red
set1.lineWidth = 2.0
set1.circleRadius = 6.0 // the radius of the node circle
set1.fillAlpha = 65 / 255.0
set1.fillColor = UIColor.redColor()
set1.highlightColor = UIColor.whiteColor()
set1.drawCircleHoleEnabled = true
let lineChartData = LineChartData(xVals: xVals, dataSets: [set1])
lineChartData.setValueTextColor(UIColor.blackColor())
lineChartView.data = lineChartData
但问题是x轴值仅用作在x轴上显示的标签,但间隔不是基于我提供的实际x值。让我们用图片更好地解释一下:
就像你看到x上的值是3.89,3.93和3.94。并且它们在x轴上以相等的距离显示,即使在3.89和3.93之间也存在0.04的间隙,这是第二和第三值之间的间隙的4倍。当我使用加速度计获取数据时,我无法以恒定速率获取数据,因此我需要为LineChartView指定x值,但到目前为止我找不到方法。