make corner radius a constant create the views in the main app file and pass through so they dont get re-drawn when changing UI things
147 lines
5.8 KiB
Swift
147 lines
5.8 KiB
Swift
//
|
|
// GraphView.swift
|
|
// Feels
|
|
//
|
|
// Created by Trey Tartt on 1/8/22.
|
|
//
|
|
|
|
import Foundation
|
|
import SwiftUI
|
|
import CoreData
|
|
import Charts
|
|
|
|
struct GraphView: View {
|
|
var body: some View {
|
|
ZStack {
|
|
Color(UIColor.secondarySystemBackground)
|
|
VStack {
|
|
VStack {
|
|
HStack {
|
|
ZStack {
|
|
Color(UIColor.systemBackground)
|
|
BarChartGraph(entries: [
|
|
BarChartDataEntry(x: 1, y: Double(Int.random(in: 0...10))),
|
|
BarChartDataEntry(x: 2, y: Double(Int.random(in: 0...10))),
|
|
BarChartDataEntry(x: 3, y: Double(Int.random(in: 0...10))),
|
|
BarChartDataEntry(x: 4, y: Double(Int.random(in: 0...10))),
|
|
BarChartDataEntry(x: 5, y: Double(Int.random(in: 0...10)))
|
|
])
|
|
}
|
|
.cornerRadius(Constants.viewsCornerRaidus, corners: [.topLeft, .topRight, .bottomLeft, .bottomRight])
|
|
.padding()
|
|
|
|
ZStack {
|
|
Color(UIColor.systemBackground)
|
|
BarChartGraph(entries: [
|
|
BarChartDataEntry(x: 1, y: Double(Int.random(in: 0...10))),
|
|
BarChartDataEntry(x: 2, y: Double(Int.random(in: 0...10))),
|
|
BarChartDataEntry(x: 3, y: Double(Int.random(in: 0...10))),
|
|
BarChartDataEntry(x: 4, y: Double(Int.random(in: 0...10))),
|
|
BarChartDataEntry(x: 5, y: Double(Int.random(in: 0...10)))
|
|
])
|
|
}
|
|
.cornerRadius(Constants.viewsCornerRaidus, corners: [.topLeft, .topRight, .bottomLeft, .bottomRight])
|
|
.padding()
|
|
}
|
|
|
|
ZStack {
|
|
Color(UIColor.systemBackground)
|
|
BarChartGraph(entries: [
|
|
BarChartDataEntry(x: 1, y: Double(Int.random(in: 0...10))),
|
|
BarChartDataEntry(x: 2, y: Double(Int.random(in: 0...10))),
|
|
BarChartDataEntry(x: 3, y: Double(Int.random(in: 0...10))),
|
|
BarChartDataEntry(x: 4, y: Double(Int.random(in: 0...10))),
|
|
BarChartDataEntry(x: 5, y: Double(Int.random(in: 0...10)))
|
|
])
|
|
}
|
|
.cornerRadius(Constants.viewsCornerRaidus, corners: [.topLeft, .topRight, .bottomLeft, .bottomRight])
|
|
.padding()
|
|
|
|
ZStack {
|
|
Color(UIColor.systemBackground)
|
|
BarChartGraph(entries: [
|
|
BarChartDataEntry(x: 1, y: Double(Int.random(in: 0...10))),
|
|
BarChartDataEntry(x: 2, y: Double(Int.random(in: 0...10))),
|
|
BarChartDataEntry(x: 3, y: Double(Int.random(in: 0...10))),
|
|
BarChartDataEntry(x: 4, y: Double(Int.random(in: 0...10))),
|
|
BarChartDataEntry(x: 5, y: Double(Int.random(in: 0...10)))
|
|
])
|
|
}
|
|
.cornerRadius(Constants.viewsCornerRaidus, corners: [.topLeft, .topRight, .bottomLeft, .bottomRight])
|
|
.padding()
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
struct BarChartGraph: UIViewRepresentable {
|
|
//Bar chart accepts data as array of BarChartDataEntry objects
|
|
var entries : [BarChartDataEntry]
|
|
|
|
// this func is required to conform to UIViewRepresentable protocol
|
|
func makeUIView(context: Context) -> BarChartView {
|
|
//crate new chart
|
|
let chart = BarChartView()
|
|
chart.drawGridBackgroundEnabled = false
|
|
chart.drawValueAboveBarEnabled = false
|
|
|
|
chart.xAxis.drawAxisLineEnabled = false
|
|
chart.xAxis.labelTextColor = .clear
|
|
|
|
chart.rightAxis.drawAxisLineEnabled = false
|
|
chart.rightAxis.labelTextColor = .clear
|
|
|
|
chart.leftAxis.drawAxisLineEnabled = false
|
|
chart.leftAxis.labelTextColor = .clear
|
|
|
|
chart.xAxis.drawGridLinesEnabled = false
|
|
chart.leftAxis.drawGridLinesEnabled = false
|
|
chart.leftAxis.axisLineColor = .clear
|
|
chart.rightAxis.axisLineColor = .clear
|
|
|
|
chart.legend.textColor = .clear
|
|
chart.legend.enabled = false
|
|
|
|
chart.drawBordersEnabled = false
|
|
chart.drawMarkers = false
|
|
// chart.yAxis.drawGridLinesEnabled = false
|
|
chart.rightAxis.drawGridLinesEnabled = false
|
|
chart.borderColor = .clear
|
|
//it is convenient to form chart data in a separate func
|
|
chart.data = addData()
|
|
return chart
|
|
}
|
|
|
|
// this func is required to conform to UIViewRepresentable protocol
|
|
func updateUIView(_ uiView: BarChartView, context: Context) {
|
|
//when data changes chartd.data update is required
|
|
uiView.data = addData()
|
|
}
|
|
|
|
func addData() -> BarChartData{
|
|
let data = BarChartData()
|
|
//BarChartDataSet is an object that contains information about your data, styling and more
|
|
let dataSet = BarChartDataSet(entries: entries)
|
|
// change bars color to green
|
|
dataSet.colors = [NSUIColor.green]
|
|
//change data label
|
|
data.append(dataSet)
|
|
return data
|
|
}
|
|
|
|
typealias UIViewType = BarChartView
|
|
}
|
|
|
|
|
|
struct GraphView_Previews: PreviewProvider {
|
|
static var previews: some View {
|
|
Group {
|
|
GraphView()
|
|
|
|
GraphView()
|
|
.preferredColorScheme(.dark)
|
|
}
|
|
}
|
|
}
|