github.com/pbberlin/tools@v0.0.0-20160910141205-7aa5421c2169/big_query/bigquery_legend_and_scale.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  	"bytes"
     7  	"fmt"
     8  
     9  	"net/http"
    10  	//"appengine"
    11  
    12  	"github.com/pbberlin/tools/colors"
    13  	"github.com/pbberlin/tools/net/http/htmlfrag"
    14  	"github.com/pbberlin/tools/net/http/loghttp"
    15  )
    16  
    17  var p func(a ...interface{}) string = fmt.Sprint
    18  var f func(format string, a ...interface{}) string = fmt.Sprintf
    19  
    20  func legendAsHTML(w http.ResponseWriter, r *http.Request, m map[string]interface{}) {
    21  
    22  	b1 := new(bytes.Buffer)
    23  	defer func() {
    24  		w.Header().Set("Content-Type", "text/html")
    25  		w.Write(b1.Bytes())
    26  	}()
    27  
    28  	b1, _ = disLegend(w, r)
    29  
    30  }
    31  
    32  // display Legend
    33  func disLegend(w http.ResponseWriter, r *http.Request) (b1 *bytes.Buffer, m map[string]string) {
    34  
    35  	//c := appengine.NewContext(r)
    36  
    37  	b1 = new(bytes.Buffer)
    38  	m = make(map[string]string)
    39  
    40  	cd1 := GetChartDataFromDatastore(w, r, "chart_data_01")
    41  	cd := *cd1
    42  
    43  	span := htmlfrag.GetSpanner()
    44  
    45  	widthLabel := 80
    46  	widthColorBox := 120
    47  	widthDiv := widthLabel + widthColorBox + 2*4
    48  
    49  	b1.WriteString(f("<div style='width:%dpx;margin:4px; padding: 4px; line-height:140%%; background-color:#eee;'>", widthDiv))
    50  
    51  	for langIndex, lang := range cd.VLangs {
    52  
    53  		gci := langIndex % len(colors.GraphColors) // graph color index
    54  		// %x is the hex format, %2.2x makes padding zeros
    55  		col := f("%2.2x%2.2x%2.2x", colors.GraphColors[gci][0],
    56  			colors.GraphColors[gci][1],
    57  			colors.GraphColors[gci][2])
    58  
    59  		b1.WriteString(span(lang, widthLabel))
    60  
    61  		block := f("<div style='display:inline-block;width:%dpx;height:5px; background-color:%s;'></div>", widthColorBox, col)
    62  		b1.WriteString(span(block, widthColorBox))
    63  
    64  		b1.WriteString("<br>")
    65  		m[lang] = col
    66  
    67  	}
    68  	b1.WriteString("</div>")
    69  
    70  	return
    71  }
    72  
    73  func init() {
    74  	http.HandleFunc("/big-query/legend", loghttp.Adapter(legendAsHTML))
    75  }