Mercurial > public > stock-charts
diff 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 |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Sources/StockCharts/LineChart/Helpers/ChartLabel.swift Fri Apr 30 17:40:33 2021 +0200 @@ -0,0 +1,50 @@ +// +// ChartLabel.swift +// StockCharts +// +// Created by Dennis Concepción Martín on 30/4/21. +// + +import SwiftUI + +public struct ChartLabel: View { + var data: [Double] + var dates: [String]? + var hours: [String]? + + @Binding var indexPosition: Int // Data point position + + public var body: some View { + HStack { + Group { + if let dates = self.dates { + let date = formatStringDate(dates[indexPosition]) + Text(date) + } + if let hours = self.hours { + let hour = hours[indexPosition] + Text(hour) + } + Text("\(data[indexPosition], specifier: "%.2f")") + .foregroundColor(Color(.systemBlue)) + } + .font(.headline) + } + } + + /* + Take string in format yy-MM-dd (2021-01-01) and transform it + to long default string format + */ + public func formatStringDate(_ stringDate: String) -> String { + let dateFormatter = DateFormatter() + dateFormatter.dateFormat = "yy-MM-dd" + let date = dateFormatter.date(from: stringDate) + + // Format date to the final format + dateFormatter.dateStyle = .long + let finalDate = dateFormatter.string(from: date!) + + return finalDate + } +}