GO + Ichartjs + JSONP for data graph analysis background

Source: Internet
Author: User
Tags ichart
This is a creation in Article, where the information may have evolved or changed.

1. Go development Tool-liteide (Installation instruction)

2. Under Windows Go Environment configuration

Download the installation package and configure the environment variables.

3. Go environment configuration under Linux

Assume that you are already in the GO installation directory $GO _install_dir
#hg Clone-u Release Https://code.google.com/p/go
#cd GO/SRC
#./all.bash
The installation succeeds when the word "all TESTS PASSED" appears after running All.bash. The Unix-style commands above are similar to those installed under Windows, except that the All.bat is running, and the compiler called is MinGW gcc.
Then set a few environment variables (vi/etc/profile source/etc/profile)
#export goroot= $HOME/go
#export gobin= $GOROOT/bin
#export path= $PATH: $GOBIN

Note: You can view environment variable settings through $echo $GOBIN If this 3 item is not set, references to third-party packages may have problems such as: Unknown Driver "MySQL" (Forgotten import?)

Specific installation instructions for use

All right, paste the code:

Data.go

Role: Create a Web service that provides a data interface

Knowledge Points: Build Web services, go data Objects, JSONP return data, go call MySQL Database

Testpackage mainimport ("Database/sql" "Encoding/json" "Fmt" _ "MySQL" "net/http") type DrawData struct {Name string ' JSON: "Name" ' Value []int ' JSON: "Value" ' Color string ' JSON: "Color" '}type alldrawdata struct {Label []string ' JSON: "LA BELs "' Drawdatas []drawdata ' JSON:" DrawData "'}func GetData (w http. Responsewriter, req *http. Request) {req. Parseform ()//parse parameter, default is not resolved by var jsonpcallback string = req. form["Jsonpcallback"][0]datas: = []drawdata{}alldrawdata: = Alldrawdata{}db, err: = SQL. Open ("MySQL", "username:password@tcp (127.0.0.1:3306)/test?charset=utf8") defer db. Close () Rows0, err: = db. Query ("Select A.username from TEST") if err! = Nil {panic (err)}labels: = []string{}for rows0. Next () {var value stringerr: = Rows0. Scan (&value) if nil! = Err {panic (err)}labels = append (labels, value)}rows1, err: = db. Query ("Select CNT from TEST") if err! = Nil {panic (err)}defer rows1. Close () Data1: = drawdata{}values: = []int{}for rows1. Next () {var value interr: = rows1. Scan (&value) if nil! = Err {pAnic (err)}values = append (values, value)}data1. Name = "Not Completed" data1. Value = valuesdata1. Color = "#ECAD55" datas = Append (datas, data1) Alldrawdata.label = Labelsalldrawdata.drawdatas = DATASB, _: = json. Marshal (Alldrawdata) fmt. fprintf (W, jsonpcallback+ "(" +string (b) + ")")}func Main () {http. Handlefunc ("/getdatas", GetData) http. Listenandserve (": 1234", nil) fmt. Println ("succeed!")}

Note: Go can be installed on the Internet to install MySQL, or from here to download the previous compiled MySQL package extracted to the Go directory src/pkg directory


HTML page

<! DOCTYPE html>

The end result is as follows:


Ichartjs Download Address

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.