Skip to content

Commit

Permalink
Default colors updated, legacy colors available, new gradient colors …
Browse files Browse the repository at this point in the history
…added.
  • Loading branch information
Kuraio committed Dec 2, 2017
1 parent 7ed1024 commit 5367c46
Show file tree
Hide file tree
Showing 11 changed files with 1,058 additions and 113 deletions.
Binary file added .DS_Store
Binary file not shown.
29 changes: 16 additions & 13 deletions chartjs.go
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
37 changes: 25 additions & 12 deletions color.go
Expand Up @@ -2,10 +2,23 @@ package main

import "strings"

var colorPalette = []string{
var colorPalette = [][]string{
{
`"#ffbb00"`, `"#f90085"`, `"#00bfb2"`, `"#008ffc"`, `"#fc4f00"`, `"#9d00ff"`, `"#ff0000"`, `"#00b515"`,
`"#c7f400"`, `"#f9a2a2"`, `"#007072"`, `"#e6beff"`, `"#aa5b00"`, `"#fff266"`, `"#7f0000"`, `"#aaffc3"`,
`"#7f7f00"`, `"#ffe0c1"`, `"#000080"`, `"#808080"`, `"#000000"`},
{
`"#f44336"`, `"#9c27b0"`, `"#3f51b5"`, `"#03a9f4"`, `"#009688"`, `"#8bc34a"`, `"#ffeb3b"`, `"#ff9800"`,
`"#795548"`, `"#607d8b"`, `"#e91e63"`, `"#673ab7"`, `"#2196f3"`, `"#00bcd4"`, `"#4caf50"`, `"#cddc39"`,
`"#ffc107"`, `"#ff5722"`, `"#9e9e9e"`}
`"#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 +28,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
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

0 comments on commit 5367c46

Please sign in to comment.