2016-07-10 04:11:47 -04:00
|
|
|
|
package chart
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"fmt"
|
2018-09-07 14:17:11 -04:00
|
|
|
|
"strconv"
|
2016-07-10 04:11:47 -04:00
|
|
|
|
"time"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// ValueFormatter is a function that takes a value and produces a string.
|
|
|
|
|
type ValueFormatter func(v interface{}) string
|
|
|
|
|
|
|
|
|
|
// TimeValueFormatter is a ValueFormatter for timestamps.
|
|
|
|
|
func TimeValueFormatter(v interface{}) string {
|
2017-04-25 23:01:48 -04:00
|
|
|
|
return formatTime(v, DefaultDateFormat)
|
2016-07-10 04:11:47 -04:00
|
|
|
|
}
|
|
|
|
|
|
2016-07-14 14:17:57 -04:00
|
|
|
|
// TimeHourValueFormatter is a ValueFormatter for timestamps.
|
|
|
|
|
func TimeHourValueFormatter(v interface{}) string {
|
2017-04-25 23:01:48 -04:00
|
|
|
|
return formatTime(v, DefaultDateHourFormat)
|
2016-07-14 14:17:57 -04:00
|
|
|
|
}
|
|
|
|
|
|
2016-07-17 17:25:42 -04:00
|
|
|
|
// TimeMinuteValueFormatter is a ValueFormatter for timestamps.
|
|
|
|
|
func TimeMinuteValueFormatter(v interface{}) string {
|
2017-04-25 23:01:48 -04:00
|
|
|
|
return formatTime(v, DefaultDateMinuteFormat)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// TimeDateValueFormatter is a ValueFormatter for timestamps.
|
|
|
|
|
func TimeDateValueFormatter(v interface{}) string {
|
|
|
|
|
return formatTime(v, "2006-01-02")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// TimeValueFormatterWithFormat returns a time formatter with a given format.
|
|
|
|
|
func TimeValueFormatterWithFormat(format string) ValueFormatter {
|
|
|
|
|
return func(v interface{}) string {
|
|
|
|
|
return formatTime(v, format)
|
|
|
|
|
}
|
2016-07-17 17:25:42 -04:00
|
|
|
|
}
|
|
|
|
|
|
2016-07-10 04:11:47 -04:00
|
|
|
|
// TimeValueFormatterWithFormat is a ValueFormatter for timestamps with a given format.
|
2017-04-25 23:01:48 -04:00
|
|
|
|
func formatTime(v interface{}, dateFormat string) string {
|
2016-07-10 04:11:47 -04:00
|
|
|
|
if typed, isTyped := v.(time.Time); isTyped {
|
|
|
|
|
return typed.Format(dateFormat)
|
|
|
|
|
}
|
|
|
|
|
if typed, isTyped := v.(int64); isTyped {
|
2016-07-17 17:25:42 -04:00
|
|
|
|
return time.Unix(0, typed).Format(dateFormat)
|
2016-07-10 04:11:47 -04:00
|
|
|
|
}
|
|
|
|
|
if typed, isTyped := v.(float64); isTyped {
|
2016-07-17 17:25:42 -04:00
|
|
|
|
return time.Unix(0, int64(typed)).Format(dateFormat)
|
2016-07-10 04:11:47 -04:00
|
|
|
|
}
|
2016-07-10 13:43:04 -04:00
|
|
|
|
return ""
|
2016-07-10 04:11:47 -04:00
|
|
|
|
}
|
|
|
|
|
|
2018-09-07 14:17:11 -04:00
|
|
|
|
// IntValueFormatter is a ValueFormatter for float64.
|
|
|
|
|
func IntValueFormatter(v interface{}) string {
|
|
|
|
|
switch v.(type) {
|
|
|
|
|
case int:
|
|
|
|
|
return strconv.Itoa(v.(int))
|
|
|
|
|
case int64:
|
|
|
|
|
return strconv.FormatInt(v.(int64), 10)
|
|
|
|
|
case float32:
|
|
|
|
|
return strconv.FormatInt(int64(v.(float32)), 10)
|
|
|
|
|
case float64:
|
|
|
|
|
return strconv.FormatInt(int64(v.(float64)), 10)
|
|
|
|
|
default:
|
|
|
|
|
return ""
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-07-10 04:11:47 -04:00
|
|
|
|
// FloatValueFormatter is a ValueFormatter for float64.
|
|
|
|
|
func FloatValueFormatter(v interface{}) string {
|
2016-07-14 14:17:57 -04:00
|
|
|
|
return FloatValueFormatterWithFormat(v, DefaultFloatFormat)
|
2016-07-10 04:11:47 -04:00
|
|
|
|
}
|
|
|
|
|
|
2016-07-14 14:21:41 -04:00
|
|
|
|
// PercentValueFormatter is a formatter for percent values.
|
|
|
|
|
// NOTE: it normalizes the values, i.e. multiplies by 100.0.
|
|
|
|
|
func PercentValueFormatter(v interface{}) string {
|
|
|
|
|
if typed, isTyped := v.(float64); isTyped {
|
|
|
|
|
return FloatValueFormatterWithFormat(typed*100.0, DefaultPercentValueFormat)
|
|
|
|
|
}
|
|
|
|
|
return ""
|
|
|
|
|
}
|
|
|
|
|
|
2016-07-10 04:11:47 -04:00
|
|
|
|
// FloatValueFormatterWithFormat is a ValueFormatter for float64 with a given format.
|
|
|
|
|
func FloatValueFormatterWithFormat(v interface{}, floatFormat string) string {
|
2016-08-05 23:44:40 -04:00
|
|
|
|
if typed, isTyped := v.(int); isTyped {
|
|
|
|
|
return fmt.Sprintf(floatFormat, float64(typed))
|
|
|
|
|
}
|
|
|
|
|
if typed, isTyped := v.(int64); isTyped {
|
|
|
|
|
return fmt.Sprintf(floatFormat, float64(typed))
|
|
|
|
|
}
|
2018-09-07 14:17:11 -04:00
|
|
|
|
if typed, isTyped := v.(float32); isTyped {
|
|
|
|
|
return fmt.Sprintf(floatFormat, typed)
|
|
|
|
|
}
|
|
|
|
|
if typed, isTyped := v.(float64); isTyped {
|
|
|
|
|
return fmt.Sprintf(floatFormat, typed)
|
|
|
|
|
}
|
2016-07-10 13:43:04 -04:00
|
|
|
|
return ""
|
2016-07-10 04:11:47 -04:00
|
|
|
|
}
|
2019-09-09 23:21:51 -04:00
|
|
|
|
|
|
|
|
|
// KValueFormatter is a formatter for K values.
|
|
|
|
|
func KValueFormatter(k float64, vf ValueFormatter) ValueFormatter {
|
|
|
|
|
return func(v interface{}) string {
|
|
|
|
|
return fmt.Sprintf("%0.0fσ %s", k, vf(v))
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-05-22 11:37:57 -04:00
|
|
|
|
|
|
|
|
|
// FloatValueFormatter is a ValueFormatter for float64, exponential notation, e.g. 1.52e+08.
|
|
|
|
|
func ExponentialValueFormatter(v interface{}) string {
|
|
|
|
|
return FloatValueFormatterWithFormat(v, "%.2e")
|
|
|
|
|
}
|