Skip to content

Instantly share code, notes, and snippets.

@azamsharp
Created January 26, 2020 05:24
Show Gist options
  • Save azamsharp/87e155b25c1f441370df006d77a4fe41 to your computer and use it in GitHub Desktop.
Save azamsharp/87e155b25c1f441370df006d77a4fe41 to your computer and use it in GitHub Desktop.
Bar Graph SwiftUI
//
// BarGraphView.swift
// bar-graph-azamsharp-weekly
//
// Created by Mohammad Azam on 1/25/20.
// Copyright © 2020 Mohammad Azam. All rights reserved.
//
import SwiftUI
struct BarGraphView: View {
let reports: [Report]
var body: some View {
VStack {
HStack(alignment: .lastTextBaseline) {
ForEach(self.reports, id: \.year) { report in
BarView(report: report)
}
}
}
}
}
struct BarView: View {
let report: Report
var body: some View {
let value = report.revenue / 500
let yValue = Swift.min(value * 20, 500)
return VStack {
Text(String(format: "$%.2f",report.revenue))
Rectangle()
.fill(report.revenue > 5000 ? Color.green : Color.red)
.frame(width: 100, height: CGFloat(yValue))
Text(report.year)
}
}
}
struct BarGraphView_Previews: PreviewProvider {
static var previews: some View {
BarGraphView(reports: Report.all())
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment