Skip to content

Commit

Permalink
Merge pull request #354 from SUPLA/features
Browse files Browse the repository at this point in the history
Top margin on charts improved
  • Loading branch information
przemyslawzygmunt authored Feb 28, 2024
2 parents 09626f6 + 359f297 commit 4d7bb94
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -347,44 +347,19 @@ class BaseHistoryDetailVM: BaseViewModel<BaseHistoryDetailViewState, BaseHistory
private func handleMeasurements(data: ChartData, range: DaysRange?, chartState: TemperatureChartState) {
updateView {
let dataWithActiveSet = data.activateSets(setIds: chartState.visibleSets)
let minLeftValue = dataWithActiveSet.sets
.filter { $0.setId.type.leftAxis() }
.map { $0.entries.map { $0.map { $0.value } }.minOrNull() }
.minOrNull()
let maxLeftValue = dataWithActiveSet.sets
.filter { $0.setId.type.leftAxis() }
.map { $0.entries.map { $0.map { $0.value } }.maxOrNull() }
.maxOrNull()
let minRightValue = dataWithActiveSet.sets
.filter { $0.setId.type.rightAxis() }
.map { $0.entries.map { $0.map { $0.value } }.minOrNull() }
.minOrNull()
let maxRightValue = dataWithActiveSet.sets
.filter { $0.setId.type.rightAxis() }
.map { $0.entries.map { $0.map { $0.value } }.maxOrNull() }
.maxOrNull()

return $0.changing(path: \.chartData, to: dataWithActiveSet)
.changing(path: \.withRightAxis, to: dataWithActiveSet.sets.first(where: { $0.setId.type.rightAxis() && $0.active }) != nil)
.changing(path: \.withLeftAxis, to: dataWithActiveSet.sets.first(where: { $0.setId.type.leftAxis() && $0.active }) != nil)
.changing(path: \.maxLeftAxis, to: getMaxAxisValue(minLeftValue, maxLeftValue))
.changing(path: \.minLeftAxis, to: minLeftValue?.minus(2))
.changing(path: \.maxRightAxis, to: getMaxAxisValue(minRightValue, maxRightValue))
.changing(path: \.maxLeftAxis, to: dataWithActiveSet.getAxisMaxValue({ $0.leftAxis() }))
.changing(path: \.minLeftAxis, to: dataWithActiveSet.getAxisMinValueRaw({ $0.leftAxis() })?.minus(2))
.changing(path: \.maxRightAxis, to: dataWithActiveSet.getAxisMaxValue({ $0.rightAxis() }))
.changing(path: \.minDate, to: range?.start ?? $0.minDate)
.changing(path: \.maxDate, to: range?.end ?? $0.maxDate)
.changing(path: \.loading, to: false)
}
}

private func getMaxAxisValue(_ min: Double?, _ max: Double?) -> Double? {
if let min = min,
let max = max {
return max * 1.2 - (min * 0.2)
} else {
return nil
}
}

private func updateUserState() {
@Singleton<UserStateHolder> var userStateHolder

Expand Down
12 changes: 12 additions & 0 deletions SUPLA/Model/Chart/DataType/BarChartData.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,18 @@ final class BarChartData: ChartData {
override func newInstance(sets: [HistoryDataSet]) -> ChartData {
BarChartData(dateRange, chartRange, aggregation, sets)
}

override func getAxisMaxValue(_ filter: (ChartEntryType) -> Bool) -> Double? {
let maxValue = super.getAxisMaxValue(filter)

if let maxValue = maxValue, maxValue <= 0 {
if let minValue = getAxisMinValueRaw(filter) {
return abs(minValue) * CHART_TOP_MARGIN
}
}

return maxValue
}

private func barDataSet(_ set: [ChartDataEntry], _ color: UIColor, _ type: ChartEntryType) -> BarChartDataSet {
let set = BarChartDataSet(entries: set, label: "")
Expand Down
48 changes: 40 additions & 8 deletions SUPLA/Model/Chart/DataType/ChartData.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/

let CHART_TOP_MARGIN = 0.2

/**
For bar chart we need to place all values next to each other
(distance between values must be equal to 1: x2-x1 = 1).
Expand Down Expand Up @@ -52,16 +54,14 @@ class ChartData: CoordinatesConverter, Equatable {
let sets: [HistoryDataSet]

var isEmpty: Bool {
get {
var empty = true
sets.forEach {
if (!$0.entries.isEmpty) {
empty = false
return
}
var empty = true
for set in sets {
if (!set.entries.isEmpty) {
empty = false
continue
}
return empty
}
return empty
}

init(
Expand Down Expand Up @@ -132,6 +132,38 @@ class ChartData: CoordinatesConverter, Equatable {
fatalError("newInstance(sets:) has not been implented!")
}

func getAxisMaxValue(_ filter: (ChartEntryType) -> Bool) -> Double? {
if let maxValue = getAxisMaxValueRaw(filter),
let minValue = getAxisMinValueRaw(filter)
{
if (maxValue == minValue) {
if (maxValue == 0.0) {
return 2.0
} else {
return maxValue - (maxValue * CHART_TOP_MARGIN)
}
} else {
return (maxValue * (CHART_TOP_MARGIN + 1)) - (minValue * CHART_TOP_MARGIN)
}
}

return nil
}

func getAxisMinValueRaw(_ filter: (ChartEntryType) -> Bool) -> Double? {
sets
.filter { filter($0.setId.type) }
.map { $0.entries.map { $0.map { $0.value } }.minOrNull() }
.minOrNull()
}

func getAxisMaxValueRaw(_ filter: (ChartEntryType) -> Bool) -> Double? {
sets
.filter { filter($0.setId.type) }
.map { $0.entries.map { $0.map { $0.value } }.maxOrNull() }
.maxOrNull()
}

private func toCoordinate(_ x: Double?) -> Double? {
if let x = x {
return x / divider
Expand Down

0 comments on commit 4d7bb94

Please sign in to comment.