...

Source file src/gonum.org/v1/plot/plotter/sankey_test.go

Documentation: gonum.org/v1/plot/plotter

     1  // Copyright ©2016 The Gonum Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package plotter_test
     6  
     7  import (
     8  	"testing"
     9  
    10  	"gonum.org/v1/plot"
    11  	"gonum.org/v1/plot/cmpimg"
    12  	"gonum.org/v1/plot/plotter"
    13  	"gonum.org/v1/plot/vg"
    14  	"gonum.org/v1/plot/vg/draw"
    15  	"gonum.org/v1/plot/vg/recorder"
    16  )
    17  
    18  func TestSankey_simple(t *testing.T) {
    19  	cmpimg.CheckPlot(ExampleSankey_simple, t, "sankeySimple.png")
    20  }
    21  
    22  func TestSankey_grouped(t *testing.T) {
    23  	cmpimg.CheckPlot(ExampleSankey_grouped, t, "sankeyGrouped.png")
    24  }
    25  
    26  // This test checks whether the Sankey plotter makes any changes to
    27  // the input Flows.
    28  func TestSankey_idempotent(t *testing.T) {
    29  	flows := []plotter.Flow{
    30  		{
    31  			SourceCategory:   0,
    32  			SourceLabel:      "Large",
    33  			ReceptorCategory: 1,
    34  			ReceptorLabel:    "Mohamed",
    35  			Value:            5,
    36  		},
    37  		{
    38  			SourceCategory:   0,
    39  			SourceLabel:      "Small",
    40  			ReceptorCategory: 1,
    41  			ReceptorLabel:    "Sofia",
    42  			Value:            5,
    43  		},
    44  	}
    45  	s, err := plotter.NewSankey(flows...)
    46  	if err != nil {
    47  		t.Fatal(err)
    48  	}
    49  	p := plot.New()
    50  	p.Add(s)
    51  	p.HideAxes()
    52  
    53  	// Draw the plot once.
    54  	c1 := new(recorder.Canvas)
    55  	dc1 := draw.NewCanvas(c1, vg.Centimeter, vg.Centimeter)
    56  	p.Draw(dc1)
    57  
    58  	// Draw the plot a second time.
    59  	c2 := new(recorder.Canvas)
    60  	dc2 := draw.NewCanvas(c2, vg.Centimeter, vg.Centimeter)
    61  	p.Draw(dc2)
    62  
    63  	if len(c1.Actions) != len(c2.Actions) {
    64  		t.Errorf("inconsistent number of actions: %d != %d", len(c2.Actions), len(c1.Actions))
    65  	}
    66  
    67  	for i, a1 := range c1.Actions {
    68  		if a1.Call() != c2.Actions[i].Call() {
    69  			t.Errorf("action %d: %s\n\t!= %s", i, c2.Actions[i].Call(), a1.Call())
    70  		}
    71  	}
    72  }
    73  

View as plain text