饼图文字显示不正确?

时间:2018-03-19 07:01:03

标签: ios swift pie-chart

我编写了以下代码,但饼图显示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

1 个答案:

答案 0 :(得分:0)

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步:在故事板中选择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)

输出

enter image description here

编辑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

希望它有用