Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
173 views
in Technique[技术] by (71.8m points)

swift - How to set a custom x axis 0 label in charts ios

I am using Charts by Daniel Gindi

How can I set a String 0 label on x Axis? I want it to show "Now" instead of a Date type. I've seen the function that helps to format one particular label only, but I can't get how it works. Moreover, I haven't seen any examples of it. So, how does getFormattedLabel(index: Int) work? This is what I need my xAxis to look like: Example of what I need

Thank you!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can set your xAxis.valueFormatter to your own custom class and then return now when your x value is 0. Like:

class ChartValueFormatter: NSObject, IAxisValueFormatter {

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

        if value == 0 {

            return "Now"
        }

        let dateFormatter = DateFormatter()
        dateFormatter.setLocalizedDateFormatFromTemplate("dd MMM")
        dateFormatter.locale = .current

        let date = Date(timeIntervalSince1970: value)

        return dateFormatter.string(from: date)
    }
}

For this to work you need to sort your values based on date and then set then now value timestamp to zero.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...