Skip to content

Commit eb77873

Browse files
committed
Chart: code cleanup
1 parent ae57df7 commit eb77873

File tree

1 file changed

+63
-67
lines changed

1 file changed

+63
-67
lines changed

WooCommerce/Classes/ViewRelated/Dashboard/MyStore/PeriodDataViewController.swift

Lines changed: 63 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -48,28 +48,6 @@ class PeriodDataViewController: UIViewController, IndicatorInfoProvider {
4848
}
4949
}
5050

51-
private var chartData: BarChartData? {
52-
guard let statItems = orderStats?.items, !statItems.isEmpty else {
53-
return nil
54-
}
55-
56-
var dataEntries: [BarChartDataEntry] = []
57-
var barCount = 0
58-
59-
statItems.forEach { (item) in
60-
if item.totalSales > 0.0 {
61-
// By only including the values that are greater than zero (but still incrementing barCount),
62-
// we will create nice "gaps" in the chart instead of a bunch of zero value bars.
63-
dataEntries.append(BarChartDataEntry(x: Double(barCount), y: item.totalSales))
64-
}
65-
barCount += 1
66-
}
67-
68-
let dataSet = BarChartDataSet(values: dataEntries, label: "Data")
69-
dataSet.setColor(StyleManager.wooCommerceBrandColor)
70-
return BarChartData(dataSet: dataSet)
71-
}
72-
7351
// MARK: - Initialization
7452

7553
/// Designated Initializer
@@ -145,7 +123,6 @@ private extension PeriodDataViewController {
145123
barChartView.rightAxis.enabled = false
146124
barChartView.legend.enabled = false
147125
barChartView.drawValueAboveBarEnabled = true
148-
barChartView.isUserInteractionEnabled = false
149126
barChartView.noDataText = NSLocalizedString("No data available", comment: "Text displayed when no data is available for revenue chart.")
150127
barChartView.noDataFont = StyleManager.chartLabelFont
151128
barChartView.noDataTextColor = StyleManager.wooSecondary
@@ -183,7 +160,7 @@ private extension PeriodDataViewController {
183160
}
184161

185162

186-
// MARK: - IndicatorInfoProvider Confromance
163+
// MARK: - IndicatorInfoProvider Conformance
187164
//
188165
extension PeriodDataViewController {
189166
func indicatorInfo(for pagerTabStripController: PagerTabStripViewController) -> IndicatorInfo {
@@ -192,6 +169,53 @@ extension PeriodDataViewController {
192169
}
193170

194171

172+
// MARK: - IAxisValueFormatter Conformance
173+
//
174+
extension PeriodDataViewController: IAxisValueFormatter {
175+
func stringForValue(_ value: Double, axis: AxisBase?) -> String {
176+
guard let axis = axis, let orderStats = orderStats else {
177+
return ""
178+
}
179+
180+
if axis is XAxis {
181+
if let item = orderStats.items?[Int(value)] {
182+
// FIXME: This logic could prolly be pushed into a model extension
183+
var dateString = ""
184+
switch orderStats.granularity {
185+
case .day:
186+
if let periodDate = DateFormatter.Stats.statsDayFormatter.date(from: item.period) {
187+
dateString = DateFormatter.Charts.chartsDayFormatter.string(from: periodDate)
188+
}
189+
case .week:
190+
if let periodDate = DateFormatter.Stats.statsWeekFormatter.date(from: item.period) {
191+
dateString = DateFormatter.Charts.chartsWeekFormatter.string(from: periodDate)
192+
}
193+
case .month:
194+
if let periodDate = DateFormatter.Stats.statsMonthFormatter.date(from: item.period) {
195+
dateString = DateFormatter.Charts.chartsMonthFormatter.string(from: periodDate)
196+
}
197+
case .year:
198+
if let periodDate = DateFormatter.Stats.statsYearFormatter.date(from: item.period) {
199+
dateString = DateFormatter.Charts.chartsYearFormatter.string(from: periodDate)
200+
}
201+
}
202+
203+
return dateString
204+
} else {
205+
return ""
206+
}
207+
} else {
208+
if value == 0.0 {
209+
// Do not show the 0 label on the Y axis
210+
return ""
211+
} else {
212+
return value.friendlyString()
213+
}
214+
}
215+
}
216+
}
217+
218+
195219
// MARK: - Private Helpers
196220
//
197221
private extension PeriodDataViewController {
@@ -236,7 +260,7 @@ private extension PeriodDataViewController {
236260
guard barChartView != nil else {
237261
return
238262
}
239-
barChartView.data = chartData
263+
barChartView.data = generateBarDataSet()
240264
barChartView.fitBars = true
241265
barChartView.notifyDataSetChanged()
242266
barChartView.animate(yAxisDuration: Constants.chartAnimationDuration)
@@ -245,52 +269,24 @@ private extension PeriodDataViewController {
245269
func reloadLastUpdatedField() {
246270
if lastUpdated != nil { lastUpdated.text = summaryDateUpdated }
247271
}
248-
}
249-
250272

251-
// MARK: - IAxisValueFormatter Conformance
252-
//
253-
extension PeriodDataViewController: IAxisValueFormatter {
254-
func stringForValue(_ value: Double, axis: AxisBase?) -> String {
255-
guard let axis = axis, let orderStats = orderStats else {
256-
return ""
273+
func generateBarDataSet() -> BarChartData? {
274+
guard let statItems = orderStats?.items, !statItems.isEmpty else {
275+
return nil
257276
}
258277

259-
if axis is XAxis {
260-
if let item = orderStats.items?[Int(value)] {
261-
// FIXME: This logic could prolly be pushed into a model extension
262-
var dateString = ""
263-
switch orderStats.granularity {
264-
case .day:
265-
if let periodDate = DateFormatter.Stats.statsDayFormatter.date(from: item.period) {
266-
dateString = DateFormatter.Charts.chartsDayFormatter.string(from: periodDate)
267-
}
268-
case .week:
269-
if let periodDate = DateFormatter.Stats.statsWeekFormatter.date(from: item.period) {
270-
dateString = DateFormatter.Charts.chartsWeekFormatter.string(from: periodDate)
271-
}
272-
case .month:
273-
if let periodDate = DateFormatter.Stats.statsMonthFormatter.date(from: item.period) {
274-
dateString = DateFormatter.Charts.chartsMonthFormatter.string(from: periodDate)
275-
}
276-
case .year:
277-
if let periodDate = DateFormatter.Stats.statsYearFormatter.date(from: item.period) {
278-
dateString = DateFormatter.Charts.chartsYearFormatter.string(from: periodDate)
279-
}
280-
}
281-
282-
return dateString
283-
} else {
284-
return ""
285-
}
286-
} else {
287-
if value == 0.0 {
288-
// Do not show the 0 label on the Y axis
289-
return ""
290-
} else {
291-
return value.friendlyString()
292-
}
278+
var barCount = 0
279+
var dataEntries: [BarChartDataEntry] = []
280+
statItems.forEach { (item) in
281+
dataEntries.append(BarChartDataEntry(x: Double(barCount), y: item.totalSales))
282+
barCount += 1
293283
}
284+
285+
let dataSet = BarChartDataSet(values: dataEntries, label: "Data")
286+
dataSet.setColor(StyleManager.wooCommerceBrandColor)
287+
dataSet.highlightEnabled = false
288+
dataSet.drawValuesEnabled = false // Do not draw value labels on the top of the bars
289+
return BarChartData(dataSet: dataSet)
294290
}
295291
}
296292

0 commit comments

Comments
 (0)