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

How to set 24 hour time format in x-axis of line chart? #4302

Closed
iOSUjjwal opened this issue Feb 28, 2020 · 1 comment
Closed

How to set 24 hour time format in x-axis of line chart? #4302

iOSUjjwal opened this issue Feb 28, 2020 · 1 comment

Comments

@iOSUjjwal
Copy link

I want to show a chart like below image -

LineChart

I tried creating an array with 5 values and passed them to x axis in IndexValueFormatter, but it only prints 3 values neglecting 2 values from middle of array.

Also, how to set images 2-3 pixels apart from markers like in Image we have apple/sunrise icons?

@iOSUjjwal
Copy link
Author

I got it working by adding the below code-

Create a custom X axis formatter like below -

class xAxisFormatter : IAxisValueFormatter {

public func stringForValue(_ value: Double, axis: AxisBase?) -> String {

    switch value {
    case 0:
        return "00:00"
    case 360:
        return "06:00"
    case 720:
        return "12:00"
    case 1080:
        return "18:00"
    case 1440:
        return "23:59"
    default:
        return "00:00"
    }
}

}

Add the below lines when setting up -

    lineChartView.xAxis.axisMinimum = 0
    lineChartView.xAxis.axisMaximum = 1440
    lineChartView.xAxis.setLabelCount(5, force: true)
    
    let formatter = xAxisFormatter()
    lineChartView.xAxis.valueFormatter = formatter

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

1 participant