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 (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"github.com/zieckey/goini"
|
"github.com/zieckey/goini"
|
||||||
|
"io/ioutil"
|
||||||
|
"log"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"log"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
const DataPrefix = "Data|"
|
const DataPrefix = "Data|"
|
||||||
|
const emptyRunes = " \r\t\v"
|
||||||
|
|
||||||
type ChartIf interface {
|
type ChartIf interface {
|
||||||
Parse(ini *goini.INI) (map[string]string, error)
|
Parse(ini *goini.INI, file string) (map[string]string, error)
|
||||||
Template() string
|
Template() string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -30,7 +33,7 @@ func Parse(file string) (tt TemplateArgs, err error) {
|
||||||
|
|
||||||
t, _ := ini.Get("ChartType")
|
t, _ := ini.Get("ChartType")
|
||||||
if f, ok := ChartHandlers[t]; ok {
|
if f, ok := ChartHandlers[t]; ok {
|
||||||
tt.args, err = f.Parse(ini)
|
tt.args, err = f.Parse(ini, file)
|
||||||
tt.tmpl = f.Template()
|
tt.tmpl = f.Template()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -64,3 +67,38 @@ func LookupChartFiles(dir string) ([]string, error) {
|
||||||
|
|
||||||
return files, err
|
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,33 +7,36 @@ import (
|
||||||
|
|
||||||
type PieChart int
|
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 := make(map[string]string)
|
||||||
args["ChartType"], _ = ini.Get("ChartType")
|
args["ChartType"], _ = ini.Get("ChartType")
|
||||||
args["Title"], _ = ini.Get("Title")
|
args["Title"], _ = ini.Get("Title")
|
||||||
args["SubTitle"], _ = ini.Get("SubTitle")
|
args["SubTitle"], _ = ini.Get("SubTitle")
|
||||||
args["SeriesName"], _ = ini.Get("SeriesName")
|
args["SeriesName"], _ = ini.Get("SeriesName")
|
||||||
|
|
||||||
/* Generate DataArray:
|
/* Generate DataArray:
|
||||||
[
|
[
|
||||||
['Firefox', 45.0],
|
['Firefox', 45.0],
|
||||||
['IE', 26.8],
|
['IE', 26.8],
|
||||||
['Chrome', 12.8],
|
['Chrome', 12.8],
|
||||||
['Safari', 8.5],
|
['Safari', 8.5],
|
||||||
['Opera', 6.2],
|
['Opera', 6.2],
|
||||||
['Others', 0.7]
|
['Others', 0.7]
|
||||||
]
|
]
|
||||||
*/
|
*/
|
||||||
|
|
||||||
DataArray := "[\n"
|
DataArray := "[\n"
|
||||||
kv, _ := ini.GetKvmap(goini.DefaultSection)
|
mapkeys, kvmap, err := LoadConfGetOrderMap(file)
|
||||||
for k, v := range kv {
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
for _, k := range mapkeys {
|
||||||
if !strings.HasPrefix(k, DataPrefix) {
|
if !strings.HasPrefix(k, DataPrefix) {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
key := k[len(DataPrefix):]
|
key := k[len(DataPrefix):]
|
||||||
DataArray = DataArray + "['" + key + "' , " + v + "],\n"
|
DataArray = DataArray + "['" + key + "' , " + kvmap[k] + "],\n"
|
||||||
}
|
}
|
||||||
|
|
||||||
DataArray = DataArray + "]"
|
DataArray = DataArray + "]"
|
||||||
|
|
|
@ -3,13 +3,14 @@ package main
|
||||||
import (
|
import (
|
||||||
"github.com/bitly/go-simplejson"
|
"github.com/bitly/go-simplejson"
|
||||||
"github.com/zieckey/goini"
|
"github.com/zieckey/goini"
|
||||||
|
"log"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
type SplineChart int
|
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 := make(map[string]string)
|
||||||
args["ChartType"], _ = ini.Get("ChartType")
|
args["ChartType"], _ = ini.Get("ChartType")
|
||||||
args["Title"], _ = ini.Get("Title")
|
args["Title"], _ = ini.Get("Title")
|
||||||
|
@ -21,13 +22,17 @@ func (c *SplineChart) Parse(ini *goini.INI) (map[string]string, error) {
|
||||||
|
|
||||||
datas := make([]interface{}, 0)
|
datas := make([]interface{}, 0)
|
||||||
|
|
||||||
kv, _ := ini.GetKvmap(goini.DefaultSection)
|
mapkeys, kvmap, err := LoadConfGetOrderMap(file)
|
||||||
for k, v := range kv {
|
if err != nil {
|
||||||
if !strings.HasPrefix(k, DataPrefix) {
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, key := range mapkeys {
|
||||||
|
if !strings.HasPrefix(key, DataPrefix) {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
dd := strings.Split(v, ",")
|
dd := strings.Split(kvmap[key], ",")
|
||||||
jd := make([]interface{}, 0)
|
jd := make([]interface{}, 0)
|
||||||
for _, d := range dd {
|
for _, d := range dd {
|
||||||
d = strings.TrimSpace(d)
|
d = strings.TrimSpace(d)
|
||||||
|
@ -37,7 +42,7 @@ func (c *SplineChart) Parse(ini *goini.INI) (map[string]string, error) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
json := simplejson.New()
|
json := simplejson.New()
|
||||||
json.Set("name", k[len(DataPrefix):])
|
json.Set("name", key[len(DataPrefix):])
|
||||||
json.Set("data", jd)
|
json.Set("data", jd)
|
||||||
datas = append(datas, json)
|
datas = append(datas, json)
|
||||||
}
|
}
|
||||||
|
@ -46,7 +51,7 @@ func (c *SplineChart) Parse(ini *goini.INI) (map[string]string, error) {
|
||||||
json.Set("DataArray", datas)
|
json.Set("DataArray", datas)
|
||||||
b, _ := json.Get("DataArray").Encode()
|
b, _ := json.Get("DataArray").Encode()
|
||||||
args["DataArray"] = string(b)
|
args["DataArray"] = string(b)
|
||||||
|
log.Println(args)
|
||||||
return args, nil
|
return args, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue