From 2ddf555755d54aad109a4e463feb8e3426c0a65d Mon Sep 17 00:00:00 2001 From: Serhiy Mytrovtsiy Date: Tue, 18 Aug 2020 21:04:19 +0200 Subject: [PATCH] - redraw Network chart - add max visible axis value to the Network chart (#70) --- Modules/Net/popup.swift | 6 +- StatsKit/Charts.swift | 156 ++++++++++++++++++++++------------------ 2 files changed, 89 insertions(+), 73 deletions(-) diff --git a/Modules/Net/popup.swift b/Modules/Net/popup.swift index 309ea890..e058d8bc 100644 --- a/Modules/Net/popup.swift +++ b/Modules/Net/popup.swift @@ -42,7 +42,7 @@ internal class Popup: NSView { private var initialized: Bool = false private var processesInitialized: Bool = false - private var chart: MultiLinesChartView? = nil + private var chart: NetworkChartView? = nil private var processes: [NetworkProcessView] = [] public init() { @@ -96,7 +96,7 @@ internal class Popup: NSView { view.layer?.backgroundColor = NSColor.lightGray.withAlphaComponent(0.1).cgColor view.layer?.cornerRadius = 3 - self.chart = MultiLinesChartView(frame: NSRect(x: 1, y: 0, width: view.frame.width, height: view.frame.height), num: 120) + self.chart = NetworkChartView(frame: NSRect(x: 1, y: 0, width: view.frame.width, height: view.frame.height), num: 120) view.addSubview(self.chart!) @@ -260,7 +260,7 @@ internal class Popup: NSView { self.initialized = true } - self.chart?.addValues([Double(value.upload), Double(value.download)]) + self.chart?.addValue(upload: Double(value.upload), download: Double(value.download)) }) } diff --git a/StatsKit/Charts.swift b/StatsKit/Charts.swift index e7cf1993..bab943d1 100644 --- a/StatsKit/Charts.swift +++ b/StatsKit/Charts.swift @@ -80,7 +80,6 @@ public class LineChartView: NSView { } lineColor.setStroke() - context.saveGState() let underLinePath = linePath.copy() as! NSBezierPath @@ -109,14 +108,12 @@ public class LineChartView: NSView { } } -public class MultiLinesChartView: NSView { - public var points: [[Double]]? = nil - public var transparent: Bool = true +public class NetworkChartView: NSView { + private var points: [(Double, Double)]? = nil + private var colors: [NSColor] = [NSColor.systemRed, NSColor.systemBlue] - public var colors: [NSColor] = [NSColor.systemRed, NSColor.systemBlue] - - public init(frame: NSRect, num: Int, size: Int = 2) { - self.points = Array(repeating: Array(repeating: 0, count: num), count: size) + public init(frame: NSRect, num: Int) { + self.points = Array(repeating: (0, 0), count: num) super.init(frame: frame) } @@ -134,76 +131,95 @@ public class MultiLinesChartView: NSView { guard let context = NSGraphicsContext.current?.cgContext else { return } context.setShouldAntialias(true) - var maxY: Double = self.points!.map{ $0.max() ?? 0 }.max() ?? 0 - if maxY == 0 { - maxY = 1 + var uploadMax: Double = self.points!.map{ $0.0 }.max() ?? 0 + var downloadMax: Double = self.points!.map{ $0.1 }.max() ?? 0 + if uploadMax == 0 { + uploadMax = 1 + } + if downloadMax == 0 { + downloadMax = 1 } - for i in 0.. CGFloat in - return (CGFloat(point) * xRatio) + dirtyRect.origin.x - } - let columnYPoint = { (point: Int) -> CGFloat in - return CGFloat((points[point] * Double(height)) / maxY) + dirtyRect.origin.y + 0.5 - } - - let linePath = NSBezierPath() - let x: CGFloat = columnXPoint(0) - let y: CGFloat = columnYPoint(0) - linePath.move(to: CGPoint(x: x, y: y)) - - for i in 1.. CGFloat in + return (CGFloat(point) * xRatio) + dirtyRect.origin.x } + let uploadYPoint = { (point: Int) -> CGFloat in + return CGFloat((self.points![point].0 * Double(height/2)) / uploadMax) + dirtyRect.origin.y + zero + } + let downloadYPoint = { (point: Int) -> CGFloat in + return height/2 - (CGFloat((self.points![point].1 * Double(height/2)) / downloadMax) + dirtyRect.origin.y) + } + + let uploadlinePath = NSBezierPath() + uploadlinePath.move(to: CGPoint(x: columnXPoint(0), y: uploadYPoint(0))) + + let downloadlinePath = NSBezierPath() + downloadlinePath.move(to: CGPoint(x: columnXPoint(0), y: downloadYPoint(0))) + + for i in 1..