add bar and pie

This commit is contained in:
skoo87 2013-09-05 15:54:13 +08:00
parent 531d19f530
commit 7b1af58dbd
9 changed files with 181 additions and 18 deletions

2
.gitignore vendored
View File

@ -20,3 +20,5 @@ _cgo_export.*
_testmain.go _testmain.go
*.exe *.exe
goplot

67
bar.go Normal file
View File

@ -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("<canvas id=\"%s\" height=\"%d\" width=\"%d\"></canvas>", 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
}

View File

@ -1 +0,0 @@
package main

View File

@ -174,8 +174,8 @@ func ParseDataFile(file string) ([]*ChartDataType, error) {
return charts, nil return charts, nil
} }
func LookupCurrentDir(dir string) ([]*ChartDataType, error) { func LookupCurrentDir(dir string) ([]string, error) {
var data []*ChartDataType var tmp []string = make([]string, 0, 5)
err := filepath.Walk(dir, func(path string, f os.FileInfo, err error) error { err := filepath.Walk(dir, func(path string, f os.FileInfo, err error) error {
if f == nil { if f == nil {
@ -190,12 +190,11 @@ func LookupCurrentDir(dir string) ([]*ChartDataType, error) {
return err return err
} else if ok { } else if ok {
if path == f.Name() { if path == f.Name() {
data, err = ParseDataFile(path) tmp = append(tmp, path)
return err
} }
} }
return nil return nil
}) })
return data, err return tmp, err
} }

7
examples/bar.chart Normal file
View File

@ -0,0 +1,7 @@
===
{ "Name" : "bar", "Height" : 300, "Width" : 550 }
---
aaa 10 13
bbb 15 12
ccc 23 14
ddd 20 24

7
examples/pie.chart Normal file
View File

@ -0,0 +1,7 @@
===
{ "Name" : "pie", "Height" : 300, "Width" : 550 }
---
aaa 10
bbb 15
ccc 23
ddd 20

View File

@ -1,5 +1,9 @@
package main package main
const start = `version: 1.0
http://localhost:8000`
func main() { func main() {
ListenAndServe(":8000") println(start)
println(ListenAndServe(":8000").Error())
} }

57
pie.go
View File

@ -1 +1,58 @@
package main 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("<canvas id=\"%s\" height=\"%d\" width=\"%d\"></canvas>", 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
}

View File

@ -19,6 +19,9 @@ const html = `{{define "T"}}
</style> </style>
</head> </head>
<body> <body>
<div style="padding-top:30px;">
By <a href="http://www.bigendian123.com/skoo.html" target="_blank">skoo</a>
</div>
<div style="padding-top:30px;"></div> <div style="padding-top:30px;"></div>
{{.Canvas}} {{.Canvas}}
<script> <script>
@ -36,10 +39,26 @@ type ChartIf interface {
NewChart(string) string NewChart(string) string
} }
var ChartHandlers = make(map[string]ChartIf) var (
ChartHandlers = make(map[string]ChartIf)
ChartFiles []string
Index int
)
func handler(w http.ResponseWriter, r *http.Request) { func handler(w http.ResponseWriter, r *http.Request) {
datas, err := LookupCurrentDir(".") if len(ChartFiles) == 0 {
return
}
var file string
if Index < len(ChartFiles) {
file = ChartFiles[Index]
Index++
} else {
Index = 0
file = ChartFiles[Index]
}
datas, err := ParseDataFile(file)
if err != nil { if err != nil {
w.Write([]byte(err.Error())) w.Write([]byte(err.Error()))
return return
@ -75,21 +94,23 @@ func handler(w http.ResponseWriter, r *http.Request) {
} }
} }
t, err1 := template.New("foo").Parse(html) if t, err := template.New("foo").Parse(html); err != nil {
if err1 != nil {
w.Write([]byte(err1.Error()))
return
}
err = t.ExecuteTemplate(w, "T", Args)
if err != nil {
w.Write([]byte(err.Error())) w.Write([]byte(err.Error()))
return } else {
if err = t.ExecuteTemplate(w, "T", Args); err != nil {
w.Write([]byte(err.Error()))
}
} }
} }
func ListenAndServe(addr string) error { func ListenAndServe(addr string) error {
http.HandleFunc("/", handler) http.HandleFunc("/", handler)
http.HandleFunc("/favicon.ico", func(w http.ResponseWriter, r *http.Request) {}) http.HandleFunc("/favicon.ico", func(w http.ResponseWriter, r *http.Request) {})
var err error
ChartFiles, err = LookupCurrentDir(".")
if err != nil {
return err
}
return http.ListenAndServe(addr, nil) return http.ListenAndServe(addr, nil)
} }