如何在chartjs中的列表中使用alpha vantage api?

时间:2019-04-10 18:00:47

标签: javascript api chart.js alphavantage

我有此代码并运行它,但是我想将cahnge的源代码转换为alpha优势? 这个api是:

https://www.alphavantage.co/query?function=TIME_SERIES_DAILY&symbol=aapl&apikey=3FJ485A1KCAZCODB

我如何在代码中使用此api, 我的代码是:

func fetchAllUsers(completion: @escaping (_ message: String) -> Void){
        //User or advertiser?
        Database.database().reference(withPath: "Advertiser").child(uid).observeSingleEvent(of: .value, with: { (snapshot) in

            if snapshot.exists(){
                myAdvertiserVar.advertiser = true
                self.currentUserKind = "Advertiser"
                self.otherUserKind = "Users"
            }
            else{
                self.currentUserKind = "Users"
                self.otherUserKind = "Advertiser"
            }

            // Fetch
            let query = self.ref?.child(self.otherUserKind).queryOrdered(byChild: "email")
            query?.observeSingleEvent(of: .value) {
                (snapshot) in
                for child in snapshot.children.allObjects as! [DataSnapshot] {
                    let id = child.key
                    //If Already Accepted, don't fetch
                    Database.database().reference(withPath: self.currentUserKind).child(self.uid).child("Accepted").child(id).observeSingleEvent(of: .value, with: {(accepted) in
                        if accepted.exists(){
                            print("\(id) är redan Accepted")
                        }
                        else{
                            if myAdvertiserVar.advertiser == true{
                                let value = child.value as? NSDictionary
                                let username = value?["Username"] as? String
                                let occupation = value?["Occupation"] as? String
                                let age = value?["Age"] as? String
                                let bio = value?["Bio"] as? String
                                let email = value?["email"] as? String
                                let user = User(id: id, username: username, occupation: occupation, age: age, bio: bio, email: email)
                                self.usersArray.append(user)
                            }
                            else{
                                let value = child.value as? NSDictionary
                                let username = value?["Owner"] as? String
                                let occupation = value?["Location"] as? String
                                let age = value?["Rent"] as? String
                                let bio = value?["About"] as? String
                                let email = value?["email"] as? String
                                let user = User(id: id, username: username, occupation: occupation, age: age, bio: bio, email: email)
                                self.usersArray.append(user)
                            }
                        }
                    })
                }
                print(self.usersArray.count)
                completion("Users list fetched")
            }
        })
    }

代码已运行并显示结果,但我需要在此代码中使用api,感谢您阅读我的问题,请帮忙

1 个答案:

答案 0 :(得分:3)

您不需要所有这些代码。请像这样更改代码

$(function () {
    var urls = 'https://www.alphavantage.co/query?function=TIME_SERIES_DAILY&symbol=aapl&apikey=3FJ485A1KCAZCODB';
    $.ajax({
        url: urls,        
        dataType: 'json',
        contentType: "application/json",
        success: function (data) {
            console.log(data['Time Series (Daily)']);

        // split the data set into ohlc and volume
        var ohlc = [],
          volume = [],
          dataLength = data['Time Series (Daily)'],
          i = 0;
          for(var time in dataLength)
          {
            var stock_info = dataLength[time];  
            ohlc.push([        
              time,           
              Number(stock_info["1. open"]),
              Number(stock_info["2. high"]),
              Number(stock_info["3. low"]),
              Number(stock_info["4. close"])

            ]);

            volume.push([
                time, // the date                   
                Number(stock_info["5. volume"]) // the volume
            ]);
        }
....