mirror of https://github.com/zieckey/gochart.git
finished column plot. but pie plot has problem
This commit is contained in:
parent
dcfbbc426a
commit
cd617973d1
|
@ -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) {
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
ChartType = pie
|
||||
Title = Browser market shares at a specific website, 2014
|
||||
SubTitle = from website.com
|
||||
SerieName = Browser shares
|
||||
SeriesName = Browser shares
|
||||
|
||||
# The data and the name of the pieces of the pie
|
||||
Data|Firefox = 45.0
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
88
server.go
88
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
|
||||
tt, err := Parse(ChartFiles[Index])
|
||||
if err != nil {
|
||||
w.Write([]byte(err.Error()))
|
||||
return
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
|
|
|
@ -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
|
||||
}
|
|
@ -46,7 +46,7 @@ var TemplatePieHtml = `{{define "T"}}
|
|||
},
|
||||
series: [{
|
||||
// name: 'Browser share',
|
||||
name : '{{.SerieName}}',
|
||||
name : '{{.SeriesName}}',
|
||||
data:
|
||||
{{.DataArray}}
|
||||
/*
|
||||
|
|
Loading…
Reference in New Issue