gpt4 book ai didi

swift - 使用 iOS 图表的组合图表(折线图和条形图)

转载 作者:搜寻专家 更新时间:2023-10-31 21:53:19 25 4
gpt4 key购买 nike

我正在尝试在 swift 中组合折线图和条形图(iOS 图表)。 x 轴和 y 轴似乎都已正确缩放,但数据未显示在图表上。我错过了什么? enter image description here

import UIKit
import Charts

class CombinedChartsTest: UIViewController {


@IBOutlet weak var combinedChart: CombinedChartView!


let months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]
let unitsSold = [2.0, 4.0, 6.0, 3.0, 12.0, 16.0, 4.0, 17.0, 2.0, 4.0, 5.0, 4.0]

override func viewDidLoad() {
super.viewDidLoad()

setChart(months, yValuesLineChart: unitsSold, yValuesBarChart: unitsSold)
}

func setChart(xValues: [String], yValuesLineChart: [Double], yValuesBarChart: [Double]) {
combinedChart.noDataText = "Please provide data for the chart."

var yVals1 : [ChartDataEntry] = [ChartDataEntry]()
var yVals2 : [BarChartDataEntry] = [BarChartDataEntry]()

for i in 0..<xValues.count {

yVals1.append(ChartDataEntry(value: yValuesLineChart[i], xIndex: i))
yVals2.append(BarChartDataEntry(value: yValuesBarChart[i] - 1, xIndex: i))

}

let lineChartSet = LineChartDataSet(yVals: yVals1, label: "Line Data")
let barChartSet: BarChartDataSet = BarChartDataSet(yVals: yVals2, label: "Bar Data")

let data: CombinedChartData = CombinedChartData(xVals: xValues, dataSets: [lineChartSet,barChartSet])

combinedChart.data = data

}
}

最佳答案

我终于弄清楚缺少了什么。

import UIKit
import Charts

class CombinedChartsTest: UIViewController {


@IBOutlet weak var combinedChartView: CombinedChartView!


let months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]
let unitsSold = [2.0, 4.0, 6.0, 3.0, 12.0, 16.0, 4.0, 17.0, 2.0, 4.0, 5.0, 4.0]

override func viewDidLoad() {
super.viewDidLoad()

setChart(months, yValuesLineChart: unitsSold, yValuesBarChart: unitsSold)
}

func setChart(xValues: [String], yValuesLineChart: [Double], yValuesBarChart: [Double]) {
combinedChartView.noDataText = "Please provide data for the chart."

var yVals1 : [ChartDataEntry] = [ChartDataEntry]()
var yVals2 : [BarChartDataEntry] = [BarChartDataEntry]()

for i in 0..<xValues.count {

yVals1.append(ChartDataEntry(value: yValuesLineChart[i], xIndex: i))
yVals2.append(BarChartDataEntry(value: yValuesBarChart[i] - 1, xIndex: i))

}

let lineChartSet = LineChartDataSet(yVals: yVals1, label: "Line Data")
let barChartSet: BarChartDataSet = BarChartDataSet(yVals: yVals2, label: "Bar Data")


let data: CombinedChartData = CombinedChartData(xVals: xValues)
data.barData = BarChartData(xVals: xValues, dataSets: [barChartSet])
data.lineData = LineChartData(xVals: xValues, dataSets: [lineChartSet])

combinedChartView.data = data

}
}

关于swift - 使用 iOS 图表的组合图表(折线图和条形图),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36150886/

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