我编写了以下代码,但饼图显示 like this
let months = ["Jan", "Feb", "Apr", "Jun"]
let unitsSold = [68, 10, 12, 10]
setChart(dataPoints: months, values: unitsSold)
func setChart(dataPoints: [String], values: [Int]) {
let screenBounds = UIScreen.main.bounds
let width = screenBounds.width
let height = screenBounds.height
let frame = CGRect(x: 10, y: 10, width: width, height: height/1.5)
let chart = PieChartView(frame: frame)
var dataEntries: [ChartDataEntry] = []
for i in 0..<dataPoints.count {
let dataEntry = ChartDataEntry(x: Double(values[i]), y: Double(i))
dataEntries.append(dataEntry)
}
let pieChartDataSet = PieChartDataSet(values: dataEntries, label: "Units Sold")
var colors: [UIColor] = []
for _ 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)
}
pieChartDataSet.colors = colors
let data = PieChartData(dataSet: pieChartDataSet)
piecharView.data = data
piecharView.backgroundColor = UIColor.white
piecharView.noDataText = "No Data Available"
piecharView.isUserInteractionEnabled = true
let d = Description()
d.text = ""
piecharView.chartDescription = d
piecharView.centerText = "HEALTH SCORE"
piecharView.holeRadiusPercent = 0.5
piecharView.transparentCircleColor = UIColor.clear
piecharView.usePercentValuesEnabled = true
piecharView.drawEntryLabelsEnabled = true
pieChartDataSet.colors = colors
}
但我想要这样
pie Chart
我启用了百分比,但仍然没有显示我想要的饼图。
Now it is showing like this
Best Answer-推荐答案 strong>
let data = PieChartData(dataSet: pieChartDataSet) 这行之后
输入这段代码
let pFormatter = NumberFormatter()
pFormatter.numberStyle = .percent
pFormatter.maximumFractionDigits = 1
pFormatter.multiplier = 1
pFormatter.percentSymbol = " %"
data.setValueFormatter(DefaultValueFormatter(formatter: pFormatter))
data.setValueFont(UIFont(name: "HelveticaNeue-Light", size: 11)!)
data.setValueTextColor(.white)
然后
piecharView.data = data
和
piecharView.setNeedDisplay()
编辑
第 1 步:转到 Storybaord 并为 pieChartView 设置适当的约束
第 2 步:在 Storyboard中选择 PieCharView 并从右侧面板中将其设置为 PieChartView 类。
第 3 步:移除旧的 IBOutlet 并附加新的 IBoutlet 应该如下所示
@IBOutlet weak var pieChartView: PieChartView!
在你的类中添加这个方法
func setCharts(dataPoints:[String], values : [Double]) {
var dataEntries = [PieChartDataEntry]()
for i in 0..<dataPoints.count {
let dataEntry = PieChartDataEntry(value: values[i], label: dataPoints[i])
dataEntries.append(dataEntry)
}
let pieChartDataSet = PieChartDataSet(values: dataEntries, label: "Unit Sold")
pieChartDataSet.sliceSpace = 3
pieChartDataSet.selectionShift = 5
var colors: [UIColor] = []
for _ 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)
}
pieChartDataSet.colors = colors
let pieChartData = PieChartData(dataSet: pieChartDataSet)
let pFormatter = NumberFormatter()
pFormatter.numberStyle = .percent
pFormatter.maximumFractionDigits = 1
pFormatter.multiplier = 1
pFormatter.percentSymbol = " %"
pieChartData.setValueFormatter(DefaultValueFormatter(formatter: pFormatter))
pieChartData.setValueFont(UIFont(name: "HelveticaNeue-Light", size: 11)!)
pieChartData.setValueTextColor(.white)
self.pieChartView.data = pieChartData
self.pieChartView.setNeedsDisplay()
self.pieChartView.animate(xAxisDuration: 2.0,easingOption: .easeOutElastic)
}
并从 viewDidLoad 粘贴以下
let months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun"]
let unitsSold = [20.0, 4.0, 6.0, 3.0, 12.0, 16.0]
setCharts(dataPoints: months, values: unitsSold)
输出:
编辑 2
var attrString: NSMutableAttributedString?
let paragraphStyle = NSParagraphStyle.default.mutableCopy() as! NSMutableParagraphStyle
paragraphStyle.lineBreakMode = NSLineBreakMode.byWordWrapping
paragraphStyle.alignment = .center
attrString = NSMutableAttributedString(string: "HEALTH SCORE HEALTH SCORE HEALTH SCORE HEALTH SCORE")
attrString?.setAttributes([
NSAttributedStringKey.foregroundColor: NSUIColor.black,
NSAttributedStringKey.font: NSUIFont.systemFont(ofSize: 12.0),
NSAttributedStringKey.paragraphStyle: paragraphStyle
], range: NSMakeRange(0, attrString!.length))
pieChartView.centerAttributedText = attrString
希望对你有帮助
关于ios - 饼图文本显示不正确?,我们在Stack Overflow上找到一个类似的问题:
https://stackoverflow.com/questions/49357373/
|