Skip to content

Instantly share code, notes, and snippets.

@shawn-kb
Created February 3, 2016 17:11
Show Gist options
  • Save shawn-kb/46ea72a63002569c9b94 to your computer and use it in GitHub Desktop.
Save shawn-kb/46ea72a63002569c9b94 to your computer and use it in GitHub Desktop.
lines not showing up

import UIKit
import Alamofire
import SwiftCharts
class PressureSensorViewController: UIViewController {
private var chart: Chart?
@IBOutlet weak var lineChartView: ChartLinesView!
override func viewDidLoad() {
super.viewDidLoad()
setChartData()
}
func setChartData(){
let site = ActiveSite.sharedInstance.siteObject
let url = "http://api.pivotrac.com:3000/pressure_sensors/recent_data_plots"
var arrayOfPlotPoints: [(Double, Double)] = []
var lowestDate: Double = 0
var highestDate: Double = 0
var lowestValue: Double = 0
var highestValue: Double = 0
Alamofire.request(.GET, url, parameters: ["id": site.pressureSensorID])
.responseJSON { response in
//print(response.result) // result of response serialization
// handle network problem
switch response.result {
case .Success(_):
print("success code back from api server for sites request")
case .Failure(_):
let alertView = UIAlertController(title: "Connect Error", message: "Network error, please try again", preferredStyle: .Alert)
let alertAction = UIAlertAction(title: "OK", style: .Default) { _ in
}
alertView.addAction(alertAction)
self.presentViewController(alertView, animated: true) {}
}
if let jsonResult = response.result.value {
//print(response.result.value!)
let arrayOfJSONDataPlots = JSON(jsonResult)
for (key, item) in arrayOfJSONDataPlots {
var timestamp: Double = item[1].double!
let psi: Double = item[0].double! + 6.0
// set the initial values for my comparison variables
if lowestDate == 0 {
lowestValue = psi
highestValue = psi
lowestDate = timestamp
highestDate = timestamp
}
if highestValue > psi{
highestValue = psi
}
if lowestValue < psi{
lowestValue = psi
}
if timestamp < lowestDate{
lowestDate = timestamp
}
if timestamp > highestDate {
highestDate = timestamp
}
arrayOfPlotPoints.append((psi, timestamp))
}
print("found lo psi = \(lowestValue) on \(lowestDate)")
print("found hi psi = \(highestValue) on \(highestDate)")
}
else {
print("JSON data is nil.")
}
} // end of json sites request
let upperLimit: Double = 155.0
let lowerLimit: Double = 0.0
let width = lineChartView.bounds.width
let height = lineChartView.bounds.height
let chartConfig = ChartConfigXY(
xAxisConfig: ChartAxisConfig(from: 0, to: 14, by: 2),
yAxisConfig: ChartAxisConfig(from: 0, to: 15, by: 2)
)
let chart = LineChart(
frame: CGRectMake(0, 70, 300, 500),
chartConfig: chartConfig,
xTitle: "X axis",
yTitle: "Y axis",
lines: [
//(chartPoints: [(2.0, 10.6), (4.2, 5.1), (7.3, 3.0), (8.1, 5.5), (14.0, 8.0)], color: UIColor.redColor()),
(chartPoints: arrayOfPlotPoints, color: UIColor.blueColor())
]
)
lineChartView.addSubview(chart.view)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment