mirror of https://github.com/zieckey/gochart.git
Merge pull request #5 from guojun1992/iss4
fix iss#4 display data by order
This commit is contained in:
commit
efb9e025f3
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue