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

IAxisValueFormatter Error: Index out of range #3422

Closed
chetanpanchal94 opened this issue Apr 25, 2018 · 2 comments
Closed

IAxisValueFormatter Error: Index out of range #3422

chetanpanchal94 opened this issue Apr 25, 2018 · 2 comments

Comments

@chetanpanchal94
Copy link

chetanpanchal94 commented Apr 25, 2018

What did you do?

I have created one IAxisValueFormatter which returns xAxis label value as string.
Chart is being displayed and xAxis labels are also being displayed using the formatter which i have created. Once chart is displayed, when I am zooming in the line chart and stack bar chart till all xAxsis labels are visible and change device orientation from portrait to landscape app crashes and gives me error index out of range

My XAxisValueFormatter

// Stores xAxis labels

var xAxisLabels = [String]()

public class XAxisValueFormatter: NSObject, IAxisValueFormatter {

    private let dateFormatter = DateFormatter()

    public func stringForValue(_ value: Double, axis: AxisBase?) -> String {
    
    return xAxisLabels[Int(value)] // error

    }
}

What did you expect to happen?

After zooming in line and stack bar chart till all its xAxis labels are visible and change device orientation from portrait to landscape chart should be displayed in landscape mode.

What happened instead?

App is crashing and gives me error on the below line
" return xAxisLabels[Int(value)] "

Solution

Need help or suggest me other IAxisValueFormatter which can be used to display xAxis label value as string

@chetanpanchal94
Copy link
Author

chetanpanchal94 commented May 29, 2018

Easy fix

var xAxisLabels = [String]()

public class XAxisValueFormatter: NSObject, IAxisValueFormatter 
{
     private let dateFormatter = DateFormatter()
     public func stringForValue(_ value: Double, axis: AxisBase?) -> String 
     {
        if(Int(value) >= xAxisLabels.count) 
        {
           return ""
        }
      return xAxisLabels[Int(value)]
   }
 }

@denys-meloshyn
Copy link

Did you have any chance to find why it is happening?

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