ios 图表 - 寻找在 y 轴上创建自定义间隔的方法 - swift
ios charts - Looking for a way to make custom intervals on the y-axis - swift
我正在寻找一种在 ios 图表的 y 轴上设置自定义间隔的方法。因此,我正在寻找一种方法将步长更改为 60,而不是预先计算的 100 间隔。
而不是这个:
我正在寻找这个:
我使用 YAxisRenderer
解决了我自己的问题。我设置了自定义 YAxisRenderer
并更改了 computeAxisValues
函数以满足我自己的需要。这是我的代码:
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)
}
}
}
}
我的代码将左侧 y 轴设置为新的 YAxisRenderer
:
let transformer = barChart.getTransformer(forAxis:.left)
let viewPortHandler = barChart.leftYAxisRenderer.viewPortHandler
barChart.leftYAxisRenderer = TimelineYAxisRender(viewPortHandler: viewPortHandler, yAxis: barChart.leftAxis, transformer: transformer)
我使用了舍入系统,所以我只会加载用户可以看到的图表部分的标签,因为加载每个标签会占用太多内存并导致模拟器卡顿。我还使用 max - min 来确定要加载哪些标签以创建平滑缩放。加载新标签的时间可以通过更改开关盒的参数来更改,缩放量可以通过更改实际附加到 axis.entries
.
的内容来更改
我正在寻找一种在 ios 图表的 y 轴上设置自定义间隔的方法。因此,我正在寻找一种方法将步长更改为 60,而不是预先计算的 100 间隔。
而不是这个:
我正在寻找这个:
我使用 YAxisRenderer
解决了我自己的问题。我设置了自定义 YAxisRenderer
并更改了 computeAxisValues
函数以满足我自己的需要。这是我的代码:
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)
}
}
}
}
我的代码将左侧 y 轴设置为新的 YAxisRenderer
:
let transformer = barChart.getTransformer(forAxis:.left)
let viewPortHandler = barChart.leftYAxisRenderer.viewPortHandler
barChart.leftYAxisRenderer = TimelineYAxisRender(viewPortHandler: viewPortHandler, yAxis: barChart.leftAxis, transformer: transformer)
我使用了舍入系统,所以我只会加载用户可以看到的图表部分的标签,因为加载每个标签会占用太多内存并导致模拟器卡顿。我还使用 max - min 来确定要加载哪些标签以创建平滑缩放。加载新标签的时间可以通过更改开关盒的参数来更改,缩放量可以通过更改实际附加到 axis.entries
.