mirror of
https://github.com/yJason/ClashX-Dashboard.git
synced 2026-03-01 00:35:19 +08:00
fix: TrafficGraphView upperBound
This commit is contained in:
@@ -11,33 +11,33 @@ fileprivate let labelsCount = 4
|
|||||||
|
|
||||||
struct TrafficGraphView: View {
|
struct TrafficGraphView: View {
|
||||||
@Binding var values: [CGFloat]
|
@Binding var values: [CGFloat]
|
||||||
|
|
||||||
@State var graphColor: DSFColor
|
@State var graphColor: DSFColor
|
||||||
|
|
||||||
init(values: Binding<[CGFloat]>,
|
init(values: Binding<[CGFloat]>,
|
||||||
graphColor: DSFColor) {
|
graphColor: DSFColor) {
|
||||||
self._values = values
|
self._values = values
|
||||||
self.graphColor = graphColor
|
self.graphColor = graphColor
|
||||||
|
|
||||||
updateChart(values.wrappedValue)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@State private var labels = [String]()
|
@State private var labels = [String]()
|
||||||
@State private var dataSource = DSFSparkline.DataSource()
|
@State private var dataSource = DSFSparkline.DataSource()
|
||||||
|
@State private var currentMaxValue: CGFloat = 0
|
||||||
|
|
||||||
var body: some View {
|
var body: some View {
|
||||||
HStack {
|
HStack {
|
||||||
VStack {
|
VStack {
|
||||||
ForEach(Array(labels.enumerated()), id: \.offset) {
|
ForEach(labels, id: \.self) {
|
||||||
Text($0.element)
|
Text($0)
|
||||||
.font(.system(size: 11, weight: .light))
|
.font(.system(size: 11, weight: .light))
|
||||||
Spacer()
|
Spacer()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
graphView
|
graphView
|
||||||
}
|
}
|
||||||
|
.onAppear {
|
||||||
|
updateChart(values)
|
||||||
|
}
|
||||||
.onChange(of: values) { newValue in
|
.onChange(of: values) { newValue in
|
||||||
updateChart(newValue)
|
updateChart(newValue)
|
||||||
}
|
}
|
||||||
@@ -83,6 +83,13 @@ struct TrafficGraphView: View {
|
|||||||
func updateChart(_ values: [CGFloat]) {
|
func updateChart(_ values: [CGFloat]) {
|
||||||
let max = values.max() ?? CGFloat(labelsCount) * 1000
|
let max = values.max() ?? CGFloat(labelsCount) * 1000
|
||||||
|
|
||||||
|
if currentMaxValue != 0 && currentMaxValue == max {
|
||||||
|
self.dataSource.set(values: values)
|
||||||
|
return
|
||||||
|
} else {
|
||||||
|
currentMaxValue = max
|
||||||
|
}
|
||||||
|
|
||||||
let byte = Int64(max)
|
let byte = Int64(max)
|
||||||
let kb = byte / 1000
|
let kb = byte / 1000
|
||||||
|
|
||||||
@@ -111,15 +118,12 @@ struct TrafficGraphView: View {
|
|||||||
v3 = 1_000_000
|
v3 = 1_000_000
|
||||||
}
|
}
|
||||||
|
|
||||||
v1 = (v1 * 10).rounded() / 10
|
let vv = Double(labelsCount) / 10
|
||||||
|
|
||||||
let vv = v1.truncatingRemainder(dividingBy: 1) == 0 ? Double(labelsCount) : Double(labelsCount) / 10
|
|
||||||
|
|
||||||
if v1.truncatingRemainder(dividingBy: vv) != 0 {
|
if v1.truncatingRemainder(dividingBy: vv) != 0 {
|
||||||
v1 = ((v1 / vv).rounded() + 1) * vv
|
v1 = Double((Int(v1 / vv) + 1)) * vv
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
var re = [String]()
|
var re = [String]()
|
||||||
|
|
||||||
for i in 0...labelsCount {
|
for i in 0...labelsCount {
|
||||||
@@ -129,17 +133,10 @@ struct TrafficGraphView: View {
|
|||||||
re = re.reversed()
|
re = re.reversed()
|
||||||
let _ = re.removeLast()
|
let _ = re.removeLast()
|
||||||
|
|
||||||
|
let upperBound = CGFloat(v1*v3*1000)
|
||||||
|
|
||||||
self.dataSource.set(values: values)
|
self.dataSource.set(values: values)
|
||||||
|
self.dataSource.setRange(lowerBound: 0, upperBound: upperBound)
|
||||||
let upperBound = CGFloat(v1*v3)
|
|
||||||
if upperBound != 0,
|
|
||||||
let old = self.dataSource.range?.upperBound,
|
|
||||||
old != upperBound {
|
|
||||||
self.dataSource.setRange(lowerBound: 0, upperBound: upperBound)
|
|
||||||
self.dataSource.resetRange()
|
|
||||||
}
|
|
||||||
|
|
||||||
self.labels = re
|
self.labels = re
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user