无法为饼图设置自定义颜色
cannot set custom colors for pie charts
我正在使用 iOS 图表制作饼图,但我不知道如何将颜色设置为具有 RGBA 值的自定义颜色。这是代码
var colors: [UIColor] = []
colors.append(UIColor.init(red: 206, green: 173, blue: 128, alpha: 1))
colors.append(UIColor.init(red: 136, green: 169, blue: 132, alpha: 1))
colors.append(UIColor.init(red: 183, green: 73, blue: 32, alpha: 1))
colors.append(UIColor.init(red: 106, green: 152, blue: 150, alpha: 1))
colors.append(UIColor.init(red: 226, green: 120, blue: 123, alpha: 1))
colors.append(UIColor.init(red: 47, green: 122, blue: 153, alpha: 1))
chartDataSet.colors = colors
但它不起作用,图表看起来像
this
编辑:这是函数的其余部分
func updateChartData() {
let chartDataSet = PieChartDataSet(entries: moods, label: nil)
let chartData = PieChartData(dataSet: chartDataSet)
chartDataSet.drawValuesEnabled = false
pieChartVIew.drawEntryLabelsEnabled = false
var colors: [UIColor] = []
colors.append(UIColor.init(red: 206, green: 173, blue: 128, alpha: 1))
colors.append(UIColor.init(red: 136, green: 169, blue: 132, alpha: 1))
colors.append(UIColor.init(red: 183, green: 73, blue: 32, alpha: 1))
colors.append(UIColor.init(red: 106, green: 152, blue: 150, alpha: 1))
colors.append(UIColor.init(red: 226, green: 120, blue: 123, alpha: 1))
colors.append(UIColor.init(red: 47, green: 122, blue: 153, alpha: 1))
chartDataSet.colors = colors
pieChartVIew.holeColor = UIColor.clear
pieChartVIew.data = chartData
}
这里是我设置值的地方
var happyDataEntry = PieChartDataEntry(value: 0)
var happyCount = 0
var moods = [PieChartDataEntry]()
override func viewDidLoad() {
super.viewDidLoad()
happyDataEntry.value = Double(happyCount)
happyDataEntry.label = "happy!"
moods = [happyDataEntry}
updateChartData()
}
我稍微更新了你的代码:(值更改为:var happyCount = 1)
var happyDataEntry = PieChartDataEntry(value: 0)
var happyCount = 1
var moods = [PieChartDataEntry]()
viewDidLoad 方法:
override func viewDidLoad() {
super.viewDidLoad()
happyDataEntry.value = Double(happyCount)
happyDataEntry.label = "happy!"
// moods.append(happyDataEntry) //If you uncomment this line you will see pie chart with 2 colors.
moods.append(happyDataEntry)
updateChartData()
}
更新图表方法:(我更新了color init,请看一下。)
func updateChartData() {
let chartDataSet = PieChartDataSet(entries: moods, label: nil)
let chartData = PieChartData(dataSet: chartDataSet)
chartDataSet.drawValuesEnabled = false
pieChartVIew.drawEntryLabelsEnabled = false
var colors: [UIColor] = []
colors.append(UIColor.init(red: 206.0/255.0, green: 173.0/255.0, blue: 128.0/255.0, alpha: 1.0))
colors.append(UIColor.init(red: 136.0/255.0, green: 169.0/255.0, blue: 132.0/255.0, alpha: 1.0))
colors.append(UIColor.init(red: 183.0/255.0, green: 73.0/255.0, blue: 32.0/255.0, alpha: 1.0))
colors.append(UIColor.init(red: 106.0/255.0, green: 152.0/255.0, blue: 150.0/255.0, alpha: 1.0))
colors.append(UIColor.init(red: 226.0/255.0, green: 120.0/255.0, blue: 123.0/255.0, alpha: 1.0))
colors.append(UIColor.init(red: 47.0/255.0, green: 122.0/255.0, blue: 153.0/255.0, alpha: 1.0))
chartDataSet.colors = colors
pieChartVIew.holeColor = UIColor.clear
pieChartVIew.data = chartData
}
有问题请评论
乐于助人!
我正在使用 iOS 图表制作饼图,但我不知道如何将颜色设置为具有 RGBA 值的自定义颜色。这是代码
var colors: [UIColor] = []
colors.append(UIColor.init(red: 206, green: 173, blue: 128, alpha: 1))
colors.append(UIColor.init(red: 136, green: 169, blue: 132, alpha: 1))
colors.append(UIColor.init(red: 183, green: 73, blue: 32, alpha: 1))
colors.append(UIColor.init(red: 106, green: 152, blue: 150, alpha: 1))
colors.append(UIColor.init(red: 226, green: 120, blue: 123, alpha: 1))
colors.append(UIColor.init(red: 47, green: 122, blue: 153, alpha: 1))
chartDataSet.colors = colors
但它不起作用,图表看起来像
this
编辑:这是函数的其余部分
func updateChartData() {
let chartDataSet = PieChartDataSet(entries: moods, label: nil)
let chartData = PieChartData(dataSet: chartDataSet)
chartDataSet.drawValuesEnabled = false
pieChartVIew.drawEntryLabelsEnabled = false
var colors: [UIColor] = []
colors.append(UIColor.init(red: 206, green: 173, blue: 128, alpha: 1))
colors.append(UIColor.init(red: 136, green: 169, blue: 132, alpha: 1))
colors.append(UIColor.init(red: 183, green: 73, blue: 32, alpha: 1))
colors.append(UIColor.init(red: 106, green: 152, blue: 150, alpha: 1))
colors.append(UIColor.init(red: 226, green: 120, blue: 123, alpha: 1))
colors.append(UIColor.init(red: 47, green: 122, blue: 153, alpha: 1))
chartDataSet.colors = colors
pieChartVIew.holeColor = UIColor.clear
pieChartVIew.data = chartData
}
这里是我设置值的地方
var happyDataEntry = PieChartDataEntry(value: 0)
var happyCount = 0
var moods = [PieChartDataEntry]()
override func viewDidLoad() {
super.viewDidLoad()
happyDataEntry.value = Double(happyCount)
happyDataEntry.label = "happy!"
moods = [happyDataEntry}
updateChartData()
}
我稍微更新了你的代码:(值更改为:var happyCount = 1)
var happyDataEntry = PieChartDataEntry(value: 0)
var happyCount = 1
var moods = [PieChartDataEntry]()
viewDidLoad 方法:
override func viewDidLoad() {
super.viewDidLoad()
happyDataEntry.value = Double(happyCount)
happyDataEntry.label = "happy!"
// moods.append(happyDataEntry) //If you uncomment this line you will see pie chart with 2 colors.
moods.append(happyDataEntry)
updateChartData()
}
更新图表方法:(我更新了color init,请看一下。)
func updateChartData() {
let chartDataSet = PieChartDataSet(entries: moods, label: nil)
let chartData = PieChartData(dataSet: chartDataSet)
chartDataSet.drawValuesEnabled = false
pieChartVIew.drawEntryLabelsEnabled = false
var colors: [UIColor] = []
colors.append(UIColor.init(red: 206.0/255.0, green: 173.0/255.0, blue: 128.0/255.0, alpha: 1.0))
colors.append(UIColor.init(red: 136.0/255.0, green: 169.0/255.0, blue: 132.0/255.0, alpha: 1.0))
colors.append(UIColor.init(red: 183.0/255.0, green: 73.0/255.0, blue: 32.0/255.0, alpha: 1.0))
colors.append(UIColor.init(red: 106.0/255.0, green: 152.0/255.0, blue: 150.0/255.0, alpha: 1.0))
colors.append(UIColor.init(red: 226.0/255.0, green: 120.0/255.0, blue: 123.0/255.0, alpha: 1.0))
colors.append(UIColor.init(red: 47.0/255.0, green: 122.0/255.0, blue: 153.0/255.0, alpha: 1.0))
chartDataSet.colors = colors
pieChartVIew.holeColor = UIColor.clear
pieChartVIew.data = chartData
}
有问题请评论
乐于助人!