gpt4 book ai didi

ios - “How to Use iOS Charts API to Create Beautiful Charts in Swift” 之后的困难

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:36:54 24 4
gpt4 key购买 nike

AppCoda 教程,日期为 2015 年 6 月 23 日,指导您完成一个没有绘制 View 的入门项目。但是,我的项目错误地绘制了示例条形图;在教程中,所有的条都出现了,并且被正确标记了。我把我的项目版本放在这里IOChartsDemo .

this is what happens in my project

开发环境:

  • macOS 12.5.5、Xcode 8.3.3 (8E3004b)
  • 图表 3.0.2

我下载了教程中指示的入门项目。

教程中的起始项目没有构建,存在 api 问题。按照教程中的说明进行操作并更正错误后,我得到了这个(问题行已被注释掉):

class BarChartViewController: UIViewController {
@IBOutlet weak var barChartView: BarChartView!

var months: [String]!

override func viewDidLoad() {
super.viewDidLoad()
// barChartView.noDataTextDescription = "Just because" type BarChartView doest have noDataTextDescription
months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]
let unitsSold = [20.0, 4.0, 6.0, 3.0, 12.0, 16.0, 4.0, 18.0, 2.0, 4.0, 5.0, 4.0]

// setChart(months, values: unitsSold) missing argument label dataPoints: in call
setChart(dataPoints: months, values: unitsSold)
}

func setChart(dataPoints: [String], values: [Double]) {
barChartView.noDataText = "You need to provide data for the chart."
var dataEntries: [BarChartDataEntry] = []

for i in 0..<dataPoints.count {
// let dataEntry = BarChartDataEntry(value: values[i], xIndex: i) // argument labeld dont match
let dataEntry = BarChartDataEntry(x: values[i], y: Double(i), data: dataPoints[i] as AnyObject)

dataEntries.append(dataEntry)
}

let chartDataSet = BarChartDataSet(values: dataEntries, label: "Units Sold")

// let chartData = BarChartData(xVals: months, dataSet: chartDataSet) cannot invoke initializer BarCharData with arg list of (xVals: [String]!, dataSet: BarCharDataSet
let chartData = BarChartData(dataSets: [chartDataSet])

barChartView.data = chartData
}

此时,当我运行该应用程序时,我得到了之前指示的结果。我在通过图表 api 后修复了这些问题。我想这个问题是由图表 2.x 到 3.x 的变化引起的。

如果您是图表专家,我恳请您提供帮助。

最佳答案

我一直在研究你的问题,首先,你已经反转了 yx 的值

解决这个问题

这一行 let dataEntry = BarChartDataEntry(x: values[i], y: Double(i), data: dataPoints[i] as AnyObject) 必须替换为这一行 let dataEntry = BarChartDataEntry(x: Double(i), y: values[i], data: dataPoints[i] as AnyObject)

然后您必须使用 IndexAxisValueFormatter 类才能在底部显示图例,我指的是月份名称

添加这一行

barChartView.xAxis.valueFormatter = IndexAxisValueFormatter(values: months)
barChartView.xAxis.labelCount = months.count
barChartView.xAxis.labelPosition = .bottom

这是完整的代码

import UIKit
import Charts

class BarChartViewController: UIViewController {
@IBOutlet weak var barChartView: BarChartView!

var months: [String]!

override func viewDidLoad() {
super.viewDidLoad()
// barChartView.noDataTextDescription = "Just because" type BarChartView doest have noDataTextDescription
months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]
let unitsSold = [20.0, 4.0, 6.0, 3.0, 12.0, 16.0, 4.0, 18.0, 2.0, 4.0, 5.0, 4.0]

//setChart(months, values: unitsSold) missing argument label dataPoints: in call
setChart(dataPoints: months, values: unitsSold)
}

func setChart(dataPoints: [String], values: [Double]) {
barChartView.noDataText = "You need to provide data for the chart."
var dataEntries: [BarChartDataEntry] = []

for i in 0..<dataPoints.count {
// let dataEntry = BarChartDataEntry(value: values[i], xIndex: i) // argument labeld dont match
let dataEntry = BarChartDataEntry(x: Double(i), y: values[i], data: dataPoints[i] as AnyObject)
dataEntries.append(dataEntry)
}

let chartDataSet = BarChartDataSet(values: dataEntries, label: "Units Sold")

// let chartData = BarChartData(xVals: months, dataSet: chartDataSet) cannot invoke initializer BarCharData with arg list of (xVals: [String]!, dataSet: BarCharDataSet
let chartData = BarChartData(dataSets: [chartDataSet])

barChartView.data = chartData
//barChartView.xAxis.ent
barChartView.xAxis.valueFormatter = IndexAxisValueFormatter(values: months)
barChartView.xAxis.labelCount = months.count
barChartView.xAxis.labelPosition = .bottom

}

}

这就是它的样子,希望这对你有帮助

enter image description here

关于ios - “How to Use iOS Charts API to Create Beautiful Charts in Swift” 之后的困难,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44877031/

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