mirror of
https://github.com/wahyd4/libr-ios.git
synced 2026-08-09 05:06:46 +10:00
141 lines
4.9 KiB
Swift
141 lines
4.9 KiB
Swift
//
|
|
// GraphsUIViewController.swift
|
|
// librx
|
|
//
|
|
// Created by Yunlong Zheng on 15/10/13.
|
|
// Copyright © 2015年 librx. All rights reserved.
|
|
//
|
|
|
|
import UIKit
|
|
|
|
class GraphsUIViewController: UIViewController, LineChartDelegate {
|
|
|
|
var label = UILabel()
|
|
var lineChart: LineChart!
|
|
var userId: Int = 0
|
|
|
|
override func viewDidLoad() {
|
|
|
|
LibrxService.userData(self.userId) { [weak self](json) -> () in
|
|
|
|
var xlabels00:[String] = ["","","",""]
|
|
var data00:[CGFloat] = [0.0,0.0,0.0]
|
|
var data01:[CGFloat] = [0.0,0.0,0.0]
|
|
var data02:[CGFloat] = [0.0,0.0,0.0]
|
|
|
|
self?.lineChart = LineChart()
|
|
|
|
if let selfView = self {
|
|
|
|
if (json != nil) {
|
|
|
|
xlabels00.removeAll()
|
|
data00.removeAll()
|
|
data01.removeAll()
|
|
data02.removeAll()
|
|
|
|
let shares = json["shares"].arrayValue
|
|
let likes = json["likes"].arrayValue
|
|
let bookmarks = json["bookmarks"].arrayValue
|
|
|
|
for index in 0..<shares.count {
|
|
|
|
let data = shares[index]
|
|
let label = data["label"].stringValue
|
|
let value = data["value"].floatValue
|
|
data00.append(CGFloat(value))
|
|
xlabels00.append(label)
|
|
|
|
}
|
|
|
|
for index in 0..<likes.count {
|
|
|
|
let data = likes[index]
|
|
let value = data["value"].floatValue
|
|
data01.append(CGFloat(value))
|
|
|
|
}
|
|
|
|
for index in 0..<bookmarks.count {
|
|
|
|
let data = bookmarks[index]
|
|
let value = data["value"].floatValue
|
|
data02.append(CGFloat(value))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
var views: [String: AnyObject] = [:]
|
|
|
|
selfView.label.text = "..."
|
|
selfView.label.translatesAutoresizingMaskIntoConstraints = false
|
|
selfView.label.textAlignment = NSTextAlignment.Center
|
|
selfView.label.font = UIFont(name: selfView.label.font.fontName, size: 12)
|
|
|
|
selfView.view.addSubview(selfView.label)
|
|
views["label"] = selfView.label
|
|
selfView.view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-[label]-|", options: [], metrics: nil, views: views))
|
|
selfView.view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-10-[label]", options: [], metrics: nil, views: views))
|
|
|
|
selfView.lineChart.animation.enabled = true
|
|
selfView.lineChart.area = true
|
|
|
|
selfView.lineChart.x.labels.visible = true
|
|
selfView.lineChart.x.grid.count = 5
|
|
selfView.lineChart.x.grid.color = UIColor(red: 238/255.0, green: 238/255.0, blue: 238/255.0, alpha: 1)
|
|
|
|
selfView.lineChart.y.grid.count = 5
|
|
|
|
selfView.lineChart.x.labels.values = xlabels00
|
|
selfView.lineChart.y.labels.visible = true
|
|
|
|
selfView.lineChart.addLine(data00)
|
|
selfView.lineChart.addLine(data01)
|
|
selfView.lineChart.addLine(data02)
|
|
|
|
selfView.lineChart.translatesAutoresizingMaskIntoConstraints = false
|
|
selfView.lineChart.delegate = self
|
|
selfView.view.addSubview(selfView.lineChart)
|
|
views["chart"] = selfView.lineChart
|
|
|
|
selfView.view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-[chart]-|", options: [], metrics: nil, views: views))
|
|
selfView.view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:[label]-[chart(==200)]", options: [], metrics: nil, views: views))
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
override func didReceiveMemoryWarning() {
|
|
super.didReceiveMemoryWarning()
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
* Line chart delegate method.
|
|
*/
|
|
func didSelectDataPoint(x: CGFloat, yValues: Array<CGFloat>) {
|
|
label.text = "分享:\(yValues[0]), 喜欢:\(yValues[1]),收藏:\(yValues[2])"
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
* Redraw chart on device rotation.
|
|
*/
|
|
override func didRotateFromInterfaceOrientation(fromInterfaceOrientation: UIInterfaceOrientation) {
|
|
if let chart = lineChart {
|
|
chart.setNeedsDisplay()
|
|
}
|
|
}
|
|
|
|
|
|
}
|