Skip to content

Commit cf01866

Browse files
committed
Localize timeseries tooltips
1 parent ebfa317 commit cf01866

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

Kit/plugins/Charts.swift

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,8 @@ internal func scaleValue(scale: Scale = .linear, value: Double, maxValue: Double
7373
return y
7474
}
7575

76+
private let subtitleFont = NSFont.systemFont(ofSize: 9, weight: .medium)
77+
7678
private func drawToolTip(_ frame: NSRect, _ point: CGPoint, _ size: CGSize, value: String, subtitle: String? = nil) {
7779
guard !value.isEmpty else { return }
7880

@@ -111,7 +113,7 @@ private func drawToolTip(_ frame: NSRect, _ point: CGPoint, _ size: CGSize, valu
111113
str.draw(with: rect)
112114

113115
if let subtitle {
114-
attributes[NSAttributedString.Key.font] = NSFont.systemFont(ofSize: 9, weight: .medium)
116+
attributes[NSAttributedString.Key.font] = subtitleFont
115117
attributes[NSAttributedString.Key.foregroundColor] = (isDarkMode ? NSColor.white : NSColor.textColor).withAlphaComponent(0.7)
116118
rect = CGRect(x: position.x, y: position.y, width: size.width-8, height: 9)
117119
str = NSAttributedString.init(string: subtitle, attributes: attributes)
@@ -138,6 +140,7 @@ public class LineChartView: NSView {
138140
private var scale: Scale
139141
private var fixedScale: Double
140142
private var zeroValue: Double
143+
private var subtitleWidth: Double = 0
141144

142145
private var cursor: NSPoint? = nil
143146
private var stop: Bool = false
@@ -152,7 +155,9 @@ public class LineChartView: NSView {
152155

153156
super.init(frame: frame)
154157

155-
self.dateFormatter.dateFormat = "dd/MM HH:mm:ss"
158+
self.dateFormatter.locale = Locale.current
159+
self.dateFormatter.dateStyle = .short
160+
self.dateFormatter.timeStyle = .medium
156161

157162
self.addTrackingArea(NSTrackingArea(
158163
rect: CGRect(x: 0, y: 0, width: self.frame.width, height: self.frame.height),
@@ -335,10 +340,11 @@ public class LineChartView: NSView {
335340
path.stroke()
336341

337342
let date = self.dateFormatter.string(from: nearest.value.ts)
343+
self.subtitleWidth = max(self.subtitleWidth, date.widthOfString(usingFont: subtitleFont))
338344
let roundedValue = (nearest.value.value * 100).rounded(toPlaces: 2)
339345
let strValue = roundedValue >= 1 ? "\(Int(roundedValue))\(suffix)" : "\(roundedValue)\(suffix)"
340346
let value = toolTipFunc != nil ? toolTipFunc!(nearest.value) : strValue
341-
drawToolTip(self.frame, CGPoint(x: nearest.point.x+4, y: nearest.point.y+4), CGSize(width: 78, height: height), value: value, subtitle: date)
347+
drawToolTip(self.frame, CGPoint(x: nearest.point.x+4, y: nearest.point.y+4), CGSize(width: self.subtitleWidth+8, height: height), value: value, subtitle: date)
342348
}
343349
}
344350
}

0 commit comments

Comments
 (0)