KBuabok KBuabok - 1 month ago 263
Swift Question

swift set image center pie chart

I am using a pie chart from

iOS-Charts


I want to add an image to the center of the chart instead of the pie chart.

image

What should I do?

This is the code I am currently using:

func updatePieChart(){
let track = ["", ""]
let money = [65, 45]
var entries = [PieChartDataEntry]()
for (index, value) in money.enumerated() {
let entry = PieChartDataEntry()
entry.y = Double(value)
entry.label = track[index]
entries.append( entry)
}
let set = PieChartDataSet( values: entries, label: "Pie Chart")
var colors: [UIColor] = []

for _ in 0..<money.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)
}
set.colors = colors
let data = PieChartData(dataSet: set)
pieview.data = data
pieview.noDataText = "No data available"
// user interaction
pieview.isUserInteractionEnabled = true

let d = Description()
d.text = "iOSCharts.io"
pieview.chartDescription = d
pieview.centerText = "Pie Chart"
pieview.holeRadiusPercent = 0.9
pieview.transparentCircleColor = UIColor.clear
pieview.holeRadiusPercent = 0.65
pieview.transparentCircleRadiusPercent = 0
pieview.legend.enabled = false
pieview.chartDescription?.enabled = false

Answer Source

Please try this :

let attachment = NSTextAttachment()
attachment.image = UIImage(named: "your image")
let attachmentString = NSAttributedString(attachment: attachment)
let labelImg = NSMutableAttributedString(string: "")
labelImg.append(attachmentString)

pieChartView.centerAttributedText = labelImg