我正在尝试设计一个ios应用程序以在折线图中显示json数据。
首先,这是我的json数据。
{
TH_5min: [
{
Data: "2019-02-23T00:00:00",
Time: "11:00:00",
XTP_A: 10.5, //temperature 1
XHP_A: 11.5, //humidity 1
XTP_B: 33.5,
XHP_B: 44.6,
XTP_C: 88.9,
XHP_C: 66.6,
XTP_D: 77.9,
XHP_D: 99.6,
XTP_E: 87.87,
XHP_E: 66.66
},
{
Data: "2019-02-23T00:00:00",
Time: "11:05:00",
XTP_A: 55.2, //temperature 1
XHP_A: 44.3, //humidity 1
XTP_B: 66.6,
XHP_B: 77.87,
XTP_C: 87.77,
XHP_C: 87.87,
XTP_D: 8.87,
XHP_D: 78.78,
XTP_E: 87.78,
XHP_E: 87.87
}
]
}
这是我对显示json数据的快速代码的实现。
override func viewDidLoad() {
super.viewDidLoad()
apiip = APIip
getlatestTh_5min()
@objc func getlatestTh_5min(){
guard let th_5minUrl = URL(string: "http://" + apiip + "/api/Th_5min") else{
return
}
let request = URLRequest(url: th_5minUrl)
let task = URLSession.shared.dataTask(with: request, completionHandler: {(data,response,error) -> Void in
if let error = error {
print(error)
return
}
if let data = data {
self.th_5mins = self.pardrJsonData(data: data)
self.getchat()
}
})
task.resume()
//getchat()
}
func pardrJsonData(data: Data) -> [Th_5min]{
var th_5mins = [Th_5min]()
do {
let jsonResult = try JSONSerialization.jsonObject(with: data, options: JSONSerialization.ReadingOptions.mutableContainers) as? NSDictionary
let jsonTh_5mins = jsonResult?["TH_5min"] as! [AnyObject]
print(jsonTh_5mins)
print(th_5mins.count)
for jsonTh_5min in jsonTh_5mins{
var th_5min = Th_5min()
th_5min.Data = jsonTh_5min["Data"] as! String
th_5min.Time = jsonTh_5min["Time"] as! String
th_5min.XTP_A = jsonTh_5min["XTP_A"] as! Double
th_5min.XHP_A = jsonTh_5min["XHP_A"] as! Double
print(th_5min)
th_5mins.append(th_5min)
//getchat()
} }catch{
print(error)
}
//getchat()
return th_5mins
}
这就是我使用快速代码绘制折线图的方式。
@objc func getchat(){
chartView = LineChartView()
chartView.frame = CGRect(x: 20, y: 80, width: self.view.bounds.width-20,height: self.view.bounds.height-100)
self.view.addSubview(chartView)
var dataEntries1 = [ChartDataEntry]()
for i in 0..<th_5mins.count {
chartView.xAxis.valueFormatter = IndexAxisValueFormatter(values: [th_5mins[i].Time])
let y = th_5mins[i].XTP_A
let entry = ChartDataEntry.init(x: Double(i), y: Double(y))
dataEntries1.append(entry)
}
let chartDataSet1 = LineChartDataSet(entries: dataEntries1, label: "temperature")
chartDataSet1.colors = [NSUIColor.red]
var dataEntries2 = [ChartDataEntry]()
for i in 0..<th_5mins.count {
chartView.xAxis.valueFormatter = IndexAxisValueFormatter(values: [th_5mins[i].Time])
let y = th_5mins[i].XHP_A
let entry = ChartDataEntry.init(x: Double(i), y: Double(y))
dataEntries2.append(entry)
}
let chartDataSet2 = LineChartDataSet(entries: dataEntries2, label: "humidity")
chartDataSet2.colors = [NSUIColor.black]
let chartData = LineChartData(dataSets: [chartDataSet1, chartDataSet2])
chartView.data = chartData
}
}
这是我工作的结果。
尽管成功显示了json数据,但我不知道为什么加载时间很长,我希望json数据中的“时间”可以显示在上方的X轴上,标有我的温度和湿度,并且无法成功。 我也希望可以将折线图视图实现为布局。
答案 0 :(得分:0)
“我不知道为什么它要长时间加载”。您是说图形在打开视图后不会立即加载吗?这是因为数据正在从远程源异步加载(现在正确,做得很好)。通过网络下载JSON可能需要花费几秒钟的时间。那没问题。您可以在浏览器中测试端点,并查看响应需要多长时间。
“我希望json数据中的'时间'可以显示在上方的X轴上”。是。您可以在循环外进行//store/index.js
export const actions = {
nuxtClientInit({ commit }, { req }) {
const autho = localStorage.getItem('auth._token.local') //or whatever yours is called
commit('SET_AUTHO', autho)
console.log('From nuxtClientInit - '+autho)
}
}
的分配,并且应将所有标签作为class MyTileView: UIView
{
var drawBounds = CGRect(x: 0, y: 0, width: 0, height: 0)
let drawBarrierQueue = DispatchQueue(label: "com.example.app",
qos: .userInteractive, // draw operations require the highest priority threading available
attributes: .concurrent,
target: nil)
override func layoutSubviews() {
drawBarrierQueue.sync(flags: .barrier) { // a barrier operation waits for active operations to finish before starting, and prevents other operations from starting until this one has finished
super.layoutSubviews();
self.drawBounds = self.bounds
// do other stuff that should hold up drawing
}
}
override func draw(_ layer: CALayer, in ctx: CGContext)
{
drawBarrierQueue.sync {
// do all of your drawing
ctx.setFillColor(red: 1.0, green: 0, blue: 0, alpha: 1.0)
ctx.fill(drawBounds)
}
}
}
传递到构造函数中。尝试以下代码,替换等效循环:-
IndexAxisValueFormatter
请注意,根据时间进行绘图的方法将产生均匀分布的图形,而与您的时间间隔无关(例如,如果前两个之间的读数相隔5分钟,而下两个之间的相隔5年,则它们将它们之间仍然出现均匀的缝隙。