changeset 103:37079599f9c8

Fix normalize data line chart view
author Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
date Sun, 31 Jan 2021 19:00:23 +0100
parents a17ccf20f4a3
children eb7e80081672
files LazyBear.xcodeproj/project.pbxproj LazyBear.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved LazyBear.xcodeproj/project.xcworkspace/xcuserdata/dennis.xcuserdatad/UserInterfaceState.xcuserstate lazybear/Jobs/normalize.swift lazybear/Views/LineChart.swift lazybear/Views/Stock.swift
diffstat 6 files changed, 41 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/LazyBear.xcodeproj/project.pbxproj	Sun Jan 31 18:23:26 2021 +0100
+++ b/LazyBear.xcodeproj/project.pbxproj	Sun Jan 31 19:00:23 2021 +0100
@@ -18,6 +18,7 @@
 		95612C512598D48200F7698F /* SearchBar.swift in Sources */ = {isa = PBXBuildFile; fileRef = 95612C4F2598D48200F7698F /* SearchBar.swift */; };
 		95621AD925BF2EDB00BB17FC /* CloudKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 95621AD825BF2EDB00BB17FC /* CloudKit.framework */; };
 		95700BC625BD9D12009CEEFE /* IexApi.swift in Sources */ = {isa = PBXBuildFile; fileRef = 95700BC525BD9D12009CEEFE /* IexApi.swift */; };
+		95825AFC25C7255600465409 /* normalize.swift in Sources */ = {isa = PBXBuildFile; fileRef = 95825AFB25C7255600465409 /* normalize.swift */; };
 		958B678525C42B2400BF9F89 /* ApiAccess.swift in Sources */ = {isa = PBXBuildFile; fileRef = 958B678425C42B2400BF9F89 /* ApiAccess.swift */; };
 		9597CE0125C1DC0A004DDFED /* LogoModifier.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9597CE0025C1DC0A004DDFED /* LogoModifier.swift */; };
 		9597CE0425C1DFE7004DDFED /* LogoPlaceholder.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9597CE0325C1DFE7004DDFED /* LogoPlaceholder.swift */; };
@@ -61,6 +62,7 @@
 		95621AD725BF2EC500BB17FC /* LazyBear.entitlements */ = {isa = PBXFileReference; lastKnownFileType = text.plist.entitlements; path = LazyBear.entitlements; sourceTree = "<group>"; };
 		95621AD825BF2EDB00BB17FC /* CloudKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = CloudKit.framework; path = System/Library/Frameworks/CloudKit.framework; sourceTree = SDKROOT; };
 		95700BC525BD9D12009CEEFE /* IexApi.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; name = IexApi.swift; path = LazyBear/Network/IexApi.swift; sourceTree = SOURCE_ROOT; };
+		95825AFB25C7255600465409 /* normalize.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; name = normalize.swift; path = lazybear/Jobs/normalize.swift; sourceTree = SOURCE_ROOT; };
 		958B678425C42B2400BF9F89 /* ApiAccess.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; name = ApiAccess.swift; path = lazybear/ApiAccess.swift; sourceTree = SOURCE_ROOT; };
 		9597CE0025C1DC0A004DDFED /* LogoModifier.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LogoModifier.swift; sourceTree = "<group>"; };
 		9597CE0325C1DFE7004DDFED /* LogoPlaceholder.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LogoPlaceholder.swift; sourceTree = "<group>"; };
@@ -161,6 +163,7 @@
 			isa = PBXGroup;
 			children = (
 				95AB4A79259DCBAE0064C9C1 /* ReadJson.swift */,
+				95825AFB25C7255600465409 /* normalize.swift */,
 			);
 			path = Jobs;
 			sourceTree = "<group>";
@@ -330,6 +333,7 @@
 				954D992525A2123B001F7F60 /* HistoricalPricesModel.swift in Sources */,
 				95FE646725C2DC580052832E /* WatchlistCompany+CoreDataClass.swift in Sources */,
 				95E411A725BEE03000A9C23F /* Watchlist.swift in Sources */,
+				95825AFC25C7255600465409 /* normalize.swift in Sources */,
 				95F7CAF625ADC7B7009E0E7C /* LazyBear.xcdatamodeld in Sources */,
 				95FE646825C2DC580052832E /* WatchlistCompany+CoreDataProperties.swift in Sources */,
 			);
--- a/LazyBear.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved	Sun Jan 31 18:23:26 2021 +0100
+++ b/LazyBear.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved	Sun Jan 31 19:00:23 2021 +0100
@@ -2,6 +2,15 @@
   "object": {
     "pins": [
       {
+        "package": "Charts",
+        "repositoryURL": "https://github.com/danielgindi/Charts.git",
+        "state": {
+          "branch": null,
+          "revision": "9b24868b61a75666355d824eeb97290e7f0dca42",
+          "version": "4.0.0"
+        }
+      },
+      {
         "package": "SDWebImage",
         "repositoryURL": "https://github.com/SDWebImage/SDWebImage.git",
         "state": {
Binary file LazyBear.xcodeproj/project.xcworkspace/xcuserdata/dennis.xcuserdatad/UserInterfaceState.xcuserstate has changed
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/lazybear/Jobs/normalize.swift	Sun Jan 31 19:00:23 2021 +0100
@@ -0,0 +1,21 @@
+//
+//  normalize.swift
+//  LazyBear
+//
+//  Created by Dennis Concepción Martín on 31/1/21.
+//
+
+import SwiftUI
+
+func normalize(_ data: [Double]) -> [DataPoint] {
+    var normalData = [DataPoint]()
+    let min = data.min()!
+    let max = data.max()!
+    
+    for value in data {
+        let normal = (value - min) / (max - min)
+        normalData.append(DataPoint(value: normal))
+    }
+    
+    return normalData
+}
--- a/lazybear/Views/LineChart.swift	Sun Jan 31 18:23:26 2021 +0100
+++ b/lazybear/Views/LineChart.swift	Sun Jan 31 19:00:23 2021 +0100
@@ -22,6 +22,7 @@
                     .stroke(lineColor, lineWidth: lineWidth)
             }
             
+            // Add the data points on the line
             /*
             if lineColor != .clear {
                 LineChartShape(dataPoints: dataPoints, pointSize: pointSize, drawingLines: false)
--- a/lazybear/Views/Stock.swift	Sun Jan 31 18:23:26 2021 +0100
+++ b/lazybear/Views/Stock.swift	Sun Jan 31 19:00:23 2021 +0100
@@ -45,9 +45,12 @@
                     getUrl(range: period[selectedPeriod])
                 })
             
-            let prices = data.map { DataPoint(value: $0.close) }
-            LineChart(dataPoints: prices, lineColor: .green, lineWidth: 2)
-                .frame(height: 400)
+            let prices = data.map { $0.close }
+            if showingLineChart {
+                let normalPrices = normalize(prices)
+                LineChart(dataPoints: normalPrices, lineColor: .green, lineWidth: 2)
+                    .frame(width: 400, height: 300)
+            }
         }
         .padding([.leading, .trailing])
         .onAppear { getUrl(range: period[selectedPeriod]) }