iOS图表:值实际上是哪里?

发布于 2025-01-19 19:20:41 字数 1411 浏览 2 评论 0原文

我有以下 Xaxis 格式化程序:

extension ChartXAxisFormatter: IAxisValueFormatter {

    func stringForValue(_ value: Double, axis: AxisBase?) -> String {
        guard let referenceTimeInterval = referenceTimeInterval,
        let selectedFrame = selectedFrame
        else {
            return ""
        }
        
        let date = Date(timeIntervalSince1970: value * 3600 * 24 + referenceTimeInterval)
        
        if(selectedFrame == .week) {
            let dateFormatter = DateFormatter()
            dateFormatter.dateFormat = "E"
            return dateFormatter.string(from: date).capitalized
        }
        else if(selectedFrame == .month) {
            let dateFormatter = DateFormatter()
            dateFormatter.dateFormat = "dd"
            return dateFormatter.string(from: date).capitalized
        }
        else {
            let dateFormatter = DateFormatter()
            dateFormatter.dateFormat = "MMM"
            return dateFormatter.string(from: date).capitalized
        }
    }

}

我这样称呼它: xAxis.valueFormatter = ChartXAxisFormatter(referenceTimeInterval: referenceTimeInterval, selectedFrame: selectedFrame)

但是,这只是格式化值,它实际上并没有决定x 轴中出现哪些值。

当我运行它时,我得到类似的结果: 5 5 6 6 6 7

我可以通过设置 xAxis.capsularity = 1 来解决这个问题,但是我得到 5 6 而没有 7。

实际上决定出现什么值的是什么?我怎样才能获得唯一的值?

例如,如果我有权访问它使用的值,我可以调用类似 Array(Set(array)) 的内容

I have the following Xaxis formatter:

extension ChartXAxisFormatter: IAxisValueFormatter {

    func stringForValue(_ value: Double, axis: AxisBase?) -> String {
        guard let referenceTimeInterval = referenceTimeInterval,
        let selectedFrame = selectedFrame
        else {
            return ""
        }
        
        let date = Date(timeIntervalSince1970: value * 3600 * 24 + referenceTimeInterval)
        
        if(selectedFrame == .week) {
            let dateFormatter = DateFormatter()
            dateFormatter.dateFormat = "E"
            return dateFormatter.string(from: date).capitalized
        }
        else if(selectedFrame == .month) {
            let dateFormatter = DateFormatter()
            dateFormatter.dateFormat = "dd"
            return dateFormatter.string(from: date).capitalized
        }
        else {
            let dateFormatter = DateFormatter()
            dateFormatter.dateFormat = "MMM"
            return dateFormatter.string(from: date).capitalized
        }
    }

}

And I call it like so: xAxis.valueFormatter = ChartXAxisFormatter(referenceTimeInterval: referenceTimeInterval, selectedFrame: selectedFrame)

However, this is just formatting the values, it isn't actually deciding what values appear in the xAxis.

When I run it, I get something like: 5 5 6 6 6 7

I can fix this by setting xAxis.granularity = 1 but I get 5 6 without the 7.

What is actually deciding what values appear? How can I get just the unique values?

For example if I had access to what values it uses I can call something like Array(Set(array))

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

脱离于你 2025-01-26 19:20:41

请参阅图表库中的xaxisRenderer。它具有computeaxisvalues函数,该功能实际上计算出显示在轴上的值。 条目轴的数组存储将要显示的值。

如果要格式化它,则必须对xaxisrenderer进行划分并覆盖computeaxisvalues函数

Refer to XAxisRenderer in the Charts library. It has a computeAxisValues function that actually calculates the values that appear on the axis. The entries array of the axis stores the values that will be displayed.

If you want to format it, you'll have to subclass the XAxisRenderer and override the computeAxisValues function

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文