Improved financial chart

This commit is contained in:
Meutel 2017-07-08 10:16:32 +02:00
parent ac32919034
commit 46da91f833

View File

@ -6,6 +6,7 @@ import "io"
import "log" import "log"
import "os" import "os"
import "strconv" import "strconv"
import "strings"
var COLUMNS []string var COLUMNS []string
@ -59,10 +60,39 @@ func printHTML(data []FinancialData, out io.Writer) {
text: 'Source: yahoo.com' text: 'Source: yahoo.com'
}, },
xAxis: {
type: 'datetime'
},
yAxis: { yAxis: {
title: { title: {
text: 'Value' text: 'Value'
} }
},
plotOptions: {
area: {
fillColor: {
linearGradient: {
x1: 0,
y1: 0,
x2: 0,
y2: 1
},
stops: [
[0, Highcharts.getOptions().colors[0]],
[1, Highcharts.Color(Highcharts.getOptions().colors[0]).setOpacity(0).get('rgba')]
]
},
marker: {
radius: 2
},
lineWidth: 1,
states: {
hover: {
lineWidth: 1
}
},
threshold: null
}
}, },
legend: { legend: {
layout: 'vertical', layout: 'vertical',
@ -71,11 +101,13 @@ func printHTML(data []FinancialData, out io.Writer) {
}, },
series: [{ series: [{
type: 'area',
name: 'Open', name: 'Open',
data: [`+getDataOpen(data)+`] data: `+getChartDataOpen(data)+`
}, { },{
type: 'area',
name: 'Close', name: 'Close',
data: [`+getDataClose(data)+`] data: `+getChartDataClose(data)+`
}] }]
}); });
</script> </script>
@ -83,24 +115,45 @@ func printHTML(data []FinancialData, out io.Writer) {
</html>`) </html>`)
} }
func getDataOpen(data []FinancialData) string { func toJSDate(date string) string {
str := "" str := "Date.UTC("
for i, v := range data { parts := strings.Split(date, "-")
str += fmt.Sprintf("%.4f", v.Open) for i, part := range parts {
if i < len(data)-1 { str += strings.TrimLeft(part, "0")
if i < len(parts)-1 {
str += "," str += ","
} }
} }
str += ")"
return str return str
} }
func getDataClose(data []FinancialData) string {
str := "" func getChartDataOpen(data []FinancialData) string {
str := "["
for i, v := range data { for i, v := range data {
str += fmt.Sprintf("%.4f", v.Close) str += "["
str += toJSDate(v.Date)
str += fmt.Sprintf(", %.4f", v.Open)
str += "]"
if i < len(data)-1 { if i < len(data)-1 {
str += "," str += ","
} }
} }
str += "]"
return str
}
func getChartDataClose(data []FinancialData) string {
str := "["
for i, v := range data {
str += "["
str += toJSDate(v.Date)
str += fmt.Sprintf(", %.4f", v.Close)
str += "]"
if i < len(data)-1 {
str += ","
}
}
str += "]"
return str return str
} }