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

Charts3.0 - Can not show all the xAxis label if more than 8 value - Swift #1938

Closed
Canmmy opened this issue Dec 8, 2016 · 2 comments
Closed

Comments

@Canmmy
Copy link

Canmmy commented Dec 8, 2016

I had create a bar chart which had the 9 categories label for the xAxis: ["Foods", "House Loan", "Insurance", "Petrol", "Saving", "Train", "Others", "Books & Magazine", "Car park"]

But in the charts it just show: Foods, Insurance, Saving, Others and Car park

Any one know how to solve it?

Here the source code for setting xAxis


 public func stringForValue(_ value: Double, axis: AxisBase?) -> String {
        return self.categories[Int(value)]
    }

func setChart(dataPoints: [String], values: [Double]) {       
     var dataEntries: [BarChartDataEntry] = []

     for i in 0..<dataPoints.count {

         let value = values[i] as Double
         let dataEntry = BarChartDataEntry(x: Double(i), y:value)
         dataEntries.append(dataEntry)
     }

     let chartDataSet = BarChartDataSet(values: dataEntries, label: "Total Amount")

     var colors: [UIColor] = []
     for _ in 0..<dataPoints.count {
         let red = Double(arc4random_uniform(256))
         let green = Double(arc4random_uniform(256))
         let blue = Double(arc4random_uniform(256))

         let color = UIColor(red: CGFloat(red/255), green: CGFloat(green/255), blue: CGFloat(blue/255), alpha: 1)
         colors.append(color)
     }

     chartDataSet.colors = colors
     chartDataSet.colors = ChartColorTemplates.joyful()
     barChartView.chartDescription?.text = ""

     barChartView.xAxis.labelPosition = .bottom
     barChartView.xAxis.drawGridLinesEnabled = false
     barChartView.xAxis.granularityEnabled = true
     barChartView.xAxis.granularity = 1.0
     barChartView.xAxis.decimals = 0

     let chartData = BarChartData()
     chartData.addDataSet(chartDataSet)
     barChartView.data = chartData
     barChartView.xAxis.valueFormatter = self

     let format = NumberFormatter()
     format.numberStyle = .decimal
     let formatter = DefaultValueFormatter(formatter: format)
     chartData.setValueFormatter(formatter)
     barChartView.animate(xAxisDuration: TimeInterval(1))
}
@liuxuan30
Copy link
Member

liuxuan30 commented Dec 9, 2016

You have to consider labels overlap first..
And look at computeAxisValues() in x axis renderer to know how it calculate interval.

@Canmmy
Copy link
Author

Canmmy commented Dec 13, 2016

No, I just need to set labelCount only

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