Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pie Chart label not show if its value 0 #3473

Closed
devssun opened this issue May 29, 2018 · 2 comments
Closed

Pie Chart label not show if its value 0 #3473

devssun opened this issue May 29, 2018 · 2 comments

Comments

@devssun
Copy link

devssun commented May 29, 2018

What did you do?

I made PieChart and it show great, but if value 0 then pie chart's label show overlapping.
I already use ValueFormatter() so if value 0 then not show value label.

let pFormatter = NumberFormatter()
pFormatter.numberStyle = .none
pFormatter.maximumFractionDigits = 1
pFormatter.multiplier = 1
pFormatter.zeroSymbol = ""
pieChartData.setValueFormatter(DefaultValueFormatter(formatter: pFormatter))
pieChartData.setValueFont(.systemFont(ofSize: 11))
pieChartData.setValueTextColor(.black)

how can I hidden label if its value 0, and show all legend

What did you expect to happen?

it its value 0, then hidden pie chart label. but show all legend

What happened instead?

I can't hidden label

Charts Environment

Charts version/Branch/Commit Number: 3.0.3
Xcode version: 9.2
Swift version: 3.2
Platform(s) running Charts: iOS 10, 11
macOS version running Xcode: 10.12.6

Demo Project

ℹ Please link to or upload a project we can download that reproduces the issue.

func setPieCharts(dataPoints: [String], values: [Double]) -> PieChartData {
        var dataEntries: [ChartDataEntry] = []
        
        for i in 0..<dataPoints.count {
            if values[i] == 0.0 {
//                continue
            }
            let dataEntry = PieChartDataEntry(value: values[i], label: dataPoints[i])
            dataEntries.append(dataEntry)
        }
        
        let pieChartDataSet = PieChartDataSet(values: dataEntries, label: nil)
        pieChartDataSet.colors = tempColor()
        
        if dataEntries.count > 3 {
            pieChartDataSet.xValuePosition = .outsideSlice
            pieChartDataSet.yValuePosition = .outsideSlice
        }else{
            pieChartDataSet.xValuePosition = .insideSlice
            pieChartDataSet.yValuePosition = .insideSlice
        }
        
        let pieChartData = PieChartData(dataSet: pieChartDataSet)
        
        
        let pFormatter = NumberFormatter()
        pFormatter.numberStyle = .none
        pFormatter.maximumFractionDigits = 1
        pFormatter.multiplier = 1
        pFormatter.zeroSymbol = ""
        pieChartData.setValueFormatter(DefaultValueFormatter(formatter: pFormatter))
        pieChartData.setValueFont(.systemFont(ofSize: 11))
        pieChartData.setValueTextColor(.black)
        return pieChartData
    }
@devssun devssun changed the title Pie Chart label not show it its value 0 Pie Chart label not show if its value 0 May 29, 2018
@cwestMobile
Copy link

I also would like to know the resolution to this. Reminding myself :)

@devssun
Copy link
Author

devssun commented Jun 1, 2018

Hello @cwestMobile I solved it use trick.
got server data and make CustomLegend use LegendEntry then key with value 0 make empty string.

like this

  1. make CustomLegend Array
    func setCustomLegend(dataPoints: [String], values: [Double]) -> [LegendEntry] {
        var customLegendEntries: [LegendEntry] = []
        for i in 0..<dataPoints.count {=
            customLegendEntries.append(LegendEntry(label: dataPoints[i], form: .default, formSize: CGFloat.nan, formLineWidth: CGFloat.nan, formLineDashPhase: 0.0, formLineDashLengths: nil, formColor: barColor[i]))
        }
        return customLegendEntries
    }
  1. setting pie chart data
func setPieCharts(dataPoints: [String], values: [Double]) -> PieChartData {
   var dataEntries: [ChartDataEntry] = []
   for i in dataPoints.count {
        let dataEntry = PieChartDataEntry(value: values[i], label: values[i] == 0.0 ? "" : dataPoints[i])
            dataEntries.append(dataEntry)
        }
  ~~~~~
  return chartData
}
  1. setting pie chart custom legend and data
chart.data = setPieCharts(dataPoints: (showChartData["key1"]?.displayName)!, values: (showChartData["key1"]?.displayCount)!)
 let chartLegend = setCustomLegend(dataPoints: (showChartData["key2"]?.displayName)!, values: (showChartData["key2"]?.displayCount)!)
let l1 = chart.legend
l1.setCustom(entries: chartLegend)

check it out! 😸

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants