0
我有一個條形圖,我試圖通過從數組中提取的數據來填充。如何遍歷Swift數組並使用iOS Chart中的數據?
我想遍歷這個數組,以填充我的圖表。
請注意:在這個例子中下面有硬編碼用於測試目的3個winningStreakDM變量,但在「現實生活」的數量可能是不同的每個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()
}
}
UPDATE:
這些是我目前收到的錯誤:
隨着玩弄和評論包含第一個錯誤的行,我已經到了這個階段,它正確地顯示了一個帶有值的圖形,但沒有包含每個玩家的用戶名的xAxis。
我有一個錯誤我已經加入這一行'self.setWinStreakChart(數據點:「勝」],值:值)'你的代碼的底部,然後調用功能,但它說「無法將類型'[BarChartDataEntry]'的值轉換爲期望的參數類型'[Double]'。我已經更新了該問題以顯示完整的功能以及它的期望。謝謝! – RDowns
@RDowns你爲什麼要調用這個函數,你的BarChartDataEntry數組被創建,因此你現在不需要調用:'let barChartDataSet = BarChartDataSet(values:values,label:「Winning Streak」)' –
api調用發生在函數來創建圖表 - 所以我不需要調用函數來填充檢索的數據?我應該向你展示完整的腳本 - 我現在出去了,但當我返回時,將用完整腳本更新問題。謝謝 – RDowns