Golang Highcharts动态数据

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

Golang Highcharts dynamic data

问题

我目前正在学习golang和一些网络相关的内容。所以请原谅我可能提出的问题不够聪明。

我的问题是,我想提供一个具有动态数据的Highchart图表。我查看了文档和示例,但无法使其工作。

Highchart示例:

<script type="text/javascript" src="http://code.jquery.com/jquery-1.10.1.js"></script>
<script type="text/javascript" src="http://code.highcharts.com/highcharts.js"></script>

<!-- 2. Add the JavaScript to initialize the chart on document ready -->
<script>
var chart; // global

/**
 * Request data from the server, add it to the graph and set a timeout to request again
 */
function requestData() {
    $.ajax({
        url: 'http://localhost:3000/',
        success: function(point) {
            var series = chart.series[0],
                shift = series.data.length > 20; // shift if the series is longer than 20

            // add the point
            chart.series[0].addPoint(eval(point), true, shift);

            // call it again after one second
            setTimeout(requestData, 1000);
        },
        cache: false
    });
}

$(document).ready(function() {
    chart = new Highcharts.Chart({
        chart: {
            renderTo: 'container',
            defaultSeriesType: 'spline',
            events: {
                load: requestData
            }
        },
        title: {
            text: 'Live random data'
        },
        xAxis: {
            type: 'datetime',
            tickPixelInterval: 150,
            maxZoom: 20 * 1000
        },
        yAxis: {
            minPadding: 0.2,
            maxPadding: 0.2,
            title: {
                text: 'Value',
                margin: 80
            }
        },
        series: [{
            name: 'Random data',
            data: [1]
        }]
    });
});
</script>
<body>
    <!-- 3. Add the container -->
    <div id="container" style="width: 800px; height: 400px; margin: 0 auto"></div>
</body>

我的服务器应该提供一个符合请求的JSON编码字符串。

func main() {
    http.HandleFunc("/", foo)
    http.ListenAndServe(":3000", nil)
}

func foo(w http.ResponseWriter, r *http.Request) {
    numbers := []int{14, 5}
    js, err := json.Marshal(numbers)
    if err != nil {
        http.Error(w, err.Error(), http.StatusInternalServerError)
        return
    }
    fmt.Println("Received Request")
    w.Header().Set("Content-Type", "text/json")
    w.Write(js)
}

我可以看到Highchart发起了一个请求。我猜测是ajax调用无法理解我的JSON?

提前感谢您的帮助 Golang Highcharts动态数据

编辑:我是否也需要返回一个成功的消息?

英文:

I am currently learning golang and a bit of webstuff on the way. So excuse my maybe not so smart question

My problem is that I want to provide a Highchart with dynamic Data. I looked up the documentation and the example but cannot get it to work.

The Highchart example:

		&lt;script type=&quot;text/javascript&quot; src=&quot;http://code.jquery.com/jquery-1.10.1.js&quot;&gt;&lt;/script&gt;
&lt;script type=&quot;text/javascript&quot; src=&quot;http://code.highcharts.com/highcharts.js&quot;&gt;&lt;/script&gt;
&lt;!-- 2. Add the JavaScript to initialize the chart on document ready --&gt;
&lt;script&gt;
var chart; // global
/**
* Request data from the server, add it to the graph and set a timeout to request again
*/
function requestData() {
$.ajax({
url: &#39;http://localhost:3000/&#39;,
success: function(point) {
var series = chart.series[0],
shift = series.data.length &gt; 20; // shift if the series is longer than 20
// add the point
chart.series[0].addPoint(eval(point), true, shift);
// call it again after one second
setTimeout(requestData, 1000);
},
cache: false
});
}
$(document).ready(function() {
chart = new Highcharts.Chart({
chart: {
renderTo: &#39;container&#39;,
defaultSeriesType: &#39;spline&#39;,
events: {
load: requestData
}
},
title: {
text: &#39;Live random data&#39;
},
xAxis: {
type: &#39;datetime&#39;,
tickPixelInterval: 150,
maxZoom: 20 * 1000
},
yAxis: {
minPadding: 0.2,
maxPadding: 0.2,
title: {
text: &#39;Value&#39;,
margin: 80
}
},
series: [{
name: &#39;Random data&#39;,
data: [1]
}]
});
});
&lt;/script&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;!-- 3. Add the container --&gt;
&lt;div id=&quot;container&quot; style=&quot;width: 800px; height: 400px; margin: 0 auto&quot;&gt;&lt;/div&gt;

My Server should Provide a json encoding string as requested.

func main(){
http.HandleFunc(&quot;/&quot;,foo)
http.ListenAndServe(&quot;:3000&quot;, nil)
}
func foo(w http.ResponseWriter, r *http.Request) {
numbers := []int{14,5}
js, err := json.Marshal(numbers)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
fmt.Println(&quot;Received Request&quot;)
w.Header().Set(&quot;Content-Type&quot;, &quot;text/json&quot;)
w.Write(js)
}

I can see that the highchart makes a request. My guess it that the ajax call does not understand my json?

Thanks for any help in advance Golang Highcharts动态数据

Edit: Do I habe to do a success message as a return too?

答案1

得分: 1

错误(感谢@jmaloney的提示)

{"readyState":4,"status":200,"statusText":"success"}
ajax.html:28 parsererror: 错误:未调用jQuery110109016359453089535_1446814074235

在我的go服务器上简单地设置

w.Header().Set("Access-Control-Allow-Origin", "*")

解决了这个问题 Golang Highcharts动态数据

英文:

The error (thx to @jmaloney 's hint)

{&quot;readyState&quot;:4,&quot;status&quot;:200,&quot;statusText&quot;:&quot;success&quot;}
ajax.html:28 parsererror: Error: jQuery110109016359453089535_1446814074235 was not called

A simple

w.Header().Set(&quot;Access-Control-Allow-Origin&quot;, &quot;*&quot;)

on my go-Server solves it Golang Highcharts动态数据

huangapple
  • 本文由 发表于 2015年11月6日 01:06:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/33550607.html
匿名

发表评论

匿名网友

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

确定