3

I'm making a chart by using chart iOS framework. but the value will overlay when the slice is tiny. How can I hide it? This question is similar to this GitHub link, But I don't understand how it works. Do I just need to add the code in my View Controller or drag the PieChartRenderer.swift file to my project?

Can someone explain to me how to use the pull request or some open public function...

Sorry I'm new in iOS framework.

enter image description here

This is my code.

@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)
        }...
 }
Lester
  • 701
  • 1
  • 9
  • 47
  • Check this pull request out: https://github.com/danielgindi/Charts/pull/2062 – Christoph Nov 24 '17 at 12:19
  • I don't understand need to use which part of code because I keep getting error, like `have no member ` . how to use it properly? @Christoph – Lester Nov 24 '17 at 12:26

2 Answers2

3

Create a custom formatter, I set the minNumber as 10.0 and the empty string is returned when a value is less than the minNumber, otherwise the value is returned.

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) + "%"
        }
    }

}    

Then make sure you set the totalValue variable, store it in UserDefaults (to make it possible to access it in the formatter) and set the formatter for your graph

    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:

Result

DevB2F
  • 4,674
  • 4
  • 36
  • 60
  • Oh thanks! I can generate the value to percentage. How about hiding the label? I can't use this to disable `chartView.drawEntryLabelsEnabled = false` @DevB2F – Lester Nov 25 '17 at 04:25
  • I don’t understand the point of hiding the label. Do you still see some part of the label when the label text is the empty string? – DevB2F Nov 25 '17 at 04:46
  • The code is hiding the percentage value only, the label below the percentage (screenshot) still there and overlapping each other. @DevB2F – Lester Nov 25 '17 at 04:54
  • Ok, I updated the question, is that enough? If not I try to upload my project zip @DevB2F – Lester Nov 25 '17 at 05:33
  • Here is my [Project Zip Link](https://www.dropbox.com/s/afdgbakrocn9ygj/Finnciti.zip?dl=0), Check PieChartVC @DevB2F – Lester Nov 25 '17 at 05:53
  • No Idea? Any suggestion? @DevB2F – Lester Nov 26 '17 at 06:44
  • Try turning off everything related to percent to make sure no % sign is shown. In the custom formatter add the % behind the correct value string. – DevB2F Nov 26 '17 at 06:48
  • I think this is not the good way, because when the overall value is small than 10, then it will also hide the value when the slice is not tiny. have any function that can count the label and slice size then hide the value and label? @DevB2F – Lester Nov 26 '17 at 15:04
  • Oh, Thanks! I figured out how to hide the label also, copy the same format on `valueAndColor()`, like this `if(valueToUse < minNumber) {dataEntries[i].label = ""}` – Lester Nov 27 '17 at 05:12
  • Can I ask one more thing? How to hide the value line? if I add this code `set.valueLineWidth = 0.0`, it will hide all the value line.. @DevB2F – Lester Nov 27 '17 at 08:49
  • You should ask a new question – DevB2F Nov 27 '17 at 15:00
  • Ok, this is the link: https://stackoverflow.com/questions/47507228/allow-piechartview-to-hide-value-line-for-tiny-slices-in-swift. @DevB2F – Lester Nov 27 '17 at 15:04
0

Newer versions of the Charts library have added this feature and made it a simple property to set on the instance of the PieChartView:

pieChart.sliceTextDrawingThreshold = 20

The sliceTextDrawingThreshold property sets the minimum angle that is required for a label to be drawn.

Ryan Grimm
  • 1,972
  • 1
  • 11
  • 7