美文网首页
iOS Charts库的简单使用 - PieChartView

iOS Charts库的简单使用 - PieChartView

作者: gaookey | 来源:发表于2020-10-20 20:27 被阅读0次

    iOS Charts库的简单使用 - BarChartView

    iOS Charts库的简单使用 - PieChartView

    iOS Charts库的简单使用 - LineChartView

    iOS Charts库的简单使用 - CombinedChartView

    Charts 库各类之间属性基本互通,可相互参考,更多属性参看 iOS Charts库的简单使用 - BarChartView

    更多使用方法及属性参考 MPAndroidChart 文档:MPAndroidChart Documentation

    最后附上 demo,demo有个别错误未修改,请甄别。




    PieChartView 的部分属性及简单使用

    开局一张简单的饼图

    let pieChartView = PieChartView(frame: CGRect(x: 0, y: 300, width: UIScreen.main.bounds.width, height: 200))
    view.addSubview(pieChartView)
    
    let datas: [Double] = [32, 12, 65, 23, 50]
    
    var values = [PieChartDataEntry]()
    for i in 0..<datas.count {
        values.append(PieChartDataEntry(value: datas[i], label: "\(datas[i])"))
    }
    
    let dataSet = PieChartDataSet(entries: values, label: "图例")
    dataSet.colors = ChartColorTemplates.vordiplom()
    
    let data = PieChartData(dataSet: dataSet)
    
    pieChartView.data = data
    
    image.png

    属性

    //是否根据所提供的数据, 将显示数据转换为百分比格式。默认false
    pieChartView.usePercentValuesEnabled = true
    
    //空心半径占比。默认0.5
    pieChartView.holeRadiusPercent = 0.2
    
    //半透明空心半径占比。默认0.55
    pieChartView.transparentCircleRadiusPercent = 0.55
    //半透明空心的颜色。
    pieChartView.transparentCircleColor = .red
    
    //是否允许拖拽旋转。默认true
    pieChartView.rotationEnabled = true
    
    pieChartView.dragDecelerationEnabled = true
    //拖拽旋转后惯性系数0-1。默认0.9
    pieChartView.dragDecelerationFrictionCoef = 0.9
    
    //设置为false,禁止点击手势高亮显示值,值仍然可以通过拖动或编程方式突出显示。默认true
    pieChartView.highlightPerTapEnabled = true
    
    //标记,指示是否应该绘制入口标签。默认 YES
    pieChartView.drawEntryLabelsEnabled = true
    //条目标签的颜色。
    pieChartView.entryLabelColor = .red
    //条目标签字体大小。
    pieChartView.entryLabelFont = .systemFont(ofSize: 15)
    
    image.png
    //饼状图是否是空心.默认YES
    pieChartView.drawHoleEnabled = true
    //空心颜色。默认white
    pieChartView.holeColor = .cyan
    
    //是否显示中心文字。默认YES
    pieChartView.drawCenterTextEnabled = true
    //中心文字
    pieChartView.centerText = "中心文字"
    let centerAttributedText = NSMutableAttributedString(string: "中心文字")
    centerAttributedText.setAttributes([NSAttributedString.Key.foregroundColor : UIColor.red], range: NSRange(location: 0, length: "中心".count))
    //中心文字 富文本
    pieChartView.centerAttributedText = centerAttributedText
    //中心文字的偏移量
    pieChartView.centerTextOffset = CGPoint(x: 0, y: 0)
    //中心文字显示的半径百分比。默认1.0
    pieChartView.centerTextRadiusPercent = 0.5
    
    image.png
    //饼图显示的最大角度。默认360
    pieChartView.maxAngle = 180
    //饼图显示时旋转的角度。默认270
    pieChartView.rotationAngle = 180
    
    image.png
    //饼图周围的最小偏移量,影响饼图大小。默认0
    pieChartView.minOffset = 0
    
    //是否双指拖拽旋转,值为true时则单指无法拖拽旋转。默认false
    pieChartView.rotationWithTwoFingers = false
    
    //是否显示图例
    pieChartView.legend.enabled = true
    //图例在饼状图中的大小占比。默认0.95
    pieChartView.legend.maxSizePercent = 0.95
    //图例文本间隔。默认5.0
    pieChartView.legend.formToTextSpace = 15.0
    //图例字体大小。默认10.0
    pieChartView.legend.font = .systemFont(ofSize: 10)
    //图例字体颜色间隔。默认black
    pieChartView.legend.textColor = .red
    //图例在饼状图中水平方向的位置。默认left
    pieChartView.legend.horizontalAlignment = .left
    //图例在饼状图中垂直方向的位置。默认bottom
    pieChartView.legend.verticalAlignment = .bottom
    //图例排列方向。默认horizontal
    pieChartView.legend.orientation = .horizontal
    //图示样式。默认square
    pieChartView.legend.form = .square
    //图示大小。默认8.0
    pieChartView.legend.formSize = 8
    
    PieChartDataSet 属性
    //是否显示数值。默认true
    dataSet.drawValuesEnabled = true
    //文字显示位置。默认insideSlice
    dataSet.xValuePosition = .outsideSlice
    //文字显示位置。默认insideSlice
    dataSet.yValuePosition = .outsideSlice
    //数值文字大小
    dataSet.valueFont = .systemFont(ofSize: 15)
    dataSet.valueTextColor = .red
    //文字大小
    dataSet.entryLabelFont = .systemFont(ofSize: 20)
    //文字颜色
    dataSet.entryLabelColor = .orange
    
    //yValuePosition == .outsideSlice 时生效,指示线颜色和饼图切片颜色相同。默认false
    dataSet.useValueColorForLine = false
    //yValuePosition == .outsideSlice 时生效,指示线颜色。抵消useValueColorForLine。默认black
    //dataSet.valueLineColor = .red
    //yValuePosition == .outsideSlice 时生效,指示线宽度。默认1.0
    dataSet.valueLineWidth = 2
    //yValuePosition == .outsideSlice 时生效,指示线偏移量占饼图百分比。默认0.75
    dataSet.valueLinePart1OffsetPercentage = 0.75
    //yValuePosition == .outsideSlice 时生效,指示线前半部分长度占饼图百分比。默认0.3
    dataSet.valueLinePart1Length = 0.3
    //yValuePosition == .outsideSlice 时生效,指示线后半部分长度占饼图百分比。默认0.4
    dataSet.valueLinePart2Length = 0.4
    //yValuePosition == .outsideSlice 时生效,指示线后半部分长度占饼图百分比。默认0.4
    //yValuePosition == .outsideSlice 时生效,旋转饼图时,是否动态改变指示线的长度。默认true
    dataSet.valueLineVariableLength = true
    //饼图选中后的颜色
    dataSet.highlightColor = .purple
    //饼图距离边距距离。默认18
    dataSet.selectionShift = 18
    //饼片之间的距离。默认0.0
    dataSet.sliceSpace = 5
    //各饼片颜色
    dataSet.colors = ChartColorTemplates.vordiplom()
    
    //默认NO
    dataSet.automaticallyDisableSliceSpacing = false
    
    image.png

    最后附上 demo

    相关文章

      网友评论

          本文标题:iOS Charts库的简单使用 - PieChartView

          本文链接:https://www.haomeiwen.com/subject/oqtjmktx.html