设置 LineChartView 的 Y 轴的最大值 |吊舱 'Charts'

Set the maximum of the Y-Axis of a LineChartView | pod 'Charts'

我使用 'Charts' pod 创建了一个 LineChartView。

我已使绘制的线条遵循函数 (f(x) = ax^2),其中值 'a' 会在用户触摸 LineChartView 时立即随机化。通过 for in 循环,x 轴定义最大值为 100。然而,这导致 y 轴具有定义的最大值 a*10000,其中每次用户触摸 LineChartView 时都会随机创建 a。

这意味着实际上看起来图表根本没有变化。唯一可见的变化是每次随机创建 a 时,y 轴都会获得一个新的最大值。我想要一种设置 y 轴最大值的方法。大致如下:

chartView.YAxisMaximum = 10000

下面是完整的代码:

import UIKit
import Charts


class ViewController: UIViewController {
    
    var a: Double = 1
   
    var line = LineChartDataSet()
    let chartView = LineChartView()
    let data = LineChartData()
    
    @objc private func didSwipe(_ sender: UIPanGestureRecognizer) {
        if sender.state == .changed{
            
            a = Double.random(in: 0.0...4.0)
            
            
            data.removeDataSet(line)
            line.removeAll()
            
            line.colors = [.black]
            line.drawCirclesEnabled = false
            line.label = nil
            
            for i in 0...100{
                let value = ChartDataEntry(x: Double(i), y: Double(i*i) * a)
                line.append(value)
            }
            data.addDataSet(line)
            chartView.data = data
            
            
            
  
        }
        
    }
    

    override func viewDidLoad() {
        super.viewDidLoad()
        
        let swipe = UIPanGestureRecognizer()
        swipe.addTarget(self, action: #selector(didSwipe(_:)))
        

        
        line.colors = [.black]
        line.drawCirclesEnabled = false
        line.label = nil
        
        
        for i in 0...100{
            let value = ChartDataEntry(x: Double(i), y: Double(a) * Double(i*i))
            line.append(value)
        }
        
        data.addDataSet(line)
        
        
        chartView.data = data
        chartView.leftAxis.enabled = true
        chartView.rightAxis.enabled = false
        chartView.xAxis.enabled = false
        chartView.pinchZoomEnabled = false
        chartView.doubleTapToZoomEnabled = false
        chartView.animate(xAxisDuration: 1)
        chartView.frame = view.safeAreaLayoutGuide.layoutFrame
        chartView.addGestureRecognizer(swipe)
        
        view.addSubview(chartView)
    }
}

是的,您可以通过配置 LineChartView 实例的 leftAxisrightAxis 属性来设置 y 轴的最大值,如下所示。

let chartView = LineChartView()

let maxYVal = 4.0 * 10000
chartView.leftAxis.axisMaximum = maxYVal
chartView.rightAxis.axisMaximum = maxYVal