Skip to content

Commit

Permalink
Fix issues caused by missing data for Germany, Italy, and Spain.
Browse files Browse the repository at this point in the history
  • Loading branch information
mhdhejazi committed May 15, 2020
1 parent b232831 commit dc32ca3
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 8 deletions.
2 changes: 2 additions & 0 deletions Corona/Chart/TopChartView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,8 @@ class TopChartView: BaseBarChartView {
chartView.leftAxis.resetCustomAxisMax()
}

chartView.xAxis.setLabelCount(entries.count, force: false)

chartView.data = BarChartData(dataSet: dataSet)

if animated {
Expand Down
12 changes: 10 additions & 2 deletions Corona/Chart/TrendlineChartView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class TrendlineChartView: BaseLineChartView {
})
stackView.distribution = .fillProportionally
stackView.alignment = .top
stackView.spacing = 8
stackView.spacing = 4
stackView.translatesAutoresizingMaskIntoConstraints = false

self.addSubview(stackView)
Expand Down Expand Up @@ -159,8 +159,14 @@ class TrendlineChartView: BaseLineChartView {
.lazy
.sorted { $0.key < $1.key }
.drop { $0.value.number(for: mode) < (mode == .deaths ? 10 : 100) }
}.filter { !$0.isEmpty }

guard !serieses.isEmpty else {
chartView.data = nil
return
}
let totalDays = serieses.map { $0.count }.sorted().dropLast().last! /// Next to the longest (to deal with China case)

let totalDays = serieses.map { $0.count }.sorted().last!
let entries = zip(serieses.indices, serieses).map { (regionIndex, series) in
zip(series.indices.prefix(totalDays), series).map { (index, pair) -> ChartDataEntry in
let value = Double(pair.value.number(for: mode))
Expand Down Expand Up @@ -219,8 +225,10 @@ class TrendlineChartView: BaseLineChartView {
}

private func updateLegend(regions: [Region]) {
legendStack.arrangedSubviews.forEach { $0.isHidden = true }
zip(regions, legendStack.arrangedSubviews).forEach { (region, view) in
if let stack = view as? UIStackView, let colorIndex = regions.firstIndex(of: region) {
stack.isHidden = false
(stack.arrangedSubviews.first as? UILabel)?.textColor = colors[colorIndex % colors.count]
(stack.arrangedSubviews.last as? UILabel)?.text = region.localizedName.replacingOccurrences(of: " ", with: "\n")
}
Expand Down
17 changes: 11 additions & 6 deletions Corona/Data/DataManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -126,10 +126,11 @@ extension DataManager {

/// Data from Bing
if let regions = result.bing {
for region in regions {
for region in regions where !region.subRegions.isEmpty {
if let regionCode = Locale.isoCode(from: region.name),
let existingRegion = self.world.find(subRegionCode: regionCode) {
existingRegion.add(subRegions: region.subRegions, addSubData: false)
let existingRegion = self.world.find(subRegionCode: regionCode),
existingRegion.subRegions.count < region.subRegions.count / 2 {
existingRegion.subRegions = region.subRegions
}
}
}
Expand Down Expand Up @@ -157,6 +158,7 @@ extension DataManager {

/// Countries
var countries = [Region]()
var newCountries = [Region]()
countries += regions.filter { !$0.isProvince }
let provinceRegions = regions.filter { $0.isProvince }
Dictionary(grouping: provinceRegions, by: { $0.parentName }).values.forEach { subRegions in
Expand All @@ -169,12 +171,15 @@ extension DataManager {
/// Otherwise, create a new country region
if let newCountry = Region.join(subRegions: subRegions) {
countries.append(newCountry)
newCountries.append(newCountry)
}
}

/// Update US time series
if let timeSeriesRegion = timeSeriesRegions?.first(where: { $0.name == "US" }) {
countries.first { $0.name == "US" }?.timeSeries = timeSeriesRegion.timeSeries
/// Update the time series for the newly created countries
newCountries.forEach { country in
if let region = timeSeriesRegions?.first(where: { $0 == country }) {
country.timeSeries = region.timeSeries
}
}

/// World
Expand Down

0 comments on commit dc32ca3

Please sign in to comment.