快速设置图像中心饼图

时间:2017-08-21 08:36:37

标签: ios swift charts

现在我使用Charts

中的饼图

我想将图像添加到图表的中心而不是饼图。

image

我该怎么办?

代码

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

2 个答案:

答案 0 :(得分:1)

请试试这个:

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

pieChartView.centerAttributedText = labelImg

答案 1 :(得分:0)

试试这个

@IBOutlet weak var chartView: PieChartView!
@IBOutlet weak var image: UIImageView!
...

image.center = chartView.center