gpt4 book ai didi

ios - 当超过 2 列时,条形图会向上跳跃

转载 作者:行者123 更新时间:2023-11-30 12:43:07 24 4
gpt4 key购买 nike

我正在处理一个图表,按下按钮时数据集会发生变化。但看起来当我有超过 2 个柱时,图表原点就会发生变化。

Chart

两列就可以了:

2 column chart

创建数据集:

private func buildMoodDummyBarData(period: Period) -> [BarChartDataEntry] {

if period == .week {
let entry1 = BarChartDataEntry(x: 0, y: 720)
let entry2 = BarChartDataEntry(x: 1, y: 440)
let entry3 = BarChartDataEntry(x: 2, y: 0)
let entry4 = BarChartDataEntry(x: 3, y: 30)
let entry5 = BarChartDataEntry(x: 4, y: 30)

return [entry1, entry2, entry3, entry4, entry5]

} else if period == .month {

let entry1 = BarChartDataEntry(x: 0, y: 100)
let entry2 = BarChartDataEntry(x: 1, y: 300)
let entry3 = BarChartDataEntry(x: 2, y: 50)
let entry4 = BarChartDataEntry(x: 3, y: 30)
let entry5 = BarChartDataEntry(x: 4, y: 30)

return [entry1, entry2, entry3, entry4, entry5]

} else {

return []
}
}

完成构建数据集并更改条形颜色:

func buildMoodBarData(period: Period) -> BarChartData? {

let entries = buildMoodDummyBarData(period: period)

let set = BarChartDataSet(values: entries, label: nil)
set.highlightEnabled = false

let tempData = BarChartData(dataSet: set) // Just to find max
let max = tempData.yMax
let onePercent = max / 100

var colors = [UIColor]()
for item in entries {
let percentValue = item.y / onePercent
colors.append(setColor(value: percentValue))
}
set.colors = colors

let data = BarChartData(dataSet: set)
data.setDrawValues(true)
data.barWidth = 0.72

return data
}

// barChartView is storyboard outlet
private func makeMoodBarChart() {

barChartView.backgroundColor = UIColor.red
barChartView.chartDescription = nil
barChartView.leftAxis.enabled = false
barChartView.rightAxis.enabled = false
barChartView.legend.enabled = false
barChartView.xAxis.enabled = false
}

private func updateMoodBarChart(with data: BarChartData) {

barChartView.data = data
barChartView.fitBars = false
barChartView.fitScreen()
}

最佳答案

问题与数据集有关。当我将其替换为以下数据时,问题消失了。我认为出现问题是因为 Y 比例值变化太大。

private func buildMoodDummyBarData(period: Period) -> [BarChartDataEntry] {

if period == .week {
let entry1 = BarChartDataEntry(x: 0, y: 10)
let entry2 = BarChartDataEntry(x: 1, y: 50)
let entry3 = BarChartDataEntry(x: 2, y: 100)
let entry4 = BarChartDataEntry(x: 3, y: 20)
let entry5 = BarChartDataEntry(x: 4, y: 30)

return [entry1, entry2, entry3, entry4, entry5]

} else if period == .month {

let entry1 = BarChartDataEntry(x: 0, y: 100)
let entry2 = BarChartDataEntry(x: 1, y: 30)
let entry3 = BarChartDataEntry(x: 2, y: 30)
let entry4 = BarChartDataEntry(x: 3, y: 20)
let entry5 = BarChartDataEntry(x: 4, y: 30)

return [entry1, entry2, entry3, entry4, entry5]

} else {

return []
}
}

这并不意味着两个数据集中的最小值和最大值必须完全相同(20 到 100),但是如果差异太大(第一个数据集中为 30 到 320,第二个数据集中为 30 到 720),图表基线变化。

这不是理想的解决方案。

关于ios - 当超过 2 列时,条形图会向上跳跃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42008525/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com