github.com/pbberlin/tools@v0.0.0-20160910141205-7aa5421c2169/big_query/view_as_chart_or_table.go (about) 1 package big_query 2 3 // https://godoc.org/code.google.com/p/google-api-go-client/bigquery/v2 4 // https://developers.google.com/bigquery/bigquery-api-quickstart 5 import ( 6 "fmt" 7 "image" 8 "image/color" 9 "image/png" 10 "os" 11 12 "net/http" 13 14 "github.com/pbberlin/tools/charting" 15 "github.com/pbberlin/tools/colors" 16 "github.com/pbberlin/tools/net/http/htmlfrag" 17 "github.com/pbberlin/tools/net/http/loghttp" 18 "github.com/pbberlin/tools/util" 19 20 "appengine" 21 ) 22 23 func showAsTable(w http.ResponseWriter, r *http.Request, m map[string]interface{}) { 24 25 cd1 := GetChartDataFromDatastore(w, r, "chart_data_01") 26 cd := *cd1 27 28 span := htmlfrag.GetSpanner() 29 // Header row 30 fmt.Fprintf(w, span(" ", 164)) 31 for _, lg := range cd.VLangs { 32 fmt.Fprintf(w, span(lg, 88)) 33 } 34 fmt.Fprintf(w, "<br>") 35 36 for _, period := range cd.VPeriods { 37 fmt.Fprintf(w, span(period, 164)) 38 for _, lg := range cd.VLangs { 39 fmt.Fprintf(w, span(cd.M[period][lg], 88)) 40 } 41 fmt.Fprintf(w, "<br>") 42 } 43 44 } 45 46 func showAsChart(w http.ResponseWriter, r *http.Request, m map[string]interface{}) { 47 48 cd1 := GetChartDataFromDatastore(w, r, "chart_data_01") 49 cd := *cd1 50 51 c := appengine.NewContext(r) 52 53 optScale, _, _ := charting.BestScale(cd.F_max, charting.Scale_y_vm) 54 scale_max := 0.0 55 for _, val := range optScale { 56 //fmt.Fprintf(w,"%v - %v \n", tick, val) 57 fVal := util.Stof(val) 58 if fVal > scale_max { 59 scale_max = fVal 60 } 61 } 62 63 p := r.FormValue("p") 64 if p == "" { 65 p = "static/chartbg_400x960__480x1040__12x10.png" 66 } 67 68 f, err := os.Open(p) 69 loghttp.E(w, r, err, false) 70 defer f.Close() 71 72 imgRaw, whichFormat, err := image.Decode(f) 73 loghttp.E(w, r, err, false, "only jpeg and png are 'activated' ") 74 c.Infof("serving img format %v %T\n", whichFormat, imgRaw) 75 76 var img *image.RGBA 77 img, ok := imgRaw.(*image.RGBA) 78 loghttp.E(w, r, ok, false, "chart bg must have interal format RGBA") 79 80 for langIndex, lang := range cd.VLangs { 81 82 gci := langIndex % len(colors.GraphColors) // graph color index 83 84 lineCol := color.RGBA{colors.GraphColors[gci][0], 85 colors.GraphColors[gci][1], 86 colors.GraphColors[gci][2], 87 0, 88 } 89 90 //fmt.Fprintf(w,"%v %v \n",gci,lineCol) 91 92 drw := charting.FuncDrawLiner(lineCol, img) 93 xb, yb := 40, 440 94 //P0 := image.Point{xb,yb} 95 //drw( P0, lineCol,img ) 96 97 x, y := xb, yb 98 99 maxPeriods := 0 100 for _, period := range cd.VPeriods { 101 102 tmp := cd.M[period][lang] / scale_max * 400 103 y = yb - int(tmp) 104 105 drw(image.Point{x, y}, lineCol, img) 106 //fmt.Fprintf(w,"%v-%v: %v => %v => %v\n",period, lang,count,int(tmp),y) 107 x += 40 108 109 maxPeriods++ 110 if maxPeriods > 24 { 111 break 112 } 113 } 114 } 115 116 w.Header().Set("Content-Type", "image/png") 117 png.Encode(w, img) 118 119 charting.SaveImageToDatastore(w, r, img, "chart2") 120 121 } 122 123 func init() { 124 http.HandleFunc("/big-query/show-chart", loghttp.Adapter(showAsChart)) 125 http.HandleFunc("/big-query/show-table", loghttp.Adapter(showAsTable)) 126 }