Debian Codegraph @

cmd/chart/main.go

raw symbols SCIP
1 package main
2
3 import (
4 "flag"
5 "fmt"
6 "io/ioutil"
7 "os"
8 "strings"
9
10 "github.com/wcharczuk/go-chart/v2"
11 )
12
13 var (
14 outputPath = flag.String("output", "", "The output file")
15
16 inputFormat = flag.String("format", "csv", "The input format, either 'csv' or 'tsv' (defaults to 'csv')")
17 inputPath = flag.String("f", "", "The input file")
18 reverse = flag.Bool("reverse", false, "If we should reverse the inputs")
19
20 hideLegend = flag.Bool("hide-legend", false, "If we should omit the chart legend")
21 hideSMA = flag.Bool("hide-sma", false, "If we should omit simple moving average")
22 hideLinreg = flag.Bool("hide-linreg", false, "If we should omit linear regressions")
23 hideLastValues = flag.Bool("hide-last-values", false, "If we should omit last values")
24 )
25
26 func main() {
27 flag.Parse()
28 log := chart.NewLogger()
29
30 var rawData []byte
31 var err error
32 if *inputPath != "" {
33 if *inputPath == "-" {
34 rawData, err = ioutil.ReadAll(os.Stdin)
35 if err != nil {
36 log.FatalErr(err)
37 }
38 } else {
39 rawData, err = ioutil.ReadFile(*inputPath)
40 if err != nil {
41 log.FatalErr(err)
42 }
43 }
44 } else if len(flag.Args()) > 0 {
45 rawData = []byte(flag.Args()[0])
46 } else {
47 flag.Usage()
48 os.Exit(1)
49 }
50
51 var parts []string
52 switch *inputFormat {
53 case "csv":
54 parts = chart.SplitCSV(string(rawData))
55 case "tsv":
56 parts = strings.Split(string(rawData), "\t")
57 default:
58 log.FatalErr(fmt.Errorf("invalid format; must be 'csv' or 'tsv'"))
59 }
60
61 yvalues, err := chart.ParseFloats(parts...)
62 if err != nil {
63 log.FatalErr(err)
64 }
65
66 if *reverse {
67 yvalues = chart.ValueSequence(yvalues...).Reverse().Values()
68 }
69
70 var series []chart.Series
71 mainSeries := chart.ContinuousSeries{
72 Name: "Values",
73 XValues: chart.LinearRange(1, float64(len(yvalues))),
74 YValues: yvalues,
75 }
76 series = append(series, mainSeries)
77
78 smaSeries := &chart.SMASeries{
79 Name: "SMA",
80 Style: chart.Style{
81 Hidden: *hideSMA,
82 StrokeColor: chart.ColorRed,
83 StrokeDashArray: []float64{5.0, 5.0},
84 },
85 InnerSeries: mainSeries,
86 }
87 series = append(series, smaSeries)
88
89 linRegSeries := &chart.LinearRegressionSeries{
90 Name: "Values - Lin. Reg.",
91 Style: chart.Style{
92 Hidden: *hideLinreg,
93 },
94 InnerSeries: mainSeries,
95 }
96 series = append(series, linRegSeries)
97
98 mainLastValue := chart.LastValueAnnotationSeries(mainSeries)
99 mainLastValue.Style = chart.Style{
100 Hidden: *hideLastValues,
101 }
102 series = append(series, mainLastValue)
103
104 linregLastValue := chart.LastValueAnnotationSeries(linRegSeries)
105 linregLastValue.Style = chart.Style{
106 Hidden: (*hideLastValues || *hideLinreg),
107 }
108 series = append(series, linregLastValue)
109
110 smaLastValue := chart.LastValueAnnotationSeries(smaSeries)
111 smaLastValue.Style = chart.Style{
112 Hidden: (*hideLastValues || *hideSMA),
113 }
114 series = append(series, smaLastValue)
115
116 graph := chart.Chart{
117 Background: chart.Style{
118 Padding: chart.Box{
119 Top: 50,
120 },
121 },
122 Series: series,
123 }
124
125 if !*hideLegend {
126 graph.Elements = []chart.Renderable{chart.LegendThin(&graph)}
127 }
128
129 var output *os.File
130 if *outputPath != "" {
131 output, err = os.Create(*outputPath)
132 if err != nil {
133 log.FatalErr(err)
134 }
135 } else {
136 output, err = ioutil.TempFile("", "*.png")
137 if err != nil {
138 log.FatalErr(err)
139 }
140 }
141
142 if err := graph.Render(chart.PNG, output); err != nil {
143 log.FatalErr(err)
144 }
145
146 fmt.Fprintln(os.Stdout, output.Name())
147 os.Exit(0)
148 }