烛台图表在使用danielgindi / Charts框架的ios中无法正确显示

时间:2017-08-06 22:30:11

标签: ios swift swift3 candlestick-chart

    var dataEntries: [ChartDataEntry] = []
    let financialData = data

    for chartData in financialData{
            let dataEntry = CandleChartDataEntry(x: chartData.date, shadowH: chartData.High, shadowL: chartData.Low, open: chartData.Open, close: chartData.Close)
            dataEntries.append(dataEntry)
    }

    let chartDataSet = CandleChartDataSet(values: dataEntries, label: "")
    chartDataSet.axisDependency = .left
    chartDataSet.drawValuesEnabled = false
    chartDataSet.increasingColor = UIColor.green
    chartDataSet.increasingFilled = false
    chartDataSet.decreasingColor = UIColor.red
    chartDataSet.decreasingFilled = true
    chartDataSet.barSpace = 0.25
    chartDataSet.shadowColor = UIColor.darkGray
    chartDataSet.neutralColor = UIColor.blue

    let chartData = CandleChartData(dataSet: chartDataSet)
    chartView.data = chartData

    let xaxis = chartView.xAxis
    xaxis.valueFormatter = axisFormatDelegate

以下是从服务器接收的示例数据集以及显示正在显示的图表的图像。 https://api.cryptowat.ch/markets/gdax/btcusd/ohlc?periods=60

[CloseTime,OpenPrice,HighPrice,LowPrice,ClosePrice,Volume]

我需要帮助修复数据显示,因为它目前不会像蜡烛图一样。有人可以帮我解决我在这里做错了什么。

CandleStick chart data being displayed in the app

1 个答案:

答案 0 :(得分:1)

所以,如果有人遇到同样的问题,我也会遇到蜡烛问题,我找到了一个基于这篇文章的解决方案: https://github.com/danielgindi/Charts/issues/2742

  • 为您的chartView.xAxis.valueFormatter创建一个类,该类支持您使用init创建的条目。
  • 获得结果条目后,我在CandleChartDataEntry上使用了x的枚举值
  • 在继承自IAxisValueFormatter的类中的stringForValue方法中,以您希望呈现字符串的方式工作,在我的例子中是日期。

前两点:

//Check nil results
if result != nil {

//Save to Holder for future use
self.historyData = result

// Set the formatter class for the chartView
self.chartView.xAxis.valueFormatter       = dateFormatter(elementsForFormatter: self.historyData!)

//Create a new chart data entry
var chartEntries = [ChartDataEntry]()

//Loop results
result?.enumerated().forEach({ (i, element) in

    // I'm force unwrapping, but please take care of this don't do me...
    let newDataEntryValue = CandleChartDataEntry(x:Double(i),
                                                 shadowH: element["high"] as! Double,
                                                 shadowL: element["low"] as! Double,
                                                 open: element["open"] as! Double,
                                                 close: element["close"] as! Double)

    chartEntries.append(newDataEntryValue)
}) 


}
// Do rest of conf. for your chart....

最后一点(类实现):

class dateFormatter: IAxisValueFormatter {

var elements:Array<Dictionary<String,Any>>

init(elementsForFormatter:Array<Dictionary<String,Any>>) {
    self.elements = elementsForFormatter
}
 func stringForValue(_ value: Double, axis: AxisBase?) -> String {

    let formatter = DateFormatter()
    formatter.dateStyle = .short

    //Get the date based on the value which is the index
    if let time = self.elements[Int(value)]["time"] as? Double {
        print(time)
        let dateString = formatter.string(from: Date(timeIntervalSince1970: TimeInterval(time)))

        return dateString

    } else {
        return ""
    }


}
}

希望这有助于某人!