-
Notifications
You must be signed in to change notification settings - Fork 69
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #289 from Binlogo/feat/bridge_echo_enhancement
feat(examples): add sparkline and stop/start for bridge_echo on iOS
- Loading branch information
Showing
4 changed files
with
163 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import SwiftUI | ||
|
||
struct SparklineView: View { | ||
var data: [UInt64] | ||
@State private var currentIndex: Int? = nil | ||
|
||
var body: some View { | ||
GeometryReader { geometry in | ||
let height = geometry.size.height | ||
let width = geometry.size.width | ||
let maxData = data.max() ?? 1 | ||
let scale = height / CGFloat(maxData) | ||
|
||
let path = Path { path in | ||
guard let firstPoint = data.first else { return } | ||
path.move(to: CGPoint(x: 0, y: height - CGFloat(firstPoint) * scale)) | ||
|
||
for (index, value) in data.enumerated() { | ||
let x = width * CGFloat(index) / CGFloat(data.count - 1) | ||
let y = height - CGFloat(value) * scale | ||
path.addLine(to: CGPoint(x: x, y: y)) | ||
} | ||
} | ||
|
||
path.stroke(Color.blue, lineWidth: 2) | ||
.background( | ||
Color.clear | ||
.contentShape(Rectangle()) | ||
.gesture( | ||
DragGesture(minimumDistance: 0) | ||
.onChanged { value in | ||
let index = Int((value.location.x / width) * CGFloat(data.count)) | ||
if index >= 0 && index < data.count { | ||
currentIndex = index | ||
} | ||
} | ||
) | ||
) | ||
.overlay( | ||
Group { | ||
if let index = currentIndex, index < data.count { | ||
let x = width * CGFloat(index) / CGFloat(data.count - 1) | ||
let y = height - CGFloat(data[index]) * scale | ||
Text("\(data[index])") | ||
.font(.caption) | ||
.padding(5) | ||
.background(Color.white.opacity(0.8)) | ||
.cornerRadius(5) | ||
.position(x: x, y: y - 20) | ||
} | ||
} | ||
) | ||
} | ||
} | ||
} | ||
|
||
struct SparklineView_Previews: PreviewProvider { | ||
static var previews: some View { | ||
SparklineView(data: [10, 20, 15, 30, 25, 40, 35]) | ||
.frame(height: 200) | ||
.padding() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters