Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions Kit/plugins/Charts.swift
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ internal func scaleValue(scale: Scale = .linear, value: Double, maxValue: Double
return y
}

private let subtitleFont = NSFont.systemFont(ofSize: 9, weight: .medium)

private func drawToolTip(_ frame: NSRect, _ point: CGPoint, _ size: CGSize, value: String, subtitle: String? = nil) {
guard !value.isEmpty else { return }

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

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

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

super.init(frame: frame)

self.dateFormatter.dateFormat = "dd/MM HH:mm:ss"
self.dateFormatter.locale = Locale.current
self.dateFormatter.dateStyle = .short
self.dateFormatter.timeStyle = .medium

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

let date = self.dateFormatter.string(from: nearest.value.ts)
self.subtitleWidth = max(self.subtitleWidth, date.widthOfString(usingFont: subtitleFont))
let roundedValue = (nearest.value.value * 100).rounded(toPlaces: 2)
let strValue = roundedValue >= 1 ? "\(Int(roundedValue))\(suffix)" : "\(roundedValue)\(suffix)"
let value = toolTipFunc != nil ? toolTipFunc!(nearest.value) : strValue
drawToolTip(self.frame, CGPoint(x: nearest.point.x+4, y: nearest.point.y+4), CGSize(width: 78, height: height), value: value, subtitle: date)
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)
}
}
}
Expand Down