83 lines
2.6 KiB
Swift
83 lines
2.6 KiB
Swift
//
|
|
// HeaderStatsView.swift
|
|
// Feels
|
|
//
|
|
// Created by Trey Tartt on 1/8/22.
|
|
//
|
|
|
|
import SwiftUI
|
|
import Charts
|
|
|
|
struct HeaderStatsView : 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.addDataSet(dataSet)
|
|
return data
|
|
}
|
|
|
|
typealias UIViewType = BarChartView
|
|
|
|
}
|
|
|
|
|
|
|
|
struct HeaderStatsView_Previews: PreviewProvider {
|
|
static var previews: some View {
|
|
HeaderStatsView(entries: [
|
|
//x - position of a bar, y - height of a bar
|
|
BarChartDataEntry(x: 1, y: 1),
|
|
BarChartDataEntry(x: 2, y: 4),
|
|
BarChartDataEntry(x: 3, y: 3),
|
|
BarChartDataEntry(x: 4, y: 2),
|
|
BarChartDataEntry(x: 5, y: 1)
|
|
]).frame(minHeight: 85, maxHeight: 90)
|
|
}
|
|
}
|