允许PieChartView隐藏Swift中的小切片标签

时间:2017-11-24 10:10:43

标签: swift ios-charts

我使用图表iOS框架制作图表。但是当切片很小时,该值将重叠。我怎么能隐藏它?这个问题类似于GitHub link,但我不明白它是如何运作的。我只需要在View Controller中添加代码或将PieChartRenderer.swift文件拖到我的项目中吗?

有人可以向我解释如何使用拉取请求或某些开放的公共功能......

抱歉,我是iOS框架中的新手。

enter image description here

这是我的代码。

@IBOutlet weak var myChart: PieChartView!

var valueColors = [UIColor]()
var dataEntries = [PieChartDataEntry]()
var record = [Record]()
var category = [String]()
var categoryTotal : [Double] = []
var categoryArray : [String] = []

func setDataCount() {
    valueAndColor()

    let set = PieChartDataSet(values: dataEntries, label: nil)

    set.colors = valueColors
    set.valueLinePart1OffsetPercentage = 0.8
    set.valueLinePart1Length = 0.2
    set.valueLinePart2Length = 0.4
    set.xValuePosition = .outsideSlice
    set.yValuePosition = .outsideSlice
    set.selectionShift = 0.0

    let data = PieChartData(dataSet: set)
    let Formatter:ChartFormatter = ChartFormatter()
    data.setValueFormatter(Formatter)
    data.setValueFont(.systemFont(ofSize: 11, weight: .light))
    data.setValueTextColor(.black)

    myChart.data = data
    myChart.highlightValues(nil)
}

func setup(pieChartView chartView: PieChartView) {
    chartView.usePercentValuesEnabled = true
    chartView.drawSlicesUnderHoleEnabled = true
    chartView.holeRadiusPercent = 0.58
    chartView.chartDescription?.enabled = false
    chartView.drawCenterTextEnabled = true
    chartView.centerAttributedText = attributedString;
    chartView.drawHoleEnabled = true
    chartView.rotationAngle = 0
    chartView.rotationEnabled = true
    chartView.highlightPerTapEnabled = true
}

func valueAndColor(){
    for i in 0..<categoryArray.count{
        let dataEntry = PieChartDataEntry(value: categoryTotal[i], label: categoryArray[i % categoryArray.count])
        dataEntries.append(dataEntry)

        if categoryArray[i] == "吃喝" {
            valueColors.append(UIColor.yellow)
        }else if categoryArray[i] == "交通"{
            valueColors.append(UIColor.red)
        }...
 }

1 个答案:

答案 0 :(得分:2)

创建自定义格式化程序,我将minNumber设置为10.0,当值小于minNumber时返回空字符串,否则返回值。

public class ChartFormatter: NSObject, IValueFormatter{

    public func stringForValue(_ value: Double, entry: ChartDataEntry, dataSetIndex: Int, viewPortHandler: ViewPortHandler?) -> String {

        let total = UserDefaults.standard.double(forKey: "totalValue")

        var valueToUse = value/total * 100
        valueToUse = Double(round(10*valueToUse)/10)
        print("valueToUse: \(valueToUse)")
        let minNumber = 10.0

        if(valueToUse<minNumber) {
            return ""
        }
        else {
            return String(valueToUse) + "%"
        }
    }

}    

然后确保设置totalValue变量,将其存储在UserDefaults中(以便可以在格式化程序中访问它)并设置图形的格式化程序

    var totalValue = 0.0

    let units = [10.0, 4.0, 6.0, 3.0, 12.0, 16.0]
    for a in units {
        totalValue += a
    }
    UserDefaults.standard.set(totalValue, forKey: "totalValue")

    let formatter:ChartFormatter = ChartFormatter()

    data.setValueFormatter(formatter)

结果:

Result