iOS 图表、水平条不准确
iOS Charts, Horizontal bars not accurate
我正在尝试获取具有实时数据变化的水平条形图。不过我遇到了这个问题。
如您所见,所有的条形图都不准确,除了最长的条形图。
以下是我的代码。
class HorizontalBarChartViewController: UIViewController {
var values: [Double] = [0.0, 0.0, 0.0, 0.0, 0.0]
var moods = ["Annoiato", "Ansioso", "Stanco", "Triste", "Altro"]
@IBOutlet weak var chart: HorizontalBarChartView!
func updateCharts() {
var chartDataEntry = [BarChartDataEntry]()
for i in 0..<values.count {
let value = BarChartDataEntry(x: Double(i), y: values[i])
chartDataEntry.append(value)
}
let chartDataSet = BarChartDataSet(values: chartDataEntry, label: "Sigarette fumate")
chartDataSet.drawValuesEnabled = false
chartDataSet.colors = [NSUIColor.green]
let chartMain = BarChartData()
chartMain.barWidth = 0.1
chartMain.addDataSet(chartDataSet)
chart.animate(yAxisDuration: 0.5)
chart.data = chartMain
}
func configureCharts() {
chart.xAxis.valueFormatter = IndexAxisValueFormatter(values: moods)
chart.xAxis.granularity = 1.0
chart.xAxis.labelPosition = .bottom
chart.xAxis.drawGridLinesEnabled = false
chart.leftAxis.enabled = false
chart.rightAxis.granularity = 1.0
chart.rightAxis.axisMinimum = 0.0
guard let description = chart.chartDescription else {return}
description.text = ""
}
}
为了增加值,我有 5 个函数,每个函数对应每个值:
@IBAction func *nameValue*Button(_ sender: UIButton) {
values[*index*]+=1
updateCharts()
configureCharts()
}
如何将条形与右侧 y 轴的正确网格线相匹配?
这是解决方案。请检查:
func configureCharts() {
chart.xAxis.valueFormatter = IndexAxisValueFormatter(values: moods)
chart.xAxis.granularity = 1.0
chart.xAxis.labelPosition = .bottom
chart.xAxis.drawGridLinesEnabled = false
chart.rightAxis.granularity = 1.0
chart.rightAxis.axisMinimum = 0.0
chart.leftAxis.axisMinimum = 0.0 //here's the missing line
guard let description = chart.chartDescription else {return}
description.text = ""
}
let leftAxis = chartView.leftAxis
leftAxis.drawTopYLabelEntryEnabled = true
leftAxis.drawAxisLineEnabled = true
leftAxis.drawGridLinesEnabled = true
leftAxis.granularityEnabled = false
leftAxis.axisLineColor = .green
leftAxis.labelTextColor = .black
// leftAxis.setLabelCount(6, force: true)
leftAxis.axisMinimum = 0.0
leftAxis.axisMaximum = 3000.0
let highI = 2290.0
let highIncome = ChartLimitLine()
highIncome.limit = highI
highIncome. lineColor = .red
// highIncome. valueTe = .blue
highIncomeAverageLine.label = "Average"
highIncomeAverageLine.labelPosition = .bottomLeft
leftAxis.addLimitLine(highIncomeAverageLine)
我正在尝试获取具有实时数据变化的水平条形图。不过我遇到了这个问题。
如您所见,所有的条形图都不准确,除了最长的条形图。
以下是我的代码。
class HorizontalBarChartViewController: UIViewController {
var values: [Double] = [0.0, 0.0, 0.0, 0.0, 0.0]
var moods = ["Annoiato", "Ansioso", "Stanco", "Triste", "Altro"]
@IBOutlet weak var chart: HorizontalBarChartView!
func updateCharts() {
var chartDataEntry = [BarChartDataEntry]()
for i in 0..<values.count {
let value = BarChartDataEntry(x: Double(i), y: values[i])
chartDataEntry.append(value)
}
let chartDataSet = BarChartDataSet(values: chartDataEntry, label: "Sigarette fumate")
chartDataSet.drawValuesEnabled = false
chartDataSet.colors = [NSUIColor.green]
let chartMain = BarChartData()
chartMain.barWidth = 0.1
chartMain.addDataSet(chartDataSet)
chart.animate(yAxisDuration: 0.5)
chart.data = chartMain
}
func configureCharts() {
chart.xAxis.valueFormatter = IndexAxisValueFormatter(values: moods)
chart.xAxis.granularity = 1.0
chart.xAxis.labelPosition = .bottom
chart.xAxis.drawGridLinesEnabled = false
chart.leftAxis.enabled = false
chart.rightAxis.granularity = 1.0
chart.rightAxis.axisMinimum = 0.0
guard let description = chart.chartDescription else {return}
description.text = ""
}
}
为了增加值,我有 5 个函数,每个函数对应每个值:
@IBAction func *nameValue*Button(_ sender: UIButton) {
values[*index*]+=1
updateCharts()
configureCharts()
}
如何将条形与右侧 y 轴的正确网格线相匹配?
这是解决方案。请检查:
func configureCharts() {
chart.xAxis.valueFormatter = IndexAxisValueFormatter(values: moods)
chart.xAxis.granularity = 1.0
chart.xAxis.labelPosition = .bottom
chart.xAxis.drawGridLinesEnabled = false
chart.rightAxis.granularity = 1.0
chart.rightAxis.axisMinimum = 0.0
chart.leftAxis.axisMinimum = 0.0 //here's the missing line
guard let description = chart.chartDescription else {return}
description.text = ""
}
let leftAxis = chartView.leftAxis
leftAxis.drawTopYLabelEntryEnabled = true
leftAxis.drawAxisLineEnabled = true
leftAxis.drawGridLinesEnabled = true
leftAxis.granularityEnabled = false
leftAxis.axisLineColor = .green
leftAxis.labelTextColor = .black
// leftAxis.setLabelCount(6, force: true)
leftAxis.axisMinimum = 0.0
leftAxis.axisMaximum = 3000.0
let highI = 2290.0
let highIncome = ChartLimitLine()
highIncome.limit = highI
highIncome. lineColor = .red
// highIncome. valueTe = .blue
highIncomeAverageLine.label = "Average"
highIncomeAverageLine.labelPosition = .bottomLeft
leftAxis.addLimitLine(highIncomeAverageLine)