Skip to content

Commit

Permalink
feat: initialized fixed scale for charts (#1787)
Browse files Browse the repository at this point in the history
  • Loading branch information
exelban committed Apr 4, 2024
1 parent 2fdf6d6 commit 8a27539
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 10 deletions.
31 changes: 22 additions & 9 deletions Kit/plugins/Charts.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public struct circle_segment {
}
}

private func scaleValue(scale: Scale = .linear, value: Double, maxValue: Double, maxHeight: CGFloat) -> CGFloat {
private func scaleValue(scale: Scale = .linear, value: Double, maxValue: Double, maxHeight: CGFloat, limit: Double) -> CGFloat {
var value = value
if scale == .none && value > 1 && maxValue != 0 {
value /= maxValue
Expand Down Expand Up @@ -51,6 +51,11 @@ private func scaleValue(scale: Scale = .linear, value: Double, maxValue: Double,
if localMaxValue > 0 {
localMaxValue = log(maxValue*100)
}
case .fixed:
if value > limit {
value = limit
}
localMaxValue = limit
default: break
}

Expand Down Expand Up @@ -114,15 +119,18 @@ public class LineChartView: NSView {
public var transparent: Bool = true
public var color: NSColor = .controlAccentColor
public var suffix: String = "%"
public var scale: Scale

private var scale: Scale
private var fixedScale: Double

private var cursor: NSPoint? = nil
private var stop: Bool = false
private let dateFormatter = DateFormatter()

public init(frame: NSRect, num: Int, scale: Scale = .none) {
public init(frame: NSRect, num: Int, scale: Scale = .none, fixedScale: Double = 1) {
self.points = Array(repeating: nil, count: num)
self.scale = scale
self.fixedScale = fixedScale

super.init(frame: frame)

Expand Down Expand Up @@ -175,7 +183,7 @@ public class LineChartView: NSView {
}
let point = CGPoint(
x: (CGFloat(i) * xRatio) + dirtyRect.origin.x,
y: scaleValue(scale: self.scale, value: v.value, maxValue: maxValue, maxHeight: height) + dirtyRect.origin.y + offset
y: scaleValue(scale: self.scale, value: v.value, maxValue: maxValue, maxHeight: height, limit: self.fixedScale) + dirtyRect.origin.y + offset
)
line.append(point)
list.append((value: v, point: point))
Expand Down Expand Up @@ -295,8 +303,9 @@ public class LineChartView: NSView {
}
}

public func setScale(_ newScale: Scale) {
public func setScale(_ newScale: Scale, fixedScale: Double = 1) {
self.scale = newScale
self.fixedScale = fixedScale
if self.window?.isVisible ?? false {
self.display()
}
Expand Down Expand Up @@ -341,19 +350,22 @@ public class NetworkChartView: NSView {

private var minMax: Bool = false
private var scale: Scale = .none
private var fixedScale: Double
private var commonScale: Bool = true
private var reverseOrder: Bool = false

private var cursorToolTip: Bool = false
private var cursor: NSPoint? = nil
private var stop: Bool = false

public init(frame: NSRect, num: Int, minMax: Bool = true, outColor: NSColor = .systemRed, inColor: NSColor = .systemBlue, toolTip: Bool = false) {
public init(frame: NSRect, num: Int, minMax: Bool = true, outColor: NSColor = .systemRed, inColor: NSColor = .systemBlue, toolTip: Bool = false, scale: Scale = .none, fixedScale: Double = 1) {
self.minMax = minMax
self.points = Array(repeating: (0, 0), count: num)
self.topColor = inColor
self.bottomColor = outColor
self.cursorToolTip = toolTip
self.scale = scale
self.fixedScale = fixedScale
super.init(frame: frame)

self.addTrackingArea(NSTrackingArea(
Expand Down Expand Up @@ -410,11 +422,11 @@ public class NetworkChartView: NSView {

let topYPoint = { (point: Int) -> CGFloat in
let value = self.reverseOrder ? points[point].1 : points[point].0
return scaleValue(scale: self.scale, value: value, maxValue: topMax, maxHeight: self.frame.height/2) + xCenter
return scaleValue(scale: self.scale, value: value, maxValue: topMax, maxHeight: self.frame.height/2, limit: self.fixedScale) + xCenter
}
let bottomYPoint = { (point: Int) -> CGFloat in
let value = self.reverseOrder ? points[point].0 : points[point].1
return xCenter - scaleValue(scale: self.scale, value: value, maxValue: bottomMax, maxHeight: self.frame.height/2)
return xCenter - scaleValue(scale: self.scale, value: value, maxValue: bottomMax, maxHeight: self.frame.height/2, limit: self.fixedScale)
}

let topList = points.enumerated().compactMap { (i: Int, v: (Double, Double)) -> (value: Double, point: CGPoint) in
Expand Down Expand Up @@ -572,8 +584,9 @@ public class NetworkChartView: NSView {
}
}

public func setScale(_ newScale: Scale, _ commonScale: Bool) {
public func setScale(_ newScale: Scale, _ commonScale: Bool, _ fixedScale: Double = 1) {
self.scale = newScale
self.fixedScale = fixedScale
self.commonScale = commonScale

if self.window?.isVisible ?? false {
Expand Down
4 changes: 3 additions & 1 deletion Kit/types.swift
Original file line number Diff line number Diff line change
Expand Up @@ -308,9 +308,11 @@ extension Scale: CaseIterable {
public static var square: Scale { return Scale(key: "square", value: "Square") }
public static var cube: Scale { return Scale(key: "cube", value: "Cube") }
public static var logarithmic: Scale { return Scale(key: "logarithmic", value: "Logarithmic") }
public static var separator2: Scale { return Scale(key: "separator", value: "separator") }
public static var fixed: Scale { return Scale(key: "fixed", value: "Fixed") }

public static var allCases: [Scale] {
return [.none, .separator, .linear, .square, .cube, .logarithmic]
return [.none, .separator, .linear, .square, .cube, .logarithmic, .separator2, .fixed]
}

public static func fromString(_ key: String, defaultValue: Scale = .linear) -> Scale {
Expand Down

0 comments on commit 8a27539

Please sign in to comment.