Skip to content

Commit

Permalink
Add legend format specifier option in LineView (#186)
Browse files Browse the repository at this point in the history
Co-authored-by: ClaesCJ <claes@airwallet.net>
  • Loading branch information
Cljak10 and ClaesCJ committed Mar 26, 2021
1 parent eca6eda commit 9115a99
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
3 changes: 2 additions & 1 deletion Sources/SwiftUICharts/LineChart/Legend.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ struct Legend: View {
@Binding var frame: CGRect
@Binding var hideHorizontalLines: Bool
@Environment(\.colorScheme) var colorScheme: ColorScheme
var specifier: String = "%.2f"
let padding:CGFloat = 3

var stepWidth: CGFloat {
Expand Down Expand Up @@ -42,7 +43,7 @@ struct Legend: View {
ZStack(alignment: .topLeading){
ForEach((0...4), id: \.self) { height in
HStack(alignment: .center){
Text("\(self.getYLegendSafe(height: height), specifier: "%.2f")").offset(x: 0, y: self.getYposition(height: height) )
Text("\(self.getYLegendSafe(height: height), specifier: specifier)").offset(x: 0, y: self.getYposition(height: height) )
.foregroundColor(Colors.LegendText)
.font(.caption)
self.line(atHeight: self.getYLegendSafe(height: height), width: self.frame.width)
Expand Down
9 changes: 6 additions & 3 deletions Sources/SwiftUICharts/LineChart/LineView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ public struct LineView: View {
public var legend: String?
public var style: ChartStyle
public var darkModeStyle: ChartStyle
public var valueSpecifier:String
public var valueSpecifier: String
public var legendSpecifier: String

@Environment(\.colorScheme) var colorScheme: ColorScheme
@State private var showLegend = false
Expand All @@ -29,13 +30,15 @@ public struct LineView: View {
title: String? = nil,
legend: String? = nil,
style: ChartStyle = Styles.lineChartStyleOne,
valueSpecifier: String? = "%.1f") {
valueSpecifier: String? = "%.1f",
legendSpecifier: String? = "%.2f") {

self.data = ChartData(points: data)
self.title = title
self.legend = legend
self.style = style
self.valueSpecifier = valueSpecifier!
self.legendSpecifier = legendSpecifier!
self.darkModeStyle = style.darkModeStyle != nil ? style.darkModeStyle! : Styles.lineViewDarkMode
}

Expand All @@ -60,7 +63,7 @@ public struct LineView: View {
.foregroundColor(self.colorScheme == .dark ? self.darkModeStyle.backgroundColor : self.style.backgroundColor)
if(self.showLegend){
Legend(data: self.data,
frame: .constant(reader.frame(in: .local)), hideHorizontalLines: self.$hideHorizontalLines)
frame: .constant(reader.frame(in: .local)), hideHorizontalLines: self.$hideHorizontalLines, specifier: legendSpecifier)
.transition(.opacity)
.animation(Animation.easeOut(duration: 1).delay(1))
}
Expand Down

0 comments on commit 9115a99

Please sign in to comment.