From 107b1c9390706177b15c3aeeb630a4e67accd3d0 Mon Sep 17 00:00:00 2001 From: Chris Tessum Date: Fri, 28 Apr 2017 21:55:21 -0700 Subject: [PATCH] Add thumbnailer for palette.Palette --- plotter/heat_test.go | 14 +++++++- plotter/palettethumbnailer.go | 67 +++++++++++++++++++++++++++++++++++ 2 files changed, 80 insertions(+), 1 deletion(-) create mode 100644 plotter/palettethumbnailer.go diff --git a/plotter/heat_test.go b/plotter/heat_test.go index 9a25aaa3..3c29d227 100644 --- a/plotter/heat_test.go +++ b/plotter/heat_test.go @@ -5,6 +5,7 @@ package plotter import ( + "fmt" "log" "testing" @@ -48,7 +49,8 @@ func ExampleHeatMap() { 5, 6, 7, 8, 9, 10, 11, 12, })} - h := NewHeatMap(m, palette.Heat(12, 1)) + plte := palette.Heat(12, 1) + h := NewHeatMap(m, plte) p, err := plot.New() if err != nil { @@ -58,6 +60,16 @@ func ExampleHeatMap() { p.Add(h) + // Create a legend. + rng := h.Max - h.Min + thumbs := PaletteThumbnailers(plte) + for i := len(thumbs) - 1; i >= 0; i-- { + t := thumbs[i] + fmin := float64(i) / float64(len(thumbs)) + fmax := float64(i+1) / float64(len(thumbs)) + p.Legend.Add(fmt.Sprintf("%.2g - %.2g", h.Min+fmin*rng, h.Min+fmax*rng), t) + } + p.X.Padding = 0 p.Y.Padding = 0 p.X.Max = 1.5 diff --git a/plotter/palettethumbnailer.go b/plotter/palettethumbnailer.go new file mode 100644 index 00000000..b0fd751d --- /dev/null +++ b/plotter/palettethumbnailer.go @@ -0,0 +1,67 @@ +// Copyright ©2017 The gonum Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package plotter + +import ( + "image/color" + + "github.com/gonum/plot" + "github.com/gonum/plot/palette" + "github.com/gonum/plot/vg" + "github.com/gonum/plot/vg/draw" +) + +// PaletteThumbnailers creates a group of objects that can be used to +// add legend entries for the colors in a color palette. +func PaletteThumbnailers(p palette.Palette) []plot.Thumbnailer { + colors := p.Colors() + thumbnailers := make([]plot.Thumbnailer, len(colors)) + for i, c := range colors { + thumbnailers[i] = paletteThumbnailer{color: c} + } + return thumbnailers +} + +// paletteThumbnailer implements the Thumbnailer interface +// for color palettes. +type paletteThumbnailer struct { + color color.Color +} + +// Thumbnail fulfills the plot.Thumbnailer interface. +func (t paletteThumbnailer) Thumbnail(c *draw.Canvas) { + lineStyle := draw.LineStyle{ + Color: color.Black, + Width: vg.Points(1), + Dashes: []vg.Length{}, + DashOffs: 0, + } + + // Here we draw the fill. + pts := []vg.Point{ + {X: c.Min.X, Y: c.Min.Y}, + {X: c.Min.X, Y: c.Max.Y}, + {X: c.Max.X, Y: c.Max.Y}, + {X: c.Max.X, Y: c.Min.Y}, + } + poly := c.ClipPolygonY(pts) + c.FillPolygon(t.color, poly) + + // Here we draw the upper border. + pts = []vg.Point{ + {X: c.Min.X, Y: c.Max.Y}, + {X: c.Max.X, Y: c.Max.Y}, + } + outline := c.ClipLinesY(pts) + c.StrokeLines(lineStyle, outline...) + + // Here we draw the lower border. + pts = []vg.Point{ + {X: c.Min.X, Y: c.Min.Y}, + {X: c.Max.X, Y: c.Min.Y}, + } + outline = c.ClipLinesY(pts) + c.StrokeLines(lineStyle, outline...) +}