Y 轴是重复值 - iOS 图表

Y-axis is repeating values - iOS Charts

iOS Chart Framework the y-values in the chart are repeating. As shown in the picture below there are multiple 0's and 1's in the chart. 中,无论如何我都可以自定义此图表,以便没有任何重复的 y 值。因此,在这种情况下,理想情况下 y 值应为 1、2、3、4、5。我只是不想让 y 值重复。

这是因为 y 轴值当前具有介于 0 和 1 之间的小数值。这些数字实际上是 0、0.2、0.4、0.6、0.8 和 1。要正确格式化它们,请尝试修改 valueFormatterChartYAxis:

let yAxis = chart.leftAxis

let yAxisValueFormatter = NSNumberFormatter()
yAxisValueFormatter.usesSignificantDigits = true  // This will tell the formatter to display the decimal places
yAxis.valueFormatter = yAxisValueFormatter

y轴的当前范围是0到1,有6个标签。如果您添加大于 1 的值,则范围将自动调整。如果当前范围不合适,您还可以使用 customAxisMax 强制最大值。

你也可以试试:

/// When true, axis labels are controlled by the `granularity` property.
/// When false, axis values could possibly be repeated.
/// This could happen if two adjacent axis values are rounded to same value.
/// If using granularity this could be avoided by having fewer axis values visible.
public var granularityEnabled = false

并设置 yAxis.granularity = 一些值