Debian Codegraph @

value_formatter.go

raw symbols SCIP
1 package chart
2
3 import (
4 "fmt"
5 "strconv"
6 "time"
7 )
8
9 // ValueFormatter is a function that takes a value and produces a string.
10 type ValueFormatter func(v interface{}) string
11
12 // TimeValueFormatter is a ValueFormatter for timestamps.
13 func TimeValueFormatter(v interface{}) string {
14 return formatTime(v, DefaultDateFormat)
15 }
16
17 // TimeHourValueFormatter is a ValueFormatter for timestamps.
18 func TimeHourValueFormatter(v interface{}) string {
19 return formatTime(v, DefaultDateHourFormat)
20 }
21
22 // TimeMinuteValueFormatter is a ValueFormatter for timestamps.
23 func TimeMinuteValueFormatter(v interface{}) string {
24 return formatTime(v, DefaultDateMinuteFormat)
25 }
26
27 // TimeDateValueFormatter is a ValueFormatter for timestamps.
28 func TimeDateValueFormatter(v interface{}) string {
29 return formatTime(v, "2006-01-02")
30 }
31
32 // TimeValueFormatterWithFormat returns a time formatter with a given format.
33 func TimeValueFormatterWithFormat(format string) ValueFormatter {
34 return func(v interface{}) string {
35 return formatTime(v, format)
36 }
37 }
38
39 // TimeValueFormatterWithFormat is a ValueFormatter for timestamps with a given format.
40 func formatTime(v interface{}, dateFormat string) string {
41 if typed, isTyped := v.(time.Time); isTyped {
42 return typed.Format(dateFormat)
43 }
44 if typed, isTyped := v.(int64); isTyped {
45 return time.Unix(0, typed).Format(dateFormat)
46 }
47 if typed, isTyped := v.(float64); isTyped {
48 return time.Unix(0, int64(typed)).Format(dateFormat)
49 }
50 return ""
51 }
52
53 // IntValueFormatter is a ValueFormatter for float64.
54 func IntValueFormatter(v interface{}) string {
55 switch v.(type) {
56 case int:
57 return strconv.Itoa(v.(int))
58 case int64:
59 return strconv.FormatInt(v.(int64), 10)
60 case float32:
61 return strconv.FormatInt(int64(v.(float32)), 10)
62 case float64:
63 return strconv.FormatInt(int64(v.(float64)), 10)
64 default:
65 return ""
66 }
67 }
68
69 // FloatValueFormatter is a ValueFormatter for float64.
70 func FloatValueFormatter(v interface{}) string {
71 return FloatValueFormatterWithFormat(v, DefaultFloatFormat)
72 }
73
74 // PercentValueFormatter is a formatter for percent values.
75 // NOTE: it normalizes the values, i.e. multiplies by 100.0.
76 func PercentValueFormatter(v interface{}) string {
77 if typed, isTyped := v.(float64); isTyped {
78 return FloatValueFormatterWithFormat(typed*100.0, DefaultPercentValueFormat)
79 }
80 return ""
81 }
82
83 // FloatValueFormatterWithFormat is a ValueFormatter for float64 with a given format.
84 func FloatValueFormatterWithFormat(v interface{}, floatFormat string) string {
85 if typed, isTyped := v.(int); isTyped {
86 return fmt.Sprintf(floatFormat, float64(typed))
87 }
88 if typed, isTyped := v.(int64); isTyped {
89 return fmt.Sprintf(floatFormat, float64(typed))
90 }
91 if typed, isTyped := v.(float32); isTyped {
92 return fmt.Sprintf(floatFormat, typed)
93 }
94 if typed, isTyped := v.(float64); isTyped {
95 return fmt.Sprintf(floatFormat, typed)
96 }
97 return ""
98 }
99
100 // KValueFormatter is a formatter for K values.
101 func KValueFormatter(k float64, vf ValueFormatter) ValueFormatter {
102 return func(v interface{}) string {
103 return fmt.Sprintf("%0.0fσ %s", k, vf(v))
104 }
105 }
106
107 // FloatValueFormatter is a ValueFormatter for float64, exponential notation, e.g. 1.52e+08.
108 func ExponentialValueFormatter(v interface{}) string {
109 return FloatValueFormatterWithFormat(v, "%.2e")
110 }