如何迭代Swift数组并使用iOS Chart中的数据?

时间:2017-01-13 09:30:23

标签: ios arrays swift charts ios-charts

我有一个条形图,我试图填充从数组中提取的数据。

我正在尝试遍历此数组以填充我的图表。

请注意:在下面的示例中,有3个winsStreakDM变量被硬编码用于测试目的,但在“现实生活中”每个API调用的数量可能不同,因为它取决于“用户”的数量在那个联盟中。

这就是我需要遍历数组并构建数据以适应的原因。

    let winningStreakDM1 : [String : Any] = [
        "id" : 2,
        "user_name" : "Dicky",
        "winning_streak" : 5
    ]

    let winningStreakDM2 : [String : Any] = [
        "id" : 6,
        "user_name" : "G",
        "winning_streak" : 2
    ]

    let winningStreakDM3 : [String : Any] = [
        "id" : 5,
        "user_name" : "Sultan",
        "winning_streak" : 0
    ]

我的问题是我不知道如何遍历初始数组来构建我的数据以使其与上述代码一起使用。

这是我的完整脚本:

import UIKit
import Charts

class CommunityLeagueStatsVC: UIViewController {


// GRAPHS *********

@IBOutlet weak var chartView: BarChartView!
var values = [BarChartDataEntry]()

// ****************
//********CHART VARIABLES**************//

//WINS LOSSES DRAWS
var winStreak: Double = 0.0

@IBOutlet weak var leagueStatsScrollView: UIScrollView!

var noDefeats: Bool?
var noWins: Bool?
var biggestWin: String?
var biggestWinTeams: String?

var passedCommunityName: String?
@IBOutlet weak var communityName: UILabel!

var playerId2: String?
var communityId2: String?
var eMail2: String?

override func viewDidLoad() {
    super.viewDidLoad()


    let winningStreak =  ["Wins"]
    let gamesWon = [winStreak]
    setWinStreakChart(dataPoints: winningStreak, values: gamesWon)

    let defaults = UserDefaults.standard
    let Email = defaults.string(forKey: "userEmail")
    let playerId = defaults.string(forKey: "playerId")
    let commsId = defaults.string(forKey: "communityId")

    self.playerId2 = playerId
    self.communityId2 = commsId
    self.eMail2 = Email


}

 func setWinStreakChart(dataPoints: [String], values: [BarChartDataEntry]){

    xAxis.valueFormatter = WinningStreakFormatter(chartView: self.chartView)
    let barChartDataSet = BarChartDataSet(values: values, label: "Winning Streak")
    barChartDataSet.colors = ChartColorTemplates.material()

    let barChartData = BarChartData(dataSet: barChartDataSet)
    barChartData.setValueFont(UIFont.systemFont(ofSize: 12.0))
    self.chartView.data = barChartData
}


override func viewDidAppear(_ animated: Bool) {

    let myUrl = URL(string: "http://www.xxx.uk/xxx/getLeagueStats.php")
    var request = URLRequest(url:myUrl!)
    request.httpMethod = "POST"

    let postString = "player_id=\(self.playerId2!)&community_id=\(communityId2!)";

    request.httpBody = postString.data(using: String.Encoding.utf8);
    let task = URLSession.shared.dataTask(with: request) { (data: Data?, response: URLResponse?, error: Error?) in
        DispatchQueue.main.async
            {
                do{

                    let json = try JSONSerialization.jsonObject(with: data!, options: .allowFragments) as? [String:AnyObject]
                    print (json!)

                       if let dict = json?["leagueStats"] as? [String:AnyObject] {

                          //WINNING STREAK

                        var values = [BarChartDataEntry]()

                        if let dataWinStreak = dict["winningStreak"] as? [[String : Any]] {
                            print ("test one")
                            for (index,item) in dataWinStreak.enumerated() {
                                if let yValue = item["winning_streak"] as? Int, let userName = item["user_name"] as? String {
                                    print ("test two")
                                    let barChartDataEntry = BarChartDataEntry(x: Double(index), y: Double(yValue), data: userName as AnyObject?)
                                    values.append(barChartDataEntry)

                                }
                            }

                            self.setWinStreakChart(dataPoints: ["wins"], values: values)
                        }
        catch{
            print(error)
                }
        }
        }
    task.resume()

}
}

更新

这些是我目前收到的错误:

enter image description here

enter image description here enter image description here

稍微玩一下并注释掉包含我在这个阶段的第一个错误的行,它正确地显示了带有值的图形,但没有包含每个玩家的userNames的xAx。

1 个答案:

答案 0 :(得分:1)

您可以通过这种方式创建BarChartDataEntry数组。

var values = [BarChartDataEntry]()

if let dataWinStreak = dict["winningStreak"] as? [[String : Any]] {
    for (index,item) in dataWinStreak.enumerated() {
        if let yValue = item["winning_streak"] as? Int, let userName = item["user_name"] as? String {
            let barChartDataEntry = BarChartDataEntry(x: index, y: yValue, data: userName)
            values.append(barChartDataEntry)
        }
    }
}
//Now use values array

修改:您只需要更改setWinStreakChart功能,因为现在您正在处理动态数据而不是静态数据。

func setWinStreakChart(dataPoints: [String], values: [BarChartDataEntry]){

    let  xAxis : XAxis = self.chartView.xAxis;
    xAxis.labelFont = UIFont(name: "HelveticaNeue-Light", size: 10.0)!
    xAxis.labelTextColor = UIColor.black
    xAxis.drawAxisLineEnabled = false
    xAxis.drawGridLinesEnabled = true
    xAxis.granularity = 1;
    xAxis.labelPosition = .bottom

    xAxis.valueFormatter = WinningStreakFormatter(chartView: self.chartView)
    let barChartDataSet = BarChartDataSet(values: values, label: "Winning Streak")
    barChartDataSet.colors = ChartColorTemplates.material()

    let barChartData = BarChartData(dataSet: barChartDataSet)
    barChartData.setValueFont(UIFont.systemFont(ofSize: 12.0))
    self.chartView.data = barChartData
}

现在在for循环之后调用此函数,我们正在创建BarChartDataEntry数组。

self.setWinStreakChart(dataPoints: ["wins"], values: values)