 	var systolicLine : FMLineSeries? = nil
 	var diastolicLine : FMLineSeries? = nil
 	
+	var stepUpdater : FMProjectionUpdater? = nil;
+	var weightUpdater : FMProjectionUpdater? = nil;
+	var pressureUpdater : FMProjectionUpdater? = nil;
+	
 	override func viewDidLoad() {
 		super.viewDidLoad()
         metalView.device = resource.device
-        metalView.sampleCount = 2
+        metalView.sampleCount = 1
         let v : Double = 0.9
         let alpha : Double = 1
         metalView.clearColor = MTLClearColorMake(v,v,v,alpha)

		let daySec : CGFloat = 24 * 60 * 60
		dateUpdator.addRestrictionToLast(FMSourceRestriction(minValue: -7 * daySec, maxValue: daySec, expandMin: true, expandMax: true))
		
		let stepUpdator = FMProjectionUpdater()
		stepUpdator.addRestrictionToLast(FMSourceRestriction(minValue: 0, maxValue: 2000, expandMin: true, expandMax: true))
		stepUpdator.addRestrictionToLast(FMIntervalRestriction(anchor: 0, interval: 1000, shrinkMin: false, shrinkMax: false))
		
		let weightUpdator = FMProjectionUpdater()
		weightUpdator.addRestrictionToLast(FMSourceRestriction(minValue: 50, maxValue: 60, expandMin: false, expandMax: false))
		weightUpdator.addRestrictionToLast(FMIntervalRestriction(anchor: 0, interval: 5, shrinkMin: false, shrinkMax: false))
		
		let pressureUpdator = FMProjectionUpdater()
		pressureUpdator.addRestrictionToLast(FMSourceRestriction(minValue: 60, maxValue: 80, expandMin: false, expandMax: false))
		pressureUpdator.addRestrictionToLast(FMIntervalRestriction(anchor: 0, interval: 20, shrinkMin: false, shrinkMax: false))
 		
		let stepSpace : FMProjectionCartesian2D = configurator.spaceWithDimensionIds([dateDim,stepDim]) { (dimId) -> FMProjectionUpdater? in
			if(dimId == dateDim) {
				return dateUpdator
			}
			return stepUpdator
		}
		
		let weightSpace : FMProjectionCartesian2D = configurator.spaceWithDimensionIds([dateDim, weightDim]) { (dimId) -> FMProjectionUpdater? in
			return weightUpdator
		}
		
		let pressureSpace : FMProjectionCartesian2D = configurator.spaceWithDimensionIds([dateDim, pressureDim]) { (dimId) -> FMProjectionUpdater? in
			return pressureUpdator
		}
		
		stepBar = configurator.addBarToSpace(stepSpace, series: stepSeries!)
		weightLine = configurator.addLineToSpace(weightSpace, series: weightSeries!)
		systolicLine = configurator.addLineToSpace(pressureSpace, series: systolicSeries!)
		diastolicLine = configurator.addLineToSpace(pressureSpace, series: diastolicSeries!)
		
		let dateConf = FMBlockAxisConfigurator(relativePosition: 0, tickAnchor: 0, fixedInterval: daySec, minorTicksFreq: 0)
		let axis = configurator.addAxisToDimensionWithId(dateDim, belowSeries: false, configurator: dateConf, label: nil)

		systolicSeries?.info.clear()
		diastolicSeries?.info.clear()
		
		stepUpdater?.clearSourceValues(false)
		weightUpdater?.clearSourceValues(false)
		pressureUpdater?.clearSourceValues(false)
		
		let refDate : NSDate = getStartOfDate(NSDate())
		let interval : NSDateComponents = NSDateComponents()
		interval.day = 1
		let step = HKQuantityType.quantityTypeForIdentifier(HKQuantityTypeIdentifierStepCount)!
		let weight = HKQuantityType.quantityTypeForIdentifier(HKQuantityTypeIdentifierBodyMass)!
		let systolic = HKQuantityType.quantityTypeForIdentifier(HKQuantityTypeIdentifierBloodPressureSystolic)!
		let diastolic = HKQuantityType.quantityTypeForIdentifier(HKQuantityTypeIdentifierBloodPressureDiastolic)!
		
		let sort = NSSortDescriptor(key: HKSampleSortIdentifierEndDate, ascending: false)
		
		let stepQuery = HKStatisticsCollectionQuery(quantityType: step, quantitySamplePredicate: nil, options: HKStatisticsOptions.CumulativeSum, anchorDate: refDate, intervalComponents: interval)
		stepQuery.initialResultsHandler = { query, results, eror in
			if let collection = results {
				for statistic in collection.statistics() {
					if let quantity = statistic.sumQuantity() {
						let value = quantity.doubleValueForUnit(HKUnit.countUnit())
						self.stepSeries?.addPoint(CGPointMake(CGFloat(statistic.startDate.timeIntervalSinceDate(refDate)), CGFloat(value)))
						self.stepUpdater?.addSourceValue(CGFloat(value), update: false)
					}
				}
				self.stepUpdater?.updateTarget()
				self.metalView.setNeedsDisplay()
			}
		}
		
		let kg = HKUnit.gramUnitWithMetricPrefix(HKMetricPrefix.Kilo)
		let weightQuery = HKSampleQuery(sampleType:weight, predicate: nil, limit: Int(seriesCapacity), sortDescriptors: [sort]) { (query, samples, error) -> Void in
			if let array = samples {
				for sample in (array as! [HKQuantitySample]) {
					let x = CGFloat(sample.startDate.timeIntervalSinceDate(refDate))
					let val = sample.quantity.doubleValueForUnit(kg)
					self.weightSeries?.addPoint(CGPointMake(x, CGFloat(val)))
					self.weightUpdater?.addSourceValue(CGFloat(val), update: false)
				}
				self.weightUpdater?.updateTarget()
				self.metalView.setNeedsDisplay()
			}
		}
		let mmHg = HKUnit.millimeterOfMercuryUnit()
		let systolicQuery = HKSampleQuery(sampleType:systolic, predicate: nil, limit: Int(seriesCapacity), sortDescriptors: [sort]) { (query, samples, error) -> Void in
			if let array = samples {
				for sample in (array as! [HKQuantitySample]) {
					let x = CGFloat(sample.startDate.timeIntervalSinceDate(refDate))
					let val = sample.quantity.doubleValueForUnit(mmHg)
					self.systolicSeries?.addPoint(CGPointMake(x, CGFloat(val)))
					self.pressureUpdater?.addSourceValue(CGFloat(val), update: false)
				}
				self.pressureUpdater?.updateTarget()
				self.metalView.setNeedsDisplay()
			}
		}
		let diastolicQuery = HKSampleQuery(sampleType:diastolic, predicate: nil, limit: Int(seriesCapacity), sortDescriptors: [sort]) { (query, samples, error) -> Void in
			if let array = samples {
				for sample in (array as! [HKQuantitySample]) {
					let x = CGFloat(sample.startDate.timeIntervalSinceDate(refDate))
					let val = sample.quantity.doubleValueForUnit(mmHg)
					self.diastolicSeries?.addPoint(CGPointMake(x, CGFloat(val)))
					self.pressureUpdater?.addSourceValue(CGFloat(val), update: false)
				}
				self.pressureUpdater?.updateTarget()
				self.metalView.setNeedsDisplay()
			}
		}
		store.executeQuery(stepQuery)
		store.executeQuery(weightQuery)
		store.executeQuery(systolicQuery)
		store.executeQuery(diastolicQuery)
	}
	
	let calendar : NSCalendar = NSCalendar(calendarIdentifier: NSCalendarIdentifierGregorian)!
