adding jet as a generator

This commit is contained in:
Will Charczuk 2017-04-15 09:59:37 -07:00
parent c599f1a2a2
commit 3f046bbba1
3 changed files with 43 additions and 1 deletions

View file

@ -17,7 +17,7 @@ func drawChart(res http.ResponseWriter, req *http.Request) {
Show: true,
StrokeWidth: chart.Disabled,
DotWidth: 5,
DotColorProvider: chart.Viridis,
DotColorProvider: chart.Jet,
},
XValues: chart.Sequence.Random(128, 1024),
YValues: chart.Sequence.Random(128, 1024),

View file

@ -57,6 +57,11 @@ func ColorFromAlphaMixedRGBA(r, g, b, a uint32) Color {
return c
}
// ColorChannelFromFloat returns a normalized byte from a given float value.
func ColorChannelFromFloat(v float64) uint8 {
return uint8(v * 255)
}
// Color is our internal color type because color.Color is bullshit.
type Color struct {
R, G, B, A uint8

37
jet.go Normal file
View file

@ -0,0 +1,37 @@
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 {
c := drawing.Color{R: 0xff, G: 0xff, B: 0xff, A: 0xff} // white
var dv float64
if v < vmin {
v = vmin
}
if v > vmax {
v = vmax
}
dv = vmax - vmin
if v < (vmin + 0.25*dv) {
c.R = 0
c.G = drawing.ColorChannelFromFloat(4 * (v - vmin) / dv)
} else if v < (vmin + 0.5*dv) {
c.R = 0
c.B = drawing.ColorChannelFromFloat(1 + 4*(vmin+0.25*dv-v)/dv)
} else if v < (vmin + 0.75*dv) {
c.R = drawing.ColorChannelFromFloat(4 * (v - vmin - 0.5*dv) / dv)
c.B = 0
} else {
c.G = drawing.ColorChannelFromFloat(1 + 4*(vmin+0.75*dv-v)/dv)
c.B = 0
}
return c
}