Mercurial > public > lazybear
changeset 57:fa7df6b161f4
Add PieChart to Insiders
author | Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com> |
---|---|
date | Wed, 20 Jan 2021 12:08:07 +0100 |
parents | de12e60753be |
children | c9ee25555f21 |
files | LazyBear.xcodeproj/project.pbxproj LazyBear.xcodeproj/project.xcworkspace/xcuserdata/dennis.xcuserdatad/UserInterfaceState.xcuserstate lazybear/Supply views/InsiderCharts.swift lazybear/Supply views/PieChart.swift lazybear/Supply views/StockCharts.swift lazybear/Supply views/StockStats.swift |
diffstat | 6 files changed, 116 insertions(+), 6 deletions(-) [+] |
line wrap: on
line diff
--- a/LazyBear.xcodeproj/project.pbxproj Wed Jan 20 11:46:55 2021 +0100 +++ b/LazyBear.xcodeproj/project.pbxproj Wed Jan 20 12:08:07 2021 +0100 @@ -32,6 +32,7 @@ 958E472B25B1CA8B0048E770 /* FavCompanies.swift in Sources */ = {isa = PBXBuildFile; fileRef = 958E472A25B1CA8B0048E770 /* FavCompanies.swift */; }; 959B940925B5F4BC00EEB802 /* LazyColumns.swift in Sources */ = {isa = PBXBuildFile; fileRef = 959B940825B5F4BC00EEB802 /* LazyColumns.swift */; }; 959B940C25B6058E00EEB802 /* StockStats.swift in Sources */ = {isa = PBXBuildFile; fileRef = 959B940B25B6058E00EEB802 /* StockStats.swift */; }; + 95A06A0125B8432200866C00 /* PieChart.swift in Sources */ = {isa = PBXBuildFile; fileRef = 95A06A0025B8432200866C00 /* PieChart.swift */; }; 95A1ECAF25A36127001D4A21 /* InsiderTransaction.swift in Sources */ = {isa = PBXBuildFile; fileRef = 95A1ECAE25A36127001D4A21 /* InsiderTransaction.swift */; }; 95A1ECB225A36230001D4A21 /* InsiderTransactionModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 95A1ECB125A36230001D4A21 /* InsiderTransactionModel.swift */; }; 95A1ECC525A37541001D4A21 /* TransactionRow.swift in Sources */ = {isa = PBXBuildFile; fileRef = 95A1ECC425A37541001D4A21 /* TransactionRow.swift */; }; @@ -79,6 +80,7 @@ 958E472A25B1CA8B0048E770 /* FavCompanies.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; name = FavCompanies.swift; path = "lazybear/Supply views/FavCompanies.swift"; sourceTree = SOURCE_ROOT; }; 959B940825B5F4BC00EEB802 /* LazyColumns.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; name = LazyColumns.swift; path = lazybear/Functions/LazyColumns.swift; sourceTree = SOURCE_ROOT; }; 959B940B25B6058E00EEB802 /* StockStats.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = StockStats.swift; sourceTree = "<group>"; }; + 95A06A0025B8432200866C00 /* PieChart.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PieChart.swift; sourceTree = "<group>"; }; 95A1ECAE25A36127001D4A21 /* InsiderTransaction.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; name = InsiderTransaction.swift; path = lazybear/Functions/InsiderTransaction.swift; sourceTree = SOURCE_ROOT; }; 95A1ECB125A36230001D4A21 /* InsiderTransactionModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; name = InsiderTransactionModel.swift; path = lazybear/Models/InsiderTransactionModel.swift; sourceTree = SOURCE_ROOT; }; 95A1ECC425A37541001D4A21 /* TransactionRow.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TransactionRow.swift; sourceTree = "<group>"; }; @@ -151,6 +153,7 @@ 950B79FE25B1E68D00E5DB5B /* InsiderCharts.swift */, 950B7A0325B1E7E100E5DB5B /* TransactionList.swift */, 959B940B25B6058E00EEB802 /* StockStats.swift */, + 95A06A0025B8432200866C00 /* PieChart.swift */, ); name = "Supply views"; path = "lazybear/Supply views"; @@ -325,6 +328,7 @@ 95A1ECC525A37541001D4A21 /* TransactionRow.swift in Sources */, 950B79F625B1CB7A00E5DB5B /* CompanyList.swift in Sources */, 956FAF7C25AF421E0002B2C1 /* FavCompany+CoreDataProperties.swift in Sources */, + 95A06A0125B8432200866C00 /* PieChart.swift in Sources */, 95B04EB525212369000AD27F /* ContentView.swift in Sources */, 95AB4A90259DD66D0064C9C1 /* CompanyRow.swift in Sources */, 95D1BF4925ADCF7700E5D063 /* Persistence.swift in Sources */,
Binary file LazyBear.xcodeproj/project.xcworkspace/xcuserdata/dennis.xcuserdatad/UserInterfaceState.xcuserstate has changed
--- a/lazybear/Supply views/InsiderCharts.swift Wed Jan 20 11:46:55 2021 +0100 +++ b/lazybear/Supply views/InsiderCharts.swift Wed Jan 20 12:08:07 2021 +0100 @@ -11,17 +11,42 @@ struct InsiderCharts: View { @State var transaction: InsiderTransaction @State var width: CGFloat + var body: some View { VStack { - // Add charts - + PieChart(dataPoints: dataPoints()) + .shadow(radius: 10) } } - func cumulativeSum() -> [Double] { + func dataPoints() -> [DataPoint] { + var buys = [Int]() + var sells = [Int]() + for trans in transaction.result { + if trans.acquisition_disposition == "A" { + // It's a buy + buys.append(trans.number_securities_transacted) + } + else { + // It's a sell + sells.append(trans.number_securities_transacted) + } + } - return [Double]() + // Now add up all the securities transacted + let totalBuys = buys.reduce(0, +) + let totalSells = sells.reduce(0, +) + + // Finally return DataPoint for PieChart + var PieData: [DataPoint] { + [ + DataPoint(id: 1, value: Double(totalBuys), color: .red), + DataPoint(id: 1, value: Double(totalSells), color: .green), + ] + } + + return PieData } }
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lazybear/Supply views/PieChart.swift Wed Jan 20 12:08:07 2021 +0100 @@ -0,0 +1,81 @@ +// +// PieChart.swift +// LazyBear +// +// Created by Dennis Concepción Martín on 20/1/21. +// + +import SwiftUI + +struct DataPoint: Identifiable { + let id: Int + let value: Double + let color: Color + + init(value: Double, color: Color) { + self.id = Int.random(in: 1..<Int.max) + self.value = value + self.color = color + } + + init(id: Int, value: Double, color: Color) { + self.id = Int.random(in: 1..<Int.max) + self.value = value + self.color = color + } +} + +struct PieSegment: Shape, Identifiable { + let data: DataPoint + var id: Int { data.id } + var startAngle: Double + var amount: Double + + var animatableData: AnimatablePair<Double, Double> { + get { AnimatablePair(startAngle, amount) } + set { + startAngle = newValue.first + amount = newValue.second + } + } + + func path(in rect: CGRect) -> Path { + let radius = min(rect.width, rect.height) / 2 + let center = CGPoint(x: rect.width / 2, y: rect.height / 2) + + var path = Path() + path.move(to: center) + path.addRelativeArc(center: center, radius: radius, startAngle: Angle(radians: startAngle), delta: Angle(radians: amount)) + + return path + } +} + +struct PieChart: View { + let pieSegments: [PieSegment] + + init(dataPoints: [DataPoint]) { + var segments = [PieSegment]() + let total = dataPoints.reduce(0) { $0 + $1.value } + var startAngle = -Double.pi / 2 + + for data in dataPoints { + let amount = .pi * 2 * (data.value / total) + let segment = PieSegment(data: data, startAngle: startAngle, amount: amount) + segments.append(segment) + startAngle += amount + + } + + pieSegments = segments + } + + var body: some View { + ZStack { + ForEach(pieSegments) { segment in + segment + .fill(segment.data.color) + } + } + } +}
--- a/lazybear/Supply views/StockCharts.swift Wed Jan 20 11:46:55 2021 +0100 +++ b/lazybear/Supply views/StockCharts.swift Wed Jan 20 12:08:07 2021 +0100 @@ -45,7 +45,7 @@ let maxChange = change.max()! let minChange = change.min()! - StockStats(dataMax: maxChange, dataMin: minChange, width: normalSize.0, height: normalSize.1, title: "daily percentage change") + StockStats(dataMax: maxChange, dataMin: minChange, width: normalSize.0, height: normalSize.1, title: "daily change") } }