github.com/tobgu/qframe@v0.4.0/contrib/gonum/qplot/plotter_funcs.go (about) 1 package qplot 2 3 import ( 4 "gonum.org/v1/plot" 5 "gonum.org/v1/plot/plotter" 6 "gonum.org/v1/plot/vg" 7 8 "github.com/tobgu/qframe/qerrors" 9 ) 10 11 // PlotterFunc returns a plot.Plotter. 12 type PlotterFunc func(plt *plot.Plot) (plot.Plotter, error) 13 14 // LineConfig is an optional function which 15 // configures a Line after creation. 16 type LineConfig func(*plot.Plot, *plotter.Line) 17 18 // LinePlotter returns a new PlotterFunc that plots a line 19 func LinePlotter(xyer plotter.XYer, cfg LineConfig) PlotterFunc { 20 return func(plt *plot.Plot) (plot.Plotter, error) { 21 pltr, err := plotter.NewLine(xyer) 22 if err != nil { 23 return nil, qerrors.Propagate("LinePlotter", err) 24 } 25 if cfg != nil { 26 cfg(plt, pltr) 27 } 28 return pltr, nil 29 } 30 } 31 32 // BarConfig is an optional function which 33 // configures a BarChart after creation. 34 type BarConfig func(*plot.Plot, *plotter.BarChart) 35 36 // BarPlotter returns a new PlotterFunc that plots a bar 37 func BarPlotter(valuer plotter.Valuer, width vg.Length, cfg BarConfig) PlotterFunc { 38 return func(plt *plot.Plot) (plot.Plotter, error) { 39 pltr, err := plotter.NewBarChart(valuer, width) 40 if err != nil { 41 return nil, qerrors.Propagate("BarPlotter", err) 42 } 43 if cfg != nil { 44 cfg(plt, pltr) 45 } 46 return pltr, nil 47 } 48 } 49 50 // HistogramConfig is an optional function which 51 // configures a Histogram after creation. 52 type HistogramConfig func(*plot.Plot, *plotter.Histogram) 53 54 // HistogramPlotter returns a new PlotterFunc that plots a histogram 55 func HistogramPlotter(xyer plotter.XYer, n int, cfg HistogramConfig) PlotterFunc { 56 return func(plt *plot.Plot) (plot.Plotter, error) { 57 pltr, err := plotter.NewHistogram(xyer, n) 58 if err != nil { 59 return nil, qerrors.Propagate("HistogramPlotter", err) 60 } 61 if cfg != nil { 62 cfg(plt, pltr) 63 } 64 return pltr, nil 65 } 66 } 67 68 // PolygonConfig is an optional function which 69 // configures a Polygon after creation. 70 type PolygonConfig func(*plot.Plot, *plotter.Polygon) 71 72 // PolygonPlotter returns a new PlotterFunc that plots a polygon 73 func PolygonPlotter(xyer plotter.XYer, cfg PolygonConfig) PlotterFunc { 74 return func(plt *plot.Plot) (plot.Plotter, error) { 75 pltr, err := plotter.NewPolygon(xyer) 76 if err != nil { 77 return nil, qerrors.Propagate("PolygonPlotter", err) 78 } 79 if cfg != nil { 80 cfg(plt, pltr) 81 } 82 return pltr, nil 83 } 84 } 85 86 // ScatterConfig is an optional function which 87 // configures a Scatter after creation. 88 type ScatterConfig func(*plot.Plot, *plotter.Scatter) 89 90 // ScatterPlotter returns a new PlotterFunc that plots a Scatter. 91 func ScatterPlotter(xyer plotter.XYer, cfg ScatterConfig) PlotterFunc { 92 return func(plt *plot.Plot) (plot.Plotter, error) { 93 pltr, err := plotter.NewScatter(xyer) 94 if err != nil { 95 return nil, qerrors.Propagate("ScatterPlotter", err) 96 } 97 if cfg != nil { 98 cfg(plt, pltr) 99 } 100 return pltr, nil 101 } 102 } 103 104 // BoxPlotConfig is an optional function which 105 // configures a BoxPlot after creation. 106 type BoxPlotConfig func(*plot.Plot, *plotter.BoxPlot) 107 108 // BoxPlot returns a new PlotterFunc that plots a BoxPlot. 109 func BoxPlot(w vg.Length, loc float64, values plotter.Valuer, cfg BoxPlotConfig) PlotterFunc { 110 return func(plt *plot.Plot) (plot.Plotter, error) { 111 pltr, err := plotter.NewBoxPlot(w, loc, values) 112 if err != nil { 113 return nil, qerrors.Propagate("BoxPlot", err) 114 } 115 if cfg != nil { 116 cfg(plt, pltr) 117 } 118 return pltr, nil 119 } 120 } 121 122 // LabelsConfig is an optional function which 123 // configures a Labels after creation. 124 type LabelsConfig func(*plot.Plot, *plotter.Labels) 125 126 // Labels returns a new PlotterFunc that plots a plotter.Labels. 127 func Labels(labeller XYLabeller, cfg LabelsConfig) PlotterFunc { 128 return func(plt *plot.Plot) (plot.Plotter, error) { 129 pltr, err := plotter.NewLabels(labeller) 130 if err != nil { 131 return nil, qerrors.Propagate("Labels", err) 132 } 133 if cfg != nil { 134 cfg(plt, pltr) 135 } 136 return pltr, nil 137 } 138 } 139 140 // QuartConfig is an optional function which 141 // configures a QuartPlot after creation. 142 type QuartConfig func(*plot.Plot, *plotter.QuartPlot) 143 144 // QuartPlot returns a new PlotterFunc that plots a QuartPlot. 145 func QuartPlot(loc float64, values plotter.Valuer, cfg QuartConfig) PlotterFunc { 146 return func(plt *plot.Plot) (plot.Plotter, error) { 147 pltr, err := plotter.NewQuartPlot(loc, values) 148 if err != nil { 149 return nil, qerrors.Propagate("QuartPlot", err) 150 } 151 if cfg != nil { 152 cfg(plt, pltr) 153 } 154 return pltr, nil 155 } 156 } 157 158 // satisfies New<X,Y>ErrorBars function interface 159 type errorBars struct { 160 XYer 161 YErrorer 162 XErrorer 163 } 164 165 // YErrorBarsConfig is an optional function which 166 // configures a YErrorBars after creation. 167 type YErrorBarsConfig func(*plot.Plot, *plotter.YErrorBars) 168 169 // YErrorBars returns a new PlotterFunc that plots a YErrorBars. 170 func YErrorBars(xyer XYer, yerr YErrorer, cfg YErrorBarsConfig) PlotterFunc { 171 return func(plt *plot.Plot) (plot.Plotter, error) { 172 pltr, err := plotter.NewYErrorBars(errorBars{XYer: xyer, YErrorer: yerr}) 173 if err != nil { 174 return nil, qerrors.Propagate("YErrorBars", err) 175 } 176 if cfg != nil { 177 cfg(plt, pltr) 178 } 179 return pltr, nil 180 } 181 } 182 183 // XErrorBarsConfig is an optional function which 184 // configures a XErrorBars after creation. 185 type XErrorBarsConfig func(*plot.Plot, *plotter.XErrorBars) 186 187 // XErrorBars returns a new PlotterFunc that plots a XErrorBars. 188 func XErrorBars(xyer XYer, xerr XErrorer, cfg XErrorBarsConfig) PlotterFunc { 189 return func(plt *plot.Plot) (plot.Plotter, error) { 190 pltr, err := plotter.NewXErrorBars(errorBars{XYer: xyer, XErrorer: xerr}) 191 if err != nil { 192 return nil, qerrors.Propagate("XErrorBars", err) 193 } 194 if cfg != nil { 195 cfg(plt, pltr) 196 } 197 return pltr, nil 198 } 199 } 200 201 // TODO - These don't really make sense to include 202 // in the API but can easily be added with a custom PlotterFunc 203 // plotter.Function 204 // plotter.HeatMap 205 // plotter.Grid 206 // plotter.Image 207 // plotter.Sankey