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 "os"
import "strconv"
import "strings"
var COLUMNS []string
@ -59,10 +60,39 @@ func printHTML(data []FinancialData, out io.Writer) {
text: 'Source: yahoo.com'
},
xAxis: {
type: 'datetime'
},
yAxis: {
title: {
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: {
layout: 'vertical',
@ -71,11 +101,13 @@ func printHTML(data []FinancialData, out io.Writer) {
},
series: [{
type: 'area',
name: 'Open',
data: [`+getDataOpen(data)+`]
data: `+getChartDataOpen(data)+`
},{
type: 'area',
name: 'Close',
data: [`+getDataClose(data)+`]
data: `+getChartDataClose(data)+`
}]
});
</script>
@ -83,24 +115,45 @@ func printHTML(data []FinancialData, out io.Writer) {
</html>`)
}
func getDataOpen(data []FinancialData) string {
str := ""
for i, v := range data {
str += fmt.Sprintf("%.4f", v.Open)
if i < len(data)-1 {
func toJSDate(date string) string {
str := "Date.UTC("
parts := strings.Split(date, "-")
for i, part := range parts {
str += strings.TrimLeft(part, "0")
if i < len(parts)-1 {
str += ","
}
}
str += ")"
return str
}
func getDataClose(data []FinancialData) string {
str := ""
func getChartDataOpen(data []FinancialData) string {
str := "["
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 {
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
}