向饼图添加图例文本 Swift 个图表
Add legend text to pie chart Swift Charts
我正在尝试为我的图表添加图例,但它只是在没有文本的情况下出现的颜色。我不明白这是为什么?要创建我的图表,我使用:
func configure (dataPoints: [String], values: [Double]) {
chartIMG.noDataText = "Please Insert Some Data!"
var dataEntries: [ChartDataEntry] = []
for i in 0..<dataPoints.count {
let dataEntry = ChartDataEntry(x: values[i], y: Double(i))
dataEntries.append(dataEntry)
}
let pieChartDataSet = PieChartDataSet(values: dataEntries, label: "Symptoms")
let pieChartData = PieChartData(dataSet: pieChartDataSet)
chartIMG.data = pieChartData
let noZeroFormatter = NumberFormatter()
noZeroFormatter.zeroSymbol = ""
pieChartDataSet.valueFormatter = DefaultValueFormatter(formatter: noZeroFormatter)
var colors: [UIColor] = []
for i in 0..<dataPoints.count {
let red = Double(arc4random_uniform(256))
let green = Double(arc4random_uniform(256))
let blue = Double(arc4random_uniform(256))
let color = UIColor(red: CGFloat(red/255), green: CGFloat(green/255), blue: CGFloat(blue/255), alpha: 1)
colors.append(color)
}
chartIMG.highlightPerTapEnabled = false
chartIMG.usePercentValuesEnabled = false
chartIMG.drawEntryLabelsEnabled = true
chartIMG.centerText = "%"
let legend = chartIMG.legend
legend.font = UIFont(name: "Arial", size: 11)!
legend.textColor = UIColor.black
legend.form = .circle
chartIMG.animate(xAxisDuration: 2, yAxisDuration: 2)
pieChartDataSet.colors = colors
pieChartDataSet.selectionShift = 0
pieChartDataSet.sliceSpace = 2.5
}
我正在尝试实现类似于:https://i.stack.imgur.com/eiSnN.png
但似乎无法让图例与标签一起显示。知道这是为什么吗?我认为这可能是一行简单的代码,但我似乎找不到任何有用的东西。
如果有人正在寻求此帮助,请简单回答:
let dataEntry1 = PieChartDataEntry(value: Double(i), label: dataPoints[i], data: dataPoints[i] as AnyObject)
在这里回答:
我正在尝试为我的图表添加图例,但它只是在没有文本的情况下出现的颜色。我不明白这是为什么?要创建我的图表,我使用:
func configure (dataPoints: [String], values: [Double]) {
chartIMG.noDataText = "Please Insert Some Data!"
var dataEntries: [ChartDataEntry] = []
for i in 0..<dataPoints.count {
let dataEntry = ChartDataEntry(x: values[i], y: Double(i))
dataEntries.append(dataEntry)
}
let pieChartDataSet = PieChartDataSet(values: dataEntries, label: "Symptoms")
let pieChartData = PieChartData(dataSet: pieChartDataSet)
chartIMG.data = pieChartData
let noZeroFormatter = NumberFormatter()
noZeroFormatter.zeroSymbol = ""
pieChartDataSet.valueFormatter = DefaultValueFormatter(formatter: noZeroFormatter)
var colors: [UIColor] = []
for i in 0..<dataPoints.count {
let red = Double(arc4random_uniform(256))
let green = Double(arc4random_uniform(256))
let blue = Double(arc4random_uniform(256))
let color = UIColor(red: CGFloat(red/255), green: CGFloat(green/255), blue: CGFloat(blue/255), alpha: 1)
colors.append(color)
}
chartIMG.highlightPerTapEnabled = false
chartIMG.usePercentValuesEnabled = false
chartIMG.drawEntryLabelsEnabled = true
chartIMG.centerText = "%"
let legend = chartIMG.legend
legend.font = UIFont(name: "Arial", size: 11)!
legend.textColor = UIColor.black
legend.form = .circle
chartIMG.animate(xAxisDuration: 2, yAxisDuration: 2)
pieChartDataSet.colors = colors
pieChartDataSet.selectionShift = 0
pieChartDataSet.sliceSpace = 2.5
}
我正在尝试实现类似于:https://i.stack.imgur.com/eiSnN.png 但似乎无法让图例与标签一起显示。知道这是为什么吗?我认为这可能是一行简单的代码,但我似乎找不到任何有用的东西。
如果有人正在寻求此帮助,请简单回答:
let dataEntry1 = PieChartDataEntry(value: Double(i), label: dataPoints[i], data: dataPoints[i] as AnyObject)
在这里回答: