Mercurial > public > stock-charts
view Sources/StockCharts/LineChart/Helpers/LinePath.swift @ 72:552963ee4dc4
Fixing: Modifying state during view update
author | Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com> |
---|---|
date | Sat, 08 May 2021 19:06:00 +0200 |
parents | c37c93ba3f37 |
children | cc59a30661f7 |
line wrap: on
line source
// // LinePath.swift // StockCharts // // Created by Dennis Concepción Martín on 30/4/21. // import SwiftUI public struct LinePath: Shape { public var data: [Double] public var (width, height): (CGFloat, CGFloat) @Binding var pathPoints: [CGPoint] public func path(in rect: CGRect) -> Path { var path = Path() var pathPoints = [CGPoint]() let normalizedData = normalize(data) let widthBetweenDataPoints = Double(width) / Double(normalizedData.count - 1) // Remove first point let initialPoint = normalizedData[0] * Double(height) var x: Double = 0 path.move(to: CGPoint(x: x, y: initialPoint)) for y in normalizedData { if normalizedData.firstIndex(of: y) != 0 { // Skip first point x += widthBetweenDataPoints let y = y * Double(height) path.addLine(to: CGPoint(x: x, y: y)) } // Append current point to an array. Later will be used for Drag Gesture DispatchQueue.main.async { pathPoints.append(path.currentPoint!) } } return path } // public func test /* Get data -> normalize it -> 0 <= output <= 1 */ public func normalize(_ data: [Double]) -> [Double] { var normalData = [Double]() let min = data.min()! let max = data.max()! for value in data { let normal = (value - min) / (max - min) normalData.append(normal) } return normalData } }