Skip to content

Commit

Permalink
feat: 🎸 [JIRA:IOSSDKBUG-5] add variables to ChartLabelAttributes
Browse files Browse the repository at this point in the history
add two variables(font and backgroundColor) to ChartLabelAttributes, for
user to custom chart label attributes conveniently
  • Loading branch information
hengyi-zhang committed May 21, 2024
1 parent 234bf94 commit 851b3be
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 8 deletions.
3 changes: 2 additions & 1 deletion Sources/FioriCharts/Charts/Common/XAxisView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,9 @@ struct XAxisView: View {
Group {
if isShowLabels[index] {
Text(labels[index].title)
.font(.fiori(fixedSize: axis.labels.fontSize))
.font(axis.labels.targetFont)
.foregroundColor(axis.labels.color)
.background(axis.labels.backgroundColor)
.frame(maxWidth: rect.size.width / 2)
.position(x: labels[index].pos.x, y: labelYPos)
} else {
Expand Down
3 changes: 2 additions & 1 deletion Sources/FioriCharts/Charts/Common/YAxisView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,9 @@ struct YAxisView: View {
Group {
if isShowLabels[index] {
Text(labels[index].title)
.font(.fiori(fixedSize: axis.labels.fontSize))
.font(axis.labels.targetFont)
.foregroundColor(axis.labels.color)
.background(axis.labels.backgroundColor)
.position(x: labels[index].pos.x,
y: labels[index].pos.y)
.frame(maxWidth: rect.size.width)
Expand Down
51 changes: 45 additions & 6 deletions Sources/FioriCharts/Model/ChartLabelAttributes.swift
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import Combine
import FioriThemeManager
import Foundation
import SwiftUI

Expand All @@ -17,24 +19,59 @@ public class ChartLabelAttributes: ObservableObject, Identifiable, NSCopying {
/// True when the associated label(s) should be hidden.
@Published public var isHidden: Bool

/// Font for the label, has higher priority than fontSize.
@Published public var font: Font?
/// Backgroundcolor for the label.
@Published public var backgroundColor: Color?

/// When font not be configured, use .fiori(fixedSize: fontSize) as default;
var targetFont: Font {
if let font {
return font
} else {
return .fiori(fixedSize: self.fontSize)
}
}

public let id = UUID()

/// The constructor with color, fontSize, offset, isHidden and backgroundColor
public init(color: Color = .preferredColor(.tertiaryLabel),
fontSize: Double = 12,
offset: Double = 0,
isHidden: Bool = false)
isHidden: Bool = false,
backgroundColor: Color? = nil)
{
self._color = Published(initialValue: color)
self._fontSize = PublishedConstrainedValue(wrappedValue: CGFloat(fontSize), 0 ... 100)
self._offset = PublishedConstrainedValue(wrappedValue: CGFloat(offset), -10 ... 10)
self._isHidden = Published(initialValue: isHidden)
self._backgroundColor = Published(initialValue: backgroundColor)
}

/// The constructor with color, font, offset, isHidden and backgroundColor
public init(color: Color = .preferredColor(.tertiaryLabel),
font: Font?,
offset: Double = 0,
isHidden: Bool = false,
backgroundColor: Color? = nil)
{
self._color = Published(initialValue: color)
self._font = Published(initialValue: font)
self._offset = PublishedConstrainedValue(wrappedValue: CGFloat(offset), -10 ... 10)
self._isHidden = Published(initialValue: isHidden)
self._backgroundColor = Published(initialValue: backgroundColor)
}

public func copy(with zone: NSZone? = nil) -> Any {
ChartLabelAttributes(color: self.color,
fontSize: Double(self.fontSize),
offset: Double(self.offset),
isHidden: self.isHidden)
let chartLabelAttributes = ChartLabelAttributes(color: self.color,
fontSize: Double(self.fontSize),
offset: Double(self.offset),
isHidden: self.isHidden,
backgroundColor: self.backgroundColor)
// assign font property
chartLabelAttributes.font = self.font
return chartLabelAttributes
}
}

Expand All @@ -43,7 +80,9 @@ extension ChartLabelAttributes: Equatable {
lhs.color == rhs.color &&
lhs.fontSize == rhs.fontSize &&
lhs.offset == rhs.offset &&
lhs.isHidden == rhs.isHidden
lhs.isHidden == rhs.isHidden &&
lhs.font == rhs.font &&
lhs.backgroundColor == rhs.backgroundColor
}
}

Expand Down

0 comments on commit 851b3be

Please sign in to comment.