doudeng3008 2015-11-05 17:06
浏览 89
已采纳

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:

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

</head>
<body>

    <!-- 3. Add the container -->
    <div id="container" style="width: 800px; height: 400px; margin: 0 auto"></div>

My Server should Provide a json encoding string as requested.

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

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

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

  • 写回答

1条回答 默认 最新

  • doutou7961 2015-11-06 13:05
    关注

    The error (thx to @jmaloney 's hint)

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

    A simple

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

    on my go-Server solves it :)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 wegame打不开英雄联盟
  • ¥15 公司的电脑,win10系统自带远程协助,访问家里个人电脑,提示出现内部错误,各种常规的设置都已经尝试,感觉公司对此功能进行了限制(我们是集团公司)
  • ¥15 救!ENVI5.6深度学习初始化模型报错怎么办?
  • ¥30 eclipse开启服务后,网页无法打开
  • ¥30 雷达辐射源信号参考模型
  • ¥15 html+css+js如何实现这样子的效果?
  • ¥15 STM32单片机自主设计
  • ¥15 如何在node.js中或者java中给wav格式的音频编码成sil格式呢
  • ¥15 不小心不正规的开发公司导致不给我们y码,
  • ¥15 我的代码无法在vc++中运行呀,错误很多