Skip to content

Commit b31b932

Browse files
committed
Manually applied changes from PR AppPear#170
1 parent 102b51b commit b31b932

2 files changed

Lines changed: 34 additions & 17 deletions

File tree

Sources/SwiftUICharts/LineChart/LineView.swift

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,10 @@ public struct LineView: View {
1616
public var darkModeStyle: ChartStyle
1717
public var valueSpecifier: String
1818
public var legendSpecifier: String
19-
19+
public var xAxisData: [CustomStringConvertible]?
20+
21+
22+
2023
@Environment(\.colorScheme) var colorScheme: ColorScheme
2124
@State private var showLegend = false
2225
@State private var dragLocation:CGPoint = .zero
@@ -31,7 +34,9 @@ public struct LineView: View {
3134
legend: String? = nil,
3235
style: ChartStyle = Styles.lineChartStyleOne,
3336
valueSpecifier: String? = "%.1f",
34-
legendSpecifier: String? = "%.2f") {
37+
legendSpecifier: String? = "%.2f",
38+
xAxisData: [CustomStringConvertible]? = nil,
39+
) {
3540

3641
self.data = ChartData(points: data)
3742
self.title = title
@@ -40,6 +45,7 @@ public struct LineView: View {
4045
self.valueSpecifier = valueSpecifier!
4146
self.legendSpecifier = legendSpecifier!
4247
self.darkModeStyle = style.darkModeStyle != nil ? style.darkModeStyle! : Styles.lineViewDarkMode
48+
self.xAxisData = xAxisData
4349
}
4450

4551
public var body: some View {
@@ -86,7 +92,8 @@ public struct LineView: View {
8692
}
8793
.frame(width: geometry.frame(in: .local).size.width, height: 240)
8894
.offset(x: 0, y: 40 )
89-
MagnifierRect(currentNumber: self.$currentDataNumber, valueSpecifier: self.valueSpecifier)
95+
// MagnifierRect(currentNumber: self.$currentDataNumber, valueSpecifier: self.valueSpecifier)
96+
MagnifierRect(currentNumber: self.$currentDataNumber, currentXValue: self.$currentXValue, valueSpecifier: self.valueSpecifier)
9097
.opacity(self.opacity)
9198
.offset(x: self.dragLocation.x - geometry.frame(in: .local).size.width/2, y: 36)
9299
}
@@ -116,6 +123,11 @@ public struct LineView: View {
116123
let index:Int = Int(floor((toPoint.x-15)/stepWidth))
117124
if (index >= 0 && index < points.count){
118125
self.currentDataNumber = points[index]
126+
if let xAxisData = xAxisData,
127+
(index >= 0 && index < xAxisData.count)
128+
{
129+
self.currentXValue = xAxisData[index]
130+
}
119131
return CGPoint(x: CGFloat(index)*stepWidth, y: CGFloat(points[index])*stepHeight)
120132
}
121133
return .zero

Sources/SwiftUICharts/LineChart/MagnifierRect.swift

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,26 +9,31 @@ import SwiftUI
99

1010
public struct MagnifierRect: View {
1111
@Binding var currentNumber: Double
12+
@Binding var currentXValue: CustomStringConvertible?
1213
var valueSpecifier:String
1314
@Environment(\.colorScheme) var colorScheme: ColorScheme
1415
public var body: some View {
15-
ZStack{
16+
VStack{
1617
Text("\(self.currentNumber, specifier: valueSpecifier)")
1718
.font(.system(size: 18, weight: .bold))
18-
.offset(x: 0, y:-110)
1919
.foregroundColor(self.colorScheme == .dark ? Color.white : Color.black)
20-
if (self.colorScheme == .dark ){
21-
RoundedRectangle(cornerRadius: 16)
22-
.stroke(Color.white, lineWidth: self.colorScheme == .dark ? 2 : 0)
23-
.frame(width: 60, height: 260)
24-
}else{
25-
RoundedRectangle(cornerRadius: 16)
26-
.frame(width: 60, height: 280)
27-
.foregroundColor(Color.white)
28-
.shadow(color: Colors.LegendText, radius: 12, x: 0, y: 6 )
29-
.blendMode(.multiply)
20+
.padding(16)
21+
Spacer()
22+
if let currentValue = currentXValue {
23+
Text(String(describing: currentValue))
24+
.font(.system(size: 18, weight: .semibold, design: .default))
25+
.foregroundColor(self.colorScheme == .dark ? Color.white : Color.gray)
26+
.padding(16)
3027
}
3128
}
32-
.offset(x: 0, y: -15)
29+
.background(colorScheme == .dark ?
30+
AnyView(RoundedRectangle(cornerRadius: 16)
31+
.stroke(Color.white, lineWidth: self.colorScheme == .dark ? 2 : 0))
32+
:
33+
AnyView(RoundedRectangle(cornerRadius: 16)
34+
.foregroundColor(Color.white)
35+
.shadow(color: Colors.LegendText, radius: 12, x: 0, y: 6 )
36+
.blendMode(.multiply))
37+
)
3338
}
34-
}
39+
}

0 commit comments

Comments
 (0)