如何在图表iOS中设置自定义X轴0标签 [英] How to set a custom x axis 0 label in charts ios

查看:386
本文介绍了如何在图表iOS中设置自定义X轴0标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Daniel Gindi的图表

如何在x轴上设置String 0标签?我希望它显示现在"而不是日期类型. 我已经看到了仅有助于格式化一个特定标签的功能,但是我无法理解它是如何工作的.而且,我还没有看到任何示例.那么,getFormattedLabel(index: Int)如何工作? 这就是我需要的xAxis外观:

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:

谢谢!

推荐答案

这是一个代码段,可以帮助我将X轴转换为时间轴,还可以使图表上的最后一个标签成为String类型.

This is a snippet that helps me convert an X axis into timeline and also make the last label on the chart of a String type.

index == count - 2是因为通过强制将X轴上的标签计数设置为3:

index == count - 2 was used because the label count on X axis was set to 3 by force:

chart.xAxis.setLabelCount(3, force: true)
chart.xAxis.avoidFirstLastClippingEnabled = false
chart.xAxis.forceLabelsEnabled = true

class ChartXAxisFormatter: NSObject, IAxisValueFormatter {

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

        if let index = axis?.entries.firstIndex(of: value), let count = axis?.entries.count  , index == count - 2 {
            return "Now"
        }

        let dateFormatterPrint = DateFormatter()
        dateFormatterPrint.dateFormat = "HH:mm:ss"

        let date = Date(timeIntervalSince1970: value)
        let time = dateFormatterPrint.string(from: date)

            return time
      }
}

这篇关于如何在图表iOS中设置自定义X轴0标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