英文:
Highcharts data with x,y values golang struct
问题
谢谢你的翻译请求。以下是翻译好的内容:
问题更新
感谢大家回答我上次提出的不成熟的问题,然而,我仍然无法弄清楚如何处理[x,y]系列数据。
当Data是一个整数的单个数组时,它可以正常工作,但是当它是一个结构体数组时就不行了。我该如何解决这个问题?
series: [{//should work like this
data: [
["1", 29.9],
["2", 71.5],
["3", 106.4]
]
}]
type Line struct {//my struct
Data []Point `json:"data"` //this is the tricky part
}
type Point struct {
Date string
Value int
}
$(document).ready(function() {
var options = {
chart: {
renderTo: 'ERP_Chart',
type: 'spline'
},
series: []
};
$.getJSON("/Get_ERP_Chart", function(data) {
options.series = data;
var chart = new Highcharts.Chart(options);
});
});
我的服务器端 Go 代码:
type Line struct {
Data []Point `json:"data"` //this is the tricky part...
}
type Point struct {
Date string
Value int
}
func showERPChart(writer http.ResponseWriter, request *http.Request) {
var profit, expense, contacts Line
var chart []Line
rows, err := Db.Query("SELECT profit,expense,contacts,_date FROM Sells ORDER BY _date")
var prof, exp, con int
var date string
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, _ := json.Marshal(chart)
writer.Write(js)
}
希望对你有帮助!如果你还有其他问题,请随时提问。
英文:
Question Updated
Thank you all for answering my immature question asked last time, however, I still cannot figure out how to handle the [x,y] series data.
It works fine when Data is a single array of integers, but an array of struct doesn't work. How can I solve it?
series: [{//should work like this
data: [
["1", 29.9],
["2", 71.5],
["3", 106.4]
]
}]
type Line struct {//my struct
Data []Point `json:"data"` //this is the tricky part
}
type Point struct {
Date string
Value int
}
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
<script>
$(document).ready(function() {
var options = {
chart: {
renderTo: 'ERP_Chart',
type: 'spline'
},
series: []
};
$.getJSON("/Get_ERP_Chart", function(data) {
options.series = data;
var chart = new Highcharts.Chart(options);
});
});
</script>
<!-- end snippet -->
My server-side Go code
type Line struct {
Data []Point `json:"data"` //this is the tricky part...
}
type Point struct {
Date string
Value int
}
func showERPChart(writer http.ResponseWriter, request *http.Request) {
var profit, expense, contacts Line
var chart []Line
rows, err := Db.Query("SELECT profit,expense,contacts,_date FROM Sells ORDER BY _date")
var prof, exp, con int
var date string
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, _:= json.Marshal(chart)
writer.Write(js)
}
答案1
得分: 1
为了使encoding/json按照你的期望工作,你需要导出你的结构体字段。
根据encoding/json
文档:
结构体值会被编码为JSON对象。每个导出的结构体字段都会成为对象的成员,使用字段名作为对象的键,除非由于以下原因之一而省略该字段。
解码也是一样的:
Unmarshal只会设置结构体的导出字段。
英文:
For encoding/json to work the way you want to, you need to export your struct fields.
From encoding/json
docs:
> Struct values encode as JSON objects. Each exported struct field
> becomes a member of the object, using the field name as the object
> key, unless the field is omitted for one of the reasons given below.
Same for decoding:
> Unmarshal will only set exported fields of the struct.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论