fix: fixed line and bar charts title labels

This commit is contained in:
Serhiy Mytrovtsiy
2025-03-22 22:47:53 +01:00
parent 69ccc54910
commit a9f72cda99
3 changed files with 37 additions and 24 deletions

View File

@@ -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
}

View File

@@ -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
}

View File

@@ -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)