在 iOS-Charts (Xcode 7.3.1) 中设置 X 和 Y 轴时出错
Error when setting up X & Y axis in iOS-Charts (Xcode 7.3.1)
我是编码新手,如果这是一个基本问题,我很抱歉。我正在使用 Daniel Gindi 的 iOS-Charts(他 Github 上的最新版本)使用以下代码设置条形图:
import UIKit
import Charts
class BarChartViewController: UIViewController {
var barChartView: BarChartView!
var days: [String]!
override func viewDidLoad() {
super.viewDidLoad()
days = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]
let tasks = [1.0, 1.0, 1.5, 2.0, 3.0, 5.0, 0.0]
setChart(days, values: tasks)
}
func setChart(dataPoints: [String], values: [Double]) {
barChartView.noDataText = "You need to provide data for the chart."
var dataEntries: [BarChartDataEntry] = Array()
var counter = 0.0
for i in 0..<dataPoints.count {
counter += 1.0
let dataEntry = BarChartDataEntry(x: values[i], y: counter)
dataEntries.append(dataEntry)
}
let chartDataSet = BarChartDataSet(yVals: dataEntries, label: "Time")
let chartData = BarChartData(xVals: days, dataSet: chartDataSet)
barChartView.data = chartData
}
}
这里提示func setChart出错:
在
let chartDataSet = BarChartDataSet(yVals: dataEntries, label: "Time")
调用中的参数不正确(有 yVals,期望值)。但是如果把yVals改成values,会在下一行提示错误:
let chartData = BarChartData(xVals: days, dataSet: chartDataSet)
错误是:无法使用类型为“(xVals:[String]!, dataSet: BarChartDataSet)”
的参数列表调用类型 'BarChartData' 的初始值设定项
我查看了 ChartsDemo,但我不确定我到底在寻找什么(我在尝试构建演示时似乎遇到了编译错误)。
有人可以帮我解决这个问题吗?
试试这个
func setChart(dataPoints: [String], values: [Double]) {
barChartView.noDataText = "You need to provide data for the chart."
var dataEntries: [BarChartDataEntry] = Array()
var counter = 0.0
for i in 0..<dataPoints.count {
counter += 1.0
let dataEntry = BarChartDataEntry(x: values[i], y: counter)
dataEntries.append(dataEntry)
}
let chartDataSet = BarChartDataSet(values: dataEntries, label: "Time")
let chartData = BarChartData()
chartData.addDataSet(chartDataSet)
barChartView.data = chartData
}
试试这个
let chartDataSet = BarChartDataSet(values: dataEntries, label: "Units Sold")
let chartData = BarChartData()
chartData.addDataSet(chartDataSet)
barChart.animate(xAxisDuration: 2.0, yAxisDuration: 2.0)
chartDataSet.colors = ChartColorTemplates.colorful()
barChart.data = chartData
完整代码
//
// ChartViewController.swift
// OEE
//
// Created by Andrew on 07.03.2018.
// Copyright © 2018 Andrew. All rights reserved.
//
import UIKit
import Foundation
import Charts
class ChartViewController: UIViewController, IAxisValueFormatter, ChartViewDelegate {
func stringForValue(_ value: Double, axis: AxisBase?) -> String {
return months[Int(value)]
}
@IBOutlet weak var barChart: BarChartView!
var months: [String]!
override func viewDidLoad() {
super.viewDidLoad()
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]) {
barChart.noDataText = "You need to provide data for the chart."
var dataEntries: [BarChartDataEntry] = []
var counter = 0.0
for i in 0..<dataPoints.count {
counter += 1.0
let dataEntry = BarChartDataEntry(x: values[i], y: counter)
dataEntries.append(dataEntry)
}
let chartDataSet = BarChartDataSet(values: dataEntries, label: "Units Sold")
let chartData = BarChartData()
chartData.addDataSet(chartDataSet)
barChart.animate(xAxisDuration: 2.0, yAxisDuration: 2.0)
chartDataSet.colors = ChartColorTemplates.colorful()
barChart.data = chartData
//barChart.xAxis.ent
barChart.xAxis.valueFormatter = IndexAxisValueFormatter(values: months)
barChart.xAxis.labelCount = months.count
barChart.xAxis.labelPosition = .bottom
}
}
我是编码新手,如果这是一个基本问题,我很抱歉。我正在使用 Daniel Gindi 的 iOS-Charts(他 Github 上的最新版本)使用以下代码设置条形图:
import UIKit
import Charts
class BarChartViewController: UIViewController {
var barChartView: BarChartView!
var days: [String]!
override func viewDidLoad() {
super.viewDidLoad()
days = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]
let tasks = [1.0, 1.0, 1.5, 2.0, 3.0, 5.0, 0.0]
setChart(days, values: tasks)
}
func setChart(dataPoints: [String], values: [Double]) {
barChartView.noDataText = "You need to provide data for the chart."
var dataEntries: [BarChartDataEntry] = Array()
var counter = 0.0
for i in 0..<dataPoints.count {
counter += 1.0
let dataEntry = BarChartDataEntry(x: values[i], y: counter)
dataEntries.append(dataEntry)
}
let chartDataSet = BarChartDataSet(yVals: dataEntries, label: "Time")
let chartData = BarChartData(xVals: days, dataSet: chartDataSet)
barChartView.data = chartData
}
}
这里提示func setChart出错:
在
let chartDataSet = BarChartDataSet(yVals: dataEntries, label: "Time")
调用中的参数不正确(有 yVals,期望值)。但是如果把yVals改成values,会在下一行提示错误:
let chartData = BarChartData(xVals: days, dataSet: chartDataSet)
错误是:无法使用类型为“(xVals:[String]!, dataSet: BarChartDataSet)”
的参数列表调用类型 'BarChartData' 的初始值设定项我查看了 ChartsDemo,但我不确定我到底在寻找什么(我在尝试构建演示时似乎遇到了编译错误)。
有人可以帮我解决这个问题吗?
试试这个
func setChart(dataPoints: [String], values: [Double]) {
barChartView.noDataText = "You need to provide data for the chart."
var dataEntries: [BarChartDataEntry] = Array()
var counter = 0.0
for i in 0..<dataPoints.count {
counter += 1.0
let dataEntry = BarChartDataEntry(x: values[i], y: counter)
dataEntries.append(dataEntry)
}
let chartDataSet = BarChartDataSet(values: dataEntries, label: "Time")
let chartData = BarChartData()
chartData.addDataSet(chartDataSet)
barChartView.data = chartData
}
试试这个
let chartDataSet = BarChartDataSet(values: dataEntries, label: "Units Sold")
let chartData = BarChartData()
chartData.addDataSet(chartDataSet)
barChart.animate(xAxisDuration: 2.0, yAxisDuration: 2.0)
chartDataSet.colors = ChartColorTemplates.colorful()
barChart.data = chartData
完整代码
//
// ChartViewController.swift
// OEE
//
// Created by Andrew on 07.03.2018.
// Copyright © 2018 Andrew. All rights reserved.
//
import UIKit
import Foundation
import Charts
class ChartViewController: UIViewController, IAxisValueFormatter, ChartViewDelegate {
func stringForValue(_ value: Double, axis: AxisBase?) -> String {
return months[Int(value)]
}
@IBOutlet weak var barChart: BarChartView!
var months: [String]!
override func viewDidLoad() {
super.viewDidLoad()
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]) {
barChart.noDataText = "You need to provide data for the chart."
var dataEntries: [BarChartDataEntry] = []
var counter = 0.0
for i in 0..<dataPoints.count {
counter += 1.0
let dataEntry = BarChartDataEntry(x: values[i], y: counter)
dataEntries.append(dataEntry)
}
let chartDataSet = BarChartDataSet(values: dataEntries, label: "Units Sold")
let chartData = BarChartData()
chartData.addDataSet(chartDataSet)
barChart.animate(xAxisDuration: 2.0, yAxisDuration: 2.0)
chartDataSet.colors = ChartColorTemplates.colorful()
barChart.data = chartData
//barChart.xAxis.ent
barChart.xAxis.valueFormatter = IndexAxisValueFormatter(values: months)
barChart.xAxis.labelCount = months.count
barChart.xAxis.labelPosition = .bottom
}
}