Highcharts数据[x,y]由Golang编写

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

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

&lt;script&gt;
$(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);
    });
});
&lt;/script&gt;

<!-- 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:&quot;whatever&quot;,
    data: [
    [&quot;1999/12/12&quot;, 29.9],
    [&quot;1999/12/13&quot;, 71.5],
    [&quot;1999/12/14&quot;, 106.4]
    ]
}]

type Line struct {//my struct, Data is not in the correct format...
Name string  `json:&quot;name&quot;`
Data []Point `json:&quot;data&quot;`  //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 -->

&lt;script&gt;
$(document).ready(function() {
    var options = {
        chart: {
        zoomType: &#39;x&#39;,
        renderTo: &#39;ERP_Chart&#39;,
        type: &#39;line&#39;
        },
        title: {
        text: &#39;ERP Chart&#39;
        },
        series: [{}]
};

$.getJSON(&quot;/Get_ERP_Chart&quot;, function(data) {
    options.series = data;
    var chart = new Highcharts.Chart(options);
    });
});
&lt;/script&gt;

<!-- end snippet -->

Golang code:

type Line struct {
Name string  `json:&quot;name&quot;`
Data []Point `json:&quot;data&quot;`
}

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&#39;t focus on it
rows, err := Db.Query(&quot;SELECT profit,expense,contacts,_date FROM Sells ORDER BY _date&quot;)
var prof, exp, con int
var date string
profit.Name = &quot;profit&quot;
expense.Name = &quot;expense&quot;
contacts.Name = &quot;contacts&quot;
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)
//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(`[&quot;`)
    buf.WriteString(p.Date)
    buf.WriteString(`&quot;,`)
    buf.WriteString(strconv.Itoa(p.Value))
    buf.WriteRune(&#39;]&#39;)
    
    return buf.Bytes(), nil
}

Run example at Go Playground.

huangapple
  • 本文由 发表于 2017年4月27日 19:04:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/43655958.html
匿名

发表评论

匿名网友

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

确定