Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix iss#4 display data by order #5

Merged
merged 1 commit into from
Mar 16, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 41 additions & 3 deletions chart_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -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()
}

Expand Down Expand Up @@ -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
}
39 changes: 21 additions & 18 deletions chart_parser_pie.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
19 changes: 12 additions & 7 deletions chart_parser_spline.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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)
Expand All @@ -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)
}
Expand All @@ -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
}

Expand Down