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,48 +60,100 @@ 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',
align: 'right', align: 'right',
verticalAlign: 'middle' verticalAlign: 'middle'
}, },
series: [{ series: [{
name: 'Open', type: 'area',
data: [`+getDataOpen(data)+`] name: 'Open',
}, { data: `+getChartDataOpen(data)+`
name: 'Close', },{
data: [`+getDataClose(data)+`] type: 'area',
}] name: 'Close',
data: `+getChartDataClose(data)+`
}]
}); });
</script> </script>
</body> </body>
</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
} }