英文:
Highcharts data [x,y] written by golang
问题
我正在使用Go将数据写入Highcharts,但是我无法弄清楚如何处理系列数据[x,y]。
当Data是一个整数的单个数组时,它可以正常工作,但是结构体数组不起作用,可能是因为它不是正确的格式。我该如何解决这个问题?
它应该像这样工作jsfiddle,使用字符串作为x值可以正常工作,对我来说完全没问题。
series: [{//应该像这样工作
name:"whatever",
data: [
["1999/12/12", 29.9],
["1999/12/13", 71.5],
["1999/12/14", 106.4]
]
}]
type Line struct {//我的结构体,Data不是正确的格式...
Name string `json:"name"`
Data []Point `json:"data"` //这是棘手的部分
}
type Point struct {
Date string
Value int
}
脚本:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-html -->
<script>
$(document).ready(function() {
var options = {
chart: {
zoomType: 'x',
renderTo: 'ERP_Chart',
type: 'line'
},
title: {
text: 'ERP Chart'
},
series: [{}]
};
$.getJSON("/Get_ERP_Chart", function(data) {
options.series = data;
var chart = new Highcharts.Chart(options);
});
});
</script>
<!-- end snippet -->
Golang代码:
type Line struct {
Name string `json:"name"`
Data []Point `json:"data"`
}
type Point struct {
Date string
Value int
}
func showERPChart(writer http.ResponseWriter, request *http.Request) {
var profit, expense, contacts Line
var chart []Line //chart包含多个线系列。
//下面的代码只是获取数据,不要关注它
rows, err := Db.Query("SELECT profit,expense,contacts,_date FROM Sells ORDER BY _date")
var prof, exp, con int
var date string
profit.Name = "profit"
expense.Name = "expense"
contacts.Name = "contacts"
for rows.Next() {
err = rows.Scan(&prof, &exp, &con, &date)
profit.Data = append(profit.Data, Point{date, prof})
expense.Data = append(expense.Data, Point{date, exp})
contacts.Data = append(contacts.Data, Point{date, con})
}
chart = append(chart, profit)
chart = append(chart, expense)
chart = append(chart, contacts)
//完成数据读取
js, err := json.Marshal(chart)
writer.Write(js)
}
英文:
I'm writing data to highcharts using go, but I cannot figure out how to handle the series data [x,y].
It works fine when Data is a single array of integers, but an array of struct doesn't work, problably because it's not the correct format. How could I solve it?
It should work like this jsfiddle, using string as x works and is perfectly fine for me.
series: [{//should work like this
name:"whatever",
data: [
["1999/12/12", 29.9],
["1999/12/13", 71.5],
["1999/12/14", 106.4]
]
}]
type Line struct {//my struct, Data is not in the correct format...
Name string `json:"name"`
Data []Point `json:"data"` //this is the tricky part
}
type Point struct {
Date string
Value int
}
script:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-html -->
<script>
$(document).ready(function() {
var options = {
chart: {
zoomType: 'x',
renderTo: 'ERP_Chart',
type: 'line'
},
title: {
text: 'ERP Chart'
},
series: [{}]
};
$.getJSON("/Get_ERP_Chart", function(data) {
options.series = data;
var chart = new Highcharts.Chart(options);
});
});
</script>
<!-- end snippet -->
Golang code:
type Line struct {
Name string `json:"name"`
Data []Point `json:"data"`
}
type Point struct {
Date string
Value int
}
func showERPChart(writer http.ResponseWriter, request *http.Request) {
var profit, expense, contacts Line
var chart []Line //chart contains multiple series of line.
//The code below is just getting data, don't focus on it
rows, err := Db.Query("SELECT profit,expense,contacts,_date FROM Sells ORDER BY _date")
var prof, exp, con int
var date string
profit.Name = "profit"
expense.Name = "expense"
contacts.Name = "contacts"
for rows.Next() {
err = rows.Scan(&prof, &exp, &con, &date)
profit.Data = append(profit.Data, Point{date, prof})
expense.Data = append(expense.Data, Point{date, exp})
contacts.Data = append(contacts.Data, Point{date, con})
}
chart = append(chart, profit)
chart = append(chart, expense)
chart = append(chart, contacts)
//done reading data
js, err := json.Marshal(chart)
writer.Write(js)
}
答案1
得分: 1
你需要为Point
实现一个自定义编组器。以下是一个示例实现:
func (p *Point) MarshalJSON() ([]byte, error) {
var buf bytes.Buffer
buf.WriteString(`["`)
buf.WriteString(p.Date)
buf.WriteString(`",`)
buf.WriteString(strconv.Itoa(p.Value))
buf.WriteRune(']')
return buf.Bytes(), nil
}
在Go Playground上运行示例。
英文:
You need to implement custom marshaller for Point
. Below is an example implementation:
func (p *Point) MarshalJSON() ([]byte, error) {
var buf bytes.Buffer
buf.WriteString(`["`)
buf.WriteString(p.Date)
buf.WriteString(`",`)
buf.WriteString(strconv.Itoa(p.Value))
buf.WriteRune(']')
return buf.Bytes(), nil
}
Run example at Go Playground.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论