Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/add format option #162

Open
wants to merge 5 commits into
base: new-version
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 19 additions & 4 deletions Sources/SwiftUICharts/Base/Label/ChartLabel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,26 @@ public enum ChartLabelType {
case legend
}

public enum ChartLabelFormat {
case custom(completion: (Double) -> String)
case none

func format(value: Double) -> String {
switch self {
case .custom(let completion):
return completion(value)
case .none:
return String(format: "%.01f", value)
}
}
}

public struct ChartLabel: View {
@EnvironmentObject var chartValue: ChartValue
@State var textToDisplay:String = ""

private var title: String

private var format: ChartLabelFormat
private var labelSize: CGFloat {
switch labelType {
case .title:
Expand Down Expand Up @@ -62,9 +76,10 @@ public struct ChartLabel: View {
}

public init (_ title: String,
type: ChartLabelType = .title) {
type: ChartLabelType = .title, format: ChartLabelFormat = .none) {
self.title = title
labelType = type
self.format = format
}

public var body: some View {
Expand All @@ -78,7 +93,7 @@ public struct ChartLabel: View {
self.textToDisplay = self.title
}
.onReceive(self.chartValue.objectWillChange) { _ in
self.textToDisplay = self.chartValue.interactionInProgress ? String(format: "%.01f", self.chartValue.currentValue) : self.title
self.textToDisplay = self.chartValue.interactionInProgress ? self.format.format(value: self.chartValue.currentValue) : self.title
}
if !self.chartValue.interactionInProgress {
Spacer()
Expand Down