Highcharts数据与x、y值的Golang结构体

huangapple go评论110阅读模式
英文:

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)
}

图片链接
jsfiddle链接

希望对你有帮助!如果你还有其他问题,请随时提问。

英文:

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 -->

&lt;script&gt;
$(document).ready(function() {
    var options = {
        chart: {
            renderTo: &#39;ERP_Chart&#39;,
            type: &#39;spline&#39;
        },
        series: []
   };

   $.getJSON(&quot;/Get_ERP_Chart&quot;, 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:&quot;data&quot;`  //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(&quot;SELECT profit,expense,contacts,_date FROM Sells ORDER BY _date&quot;)

var prof, exp, con int
var date string

for rows.Next() {
	err = rows.Scan(&amp;prof, &amp;exp, &amp;con, &amp;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.

huangapple
  • 本文由 发表于 2017年4月18日 23:13:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/43475991.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定