Skip to content

Commit

Permalink
Merge pull request #30 from Kuraio/master
Browse files Browse the repository at this point in the history
Default colors updated, legacy colors available, new gradient colors added.
  • Loading branch information
Mariano Gappa committed Jan 3, 2018
2 parents f6cda05 + 8b10c79 commit 425aedd
Show file tree
Hide file tree
Showing 14 changed files with 1,114 additions and 188 deletions.
Binary file added .DS_Store
Binary file not shown.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ chart [options]
- `line`: render a line chart
- `scatter`: render a scatter plot chart
- `log`: use logarithmic scale (bar chart only)
- `legacy-color`: use legacy colors
- `gradient`: use color gradients
- `' '|';'|','|'\t'`: this character separates columns on each line (\t = default)
- `-t|--title`: title for the chart
- `-x`: label for the x axis
Expand Down
29 changes: 16 additions & 13 deletions chartjs.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ type inData struct {
XLabel string
YLabel string
ZeroBased bool
ColorType int
}

func (i inData) hasFloats() bool { return len(i.FSS) > 0 }
Expand Down Expand Up @@ -159,6 +160,7 @@ type cjsData struct {
Datasets []cjsDataset
TooltipCallback string
UsesTimeScale bool
ColorType int
}

type cjsDataset struct {
Expand All @@ -179,6 +181,7 @@ func (c cjsChart) data() cjsData {
d := c.labelsAndDatasets()
d.Title = c.inData.Title
d.ScaleType = c.inData.ScaleType
d.ColorType = c.inData.ColorType
d.XLabel = c.inData.XLabel
d.YLabel = c.inData.YLabel
d.ZeroBased = c.inData.ZeroBased
Expand All @@ -204,7 +207,7 @@ func (c cjsChart) labelsAndDatasets() cjsData {
Datasets: []cjsDataset{{
Fill: true,
SimpleData: c.marshalSimpleData(0),
BackgroundColor: colorFirstN(len(c.inData.FSS)),
BackgroundColor: colorFirstN(c.inData.ColorType, len(c.inData.FSS)),
}},
}
case "bar":
Expand All @@ -216,7 +219,7 @@ func (c cjsChart) labelsAndDatasets() cjsData {
Datasets: []cjsDataset{{
Fill: true,
SimpleData: c.marshalSimpleData(0),
BackgroundColor: colorFirstN(len(c.inData.FSS)),
BackgroundColor: colorFirstN(c.inData.ColorType, len(c.inData.FSS)),
}},
}
}
Expand All @@ -226,7 +229,7 @@ func (c cjsChart) labelsAndDatasets() cjsData {
Fill: true,
Label: fmt.Sprintf("category %v", i),
SimpleData: c.marshalSimpleData(i),
BackgroundColor: colorRepeat(i, len(c.inData.FSS)),
BackgroundColor: colorRepeat(c.inData.ColorType, i, len(c.inData.FSS)),
})
}
return cjsData{
Expand All @@ -242,8 +245,8 @@ func (c cjsChart) labelsAndDatasets() cjsData {
Fill: false,
Label: fmt.Sprintf("category %v", i),
SimpleData: c.marshalSimpleData(i),
BorderColor: colorIndex(i),
BackgroundColor: colorIndex(i),
BorderColor: colorIndex(c.inData.ColorType, i),
BackgroundColor: colorIndex(c.inData.ColorType, i),
})
}
return cjsData{
Expand Down Expand Up @@ -276,8 +279,8 @@ func (c cjsChart) labelsAndDatasets() cjsData {
Fill: false,
Label: fmt.Sprintf("category %v", n),
ComplexData: ds,
BorderColor: colorIndex(n),
BackgroundColor: colorIndex(n),
BorderColor: colorIndex(c.inData.ColorType, n),
BackgroundColor: colorIndex(c.inData.ColorType, n),
})
}
return cjsData{
Expand All @@ -304,8 +307,8 @@ func (c cjsChart) labelsAndDatasets() cjsData {
Fill: false,
Label: ds,
ComplexData: []cjsDataPoint{d},
BorderColor: colorIndex(len(mdss)),
BackgroundColor: colorIndex(len(mdss)),
BorderColor: colorIndex(c.inData.ColorType, len(mdss)),
BackgroundColor: colorIndex(c.inData.ColorType, len(mdss)),
}
} else {
m := mdss[ds]
Expand Down Expand Up @@ -346,17 +349,17 @@ func (c cjsChart) labelsAndDatasets() cjsData {
Fill: true,
Label: "category 0",
ComplexData: []cjsDataPoint{},
BackgroundColor: colorIndex(0),
BorderColor: colorIndex(0),
BackgroundColor: colorIndex(c.inData.ColorType, 0),
BorderColor: colorIndex(c.inData.ColorType, 0),
})
}
for j := 0; j < i; j++ {
dss[j] = cjsDataset{
Fill: true,
Label: ils[j],
ComplexData: []cjsDataPoint{},
BackgroundColor: colorIndex(j),
BorderColor: colorIndex(j),
BackgroundColor: colorIndex(c.inData.ColorType, j),
BorderColor: colorIndex(c.inData.ColorType, j),
}
}

Expand Down
47 changes: 33 additions & 14 deletions color.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,29 @@ package main

import "strings"

var colorPalette = []string{
`"#f44336"`, `"#9c27b0"`, `"#3f51b5"`, `"#03a9f4"`, `"#009688"`, `"#8bc34a"`, `"#ffeb3b"`, `"#ff9800"`,
`"#795548"`, `"#607d8b"`, `"#e91e63"`, `"#673ab7"`, `"#2196f3"`, `"#00bcd4"`, `"#4caf50"`, `"#cddc39"`,
`"#ffc107"`, `"#ff5722"`, `"#9e9e9e"`}
var colorPalette = [][]string{
{
`"#ffbb00"`, `"#f90085"`, `"#00bfb2"`, `"#008ffc"`, `"#fc4f00"`, `"#9d00ff"`, `"#ff0000"`, `"#00b515"`,
`"#c7f400"`, `"#f9a2a2"`, `"#007072"`, `"#e6beff"`, `"#aa5b00"`, `"#fff266"`, `"#7f0000"`, `"#aaffc3"`,
`"#7f7f00"`, `"#ffe0c1"`, `"#000080"`, `"#808080"`, `"#000000"`,
`"#ffe08b"`, `"#fc8bc7"`, `"#8be1dc"`, `"#8bccfd"`, `"#fdaf8b"`, `"#dba2ff"`, `"#ff7373"`, `"#a2e4a9"`,
`"#eafba2"`, `"#fcdddd"`, `"#a2cbcb"`, `"#f3e1ff"`, `"#e0c3a2"`, `"#fffac7"`, `"#d0a2a2"`, `"#d8ffe3"`,
`"#d0d0a2"`, `"#fff3e8"`, `"#a2a2d0"`, `"#d0d0d0"`,
`"#a37700"`, `"#9f0055"`, `"#007a72"`, `"#005ca1"`, `"#a13300"`, `"#56008c"`, `"#8c0000"`, `"#00630c"`,
`"#5b6f00"`, `"#885959"`, `"#003e3f"`, `"#7e688c"`, `"#5d3200"`, `"#746e2f"`, `"#460000"`, `"#4e7459"`,
`"#3a3a00"`, `"#746658"`, `"#000052"`},
{
`"#f44336"`, `"#9c27b0"`, `"#3f51b5"`, `"#03a9f4"`, `"#009688"`, `"#8bc34a"`, `"#ffeb3b"`, `"#ff9800"`,
`"#795548"`, `"#607d8b"`, `"#e91e63"`, `"#673ab7"`, `"#2196f3"`, `"#00bcd4"`, `"#4caf50"`, `"#cddc39"`,
`"#ffc107"`, `"#ff5722"`, `"#9e9e9e"`},
{
`"#08306b"`, `"#08519c"`, `"#2171b5"`, `"#4292c6"`, `"#6baed6"`, `"#9ecae1"`, `"#c6dbef"`, `"#deebf7"`,
`"#00441b"`, `"#006d2c"`, `"#238b45"`, `"#41ab5d"`, `"#74c476"`, `"#a1d99b"`, `"#c7e9c0"`, `"#e5f5e0"`,
`"#7f2704"`, `"#a63603"`, `"#d94801"`, `"#f16913"`, `"#fd8d3c"`, `"#fdae6b"`, `"#fdd0a2"`, `"#fee6ce"`,
`"#3f007d"`, `"#54278f"`, `"#6a51a3"`, `"#807dba"`, `"#9e9ac8"`, `"#bcbddc"`, `"#dadaeb"`, `"#efedf5"`,
`"#67001f"`, `"#980043"`, `"#ce1256"`, `"#e7298a"`, `"#df65b0"`, `"#c994c7"`, `"#d4b9da"`, `"#e7e1ef"`,
`"#000000"`, `"#252525"`, `"#525252"`, `"#737373"`, `"#969696"`, `"#bdbdbd"`, `"#d9d9d9"`, `"#f0f0f0"`},
}

