1、Go 開發工具 - LiteIDE(安裝使用說明)
2、windows下Go環境配置
下載安裝包,配置環境變數。
3、Linux下Go環境配置
假設已經位於Go的安裝目錄 $GO_INSTALL_DIR下
#hg clone -u release https://code.google.com/p/go
#cd go/src
#./all.bash
運行all.bash後出現”ALL TESTS PASSED”字樣時才算安裝成功。 上面是Unix風格的命令,Windows下的安裝方式類似,只不過是運行all.bat,調用的編譯器是MinGW的gcc。
然後設定幾個環境變數,
#export GOROOT=$HOME/go
#export GOBIN=$GOROOT/bin
#export PATH=$PATH:$GOBIN
註:可通過$echo $GOBIN 查看環境變數設定 若未設定此3項,引用第三方包會有問題例如:unknown driver "mysql" (forgotten import?)
具體安裝使用說明
好了,貼代碼:
data.go
作用:建立一個web服務,提供資料介面
知識點:建web服務、Go資料對象、jsonP返回資料、Go調用mysql資料庫
// 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:"labels"`DrawDatas []DrawData `json:"drawData"`}func GetData(w http.ResponseWriter, req *http.Request) {req.ParseForm() //解析參數,預設是不會解析的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 = "未完成"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!")}
註:Go 中可以按照網上提供安裝mysql,也可從這裡下載前輩整理好的mysql包解壓到go目錄的src/pkg目錄
html頁面
<!DOCTYPE html><html><head><meta charset="UTF-8" /><title>ichartjs 樣本</title><meta name="Description" content="The ichartjs's gallery center,ichartjs 樣本中心" /><meta name="Keywords" content="ichartjs demo,Html5 demo,ichartjs樣本,ichartjs樣本,Html5樣本,Html5樣本" /><script type="text/javascript" src="../js/ichart.1.2.min.js"></script> <script type="text/javascript" src="../js/jquery-latest.js"></script><link rel="stylesheet" href="../css/demo.css" type="text/css" /><script type="text/javascript">var url="http://127.0.0.1:1234/getdatas?jsonpCallback=?"; $.getJSON(url,function(data){var chart = new iChart.BarStacked2D({render : 'canvasDiv',data: data.drawData,labels:data.labels,title : {text:'今日訂單分配及完成量',width:400,align:'left',background_color : '#495998',color:'#c0c8e7'},subtitle : {width:280,height:20,fontsize:12,align:'left',color:'#c0c8e7'},padding:10,footnote : 'moyoyo.com',width : 800,height : 400,bar_height:20,background_color : '#353757',shadow : true,shadow_blur : 2,shadow_color : '#aaaaaa',shadow_offsetx : 1,shadow_offsety : 0, sub_option:{label:{color:'#ffffff',fontsize:12,fontweight:600},border : {width : 2,color : '#d3d4f0'} },label:{color:'#d3d4f0',fontsize:12,fontweight:600},showpercent:true,decimalsnum:0,legend:{enable:true,background_color : null,line_height:25,color:'#d3d4f0',fontsize:12,fontweight:600,border : {enable : false}},coordinate:{background_color : 0,axis : {color : '#c0c8e7',width : 0}, scale:[{ position:'bottom', scale_enable : false, start_scale:0, scale_space:1, end_scale:12, label:{color:'#d3d4f0',fontsize:11,fontweight:600}, listeners:{parseText:function(t,x,y){return {text:t}} }}],width:600,height:260}});//利用自訂群組件構造左側說明文本chart.plugin(new iChart.Custom({drawFn:function(){//計算位置var coo = chart.getCoordinate(),x = coo.get('originx'),y = coo.get('originy'),h = coo.height;//在左下側的位置,渲染一個單位的文字chart.target.textAlign('start').textBaseline('bottom').textFont('600 11px Verdana').fillText('總量',x-20,y+h+30,false,'#a8b2d7');}}));chart.draw();});</script></head><body><div id='canvasDiv'></div><div class='ichartjs_info'><span class='ichartjs_author'>writen by <a title="樣本的貢獻者"href="mailto:taylor@ichartjs.com">taylor</a> </span><span class='ichartjs_sm'>備忘:</span><span class='ichartjs_details'>資料均為類比。 </span></div></body></html>
最終如下:
IchartJs