Значение изменилось, но график не Swift - PullRequest
2 голосов
/ 02 августа 2020

Я использую структуру Macaw для своих диаграмм. Есть только одна проблема: если он в первый раз показывает диаграмму «X», он всегда (до закрытия приложения) будет показывать диаграмму «X». Но данные меняются (я видел это в отладчике).

Вот фрагмент моего «DahlichViewController».

class DahlichViewController: UIViewController {
    
@IBOutlet private var chartView: MacawChartView!
@IBOutlet weak var start: UIButton!
    
override func viewDidLoad() {
    super.viewDidLoad()
    chartView.backgroundColor = .clear
    chartView.contentMode = .scaleAspectFit
}

@IBAction func startPressed(_ sender: UIButton) {
    chartView.updateData(newData: createData())
    chartView.playAnimations()
}

А вот мой MacawChartView.

import Foundation
import Macaw

class MacawChartView: MacawView {
    
    static var chartData = createData()
    
    static let maxValue              = 60
    static let maxValueLineHeight    = 18
    static let lineWidth: Double     = 275
    
    static let dataDivisor             = Double(maxValue/maxValueLineHeight)
    static let adjustedData: [Double]  = chartData.map({$0.percentage / dataDivisor})
    static var animations: [Animation] = []
    
    public func updateData(newData : [ElectionBrain])
    {
        MacawChartView.chartData = newData
        updateDisplay()
    }

    public func updateDisplay()
    {
        let chart = MacawChartView.createChart()
        self.node = Group(contents: [chart])
    }
    
    static func createChart() -> Group {
        var items: [Node] = addYAxisItems() + addXAxisItems()
        items.append(createBars())
        
        return Group(contents: items, place: .identity)
    }
    
    private static func addYAxisItems() -> [Node] {
        let maxLines            = 6
        let lineInterval        = Int(maxValue / maxLines)
        let yAxisHeight: Double = 200
        let lineSpacing: Double = 30
        
        var newNodes: [Node]    = []
        
        for i in 1...maxLines {
            let y = yAxisHeight - (Double(i) * lineSpacing)
            
            let valueLine = Line(x1: -5, y1: y, x2: lineWidth, y2: y).stroke(fill: Color.white.with(a: 0.10))
            let valueText = Text(text: "\(i * lineInterval)", align: .max, baseline: .mid, place: .move(dx: -10, dy: y))
            valueText.fill = Color.white
            
            newNodes.append(valueLine)
            newNodes.append(valueText)
        }
        
        let yAxis = Line(x1: 0, y1: 0, x2: 0, y2: yAxisHeight).stroke(fill: Color.white.with(a: 0.25))
        newNodes.append(yAxis)
        
        return newNodes
    }
    
    private static func addXAxisItems() -> [Node] {
        let chartBaseY: Double = 200
        var newNodes: [Node]   = []
        
        
        for i in 1...adjustedData.count {
            let x = (Double(i) * 50)
            let valueText = Text(text: chartData[i - 1].version, align: .max, baseline: .mid, place: .move(dx: x, dy: chartBaseY + 15))
            valueText.fill = Color.white
            newNodes.append(valueText)
        }
        
        let xAxis = Line(x1: 0, y1: chartBaseY, x2: lineWidth, y2: chartBaseY).stroke(fill: Color.white.with(a: 0.25))
        newNodes.append(xAxis)
        
        return newNodes
    }
    
    private static func createBars() -> Group {
        let fill  = LinearGradient(degree: 90, from: Color(val: 0xfff264), to: Color(val: 0xd69c00).with(a: 0.33))
        let items = adjustedData.map { _ in Group() }
        
        animations = items.enumerated().map { (i: Int, item: Group) in
            item.contentsVar.animation(delay: Double(i) * 0.1 ) { t in
                let height = adjustedData[i] * t * 10
                let rect   = Rect(x: Double(i) * 50 + 25, y: 200 - height, w: 30, h: height)
                return[rect.fill(with: fill)]
            }
        }
        
        return items.group()
    }
    
     func playAnimations() {
        MacawChartView.animations.combine().play()
    }

}

Я объявил свою функцию createData () как глобальную функцию (не в V C). Проблема в том, что данные меняются, а диаграмма - нет. Все время показывает один и тот же график.

Ответы [ 2 ]

1 голос
/ 04 августа 2020

@ Рассел дайте мне хороший совет. Проблема заключалась в том, что я не обновлял adjustedData, который действительно обновляет диаграмму. Я добавил этот код в updateDisplay, и он сработал.

        MacawChartView.adjustedData = MacawChartView.chartData.map({$0.percentage / MacawChartView.dataDivisor})
0 голосов
/ 02 августа 2020

Попробуйте вызвать setNeedsLayout() в конце вашей функции updateDisplay().

...