From 7b1af58dbd7a1683e378727b3408640b9c349ae5 Mon Sep 17 00:00:00 2001 From: skoo87 Date: Thu, 5 Sep 2013 15:54:13 +0800 Subject: [PATCH] add bar and pie --- .gitignore | 2 ++ bar.go | 67 ++++++++++++++++++++++++++++++++++++++++++++++ column.go | 1 - data.go | 9 +++---- examples/bar.chart | 7 +++++ examples/pie.chart | 7 +++++ main.go | 6 ++++- pie.go | 57 +++++++++++++++++++++++++++++++++++++++ server.go | 43 +++++++++++++++++++++-------- 9 files changed, 181 insertions(+), 18 deletions(-) create mode 100644 bar.go delete mode 100644 column.go create mode 100644 examples/bar.chart create mode 100644 examples/pie.chart diff --git a/.gitignore b/.gitignore index 0026861..7e73fe4 100644 --- a/.gitignore +++ b/.gitignore @@ -20,3 +20,5 @@ _cgo_export.* _testmain.go *.exe + +goplot diff --git a/bar.go b/bar.go new file mode 100644 index 0000000..e42237c --- /dev/null +++ b/bar.go @@ -0,0 +1,67 @@ +package main + +import ( + "encoding/json" + "fmt" +) + +type barDataSetsType struct { + FillColor string `json:"fillColor"` + StrokeColor string `json:"strokeColor"` + Data []int `json:"data"` +} + +type barDataType struct { + Labels []string `json:"labels"` + Datasets []*barDataSetsType `json:"datasets"` +} + +type BarChart struct { + name string +} + +func (b *BarChart) Canvas(name string, height int, width int) string { + if height == 0 { + height = 300 + } + if width == 0 { + width = 400 + } + return fmt.Sprintf("", name, height, width) +} + +func (l *BarChart) JsonCode(c *ChartDataType) (string, error) { + bars := new(barDataType) + + barNum := c.ValueNum() + + bars.Labels = c.ItemName() + bars.Datasets = make([]*barDataSetsType, 0, barNum) + + for i := 0; i < barNum; i++ { + bar := &barDataSetsType{} + bar.FillColor = GetColorValue(i) + bar.StrokeColor = GetColorValue(i) + + bar.Data = c.ItemValue(i) + bars.Datasets = append(bars.Datasets, bar) + } + + b, err := json.Marshal(bars) + if err != nil { + return "", err + } + + return fmt.Sprintf("var barJsonStr = '%s';", string(b)), nil +} + +func (l *BarChart) NewChart(name string) string { + return fmt.Sprintf("new Chart(document.getElementById(\"%s\").getContext(\"2d\")).Bar(eval('('+barJsonStr+')'));", name) +} + +func init() { + bar := new(BarChart) + bar.name = "bar" + + ChartHandlers["bar"] = bar +} diff --git a/column.go b/column.go deleted file mode 100644 index 06ab7d0..0000000 --- a/column.go +++ /dev/null @@ -1 +0,0 @@ -package main diff --git a/data.go b/data.go index 735b700..3102f1b 100644 --- a/data.go +++ b/data.go @@ -174,8 +174,8 @@ func ParseDataFile(file string) ([]*ChartDataType, error) { return charts, nil } -func LookupCurrentDir(dir string) ([]*ChartDataType, error) { - var data []*ChartDataType +func LookupCurrentDir(dir string) ([]string, error) { + var tmp []string = make([]string, 0, 5) err := filepath.Walk(dir, func(path string, f os.FileInfo, err error) error { if f == nil { @@ -190,12 +190,11 @@ func LookupCurrentDir(dir string) ([]*ChartDataType, error) { return err } else if ok { if path == f.Name() { - data, err = ParseDataFile(path) - return err + tmp = append(tmp, path) } } return nil }) - return data, err + return tmp, err } diff --git a/examples/bar.chart b/examples/bar.chart new file mode 100644 index 0000000..cc8e02d --- /dev/null +++ b/examples/bar.chart @@ -0,0 +1,7 @@ +=== +{ "Name" : "bar", "Height" : 300, "Width" : 550 } +--- +aaa 10 13 +bbb 15 12 +ccc 23 14 +ddd 20 24 diff --git a/examples/pie.chart b/examples/pie.chart new file mode 100644 index 0000000..a5bc11f --- /dev/null +++ b/examples/pie.chart @@ -0,0 +1,7 @@ +=== +{ "Name" : "pie", "Height" : 300, "Width" : 550 } +--- +aaa 10 +bbb 15 +ccc 23 +ddd 20 diff --git a/main.go b/main.go index c61a87d..9c5a5da 100644 --- a/main.go +++ b/main.go @@ -1,5 +1,9 @@ package main +const start = `version: 1.0 +http://localhost:8000` + func main() { - ListenAndServe(":8000") + println(start) + println(ListenAndServe(":8000").Error()) } diff --git a/pie.go b/pie.go index 06ab7d0..fd7b207 100644 --- a/pie.go +++ b/pie.go @@ -1 +1,58 @@ package main + +import ( + "encoding/json" + "fmt" +) + +type pieDataSetsType struct { + Color string `json:"color"` + Value int `json:"value"` +} + +type PieChart struct { + name string +} + +func (b *PieChart) Canvas(name string, height int, width int) string { + if height == 0 { + height = 300 + } + if width == 0 { + width = 400 + } + return fmt.Sprintf("", name, height, width) +} + +func (l *PieChart) JsonCode(c *ChartDataType) (string, error) { + pieNum := c.ItemNum() + items := c.ItemValue(0) + + datasets := make([]*pieDataSetsType, 0, pieNum) + + for i := 0; i < pieNum; i++ { + pie := &pieDataSetsType{} + pie.Color = GetColorValue(i) + pie.Value = items[i] + + datasets = append(datasets, pie) + } + + b, err := json.Marshal(datasets) + if err != nil { + return "", err + } + + return fmt.Sprintf("var pieJsonStr = '%s';", string(b)), nil +} + +func (l *PieChart) NewChart(name string) string { + return fmt.Sprintf("new Chart(document.getElementById(\"%s\").getContext(\"2d\")).Pie(eval('('+pieJsonStr+')'));", name) +} + +func init() { + pie := new(PieChart) + pie.name = "pie" + + ChartHandlers["pie"] = pie +} diff --git a/server.go b/server.go index fb9e4e0..6c5d94e 100644 --- a/server.go +++ b/server.go @@ -19,6 +19,9 @@ const html = `{{define "T"}} +
+ By skoo +
{{.Canvas}}