我创建了两个数据集,chartDataSet和limitDataSet。
var dataEntries = [BarChartDataEntry]()
var limitEntries = [BarChartDataEntry]()
for i in 0..<daily[index].count {
let dataEntry = BarChartDataEntry(value: values[index][i], xIndex: i)
let limitEntry = BarChartDataEntry(value: limitValues[index][i], xIndex: i)
dataEntries.append(dataEntry)
limitEntries.append(limitEntry)
}
let chartDataSet = BarChartDataSet(yVals: dataEntries, label: label)
chartDataSet.colors = [UIColor(red: 108/255, green: 189/255, blue: 146/255, alpha: 1)]
let limitDataSet = BarChartDataSet(yVals: limitEntries, label: "Limit in mg")
limitDataSet.colors = [UIColor(red: 219/255, green: 70/255, blue: 70/255, alpha: 1)]
var dataSets = [BarChartDataSet]()
dataSets.append(chartDataSet)
dataSets.append(limitDataSet)
//this line does has error
let chartData = BarChartData(xVals: daily[index], dataSet: dataSets)
monthlyChart.data = chartData
我需要在条形图中添加数据集,但我使用的代码仅适用于单个数据集。
这是我在项目中使用的图表。 https://github.com/danielgindi/Charts
我没有在BarChart上添加2个数据集。
但是我用PieChart完成了这个。
希望这会帮助你。
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var pieChartView: PieChartView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun"]
let unitsSold = [20.0, 4.0, 6.0, 3.0, 12.0, 16.0]
setChart(months, values: unitsSold)
setChart2(months, values: unitsSold)
}
func setChart(dataPoints: [String], values: [Double]) {
var dataEntries: [ChartDataEntry] = []
for i in 0..<dataPoints.count {
let dataEntry = ChartDataEntry(value: values[i], xIndex: i)
dataEntries.append(dataEntry)
}
let pieChartDataSet = PieChartDataSet(yVals: dataEntries, label: "Units Sold")
let formatter = NSNumberFormatter()
formatter.numberStyle = .PercentStyle
formatter.maximumFractionDigits = 1
formatter.multiplier = 1.0
let pieChartData = PieChartData(xVals: dataPoints, dataSet: pieChartDataSet)
pieChartData.dataSet?.valueFormatter = formatter
pieChartView.data = pieChartData
pieChartView.holeColor = UIColor.clearColor()
pieChartView.holeRadiusPercent = 0.95
pieChartView.centerText = "Hello\nThis is Pie chart"
pieChartView.usePercentValuesEnabled = true
var colors: [UIColor] = []
for i in 0..<dataPoints.count {
let red = Double(arc4random_uniform(255))
let green = Double(arc4random_uniform(255))
let blue = Double(arc4random_uniform(255))
let color = UIColor(red: CGFloat(red/255), green: CGFloat(green/255), blue: CGFloat(blue/255), alpha: 1)
colors.append(color)
}
pieChartDataSet.colors = colors
}
func setChart2(dataPoints: [String], values: [Double]) {
var dataEntries: [ChartDataEntry] = []
for i in 0..<dataPoints.count {
let dataEntry = ChartDataEntry(value: values[i], xIndex: i)
dataEntries.append(dataEntry)
}
let pieChartDataSet = PieChartDataSet(yVals: dataEntries, label: "Units Sold")
let formatter = NSNumberFormatter()
formatter.numberStyle = .PercentStyle
formatter.maximumFractionDigits = 1
formatter.multiplier = 1.0
let pieChartData = PieChartData(xVals: dataPoints, dataSet: pieChartDataSet)
pieChartData.dataSet?.valueFormatter = formatter
pieChartView.data = pieChartData
pieChartView.holeColor = UIColor.clearColor()
pieChartView.holeRadiusPercent = 0.95
pieChartView.centerText = "Hello\nThis is Pie chart"
pieChartView.usePercentValuesEnabled = true
var colors: [UIColor] = []
for i in 0..<dataPoints.count {
let red = Double(arc4random_uniform(255))
let green = Double(arc4random_uniform(255))
let blue = Double(arc4random_uniform(255))
let color = UIColor(red: CGFloat(red/255), green: CGFloat(green/255), blue: CGFloat(blue/255), alpha: 1)
colors.append(color)
}
pieChartDataSet.colors = colors
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
快乐编码。