From a9f72cda999c50098f3d3b430ab4d231e0e997bf Mon Sep 17 00:00:00 2001 From: Serhiy Mytrovtsiy Date: Sat, 22 Mar 2025 22:47:53 +0100 Subject: [PATCH] fix: fixed line and bar charts title labels --- Kit/Widgets/BarChart.swift | 28 +++++++++++++++++----------- Kit/Widgets/LineChart.swift | 29 ++++++++++++++++++----------- Kit/helpers.swift | 4 ++-- 3 files changed, 37 insertions(+), 24 deletions(-) diff --git a/Kit/Widgets/BarChart.swift b/Kit/Widgets/BarChart.swift index 7cdb694e..f32ef8d1 100644 --- a/Kit/Widgets/BarChart.swift +++ b/Kit/Widgets/BarChart.swift @@ -25,6 +25,8 @@ public class BarChart: WidgetWrapper { private var boxSettingsView: NSSwitch? = nil private var frameSettingsView: NSSwitch? = nil + public var NSLabelCharts: [NSAttributedString] = [] + public init(title: String, config: NSDictionary?, preview: Bool = false) { var widgetTitle: String = title @@ -83,6 +85,19 @@ public class BarChart: WidgetWrapper { self.invalidateIntrinsicContentSize() self.display() } + + let style = NSMutableParagraphStyle() + style.alignment = .center + let stringAttributes = [ + NSAttributedString.Key.font: NSFont.systemFont(ofSize: 7, weight: .regular), + NSAttributedString.Key.foregroundColor: NSColor.textColor, + NSAttributedString.Key.paragraphStyle: style + ] + + for char in String(self.title.prefix(3)).uppercased().reversed() { + let str = NSAttributedString.init(string: "\(char)", attributes: stringAttributes) + self.NSLabelCharts.append(str) + } } required init?(coder: NSCoder) { @@ -131,22 +146,13 @@ public class BarChart: WidgetWrapper { } if self.labelState { - let style = NSMutableParagraphStyle() - style.alignment = .center - let stringAttributes = [ - NSAttributedString.Key.font: NSFont.systemFont(ofSize: 7, weight: .regular), - NSAttributedString.Key.foregroundColor: NSColor.textColor, - NSAttributedString.Key.paragraphStyle: style - ] - let letterHeight = self.frame.height / 3 let letterWidth: CGFloat = 6.0 var yMargin: CGFloat = 0 - for char in String(self.title.prefix(3)).uppercased().reversed() { + for char in self.NSLabelCharts { let rect = CGRect(x: x, y: yMargin, width: letterWidth, height: letterHeight) - let str = NSAttributedString.init(string: "\(char)", attributes: stringAttributes) - str.draw(with: rect) + char.draw(with: rect) yMargin += letterHeight } diff --git a/Kit/Widgets/LineChart.swift b/Kit/Widgets/LineChart.swift index 78d76d92..dae43713 100644 --- a/Kit/Widgets/LineChart.swift +++ b/Kit/Widgets/LineChart.swift @@ -57,6 +57,8 @@ public class LineChart: WidgetWrapper { private var boxSettingsView: NSSwitch? = nil private var frameSettingsView: NSSwitch? = nil + public var NSLabelCharts: [NSAttributedString] = [] + public init(title: String, config: NSDictionary?, preview: Bool = false) { var widgetTitle: String = title if config != nil { @@ -117,6 +119,19 @@ public class LineChart: WidgetWrapper { self.chart.points = list self._value = 0.38 } + + let style = NSMutableParagraphStyle() + style.alignment = .center + let stringAttributes = [ + NSAttributedString.Key.font: NSFont.systemFont(ofSize: 7, weight: .regular), + NSAttributedString.Key.foregroundColor: NSColor.textColor, + NSAttributedString.Key.paragraphStyle: style + ] + + for char in String(self.title.prefix(3)).uppercased().reversed() { + let str = NSAttributedString.init(string: "\(char)", attributes: stringAttributes) + self.NSLabelCharts.append(str) + } } required init?(coder: NSCoder) { @@ -156,24 +171,16 @@ public class LineChart: WidgetWrapper { } if self.labelState { - let style = NSMutableParagraphStyle() - style.alignment = .center - let stringAttributes = [ - NSAttributedString.Key.font: NSFont.systemFont(ofSize: 7, weight: .regular), - NSAttributedString.Key.foregroundColor: NSColor.textColor, - NSAttributedString.Key.paragraphStyle: style - ] - let letterHeight = self.frame.height / 3 let letterWidth: CGFloat = 6.0 var yMargin: CGFloat = 0 - for char in String(self.title.prefix(3)).uppercased().reversed() { + for char in self.NSLabelCharts { let rect = CGRect(x: x, y: yMargin, width: letterWidth, height: letterHeight) - let str = NSAttributedString.init(string: "\(char)", attributes: stringAttributes) - str.draw(with: rect) + char.draw(with: rect) yMargin += letterHeight } + width += letterWidth + Constants.Widget.spacing x = letterWidth + Constants.Widget.spacing } diff --git a/Kit/helpers.swift b/Kit/helpers.swift index 0e5f831a..4b19d381 100644 --- a/Kit/helpers.swift +++ b/Kit/helpers.swift @@ -807,12 +807,12 @@ internal class WidgetLabelView: NSView { NSAttributedString.Key.paragraphStyle: style ] - let title = self.title.prefix(3) + let title = String(self.title.prefix(3)).uppercased().reversed() let letterHeight = self.frame.height / 3 let letterWidth: CGFloat = self.frame.height / CGFloat(title.count) var yMargin: CGFloat = 0 - for char in title.uppercased().reversed() { + for char in title { let rect = CGRect(x: 0, y: yMargin, width: letterWidth, height: letterHeight-1) let str = NSAttributedString.init(string: "\(char)", attributes: stringAttributes) str.draw(with: rect)