go-hep.org/x/hep@v0.38.1/hplot/options.go (about)

     1  // Copyright ©2020 The go-hep 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 hplot
     6  
     7  import (
     8  	"gonum.org/v1/plot/vg/draw"
     9  )
    10  
    11  // Options encodes various options to pass to a plot.
    12  type Options func(cfg *config)
    13  
    14  // Step kind
    15  type StepsKind byte
    16  
    17  const (
    18  	NoSteps StepsKind = iota
    19  
    20  	// HiSteps connects two points by following lines: horizontal, vertical, horizontal.
    21  	// Vertical line is placed following the histogram/error-bins informations.
    22  	HiSteps
    23  
    24  	// PreSteps connects two points by following lines: vertical, horizontal.
    25  	PreSteps
    26  
    27  	// MidSteps connects two points by following lines: horizontal, vertical, horizontal.
    28  	// Vertical line is placed in the middle of the interval.
    29  	MidSteps
    30  
    31  	// PostSteps connects two points by following lines: horizontal, vertical.
    32  	PostSteps
    33  )
    34  
    35  type config struct {
    36  	bars struct {
    37  		xerrs bool
    38  		yerrs bool
    39  	}
    40  	band   bool
    41  	hinfos HInfos
    42  	log    struct {
    43  		y bool
    44  	}
    45  	glyph draw.GlyphStyle
    46  	steps StepsKind
    47  }
    48  
    49  func newConfig(opts []Options) *config {
    50  	cfg := new(config)
    51  	cfg.steps = NoSteps
    52  	for _, opt := range opts {
    53  		opt(cfg)
    54  	}
    55  	return cfg
    56  }
    57  
    58  // WithLogY sets whether the plotter in Y should handle log-scale.
    59  func WithLogY(v bool) Options {
    60  	return func(c *config) {
    61  		c.log.y = v
    62  	}
    63  }
    64  
    65  // WithXErrBars enables or disables the display of X-error bars.
    66  func WithXErrBars(v bool) Options {
    67  	return func(c *config) {
    68  		c.bars.xerrs = v
    69  	}
    70  }
    71  
    72  // WithYErrBars enables or disables the display of Y-error bars.
    73  func WithYErrBars(v bool) Options {
    74  	return func(c *config) {
    75  		c.bars.yerrs = v
    76  	}
    77  }
    78  
    79  // WithBand enables or disables the display of a colored band between Y-error bars.
    80  func WithBand(v bool) Options {
    81  	return func(c *config) {
    82  		c.band = v
    83  	}
    84  }
    85  
    86  // WithStepsKind sets the style of the connecting line (NoSteps, HiSteps, etc...)
    87  func WithStepsKind(s StepsKind) Options {
    88  	return func(c *config) {
    89  		c.steps = s
    90  	}
    91  }
    92  
    93  // WithGlyphStyle sets the glyph style of a plotter.
    94  func WithGlyphStyle(sty draw.GlyphStyle) Options {
    95  	return func(c *config) {
    96  		c.glyph = sty
    97  	}
    98  }
    99  
   100  // WithHInfo sets a given histogram info style.
   101  func WithHInfo(v HInfoStyle) Options {
   102  	return func(c *config) {
   103  		c.hinfos.Style = v
   104  	}
   105  }