0
謝謝大家回答我的不成熟的問題問最後一次,但是,我仍然無法弄清楚如何處理[X,Y]系列數據。
它工作正常時數據是一個整數的單個數組,但一個結構數組不工作。我該如何解決它?
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
}
<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>
我的服務器端運行的代碼
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)
}
請輸出您的字段名稱,即將'name string'更改爲'Name string'等。 – mkopriva