• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

ios - 饼图文本显示不正确?

[复制链接]
菜鸟教程小白 发表于 2022-12-11 19:53:19 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

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


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)

输出:

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

希望对你有帮助

关于ios - 饼图文本显示不正确?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49357373/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap