Separation of DotColorProvider and ColorProvider Interfaces (#34)

* fully functioning Viridis

* pure functions
This commit is contained in:
Chris Redford 2017-04-17 16:21:02 -07:00 committed by Will Charczuk
parent e53554fb04
commit b3dc3fef3c
7 changed files with 18 additions and 13 deletions

View file

@ -26,8 +26,8 @@ func drawChartWide(res http.ResponseWriter, req *http.Request) {
Width: 1920, //this overrides the default.
Series: []chart.Series{
chart.ContinuousSeries{
XValues: []float64{1.0, 2.0, 3.0, 4.0, 5.0},
YValues: []float64{1.0, 2.0, 3.0, 4.0, 5.0},
XValues: []float64{1.0, 2.0, 3.0, 4.0},
YValues: []float64{1.0, 2.0, 3.0, 4.0},
},
},
}

View file

@ -7,9 +7,15 @@ import (
_ "net/http/pprof"
"github.com/wcharczuk/go-chart"
"github.com/wcharczuk/go-chart/drawing"
)
func drawChart(res http.ResponseWriter, req *http.Request) {
viridisByY := func(xr, yr chart.Range, x, y float64) drawing.Color {
return chart.Viridis(y, yr.GetMin(), yr.GetMax())
}
graph := chart.Chart{
Series: []chart.Series{
chart.ContinuousSeries{
@ -17,7 +23,7 @@ func drawChart(res http.ResponseWriter, req *http.Request) {
Show: true,
StrokeWidth: chart.Disabled,
DotWidth: 5,
DotColorProvider: chart.Jet,
DotColorProvider: viridisByY,
},
XValues: chart.Sequence.Random(128, 1024),
YValues: chart.Sequence.Random(128, 1024),

Binary file not shown.

Before

Width:  |  Height:  |  Size: 12 KiB

After

Width:  |  Height:  |  Size: 27 KiB

6
jet.go
View file

@ -3,11 +3,7 @@ package chart
import "github.com/wcharczuk/go-chart/drawing"
// Jet is a color map provider based on matlab's jet color map.
func Jet(xr, yr Range, x, y float64) drawing.Color {
return getJetColour(y, yr.GetMin(), yr.GetMax())
}
func getJetColour(v, vmin, vmax float64) drawing.Color {
func Jet(v, vmin, vmax float64) drawing.Color {
c := drawing.Color{R: 0xff, G: 0xff, B: 0xff, A: 0xff} // white
var dv float64

View file

@ -46,7 +46,7 @@ type Style struct {
DotWidth float64
DotWidthProvider SizeProvider
DotColorProvider ColorProvider
DotColorProvider DotColorProvider
FillColor drawing.Color

View file

@ -39,5 +39,8 @@ type FullBoundedValueProvider interface {
// SizeProvider is a provider for integer size.
type SizeProvider func(xrange, yrange Range, x, y float64) float64
// ColorProvider is a provider for dot size.
type ColorProvider func(xrange, yrange Range, x, y float64) drawing.Color
// ColorProvider is a general provider for color ranges based on values.
type ColorProvider func(v, vmin, vmax float64) drawing.Color
// DotColorProvider is a provider for dot color.
type DotColorProvider func(xrange, yrange Range, x, y float64) drawing.Color

View file

@ -262,8 +262,8 @@ var viridisColors = [256]drawing.Color{
}
// Viridis creates a color map provider.
func Viridis(xr, yr Range, x, y float64) drawing.Color {
normalized := (y - yr.GetMin()) / yr.GetDelta()
func Viridis(v, vmin, vmax float64) drawing.Color {
normalized := (v - vmin) / (vmax - vmin)
index := uint8(normalized * 255)
return viridisColors[index]
}