Mercurial > public > stock-charts
comparison InteractiveCharts/LineChart/Helpers/LinePath.swift @ 8:959175ee5ebd
Implement interaction with ChartView
author | Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com> |
---|---|
date | Mon, 26 Apr 2021 23:06:42 +0200 |
parents | f828c7c408d4 |
children |
comparison
equal
deleted
inserted
replaced
7:a9690565726b | 8:959175ee5ebd |
---|---|
8 import SwiftUI | 8 import SwiftUI |
9 | 9 |
10 struct LinePath: Shape { | 10 struct LinePath: Shape { |
11 var data: [Double] | 11 var data: [Double] |
12 var (width, height): (CGFloat, CGFloat) | 12 var (width, height): (CGFloat, CGFloat) |
13 @Binding var pathPoints: [CGPoint] | |
13 | 14 |
14 func path(in rect: CGRect) -> Path { | 15 func path(in rect: CGRect) -> Path { |
15 var path = Path() | 16 var path = Path() |
16 | 17 |
17 let normalizedData = normalize(data) | 18 let normalizedData = normalize(data) |
18 let widthBetweenDataPoints = Double(width) / Double(normalizedData.count - 1) // Remove first point | 19 let widthBetweenDataPoints = Double(width) / Double(normalizedData.count - 1) // Remove first point |
19 let initialPoint = normalizedData[0] * Double(height) | 20 let initialPoint = normalizedData[0] * Double(height) |
20 var x: Double = 0 | 21 var x: Double = 0 |
21 | |
22 var pathPoints = [CGPoint]() | |
23 | 22 |
24 path.move(to: CGPoint(x: x, y: initialPoint)) | 23 path.move(to: CGPoint(x: x, y: initialPoint)) |
25 for y in normalizedData { | 24 for y in normalizedData { |
26 if normalizedData.firstIndex(of: y) != 0 { // Skip first point | 25 if normalizedData.firstIndex(of: y) != 0 { // Skip first point |
27 x += widthBetweenDataPoints | 26 x += widthBetweenDataPoints |