我正在尝试使用Charts Framework创建烛台图,并使用Codable作为JSON,通过myclass可以显示为:
struct ChartDataPair: Codable {
var DateTime: String = ""
var Open: Double = 0.0
var High: Double = 0.0
var Low: Double = 0.0
var Close: Double = 0.0
}
哪个会创建一个chartDataPairs数组,如下所示:
struct ChartData: Codable {
var chartDataPairs: [ChartDataPair]
}
我要获取的值如下所示:
{"chartDataPairs":
[{
"DateTime": "2018/10/1 10:00:01",
"Open": 50.05,
"High": 50.05,
"Low": 49.00,
"Close":49.00
},
{
"DateTime": "2018/10/1 10:05:02",
"Open": 51.05,
"High": 54.06,
"Low": 40.00,
"Close":45.06
},
{
"DateTime": "2018/10/1 10:10:02",
"Open": 50.05,
"High": 64.06,
"Low": 40.00,
"Close":58.06
}]
}
数据只是一个样本,所以只写了3个值。现在,我只需要获取时间并将DateTime字符串转换为Double即可在图表的x轴上进行绘制。对于我正在使用:
var dataEntries: [ChartDataEntry] = []
guard let financialData = dataChart.self else {
return
}
for chartData in financialData{
let open = chartData.Open
let close = chartData.Close
let high = chartData.High
let low = chartData.Low
let datetime = chartData.DateTime
let formatter = DateFormatter()
formatter.dateFormat = "YYYY/MM/dd HH:mm:ss"
let yourDate = formatter.date(from: datetime)
formatter.dateFormat = "HH:mm"
let myStringafd = formatter.string(from: yourDate!)
let time = myStringafd
let components = time.characters.split { $0 == ":" } .map { (x) -> Int in return Int(String(x))! }
let hours = components[0]
let minutes = components[1]
let double1 = Double("\(hours).\(minutes)")
let dataEntry = CandleChartDataEntry(x: double1! , shadowH: high, shadowL: low, open: open, close: close)
dataEntries.append(dataEntry)
}
let chartDataSet = CandleChartDataSet(values: dataEntries, label: "")
chartDataSet.axisDependency = .left
chartDataSet.drawIconsEnabled = false
chartDataSet.shadowColor = .darkGray
chartDataSet.shadowWidth = 0.7
chartDataSet.decreasingColor = .red
chartDataSet.decreasingFilled = true // fill up the decreasing field color
chartDataSet.increasingColor = UIColor(red: 122/255, green: 242/255, blue: 84/255, alpha: 1)
chartDataSet.increasingFilled = true // fill up the increasing field color
chartDataSet.neutralColor = .blue
chartDataSet.barSpace = 1.0
chartDataSet.drawValuesEnabled = false
let chartData = CandleChartData(dataSet: chartDataSet)
candlestickView.data = chartData
我知道将时间转换为double ins的正确性是不正确的,在这里我需要一些有关将datetime转换为double值的帮助。
第二个问题是烛台的横条宽度,我无法减小烛台的宽度。
我想用诸如HH:MM之类的时间值以某些间隔(例如15分钟,50分钟,4小时等)填充x轴。
在下面的给定链接中,我遵循的几个问题和建议: iOS-Charts Library: x-axis labels without backing data not showing
关于此问题:candlestickView.xAxisRenderer = XAxisWeekRenderer()
不起作用。它呼吁使用视口处理器,x轴和进行变换。
尽管我可以从自定义IAxisValueFormatter获得自定义标签。 x轴上两个值之间的间隔不是我想要的15分钟或50分钟或4小时等的间隔。
ios Charts 3.0 - Align x labels (dates) with plots
在上述链接上,我无法获得minTimeInterval
and referenceTimeInterval
基本上,我要做的是绘制从X轴上的JSON获取的字符串中的小时和分钟,并在创建自定义X轴的同时在X轴的值之间创建自定义间隔标签。
我的图表当前显示为: Candlestick Chart
答案 0 :(得分:0)
我要显示相同的内容,所以我用这种方式显示数据
let xAxis = chartView.xAxis
var dataPoints = [String]()
for i in 0 ..< arrData.count
{
let timeStampFrom = arrData[i].time
dataPoints.append(self.stringFromTimestamp(timeStampFrom, strDateFormat: "h a"))
}
xAxis.valueFormatter = IndexAxisValueFormatter(values:dataPoints)
xAxis.setLabelCount(5, force: false)
一些需要的功能
func stringFromTimestamp(_ timeInterval : Double ,strDateFormat : String)->String
{
let dateFormatter = DateFormatter()
self.setDateFormat(dateFormatter,dateFormat: strDateFormat)
let date = Date(timeIntervalSince1970: TimeInterval(timeInterval))
return dateFormatter.string(from: date)
}
这是输出