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

Charts 3.0 labelCount bug? #1767

Closed
Asavarkhul opened this issue Nov 2, 2016 · 4 comments
Closed

Charts 3.0 labelCount bug? #1767

Asavarkhul opened this issue Nov 2, 2016 · 4 comments

Comments

@Asavarkhul
Copy link

Asavarkhul commented Nov 2, 2016

When we want to display only 1 xAxis label, the labelCount force to show multiple label but we want to display only one line..

For example :

if labelCount = 2 (default minimum value)

capture d ecran 2016-11-02 a 11 44 25

But if we force this to 3 for example

capture d ecran 2016-11-02 a 11 44 33

Is this behavior a bug? or do we have miss something?

Here is our code :

var yVals = [BarChartDataEntry]()
var xVals = [String]()
            
if chartDataCompanies.count > 0 {
   let maxDataEntry: Int = 15
                
   //sort
   var chartDataCompanies = chartDataCompanies.values.filter({$0.solvedRate > 0.0 && $0.issuesCount > 0}).sorted(by: {$0.solvedRate > $1.solvedRate})
                
   //subArray
   let index = min(maxDataEntry, chartDataCompanies.count)
   let companies = chartDataCompanies[0..<index]
                
   for (index, data) in companies.enumerated() {
      yVals.append(BarChartDataEntry(x: Double(index), y: data.solvedRate))
      xVals.append(data.descriptionString)
   }
                
   let barDataSet = BarChartDataSet(values: yVals, label: nil)
   barDataSet.drawValuesEnabled = false
   barDataSet.colors =  PreferredChartsColors
                
   let chartData = BarChartData(dataSets: [barDataSet])
   chartData.setValueFont(UIFont.preferredFont(forTextStyle: UIFontTextStyle.subheadline))
   reloadChartViewWithData(chartData)
                
   self.chartView.xAxis.labelCount = xVals.count
   self.chartView.xAxis.valueFormatter = DefaultAxisValueFormatter { (value, axis) -> String in
      return xVals[Int(value)]
   }
} else {
   let barDataSet = BarChartDataSet(values: yVals, label: nil)
   let chartData = BarChartData(dataSets: [barDataSet])
   reloadChartViewWithData(chartData)
}
@liuxuan30
Copy link
Member

looks like you don't turn on axis.forceLabelsEnabled.
BTW, look at computeAxisValues(min yMin: Double, max yMax: Double) and you will know how it works.

@Asavarkhul
Copy link
Author

@liuxuan30 Hello, can you try with this example project https://github.com/Asavarkhul/ChartsSampleProject this simply show how we implement it and show directly the bug

@liuxuan30
Copy link
Member

liuxuan30 commented Nov 11, 2016

I can' debug your code because you don't have Charts source code. But I do tried it in ChartsDemo - horizontal bar chart, it works I think. Would you try it first?

Some thing you could try:
double barWidth = 9.0;
double spaceForBar = 10.0;

Also, I am wrong about labelCount when you set it to 1: it's range is 2 - 25.

And:
Be careful calling below two func, one enables forceLabelsEnabled and the one ther one not:

    open var labelCount: Int
    {
        get
        {
            return _labelCount
        }
        set
        {
            _labelCount = newValue

            if _labelCount > 25
            {
                _labelCount = 25
            }
            if _labelCount < 2
            {
                _labelCount = 2
            }

            forceLabelsEnabled = false
        }
    }

    open func setLabelCount(_ count: Int, force: Bool)
    {
        self.labelCount = count
        forceLabelsEnabled = force
    }

One thing to mention:
Set chartView.data = data at last after you configure your chart, or call notifyDataSetChanged manually. You put it in a thread, which may cause side effect.

@liuxuan30
Copy link
Member

liuxuan30 commented Nov 11, 2016

OK I found what's your problem:
you don't set granularity and you only have 1 x which is 0 - an embrassing number, so it calculates the x range is [-0.4, -0.2, 0, 0.2, 0.4], so it has several labels, but you format them as one label. That's what granularity is supposed to solve.

You should set granularity like to 1, then you only get 1 label
Don't forget to change barWidth, so you bar don't seem too wide.

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

2 participants