Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
add value plot and fix crash when graphicView destroy with animating
  • Loading branch information
RiuHDuo committed Dec 8, 2017
commit d1b5dc80f896cf5980f84ce710623adfcdb7ad4f
66 changes: 66 additions & 0 deletions Classes/Drawing/ValueDrawingLayer.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
//
// ValueDrawingLayer.swift
// ScrollableGraphView
//
// Created by RiuHDuo on 2017/12/8.
// Copyright © 2017年 SGV. All rights reserved.
//

import UIKit

class ValueDrawingLayer: ScrollableGraphViewDrawingLayer {

let font: UIFont
let color: UIColor

private var textLayers = [CATextLayer]()

init(frame: CGRect, font: UIFont, color: UIColor) {

self.font = font
self.color = color

super.init(viewportWidth: frame.size.width, viewportHeight: frame.size.height)
}

required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}

private func createDataPointPath() {

for layer in self.textLayers{
layer.removeFromSuperlayer()
}

guard let activePointsInterval = self.owner?.graphViewDrawingDelegate?.intervalForActivePoints() else{ return}
for i in activePointsInterval {

var location = CGPoint.zero

if let pointLocation = owner?.graphPoint(forIndex: i) {
location = pointLocation.location
}

let layer = CATextLayer()
layer.contentsScale = UIScreen.main.scale
layer.string = String(format:"%.2f", self.owner?.data[i] ?? 0)
layer.bounds = CGRect(x: 0, y: 0, width: 60, height: 14)
layer.font = self.font.fontName as CFTypeRef
layer.fontSize = self.font.pointSize
layer.alignmentMode = kCAAlignmentCenter
layer.position = CGPoint(x: location.x, y: location.y - 10)
layer.foregroundColor = UIColor.black.cgColor
self.addSublayer(layer)
self.textLayers.append(layer)
}


}



override func updatePath() {
createDataPointPath()
}
}
40 changes: 23 additions & 17 deletions Classes/Plots/Plot.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ open class Plot {
// The id for this plot. Used when determining which data to give it in the dataSource
open var identifier: String!

weak var graphViewDrawingDelegate: ScrollableGraphViewDrawingDelegate! = nil
weak var graphViewDrawingDelegate: ScrollableGraphViewDrawingDelegate? = nil

// Animation Settings
// ##################
Expand All @@ -22,6 +22,8 @@ open class Plot {
}
}
}

var data = [Int:Double]()
/// The animation style.
open var adaptAnimationType = ScrollableGraphViewAnimationType.easeOut
/// If adaptAnimationType is set to .Custom, then this is the easing function you would like applied for the animation.
Expand Down Expand Up @@ -64,7 +66,7 @@ open class Plot {
}
}

graphViewDrawingDelegate.updatePaths()
graphViewDrawingDelegate?.updatePaths()
}

private func animate(point: GraphPoint, to position: CGPoint, withDelay delay: Double = 0) {
Expand Down Expand Up @@ -151,9 +153,10 @@ open class Plot {

let value = range.min

let position = graphViewDrawingDelegate.calculatePosition(atIndex: i, value: value)
let point = GraphPoint(position: position)
graphPoints.append(point)
if let position = graphViewDrawingDelegate?.calculatePosition(atIndex: i, value: value){
let point = GraphPoint(position: position)
graphPoints.append(point)
}
}
}

Expand All @@ -170,9 +173,10 @@ open class Plot {

let value = data[dataPosition]

let newPosition = graphViewDrawingDelegate.calculatePosition(atIndex: i, value: value)
graphPoints[i].x = newPosition.x
graphPoints[i].y = newPosition.y
if let newPosition = graphViewDrawingDelegate?.calculatePosition(atIndex: i, value: value){
graphPoints[i].x = newPosition.x
graphPoints[i].y = newPosition.y
}
}
}

Expand All @@ -185,11 +189,12 @@ open class Plot {
let dataPosition = index
let value = data[dataPosition]

let newPosition = graphViewDrawingDelegate.calculatePosition(atIndex: activatedPointIndex, value: value)
graphPoints[activatedPointIndex].x = newPosition.x
graphPoints[activatedPointIndex].y = newPosition.y

index += 1
if let newPosition = graphViewDrawingDelegate?.calculatePosition(atIndex: activatedPointIndex, value: value){
graphPoints[activatedPointIndex].x = newPosition.x
graphPoints[activatedPointIndex].y = newPosition.y

index += 1
}
}
}

Expand All @@ -200,10 +205,11 @@ open class Plot {
// For any visible points, kickoff the animation to their new position after the axis' min/max has changed.
var dataIndex = 0
for pointIndex in pointsToAnimate {
let newPosition = graphViewDrawingDelegate.calculatePosition(atIndex: pointIndex, value: data[dataIndex])
let point = graphPoints[pointIndex]
animate(point: point, to: newPosition, withDelay: Double(dataIndex) * delay)
dataIndex += 1
if let newPosition = graphViewDrawingDelegate?.calculatePosition(atIndex: pointIndex, value: data[dataIndex]){
let point = graphPoints[pointIndex]
animate(point: point, to: newPosition, withDelay: Double(dataIndex) * delay)
dataIndex += 1
}
}
}

Expand Down
31 changes: 31 additions & 0 deletions Classes/Plots/ValuePlot.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
//
// ValuePlot.swift
// ScrollableGraphView
//
// Created by RiuHDuo on 2017/12/8.
// Copyright © 2017年 SGV. All rights reserved.
//

import UIKit

open class ValuePlot: Plot {
private var dataPointLayer: ValueDrawingLayer?

open var font = UIFont.systemFont(ofSize: 10)
open var color = UIColor.black

public init(identifier: String) {
super.init()
self.identifier = identifier
}

override func layers(forViewport viewport: CGRect) -> [ScrollableGraphViewDrawingLayer?] {
createLayers(viewport: viewport)
return [dataPointLayer]
}

private func createLayers(viewport: CGRect) {
dataPointLayer = ValueDrawingLayer(frame: viewport, font: font, color: color)
dataPointLayer?.owner = self
}
}
1 change: 1 addition & 0 deletions Classes/ScrollableGraphView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -659,6 +659,7 @@ import UIKit

for i in activeInterval.startIndex ..< activeInterval.endIndex {
let dataForIndexI = dataSource?.value(forPlot: plot, atIndex: i) ?? 0
plot.data[i] = dataForIndexI
dataForInterval.append(dataForIndexI)
}

Expand Down