var colorI = 0

Expand All @@ -15,34 +34,34 @@ func colorReset() {
}

// Next iterates through the color palette.
func colorNext() string {
result := colorPalette[colorI]
func colorNext(i int) string {
result := colorPalette[i][colorI]
colorI++
if colorI > len(colorPalette)-1 {
if colorI > len(colorPalette[i])-1 {
colorI = 0
}

return result
}

func colorIndex(i int) string {
return colorPalette[i%len(colorPalette)]
func colorIndex(i, j int) string {
return colorPalette[i][j%len(colorPalette[i])]
}

// FirstN returns a comma-separated string of the first n colors in the palette.
func colorFirstN(n int) string {
func colorFirstN(i, n int) string {
k := 0
var cs []string
for j := 0; j < n; j++ {
cs = append(cs, colorPalette[k])
cs = append(cs, colorPalette[i][k])
k++
if k > len(colorPalette)-1 {
if k > len(colorPalette[i])-1 {
k = 0
}
}
return strings.Join(cs, ",")
}

func colorRepeat(i, n int) string {
return strings.Repeat(colorIndex(i)+",", n)
func colorRepeat(i, j, n int) string {
return strings.Repeat(colorIndex(i, j)+",", n)
}
70 changes: 38 additions & 32 deletions color_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,48 +7,54 @@ import (
)

func TestColorNextCycles(t *testing.T) {
colorReset()
color := colorNext()
for i := 0; i < len(colorPalette)-1; i++ {
colorNext()
}
newColor := colorNext()
for i := 0; i < len(colorPalette); i++ {
colorReset()
color := colorNext(i)
for j := 0; j < len(colorPalette[i])-1; j++ {
colorNext(i)
}
newColor := colorNext(i)

if color != newColor {
t.Error("the colorNext function doesn't seem to cycle after finishing available colors")
if color != newColor {
t.Error("the colorNext function doesn't seem to cycle after finishing available colors")
}
}
}

func TestColorReset(t *testing.T) {
colorReset()
color := colorNext()
for i := 0; i < len(colorPalette)/2; i++ {
colorNext()
}
colorReset()
newColor := colorNext()
for i := 0; i < len(colorPalette); i++ {
colorReset()
color := colorNext(i)
for j := 0; j < len(colorPalette[i])/2; j++ {
colorNext(i)
}
colorReset()
newColor := colorNext(i)

if color != newColor {
t.Errorf("the colorReset() function doesn't seem to restart the color palette iterator; mismatch: %v != %v", color, newColor)
if color != newColor {
t.Errorf("the colorReset() function doesn't seem to restart the color palette iterator; mismatch: %v != %v", color, newColor)
}
}
}

func TestColorFirstN(t *testing.T) {
for i := 0; i < len(colorPalette)-1; i++ {
cs := strings.Split(colorFirstN(i), ",")
if i > 0 && len(cs) != i {
t.Errorf("the colorFirstN() function returned %v colors when asked for %v colors", len(cs), i)
}
d := map[string]struct{}{}
for _, c := range cs {
d[c] = struct{}{}
}
if len(d) < len(cs) {
t.Errorf("the colorFirstN(%v) function contained %v duplicates! %v != %v", i, len(cs)-len(d), cs, d)
}
if i >= 2 {
if !reflect.DeepEqual(cs[:i-1], strings.Split(colorFirstN(i-1), ",")) {
t.Errorf("the colorFirstN(%v) function returned different initial colours than colorFirstN(%v-1)", i, i)
for i := 0; i < len(colorPalette); i++ {
for j := 0; j < len(colorPalette[i])-1; j++ {
cs := strings.Split(colorFirstN(i, j), ",")
if j > 0 && len(cs) != j {
t.Errorf("the colorFirstN() function returned %v colors when asked for %v colors", len(cs), j)
}
d := map[string]struct{}{}
for _, c := range cs {
d[c] = struct{}{}
}
if len(d) < len(cs) {
t.Errorf("the colorFirstN(%v) function contained %v duplicates! %v != %v", j, len(cs)-len(d), cs, d)
}
if j >= 2 {
if !reflect.DeepEqual(cs[:j-1], strings.Split(colorFirstN(i, j-1), ",")) {
t.Errorf("the colorFirstN(%v) function returned different initial colours than colorFirstN(%v-1)", j, j)
}
}
}
}
Expand Down
Loading

0 comments on commit 425aedd

Please sign in to comment.