1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
| import SwiftUI
struct LineView: View { var data: [(Double)] var symbol: String? var name: String? var price: Double var yestclose: Double var compute: Double var height: CGFloat public var body: some View { HStack(alignment: .center) { VStack(alignment: .leading) { if (self.name != nil) { Text(self.name!) .font(.system(size: 14, weight: Font.Weight.bold, design: Font.Design.default)) .foregroundColor(.font_foreground) Text(self.symbol!) .font(.system(size: 11, weight: Font.Weight.medium, design: Font.Design.default)) .foregroundColor(.main_color_1) } else { Text(self.symbol!) .font(.system(size: 14, weight: Font.Weight.medium, design: Font.Design.default)) .foregroundColor(.font_foreground) } } Spacer().frame(width: 75) GeometryReader { reader in Line(data: self.data, yestclose: self.yestclose, frame: .constant(reader.frame(in: .local)) ).offset(x: 0, y: height/2) }
Spacer().frame(width: 30) VStack(alignment: .leading) { Text(String(format: "%.3f", self.price)) .font(.system(size: 14, weight: Font.Weight.bold, design: Font.Design.default)) .foregroundColor(.font_foreground) Text(String(format: "%.2f", self.compute) + "%") .font(.system(size: 11, weight: Font.Weight.bold, design: Font.Design.default)) .foregroundColor(self.compute > 0 ? .red : .green) } }.lineLimit(1) .padding(EdgeInsets.init(top: 15, leading: 15, bottom: 15, trailing: 15)) } }
//struct LineView_Previews: PreviewProvider { // static var previews: some View { // LineView(data: [8,23,54,32,12,37,7,23,43], symbol: 200.0, name: "Full chart", price: 23) // } //}
struct Line: View { var data: [(Double)] var yestclose: Double @Binding var frame: CGRect
let padding:CGFloat = 10 var stepWidth: CGFloat { if data.count < 2 { return 0 } return frame.size.width / CGFloat(data.count - 1) } var stepHeight: CGFloat { var min: Double? var max: Double? let points = self.data if let minPoint = points.min(), let maxPoint = points.max(), minPoint != maxPoint { min = minPoint max = maxPoint } else { return 0 } if let min = min, let max = max, min != max { return (frame.size.height - padding) / CGFloat(max - min) } return 0 } var path: Path { let points = self.data return Path.lineChart(points: points, step: CGPoint(x: stepWidth, y: stepHeight)) } var yesterDayPath: Path { let points = [Double](repeating: self.yestclose, count: self.data.count) return Path.lineDotted(points: points, step: CGPoint(x: stepWidth, y: stepHeight), min: self.data.min() ?? 0) } public var body: some View { ZStack { self.path .stroke(Color.font_foreground ,style: StrokeStyle(lineWidth: 1, lineJoin: .round)) .drawingGroup() self.yesterDayPath .stroke((self.data.last ?? 0) > self.yestclose ? Color.red : Color.green ,style: StrokeStyle(lineWidth: stepWidth, dash: [stepWidth/2])) .drawingGroup(opaque: false, colorMode: ColorRenderingMode.extendedLinear) } } }
|