@@ -17,6 +17,15 @@ extension StatsInsightsFilterDimension {
17
17
return NSLocalizedString ( " Line Chart depicting Visitors for insights. " , comment: " This description is used to set the accessibility label for the Insights chart, with Visitors selected. " )
18
18
}
19
19
}
20
+
21
+ var analyticsProperty : String {
22
+ switch self {
23
+ case . views:
24
+ return " views "
25
+ case . visitors:
26
+ return " visitors "
27
+ }
28
+ }
20
29
}
21
30
22
31
// MARK: - InsightsLineChart
@@ -29,10 +38,6 @@ class InsightsLineChart {
29
38
private( set) var lineChartData : [ LineChartDataConvertible ] = [ ]
30
39
private( set) var lineChartStyling : [ LineChartStyling ] = [ ]
31
40
32
- private( set) var thisWeekTotal : Int = 0
33
- private( set) var prevWeekTotal : Int = 0
34
-
35
-
36
41
init ( data: [ StatsSummaryTimeIntervalDataAsAWeek ] , filterDimension: StatsInsightsFilterDimension = . views) {
37
42
rawChartData = data
38
43
self . filterDimension = filterDimension
@@ -45,47 +50,36 @@ class InsightsLineChart {
45
50
46
51
private static let dataSetValueFormatter = DefaultValueFormatter ( decimals: 0 )
47
52
53
+ /// Transforms the raw data into the line chart data and styling.
54
+ /// similar to PeriodChart transform
55
+ /// - Returns: A tuple containing the line chart data and styling.
48
56
func transform( ) -> ( lineChartData: [ LineChartDataConvertible ] , lineChartStyling: [ LineChartStyling ] ) {
49
57
var thisWeekEntries = [ ChartDataEntry] ( )
50
58
var prevWeekEntries = [ ChartDataEntry] ( )
51
59
52
60
switch filterDimension {
53
61
case . views:
54
- rawChartData. forEach { statsSummaryTimeIntervalDataAsAWeek in
55
- switch statsSummaryTimeIntervalDataAsAWeek {
56
- case . thisWeek( let data) :
57
- thisWeekTotal = data. summaryData. compactMap ( { $0. viewsCount} ) . reduce ( 0 , + )
58
-
59
- for (index, statsSummaryData) in data. summaryData. enumerated ( ) {
60
- thisWeekEntries. append ( ChartDataEntry ( x: Double ( index) , y: Double ( statsSummaryData. viewsCount) ) )
61
- }
62
- case . prevWeek( let data) :
63
- prevWeekTotal = data. summaryData. compactMap ( { $0. viewsCount} ) . reduce ( 0 , + )
64
-
65
- for (index, statsSummaryData) in data. summaryData. enumerated ( ) {
66
- prevWeekEntries. append ( ChartDataEntry ( x: Double ( index) , y: Double ( statsSummaryData. viewsCount) ) )
67
- }
68
- }
69
- }
62
+ ( thisWeekEntries, prevWeekEntries) = filterData ( path: \StatsSummaryData . viewsCount)
70
63
case . visitors:
71
- rawChartData. forEach { statsSummaryTimeIntervalDataAsAWeek in
72
- switch statsSummaryTimeIntervalDataAsAWeek {
73
- case . thisWeek( let data) :
74
- thisWeekTotal = data. summaryData. compactMap ( { $0. visitorsCount} ) . reduce ( 0 , + )
75
-
76
- for (index, statsSummaryData) in data. summaryData. enumerated ( ) {
77
- thisWeekEntries. append ( ChartDataEntry ( x: Double ( index) , y: Double ( statsSummaryData. visitorsCount) ) )
78
- }
79
- case . prevWeek( let data) :
80
- prevWeekTotal = data. summaryData. compactMap ( { $0. visitorsCount} ) . reduce ( 0 , + )
81
-
82
- for (index, statsSummaryData) in data. summaryData. enumerated ( ) {
83
- prevWeekEntries. append ( ChartDataEntry ( x: Double ( index) , y: Double ( statsSummaryData. visitorsCount) ) )
84
- }
85
- }
86
- }
64
+ ( thisWeekEntries, prevWeekEntries) = filterData ( path: \StatsSummaryData . visitorsCount)
87
65
}
88
66
67
+ var chartData = createLineChartData ( thisWeekEntries: thisWeekEntries, prevWeekEntries: prevWeekEntries)
68
+ let lineChartDataConvertibles = createLineChartDataConvertibles ( chartData: chartData)
69
+
70
+ let chartStyling : [ LineChartStyling ] = [
71
+ ViewsInsightsLineChartStyling ( primaryLineColor: Constants . primaryLineColorViews,
72
+ secondaryLineColor: Constants . secondaryLineColor,
73
+ primaryHighlightColor: Constants . primaryHighlightColor) ,
74
+ VisitorsInsightsLineChartStyling ( primaryLineColor: Constants . primaryLineColorVisitors,
75
+ secondaryLineColor: Constants . secondaryLineColor,
76
+ primaryHighlightColor: Constants . primaryHighlightColor) ,
77
+ ]
78
+
79
+ return ( lineChartDataConvertibles, chartStyling)
80
+ }
81
+
82
+ func createLineChartData( thisWeekEntries: [ ChartDataEntry ] , prevWeekEntries: [ ChartDataEntry ] ) -> [ LineChartData ] {
89
83
var chartData = [ LineChartData] ( )
90
84
91
85
let thisWeekDataSet = LineChartDataSet ( entries: thisWeekEntries,
@@ -96,7 +90,12 @@ class InsightsLineChart {
96
90
let viewsChartData = LineChartData ( dataSets: viewsDataSets)
97
91
chartData. append ( viewsChartData)
98
92
93
+ return chartData
94
+ }
95
+
96
+ func createLineChartDataConvertibles( chartData: [ LineChartData ] ) -> [ LineChartDataConvertible ] {
99
97
var lineChartDataConvertibles = [ LineChartDataConvertible] ( )
98
+
100
99
for filterDimension in StatsInsightsFilterDimension . allCases {
101
100
let filterIndex = filterDimension. rawValue
102
101
@@ -108,17 +107,27 @@ class InsightsLineChart {
108
107
break
109
108
}
110
109
111
- let horizontalAxisFormatter = HorizontalAxisFormatter ( initialDateInterval: 1.0 , period: . day)
112
- let chartStyling : [ LineChartStyling ] = [
113
- ViewsInsightsLineChartStyling ( primaryLineColor: primaryLineColor ( forFilterDimension: . views) ,
114
- secondaryLineColor: secondaryLineColor ( ) ,
115
- primaryHighlightColor: primaryHighlightColor ( ) ) ,
116
- VisitorsInsightsLineChartStyling ( primaryLineColor: primaryLineColor ( forFilterDimension: . visitors) ,
117
- secondaryLineColor: secondaryLineColor ( ) ,
118
- primaryHighlightColor: primaryHighlightColor ( ) ) ,
119
- ]
110
+ return lineChartDataConvertibles
111
+ }
120
112
121
- return ( lineChartDataConvertibles, chartStyling)
113
+ func filterData( path: KeyPath < StatsSummaryData , Int > ) -> ( thisWeekEntries: [ ChartDataEntry ] , prevWeekEntries: [ ChartDataEntry ] ) {
114
+ var thisWeekEntries = [ ChartDataEntry] ( )
115
+ var prevWeekEntries = [ ChartDataEntry] ( )
116
+
117
+ rawChartData. forEach { statsSummaryTimeIntervalDataAsAWeek in
118
+ switch statsSummaryTimeIntervalDataAsAWeek {
119
+ case . thisWeek( let data) :
120
+ for (index, statsSummaryData) in data. summaryData. enumerated ( ) {
121
+ thisWeekEntries. append ( ChartDataEntry ( x: Double ( index) , y: Double ( statsSummaryData [ keyPath: path] ) ) )
122
+ }
123
+ case . prevWeek( let data) :
124
+ for (index, statsSummaryData) in data. summaryData. enumerated ( ) {
125
+ prevWeekEntries. append ( ChartDataEntry ( x: Double ( index) , y: Double ( statsSummaryData [ keyPath: path] ) ) )
126
+ }
127
+ }
128
+ }
129
+
130
+ return ( thisWeekEntries: thisWeekEntries, prevWeekEntries: prevWeekEntries)
122
131
}
123
132
124
133
func primaryLineColor( forFilterDimension filterDimension: StatsInsightsFilterDimension ) -> UIColor {
@@ -129,21 +138,14 @@ class InsightsLineChart {
129
138
return UIColor ( light: . muriel( name: . purple, . shade50) , dark: . muriel( name: . purple, . shade50) )
130
139
}
131
140
}
132
-
133
- func secondaryLineColor( ) -> UIColor {
134
- return UIColor ( light: . textQuaternary, dark: . textTertiary)
135
- }
136
-
137
- func primaryHighlightColor( ) -> UIColor ? {
138
- return Constants . primaryHighlightColor
139
- }
140
141
}
141
142
142
143
private extension InsightsLineChart {
143
144
enum Constants {
144
- static var primaryHighlightColor : UIColor {
145
- return UIColor ( red: 209.0 / 255.0 , green: 209.0 / 255.0 , blue: 214.0 / 255.0 , alpha: 1.0 )
146
- }
145
+ static let primaryHighlightColor : UIColor = UIColor ( red: 209.0 / 255.0 , green: 209.0 / 255.0 , blue: 214.0 / 255.0 , alpha: 1.0 )
146
+ static let secondaryLineColor : UIColor = UIColor ( light: . textQuaternary, dark: . textTertiary)
147
+ static let primaryLineColorViews : UIColor = UIColor ( light: . muriel( name: . blue, . shade50) , dark: . muriel( name: . blue, . shade50) )
148
+ static let primaryLineColorVisitors : UIColor = UIColor ( light: . muriel( name: . purple, . shade50) , dark: . muriel( name: . purple, . shade50) )
147
149
}
148
150
}
149
151
@@ -162,7 +164,7 @@ private struct ViewsInsightsLineChartStyling: LineChartStyling {
162
164
let primaryHighlightColor : UIColor ?
163
165
let labelColor : UIColor = UIColor ( light: . secondaryLabel, dark: . tertiaryLabel)
164
166
let legendColor : UIColor ? = . primary( . shade60)
165
- let legendTitle : String ? = NSLocalizedString ( " Views " , comment: " This appears in the legend of the insights line chart" )
167
+ let legendTitle : String ? = NSLocalizedString ( " Views " , comment: " Title for Views count in the legend of the Stats Insights views and visitors line chart" )
166
168
let lineColor : UIColor = . neutral( . shade5)
167
169
let yAxisValueFormatter : IAxisValueFormatter = VerticalAxisFormatter ( )
168
170
}
@@ -175,7 +177,7 @@ private struct VisitorsInsightsLineChartStyling: LineChartStyling {
175
177
let primaryHighlightColor : UIColor ?
176
178
let labelColor : UIColor = UIColor ( light: . secondaryLabel, dark: . tertiaryLabel)
177
179
let legendColor : UIColor ? = . primary( . shade60)
178
- let legendTitle : String ? = NSLocalizedString ( " Visitors " , comment: " This appears in the legend of the insights line chart" )
180
+ let legendTitle : String ? = NSLocalizedString ( " Visitors " , comment: " Title for Visitors count in the legend of the Stats Insights views and visitors line chart" )
179
181
let lineColor : UIColor = . neutral( . shade5)
180
182
let yAxisValueFormatter : IAxisValueFormatter = VerticalAxisFormatter ( )
181
183
}
0 commit comments