Update graph when the value changes

1

I have created 3 UIView , each one is a graphic that I used from the PNChart library. I show them correctly all OK.

The problem comes when I want to update the value. That is, if initially the graph was in 60% and I update it to 80% is updated but I want to do it automatically. to verify if the value has changed and update the% with the new value.

I've tried it with addTarget but it does not appear in the properties of PNChart and I do not know how to do it.

UPDATED CODE AND RUNNING

import UIKit
import ALSystemUtilities
import PNChart


    class CPU: UIView {
    var lineChart:PNCircleChart?
    override func layoutSubviews() {
        lineChart = PNCircleChart(frame: CGRectMake(0 , 0, self.bounds.width, self.bounds.height) , total: 100, current: 0, clockwise: true, shadow: true, shadowColor: UIColor(red:0.94, green:0.95, blue:0.95, alpha:1.0), displayCountingLabel: true, overrideLineWidth: 13)
            lineChart!.strokeColor = UIColor(red:0.42, green:0.79, blue:0.86, alpha:1.0)
            self.backgroundColor = UIColor.whiteColor()
            lineChart!.strokeChart()
            self.addSubview(lineChart!)


            // Añadimos un timer para hacer update cada x tiempo
            NSTimer.scheduledTimerWithTimeInterval(3.0, target: self, selector: "updateValues", userInfo: nil, repeats: true)
        }
    func updateValues() {
        lineChart!.updateChartByCurrent(cpu_usage())
        print(cpu_usage())
    }

}
    
asked by Bogdan 19.01.2016 в 19:22
source

1 answer

0

To begin with, I think you're messing with so much UIView, features, etc. I think you should simplify the implementation as much as you can. In this, sense, I propose the following alternative. I have simplified the implementation a bit so you can see how to do it:

//
//  ViewController.swift
//  TestChart
//
//  Created by mhergon on 19/1/16.
//  Copyright © 2016 Marc Hervera. All rights reserved.
//

import UIKit
import ALSystemUtilities
import PNChart

class ViewController: UIViewController {

    // MARK: - Properties
    var cpuChart: PNCircleChart?
    var ramChart: PNCircleChart?

    var cpuValue: CGFloat = 0.0 {
        didSet {
            cpuChart?.updateChartByCurrent(cpuValue)
        }
    }
    var ramTotal: Int = ALMemory.totalMemory()
    var ramValue: CGFloat = 0.0 {
        didSet {
            ramChart?.updateChartByCurrent(ramValue)
        }
    }


    // MARK: - Methods
    override func viewDidLoad() {
        super.viewDidLoad()

        // Init
        let cpuFrame = CGRect(x: 10, y: 20, width: 240, height: 240)
        if let cpuChart = PNCircleChart(frame: cpuFrame, total: 100, current: cpuValue, clockwise: true) {

            cpuChart.strokeColor = UIColor.redColor()
            view.addSubview(cpuChart)
            cpuChart.strokeChart()

            self.cpuChart = cpuChart

        }

        let ramFrame = CGRect(x: 10, y: 260, width: 240, height: 240)
        if let ramChart = PNCircleChart(frame: ramFrame, total: ramTotal, current: ramValue, clockwise: true) {

            ramChart.strokeColor = UIColor.greenColor()
            ramChart.displayAnimated = false
            view.addSubview(ramChart)
            ramChart.strokeChart()

            self.ramChart = ramChart
        }

        // Actualizamos la primera vez
        updateValues()

        // Añadimos un timer para hacer update cada x tiempo
        let updateTimer = NSTimer.scheduledTimerWithTimeInterval(3.0, target: self, selector: "updateValues", userInfo: nil, repeats: true)
        updateTimer.fire()

    }

    func updateValues() {

        // Procesador
        cpuValue = ALProcessor.cpuUsageForApp()

        // Ram
        ramValue = ALMemory.usedMemory()

    }

}

On the subject of updating the values, you can use the observation of variables and with an NSTimer update the value of the variable, so that it is automatically updated. You have to do it in the code.

    
answered by 19.01.2016 в 20:54