From cd617973d1fa9be170e57ef2f7068e8081d7bf42 Mon Sep 17 00:00:00 2001 From: "weizili.360.laptop" Date: Fri, 26 Dec 2014 21:03:34 +0800 Subject: [PATCH] finished column plot. but pie plot has problem --- data_parser.go | 67 ++++---------------------------- examples/pie.chart | 20 +++++----- pie_parser.go | 44 +++++++++++++++++++++ server.go | 92 ++++++-------------------------------------- spline_parser.go | 64 ++++++++++++++++++++++++++++++ template_pie_html.go | 2 +- 6 files changed, 137 insertions(+), 152 deletions(-) create mode 100644 pie_parser.go create mode 100644 spline_parser.go diff --git a/data_parser.go b/data_parser.go index bf76b85..012fd98 100644 --- a/data_parser.go +++ b/data_parser.go @@ -1,14 +1,12 @@ package main import ( - "fmt" - "github.com/bitly/go-simplejson" + "github.com/zieckey/goini" "log" "os" "path/filepath" - "strconv" - "strings" + ) const DataPrefix = "Data|" @@ -18,78 +16,27 @@ type ChartIf interface { Template() string } -type SplineChart int - -func (c *SplineChart) Parse(ini *goini.INI) (map[string]string, error) { - log.Printf("c=%v ini=%v\n", c, ini) - - args := make(map[string]string) - args["ChartType"] = "spline" - args["Title"], _ = ini.Get("Title") - args["SubTitle"], _ = ini.Get("SubTitle") - args["YAxisText"], _ = ini.Get("YAxisText") - args["XAxisNumbers"], _ = ini.Get("XAxisNumbers") - args["ValueSuffix"], _ = ini.Get("ValueSuffix") - - datas := make([]interface{}, 0) - - kv, _ := ini.GetKvmap(goini.DefaultSection) - for k, v := range kv { - if !strings.HasPrefix(k, DataPrefix) { - continue - } - - dd := strings.Split(v, ", ") - jd := make([]interface{}, 0) - for _, d := range dd { - - val, err := strconv.ParseFloat(d, 64) - if err == nil { - jd = append(jd, val) - } - log.Printf("ParseFloat(%v) v=%v err=%v\n", d, val, err) - } - json := simplejson.New() - json.Set("name", k[len(DataPrefix):]) - json.Set("data", jd) - datas = append(datas, json) - } - - json := simplejson.New() - json.Set("DataArray", datas) - b, _ := json.Get("DataArray").Encode() - args["DataArray"] = string(b) - - fmt.Printf("DataArray:\n%v\n", string(b)) - fmt.Printf("=========================================================>>Args:\n%v\n", args) - return args, nil -} - -func (c *SplineChart) Template() string { - return TemplateSplineHtml -} - type TemplateArgs struct { args map[string]string tmpl string } -func Parse(file string) (ta TemplateArgs, err error) { +func Parse(file string) (tt TemplateArgs, err error) { ini := goini.New() err = ini.ParseFile(file) if err != nil { - return ta, err + return tt, err } t, _ := ini.Get("ChartType") log.Printf("ini.Get ChartType=%v\n", t) if f, ok := ChartHandlers[t]; ok { log.Printf("f=%v ok=%v\n", f, ok) - ta.args, err = f.Parse(ini) - ta.tmpl = f.Template() + tt.args, err = f.Parse(ini) + tt.tmpl = f.Template() } - return ta, err + return tt, err } func LookupChartFiles(dir string) ([]string, error) { diff --git a/examples/pie.chart b/examples/pie.chart index f2a3c84..babdfcd 100644 --- a/examples/pie.chart +++ b/examples/pie.chart @@ -1,12 +1,12 @@ -ChartType = pie -Title = Browser market shares at a specific website, 2014 -SubTitle = from website.com -SerieName = Browser shares +ChartType = pie +Title = Browser market shares at a specific website, 2014 +SubTitle = from website.com +SeriesName = Browser shares # The data and the name of the pieces of the pie -Data|Firefox = 45.0 -Data|IE = 26.8 -Data|Chrome = 12.8 -Data|Safari = 8.5 -Data|Opera = 6.2 -Data|Others = 0.7 \ No newline at end of file +Data|Firefox = 45.0 +Data|IE = 26.8 +Data|Chrome = 12.8 +Data|Safari = 8.5 +Data|Opera = 6.2 +Data|Others = 0.7 \ No newline at end of file diff --git a/pie_parser.go b/pie_parser.go new file mode 100644 index 0000000..7efb257 --- /dev/null +++ b/pie_parser.go @@ -0,0 +1,44 @@ +package main + +import ( + "fmt" + "github.com/zieckey/goini" + "log" + "strings" +) + +type PieChart int + +func (c *PieChart) Parse(ini *goini.INI) (map[string]string, error) { + log.Printf("c=%v ini=%v\n", c, ini) + + args := make(map[string]string) + args["ChartType"], _ = ini.Get("ChartType") + args["Title"], _ = ini.Get("Title") + args["SubTitle"], _ = ini.Get("SubTitle") + args["SeriesName"], _ = ini.Get("SeriesName") + + DataArray := "[\n" + + kv, _ := ini.GetKvmap(goini.DefaultSection) + for k, v := range kv { + if !strings.HasPrefix(k, DataPrefix) { + continue + } + + key := k[len(DataPrefix):] + DataArray = DataArray + "['" + key + "' : " + v + "],\n" + } + + DataArray = DataArray + "]" + + args["DataArray"] = DataArray + + fmt.Printf("=========================================================>>Args:\n%v\n", args) + return args, nil +} + +func (c *PieChart) Template() string { + return TemplatePieHtml +} + diff --git a/server.go b/server.go index 44dbffc..4ba3fcd 100644 --- a/server.go +++ b/server.go @@ -7,68 +7,6 @@ import ( "text/template" ) -var SplineDataArray = ` - [ - { - name: 'Tokyo', - data: [7.0, 6.9, 9.5, 14.5, 18.2, 21.5, 25.2, 26.5, 23.3, 18.3, 13.9, 9.6] - }, - { - name: 'New York', - data: [-0.2, 0.8, 5.7, 11.3, 17.0, 22.0, 24.8, 24.1, 20.1, 14.1, 8.6, 2.5] - }, - { - name: 'Berlin', - data: [-0.9, 0.6, 3.5, 8.4, 13.5, 17.0, 18.6, 17.9, 14.3, 9.0, 3.9, 1.0] - }, - { - name: 'London', - data: [3.9, 4.2, 5.7, 8.5, 11.9, 15.2, 17.0, 16.6, 14.2, 10.3, 6.6, 4.8] - } - ] -` - -var PieDataArray = ` - [ - ['Firefox', 45.0], - ['IE', 26.8], - ['Chrome', 12.8], - ['Safari', 8.5], - ['Opera', 6.2], - ['Others', 0.7] - ] -` - -var ArgsSpline = map[string]string{ - "ChartType": "spline", - "Title": "Monthly Average Temperature", - "SubTitle": "Source: WorldClimate.com", - "YAxisText": "Temperature (°C)", - "XAxisNumbers": "['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12']", - "ValueSuffix": "°C", - "DataArray": SplineDataArray, -} - -var ArgsColumn = map[string]string{ - - "ChartType": "column", - "Title": "Monthly Average Temperature", - "SubTitle": "Source: WorldClimate.com", - "YAxisText": "Temperature (°C)", - "XAxisNumbers": "['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12']", - "ValueSuffix": "°C", - "DataArray": SplineDataArray, -} - -var ArgsPie = map[string]string{ - - "ChartType": "pie", - "Title": "Browser market shares at a specific website, 2014", - "SubTitle": "Source: website.com", - "SerieName": "Browser shares", - "DataArray": PieDataArray, -} - var ( ChartHandlers = make(map[string]ChartIf) ChartFiles []string @@ -76,29 +14,19 @@ var ( ) func handler(w http.ResponseWriter, r *http.Request) { - var Args map[string]string - var tmpl string - - Args = ArgsColumn - Args = ArgsSpline - Args = ArgsPie - tmpl = TemplatePieHtml - - path := r.URL.Path[1:] - if path == "spline" { - tmplagrs, err := Parse(ChartFiles[Index]) - Index++ - Index = Index % len(ChartFiles) - if err == nil { - Args = tmplagrs.args - tmpl = TemplateSplineHtml - } + tt, err := Parse(ChartFiles[Index]) + if err != nil { + w.Write([]byte(err.Error())) + return } + + Index++ + Index = Index % len(ChartFiles) - if t, err := template.New("foo").Parse(tmpl); err != nil { + if t, err := template.New("foo").Parse(tt.tmpl); err != nil { w.Write([]byte(err.Error())) } else { - if err = t.ExecuteTemplate(w, "T", Args); err != nil { + if err = t.ExecuteTemplate(w, "T", tt.args); err != nil { w.Write([]byte(err.Error())) } } @@ -118,6 +46,8 @@ func ListenAndServe(addr string) error { } ChartHandlers["spline"] = new(SplineChart) + ChartHandlers["column"] = new(ColumnChart) + ChartHandlers["pie"] = new(PieChart) return http.ListenAndServe(addr, nil) } diff --git a/spline_parser.go b/spline_parser.go new file mode 100644 index 0000000..eae53fa --- /dev/null +++ b/spline_parser.go @@ -0,0 +1,64 @@ +package main + +import ( + "fmt" + "github.com/bitly/go-simplejson" + "github.com/zieckey/goini" + "log" + "strconv" + "strings" +) + +type SplineChart int +type ColumnChart struct { + SplineChart +} + +func (c *SplineChart) Parse(ini *goini.INI) (map[string]string, error) { + log.Printf("c=%v ini=%v\n", c, ini) + + args := make(map[string]string) + args["ChartType"], _ = ini.Get("ChartType") + args["Title"], _ = ini.Get("Title") + args["SubTitle"], _ = ini.Get("SubTitle") + args["YAxisText"], _ = ini.Get("YAxisText") + args["XAxisNumbers"], _ = ini.Get("XAxisNumbers") + args["ValueSuffix"], _ = ini.Get("ValueSuffix") + + datas := make([]interface{}, 0) + + kv, _ := ini.GetKvmap(goini.DefaultSection) + for k, v := range kv { + if !strings.HasPrefix(k, DataPrefix) { + continue + } + + dd := strings.Split(v, ", ") + jd := make([]interface{}, 0) + for _, d := range dd { + + val, err := strconv.ParseFloat(d, 64) + if err == nil { + jd = append(jd, val) + } + log.Printf("ParseFloat(%v) v=%v err=%v\n", d, val, err) + } + json := simplejson.New() + json.Set("name", k[len(DataPrefix):]) + json.Set("data", jd) + datas = append(datas, json) + } + + json := simplejson.New() + json.Set("DataArray", datas) + b, _ := json.Get("DataArray").Encode() + args["DataArray"] = string(b) + + fmt.Printf("DataArray:\n%v\n", string(b)) + fmt.Printf("=========================================================>>Args:\n%v\n", args) + return args, nil +} + +func (c *SplineChart) Template() string { + return TemplateSplineHtml +} diff --git a/template_pie_html.go b/template_pie_html.go index 7e67502..e8b70c9 100644 --- a/template_pie_html.go +++ b/template_pie_html.go @@ -46,7 +46,7 @@ var TemplatePieHtml = `{{define "T"}} }, series: [{ // name: 'Browser share', - name : '{{.SerieName}}', + name : '{{.SeriesName}}', data: {{.DataArray}} /*