【问题标题】:Set the maximum of the Y-Axis of a LineChartView | pod 'Charts'设置 LineChartView 的 Y 轴最大值 |吊舱“图表”
【发布时间】:2021-11-18 01:10:26
【问题描述】:

我使用“图表”窗格创建了 LineChartView。

我已经使绘制的线跟随一个函数 (f(x) = ax^2),其中一旦用户触摸 LineChartView,值“a”就会被随机化。 x 轴通过 for in 循环定义的最大值为 100。然而,这导致 y 轴具有定义的最大值 a*10000,其中 a 是在用户每次触摸 LineChartView 时随机创建的。

这意味着它实际上看起来根本不像图表发生变化。唯一可见的变化是,每次随机创建 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)
    }
}

【问题讨论】:

    标签: ios swift graph linechart ios-charts


    【解决方案1】:

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

    let chartView = LineChartView()
    
    let maxYVal = 4.0 * 10000
    chartView.leftAxis.axisMaximum = maxYVal
    chartView.rightAxis.axisMaximum = maxYVal
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-06-04
      • 1970-01-01
      • 2017-08-24
      • 2018-06-20
      • 2021-02-02
      • 2015-05-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多