Skip to content

Commit a9f72cd

Browse files
committed
fix: fixed line and bar charts title labels
1 parent 69ccc54 commit a9f72cd

File tree

3 files changed

+37
-24
lines changed

3 files changed

+37
-24
lines changed

Kit/Widgets/BarChart.swift

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ public class BarChart: WidgetWrapper {
2525
private var boxSettingsView: NSSwitch? = nil
2626
private var frameSettingsView: NSSwitch? = nil
2727

28+
public var NSLabelCharts: [NSAttributedString] = []
29+
2830
public init(title: String, config: NSDictionary?, preview: Bool = false) {
2931
var widgetTitle: String = title
3032

@@ -83,6 +85,19 @@ public class BarChart: WidgetWrapper {
8385
self.invalidateIntrinsicContentSize()
8486
self.display()
8587
}
88+
89+
let style = NSMutableParagraphStyle()
90+
style.alignment = .center
91+
let stringAttributes = [
92+
NSAttributedString.Key.font: NSFont.systemFont(ofSize: 7, weight: .regular),
93+
NSAttributedString.Key.foregroundColor: NSColor.textColor,
94+
NSAttributedString.Key.paragraphStyle: style
95+
]
96+
97+
for char in String(self.title.prefix(3)).uppercased().reversed() {
98+
let str = NSAttributedString.init(string: "\(char)", attributes: stringAttributes)
99+
self.NSLabelCharts.append(str)
100+
}
86101
}
87102

88103
required init?(coder: NSCoder) {
@@ -131,22 +146,13 @@ public class BarChart: WidgetWrapper {
131146
}
132147

133148
if self.labelState {
134-
let style = NSMutableParagraphStyle()
135-
style.alignment = .center
136-
let stringAttributes = [
137-
NSAttributedString.Key.font: NSFont.systemFont(ofSize: 7, weight: .regular),
138-
NSAttributedString.Key.foregroundColor: NSColor.textColor,
139-
NSAttributedString.Key.paragraphStyle: style
140-
]
141-
142149
let letterHeight = self.frame.height / 3
143150
let letterWidth: CGFloat = 6.0
144151

145152
var yMargin: CGFloat = 0
146-
for char in String(self.title.prefix(3)).uppercased().reversed() {
153+
for char in self.NSLabelCharts {
147154
let rect = CGRect(x: x, y: yMargin, width: letterWidth, height: letterHeight)
148-
let str = NSAttributedString.init(string: "\(char)", attributes: stringAttributes)
149-
str.draw(with: rect)
155+
char.draw(with: rect)
150156
yMargin += letterHeight
151157
}
152158

Kit/Widgets/LineChart.swift

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ public class LineChart: WidgetWrapper {
5757
private var boxSettingsView: NSSwitch? = nil
5858
private var frameSettingsView: NSSwitch? = nil
5959

60+
public var NSLabelCharts: [NSAttributedString] = []
61+
6062
public init(title: String, config: NSDictionary?, preview: Bool = false) {
6163
var widgetTitle: String = title
6264
if config != nil {
@@ -117,6 +119,19 @@ public class LineChart: WidgetWrapper {
117119
self.chart.points = list
118120
self._value = 0.38
119121
}
122+
123+
let style = NSMutableParagraphStyle()
124+
style.alignment = .center
125+
let stringAttributes = [
126+
NSAttributedString.Key.font: NSFont.systemFont(ofSize: 7, weight: .regular),
127+
NSAttributedString.Key.foregroundColor: NSColor.textColor,
128+
NSAttributedString.Key.paragraphStyle: style
129+
]
130+
131+
for char in String(self.title.prefix(3)).uppercased().reversed() {
132+
let str = NSAttributedString.init(string: "\(char)", attributes: stringAttributes)
133+
self.NSLabelCharts.append(str)
134+
}
120135
}
121136

122137
required init?(coder: NSCoder) {
@@ -156,24 +171,16 @@ public class LineChart: WidgetWrapper {
156171
}
157172

158173
if self.labelState {
159-
let style = NSMutableParagraphStyle()
160-
style.alignment = .center
161-
let stringAttributes = [
162-
NSAttributedString.Key.font: NSFont.systemFont(ofSize: 7, weight: .regular),
163-
NSAttributedString.Key.foregroundColor: NSColor.textColor,
164-
NSAttributedString.Key.paragraphStyle: style
165-
]
166-
167174
let letterHeight = self.frame.height / 3
168175
let letterWidth: CGFloat = 6.0
169176

170177
var yMargin: CGFloat = 0
171-
for char in String(self.title.prefix(3)).uppercased().reversed() {
178+
for char in self.NSLabelCharts {
172179
let rect = CGRect(x: x, y: yMargin, width: letterWidth, height: letterHeight)
173-
let str = NSAttributedString.init(string: "\(char)", attributes: stringAttributes)
174-
str.draw(with: rect)
180+
char.draw(with: rect)
175181
yMargin += letterHeight
176182
}
183+
177184
width += letterWidth + Constants.Widget.spacing
178185
x = letterWidth + Constants.Widget.spacing
179186
}

Kit/helpers.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -807,12 +807,12 @@ internal class WidgetLabelView: NSView {
807807
NSAttributedString.Key.paragraphStyle: style
808808
]
809809

810-
let title = self.title.prefix(3)
810+
let title = String(self.title.prefix(3)).uppercased().reversed()
811811
let letterHeight = self.frame.height / 3
812812
let letterWidth: CGFloat = self.frame.height / CGFloat(title.count)
813813

814814
var yMargin: CGFloat = 0
815-
for char in title.uppercased().reversed() {
815+
for char in title {
816816
let rect = CGRect(x: 0, y: yMargin, width: letterWidth, height: letterHeight-1)
817817
let str = NSAttributedString.init(string: "\(char)", attributes: stringAttributes)
818818
str.draw(with: rect)

0 commit comments

Comments
 (0)