Skip to content

Commit

Permalink
Manually applied changes from PR AppPear#170
Browse files Browse the repository at this point in the history
  • Loading branch information
emuwakwe committed Sep 21, 2023
1 parent 102b51b commit b31b932
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 17 deletions.
18 changes: 15 additions & 3 deletions Sources/SwiftUICharts/LineChart/LineView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ public struct LineView: View {
public var darkModeStyle: ChartStyle
public var valueSpecifier: String
public var legendSpecifier: String

public var xAxisData: [CustomStringConvertible]?



@Environment(\.colorScheme) var colorScheme: ColorScheme
@State private var showLegend = false
@State private var dragLocation:CGPoint = .zero
Expand All @@ -31,7 +34,9 @@ public struct LineView: View {
legend: String? = nil,
style: ChartStyle = Styles.lineChartStyleOne,
valueSpecifier: String? = "%.1f",
legendSpecifier: String? = "%.2f") {
legendSpecifier: String? = "%.2f",
xAxisData: [CustomStringConvertible]? = nil,
) {

self.data = ChartData(points: data)
self.title = title
Expand All @@ -40,6 +45,7 @@ public struct LineView: View {
self.valueSpecifier = valueSpecifier!
self.legendSpecifier = legendSpecifier!
self.darkModeStyle = style.darkModeStyle != nil ? style.darkModeStyle! : Styles.lineViewDarkMode
self.xAxisData = xAxisData
}

public var body: some View {
Expand Down Expand Up @@ -86,7 +92,8 @@ public struct LineView: View {
}
.frame(width: geometry.frame(in: .local).size.width, height: 240)
.offset(x: 0, y: 40 )
MagnifierRect(currentNumber: self.$currentDataNumber, valueSpecifier: self.valueSpecifier)
// MagnifierRect(currentNumber: self.$currentDataNumber, valueSpecifier: self.valueSpecifier)
MagnifierRect(currentNumber: self.$currentDataNumber, currentXValue: self.$currentXValue, valueSpecifier: self.valueSpecifier)
.opacity(self.opacity)
.offset(x: self.dragLocation.x - geometry.frame(in: .local).size.width/2, y: 36)
}
Expand Down Expand Up @@ -116,6 +123,11 @@ public struct LineView: View {
let index:Int = Int(floor((toPoint.x-15)/stepWidth))
if (index >= 0 && index < points.count){
self.currentDataNumber = points[index]
if let xAxisData = xAxisData,
(index >= 0 && index < xAxisData.count)
{
self.currentXValue = xAxisData[index]
}
return CGPoint(x: CGFloat(index)*stepWidth, y: CGFloat(points[index])*stepHeight)
}
return .zero
Expand Down
33 changes: 19 additions & 14 deletions Sources/SwiftUICharts/LineChart/MagnifierRect.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,31 @@ import SwiftUI

public struct MagnifierRect: View {
@Binding var currentNumber: Double
@Binding var currentXValue: CustomStringConvertible?
var valueSpecifier:String
@Environment(\.colorScheme) var colorScheme: ColorScheme
public var body: some View {
ZStack{
VStack{
Text("\(self.currentNumber, specifier: valueSpecifier)")
.font(.system(size: 18, weight: .bold))
.offset(x: 0, y:-110)
.foregroundColor(self.colorScheme == .dark ? Color.white : Color.black)
if (self.colorScheme == .dark ){
RoundedRectangle(cornerRadius: 16)
.stroke(Color.white, lineWidth: self.colorScheme == .dark ? 2 : 0)
.frame(width: 60, height: 260)
}else{
RoundedRectangle(cornerRadius: 16)
.frame(width: 60, height: 280)
.foregroundColor(Color.white)
.shadow(color: Colors.LegendText, radius: 12, x: 0, y: 6 )
.blendMode(.multiply)
.padding(16)
Spacer()
if let currentValue = currentXValue {
Text(String(describing: currentValue))
.font(.system(size: 18, weight: .semibold, design: .default))
.foregroundColor(self.colorScheme == .dark ? Color.white : Color.gray)
.padding(16)
}
}
.offset(x: 0, y: -15)
.background(colorScheme == .dark ?
AnyView(RoundedRectangle(cornerRadius: 16)
.stroke(Color.white, lineWidth: self.colorScheme == .dark ? 2 : 0))
:
AnyView(RoundedRectangle(cornerRadius: 16)
.foregroundColor(Color.white)
.shadow(color: Colors.LegendText, radius: 12, x: 0, y: 6 )
.blendMode(.multiply))
)
}
}
}

0 comments on commit b31b932

Please sign in to comment.