简体   繁体   English

ios 图表 - 寻找在 y 轴上制作自定义间隔的方法 - swift

[英]ios charts - Looking for a way to make custom intervals on the y-axis - swift

I'm looking for a way to set custom intervals on the y-axis in ios charts.我正在寻找一种在 ios 图表中设置自定义间隔的方法。 So, instead of the precalculated interval of 100, I'm looking for a way to change the step to 60.因此,我正在寻找一种将步长更改为 60 的方法,而不是预先计算的间隔 100。

Instead of this:而不是这个: 在此处输入图像描述

I'm looking for this:我正在寻找这个: 在此处输入图像描述

I solved my own problem by using a YAxisRenderer .我通过使用YAxisRenderer解决了我自己的问题。 I set up a custom YAxisRenderer and changed the computeAxisValues function to suit my own needs.我设置了一个自定义YAxisRenderer并更改了computeAxisValues function 以满足我自己的需要。 Here is my code:这是我的代码:

import Charts

class TimelineYAxisRender: YAxisRenderer{
 /// Sets up the axis values. Computes the desired number of labels between the two given extremes.
    @objc open override func computeAxisValues(min: Double, max: Double)
    {
        super.computeAxisValues(min: min, max: max)

        guard let axis = self.axis else { return }

        let labelCount = axis.labelCount
        let range = max - min

        // Ensure stops contains at least n elements.
        axis.entries.removeAll(keepingCapacity: true)
        axis.entries.reserveCapacity(labelCount)

        let lowestHour = Int((min/60).rounded(.down))
        let highestHour = Int((max/60).rounded(.up))

        let rangeHour = highestHour - lowestHour

        switch range {
        case 160..<600:
            for i in 0...2 * rangeHour{
                axis.entries.append(Double(i) * 30 + Double(lowestHour * 60))
            }
        case 50..<160:
            for i in 0...4 * rangeHour{
                axis.entries.append(Double(i) * 15 + Double(lowestHour * 60))
            }
        case 15..<50:
            for i in 0...12 * rangeHour{
                axis.entries.append(Double(i) * 5 + Double(lowestHour * 60))
            }
        case 0..<15:
            for i in 0...60 * rangeHour{
                axis.entries.append(Double(i) + Double(lowestHour * 60))
            }
        default:
            for i in 0...8{
                axis.entries.append(Double(i) * 180)
            }
        }

    }
}

My code to set the left y-axis to the new YAxisRenderer :我将左 y 轴设置为新YAxisRenderer的代码:

let transformer = barChart.getTransformer(forAxis:.left)
let viewPortHandler = barChart.leftYAxisRenderer.viewPortHandler
barChart.leftYAxisRenderer = TimelineYAxisRender(viewPortHandler: viewPortHandler, yAxis: barChart.leftAxis, transformer: transformer)

I used a rounding system so I would only load the labels for the part of the graph the user can see, as loading every label would take up too much memory and cause the simulator to stutter.我使用了舍入系统,因此我只会加载用户可以看到的图形部分的标签,因为加载每个 label 会占用太多 memory 并导致模拟器结结巴巴。 I also used the max - min to determine what labels to load to create a smooth zoom.我还使用了 max - min 来确定要加载哪些标签以创建平滑缩放。 When the new labels are loaded can be changed by changing the parameters for the switch cases, and the amount of zoom can be changed by changing what is actually appended to axis.entries .加载新标签的时间可以通过更改 switch case 的参数来更改,并且可以通过更改实际附加到axis.entries的内容来更改缩放量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM