From d97f5801d31f97895e9fefc73eae29b66d6f8fee Mon Sep 17 00:00:00 2001 From: guojun1992 Date: Thu, 16 Mar 2017 16:44:58 +0800 Subject: [PATCH] display data by order --- chart_parser.go | 44 +++++++++++++++++++++++++++++++++++++++--- chart_parser_pie.go | 39 ++++++++++++++++++++----------------- chart_parser_spline.go | 19 +++++++++++------- 3 files changed, 74 insertions(+), 28 deletions(-) diff --git a/chart_parser.go b/chart_parser.go index 8c493e7..2572a5a 100644 --- a/chart_parser.go +++ b/chart_parser.go @@ -3,15 +3,18 @@ package main import ( "errors" "github.com/zieckey/goini" + "io/ioutil" + "log" "os" "path/filepath" - "log" + "strings" ) const DataPrefix = "Data|" +const emptyRunes = " \r\t\v" type ChartIf interface { - Parse(ini *goini.INI) (map[string]string, error) + Parse(ini *goini.INI, file string) (map[string]string, error) Template() string } @@ -30,7 +33,7 @@ func Parse(file string) (tt TemplateArgs, err error) { t, _ := ini.Get("ChartType") if f, ok := ChartHandlers[t]; ok { - tt.args, err = f.Parse(ini) + tt.args, err = f.Parse(ini, file) tt.tmpl = f.Template() } @@ -64,3 +67,38 @@ func LookupChartFiles(dir string) ([]string, error) { return files, err } + +/** + * 读取配置文件获取有序map + * + * @return order map + * @param {[type]} ini string [description] + */ +func LoadConfGetOrderMap(configFile string) ([]string, map[string]string, error) { + stream, err := ioutil.ReadFile(configFile) + if err != nil { + return nil, nil, errors.New("cannot load config file") + } + content := string(stream) + + confMap := make(map[string]string) + mapkeys := make([]string, 0) + + lines := strings.Split(content, "\n") + for _, line := range lines { + line = strings.Trim(line, emptyRunes) + //过滤注释 //过滤非DataPrefix + if line == "" || line[0] == '#' || !strings.HasPrefix(line, DataPrefix) { + continue + } + //过滤 + parts := strings.SplitN(line, "=", 2) + if len(parts) == 2 { + key := strings.Trim(parts[0], emptyRunes) + value := strings.Trim(parts[1], emptyRunes) + mapkeys = append(mapkeys, key) + confMap[key] = value + } + } + return mapkeys, confMap, nil +} diff --git a/chart_parser_pie.go b/chart_parser_pie.go index 2aa9464..b6c9833 100644 --- a/chart_parser_pie.go +++ b/chart_parser_pie.go @@ -7,41 +7,44 @@ import ( type PieChart int -func (c *PieChart) Parse(ini *goini.INI) (map[string]string, error) { +func (c *PieChart) Parse(ini *goini.INI, file string) (map[string]string, error) { 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") -/* Generate DataArray: - [ - ['Firefox', 45.0], - ['IE', 26.8], - ['Chrome', 12.8], - ['Safari', 8.5], - ['Opera', 6.2], - ['Others', 0.7] - ] -*/ - + /* Generate DataArray: + [ + ['Firefox', 45.0], + ['IE', 26.8], + ['Chrome', 12.8], + ['Safari', 8.5], + ['Opera', 6.2], + ['Others', 0.7] + ] + */ + DataArray := "[\n" - kv, _ := ini.GetKvmap(goini.DefaultSection) - for k, v := range kv { + mapkeys, kvmap, err := LoadConfGetOrderMap(file) + if err != nil { + return nil, err + } + for _, k := range mapkeys { if !strings.HasPrefix(k, DataPrefix) { continue } - + key := k[len(DataPrefix):] - DataArray = DataArray + "['" + key + "' , " + v + "],\n" + DataArray = DataArray + "['" + key + "' , " + kvmap[k] + "],\n" } DataArray = DataArray + "]" - + args["DataArray"] = DataArray return args, nil } func (c *PieChart) Template() string { return TemplatePieHtml -} \ No newline at end of file +} diff --git a/chart_parser_spline.go b/chart_parser_spline.go index 14afb5f..a8ec984 100644 --- a/chart_parser_spline.go +++ b/chart_parser_spline.go @@ -3,13 +3,14 @@ package main import ( "github.com/bitly/go-simplejson" "github.com/zieckey/goini" + "log" "strconv" "strings" ) type SplineChart int -func (c *SplineChart) Parse(ini *goini.INI) (map[string]string, error) { +func (c *SplineChart) Parse(ini *goini.INI, file string) (map[string]string, error) { args := make(map[string]string) args["ChartType"], _ = ini.Get("ChartType") args["Title"], _ = ini.Get("Title") @@ -21,13 +22,17 @@ func (c *SplineChart) Parse(ini *goini.INI) (map[string]string, error) { datas := make([]interface{}, 0) - kv, _ := ini.GetKvmap(goini.DefaultSection) - for k, v := range kv { - if !strings.HasPrefix(k, DataPrefix) { + mapkeys, kvmap, err := LoadConfGetOrderMap(file) + if err != nil { + return nil, err + } + + for _, key := range mapkeys { + if !strings.HasPrefix(key, DataPrefix) { continue } - dd := strings.Split(v, ",") + dd := strings.Split(kvmap[key], ",") jd := make([]interface{}, 0) for _, d := range dd { d = strings.TrimSpace(d) @@ -37,7 +42,7 @@ func (c *SplineChart) Parse(ini *goini.INI) (map[string]string, error) { } } json := simplejson.New() - json.Set("name", k[len(DataPrefix):]) + json.Set("name", key[len(DataPrefix):]) json.Set("data", jd) datas = append(datas, json) } @@ -46,7 +51,7 @@ func (c *SplineChart) Parse(ini *goini.INI) (map[string]string, error) { json.Set("DataArray", datas) b, _ := json.Get("DataArray").Encode() args["DataArray"] = string(b) - + log.Println(args) return args, nil }