Skip to content

Commit

Permalink
Remove unused code, update line chart
Browse files Browse the repository at this point in the history
  • Loading branch information
Shayokh144 committed Aug 19, 2024
1 parent 9750664 commit 364873c
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 65 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,11 @@ extension DateFormatter {
dateFormatter.dateFormat = "yyyy-MM"
return dateFormatter
}()

public static let monthYearShort: DateFormatter = {
let dateFormatter = DateFormatter()
dateFormatter.calendar = Calendar(identifier: .gregorian)
dateFormatter.dateFormat = "yy-MM"
return dateFormatter
}()
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import Charts
import SwiftUI

struct LineChartScreen: View {

@StateObject private var viewModel: LineChartViewModel

@ViewBuilder private var chartView: some View {
Expand All @@ -23,17 +22,33 @@ struct LineChartScreen: View {
.foregroundStyle(by: .value("Chart type", dataSeries.type))
ForEach($dataSeries.ruleMarkDataList) { $data in
RuleMark(y: .value(data.yName, data.yValue))
.foregroundStyle(Color.orange)
.lineStyle(StrokeStyle(lineWidth: 1))
.annotation(position: .bottom,
alignment: .bottomLeading) {
Text("\(data.ruleMarkName): \(data.yValue.fractionTwoDigitString)")
.foregroundColor(.orange)
}
}
}
.chartYAxis{
AxisMarks(position: .leading, values: viewModel.chartYAxisValues)
}
.chartXAxis{
AxisMarks(position: .bottom, values: viewModel.chartXAxisValues) { value in
AxisGridLine()
AxisTick()
AxisValueLabel() {
if let date = value.as(Date.self) {
Text(DateFormatter.monthYearShort.string(from: date))
}
}
}
}
.chartXScale(
domain: firstDate...lastDate
)
.aspectRatio(1, contentMode: .fit)
.frame(maxWidth: .infinity, maxHeight: .infinity)
}
}

Expand Down Expand Up @@ -75,66 +90,3 @@ struct LineChartScreen: View {
_viewModel = StateObject(wrappedValue: viewModel)
}
}

//struct LineChartScreen: View {
//
// var newData = [PetDataModel]()
//
// var data: [PetDataSeries] {
// [PetDataSeries(type: "ABC", petData: newData)]
// }
//
// let firstDate: Date
// let lastDate: Date
// var body: some View {
// Chart(data, id: \.type) { dataSeries in
// ForEach(dataSeries.petData) { data in
// LineMark(x: .value("Year", data.year, unit: .day),
// y: .value("Population", data.population))
// }
// .foregroundStyle(by: .value("Pet type", dataSeries.type))
// .symbol(by: .value("Pet type", dataSeries.type))
// RuleMark(y: .value("Average 1", 1.5))
// .annotation(position: .bottom,
// alignment: .bottomLeading) {
// Text("average 1.5")
// .foregroundColor(.orange)
// }
// RuleMark(y: .value("Average 2", 2.5))
// .annotation(position: .bottom,
// alignment: .bottomLeading) {
// Text("average 2.5")
// .foregroundColor(.orange)
// }
// }
// .chartXScale(domain: firstDate...lastDate)
// .aspectRatio(1, contentMode: .fit)
// .padding()
// }
//
// init() {
// for i in 0 ..< 10 {
// let date = Calendar.current.date(byAdding: .day, value: i, to: .now) ?? .now
// let rep = Double.random(in: 10...100)
// newData.append(
// .init(year: date, population: rep)
// )
// }
// firstDate = newData[0].year
// lastDate = newData[9].year
// }
//}
//
//struct PetDataSeries: Identifiable {
// let type: String
// let petData: [PetDataModel]
// var id: String { type }
//}
//
//
//struct PetDataModel: Identifiable {
//
// let id = UUID()
// let year: Date
// let population: Double
//}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ final class LineChartViewModel: ObservableObject {
@Published var uiModels: [LineChartDataSeries]
@Published var uiFirstDate: Date?
@Published var uiLastDate: Date?
@Published var chartYAxisValues: [Int] = []
@Published var chartXAxisValues: [Date] = []
let viewModelData: LineChartViewModelData

init(lineChartViewModelData: LineChartViewModelData) {
Expand Down Expand Up @@ -40,6 +42,27 @@ final class LineChartViewModel: ObservableObject {
ruleMarkDataList: ruleMarks
)
uiModels = [uiModel]

// Chart Data
let maxValue = Int(viewModelData.lineChartDataList.map { $0.yValue }.max() ?? 0.0) + 20
var minValue = Int(viewModelData.lineChartDataList.map { $0.yValue }.min() ?? 0.0) - 20
if minValue < 0 {
minValue = 0
}
chartYAxisValues = stride(from: minValue, to: maxValue, by: 20).map { $0 }

chartXAxisValues.removeAll()
var currentDate = firstDate
let calendar = Calendar.current
let dayInterval = Int(viewModelData.lineChartDataList.count / 10)
while currentDate <= lastDate {
chartXAxisValues.append(currentDate)
if let nextDate = calendar.date(byAdding: .day, value: dayInterval, to: currentDate) {
currentDate = nextDate
} else {
break
}
}
}

private func getRuleMarkList(
Expand Down

0 comments on commit 364873c

Please sign in to comment.