comparison Sources/StockCharts/LineChart/Helpers/ChartLabel.swift @ 21:5135ff3343ae

Rename project to StockCharts
author Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
date Fri, 30 Apr 2021 17:40:33 +0200
parents Sources/InteractiveCharts/LineChart/Helpers/ChartLabel.swift@24dfde3727c1
children 127af64e264e
comparison
equal deleted inserted replaced
20:24dfde3727c1 21:5135ff3343ae
1 //
2 // ChartLabel.swift
3 // StockCharts
4 //
5 // Created by Dennis Concepción Martín on 30/4/21.
6 //
7
8 import SwiftUI
9
10 public struct ChartLabel: View {
11 var data: [Double]
12 var dates: [String]?
13 var hours: [String]?
14
15 @Binding var indexPosition: Int // Data point position
16
17 public var body: some View {
18 HStack {
19 Group {
20 if let dates = self.dates {
21 let date = formatStringDate(dates[indexPosition])
22 Text(date)
23 }
24 if let hours = self.hours {
25 let hour = hours[indexPosition]
26 Text(hour)
27 }
28 Text("\(data[indexPosition], specifier: "%.2f")")
29 .foregroundColor(Color(.systemBlue))
30 }
31 .font(.headline)
32 }
33 }
34
35 /*
36 Take string in format yy-MM-dd (2021-01-01) and transform it
37 to long default string format
38 */
39 public func formatStringDate(_ stringDate: String) -> String {
40 let dateFormatter = DateFormatter()
41 dateFormatter.dateFormat = "yy-MM-dd"
42 let date = dateFormatter.date(from: stringDate)
43
44 // Format date to the final format
45 dateFormatter.dateStyle = .long
46 let finalDate = dateFormatter.string(from: date!)
47
48 return finalDate
49 }
50 }