作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个采用时间序列数据的图表。时间序列数据每天最多记录一次,但不定期记录。即,数据可能如下所示:
1/1, 1500
1/13, 1600
1/14, 1700
2/22, 1800
5/1, 1400
使用图表,我的折线图用每个数据点一个单位填充 x 轴(即,在上面的示例中,x 轴上有 5 个单位,而不是 ~120 天)
如何为范围内的所有日期创建带有单位的 x 轴?
我的图表的代码:
func setChartData(data: ([String], [Double])) {
var yVals1 = [ChartDataEntry]()
for i in 0 ..< data.0.count {
yVals1.append(ChartDataEntry(value: data.1[i], xIndex: i))
}
let set1: LineChartDataSet = LineChartDataSet(yVals: yVals1, label: nil)
set1.axisDependency = .Left
set1.setColor(UIColor(red:0.502, green:0.580, blue:0.784, alpha:1.000))
set1.lineWidth = 2.0
set1.fillAlpha = 1
set1.fillColor = UIColor(red:0.502, green:0.580, blue:0.784, alpha:1.000)
set1.highlightColor = UIColor.whiteColor()
set1.drawValuesEnabled = false
set1.drawCirclesEnabled = false
set1.drawFilledEnabled = true
var dataSets : [LineChartDataSet] = [LineChartDataSet]()
dataSets.append(set1)
let data: LineChartData = LineChartData(xVals: data.0, dataSets: dataSets)
data.setValueTextColor(UIColor.whiteColor())
let legend = lineChartView.legend
legend.enabled = false
let xAxis = lineChartView.xAxis
xAxis.drawGridLinesEnabled = false
xAxis.drawAxisLineEnabled = false
xAxis.labelPosition = .Bottom
let rightAxis = lineChartView.rightAxis
rightAxis.drawAxisLineEnabled = false
rightAxis.drawLabelsEnabled = false
rightAxis.drawGridLinesEnabled = false
let leftAxis = lineChartView.leftAxis
leftAxis.drawAxisLineEnabled = false
leftAxis.gridColor = UIColor.blackColor().colorWithAlphaComponent(0.1)
leftAxis.gridLineWidth = 2
self.lineChartView.data = data
}
最佳答案
不要直接传递数据点,而是将数据点转换为包含所有相关日期作为 x 值(1 天间隔,即使该天没有关联的 y 值)并适当填写 y 值,其中它们存在。
代码:
struct StatsViewModel {
var exercise: Exercise
var workoutDates: ([String], [Double?]) {
if let lastWorkout = exercise.workoutDiary.last, firstWorkout = exercise.workoutDiary.first {
var dates = [String]()
var stats = [Double?]()
let ti:NSTimeInterval = 24*60*60 //one day
let dateFrom = firstWorkout.date
let dateTo = lastWorkout.date
var nextDate = dateFrom
let endDate = dateTo.dateByAddingTimeInterval(ti)
while nextDate.compare(endDate) == NSComparisonResult.OrderedAscending
{
dates.append(nextDate.myPrettyString)
let workout = exercise.workoutDiary.filter { $0.date.myPrettyString == nextDate.myPrettyString }.first
if let workout = workout {
stats.append(Double(workout.totalVolume))
} else {
stats.append(nil)
}
nextDate = nextDate.dateByAddingTimeInterval(ti)
}
return (dates, stats)
}
return ([], [])
}
}
根据上面生成的数据点创建 ChartDataEntry,占空:
var statsViewModel: StatsViewModel!
override func viewDidLoad() {
setChartData(statsViewModel.workoutDates)
}
func setChartData(data: ([String], [Double?])) {
var yVals1 = [ChartDataEntry]()
for i in 0 ..< data.0.count {
if let yval = data.1[i] {
yVals1.append(ChartDataEntry(value: yval, xIndex: i))
}
}
let set1: LineChartDataSet = LineChartDataSet(yVals: yVals1, label: nil)
var dataSets : [LineChartDataSet] = [LineChartDataSet]()
dataSets.append(set1)
let data: LineChartData = LineChartData(xVals: data.0, dataSets: dataSets)
data.setValueTextColor(UIColor.whiteColor())
...
}
关于swift - 如何使用 Swift Charts 创建每天 1 个单位而不是每个数据点 1 个单位的 x 轴?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38113719/
我是一名优秀的程序员,十分优秀!