go.charczuk.com@v0.0.0-20240327042549-bc490516bd1a/projects/nodes/pkg/funcs/chart_utils.go (about)

     1  /*
     2  
     3  Copyright (c) 2024 - Present. Will Charczuk. All rights reserved.
     4  Use of this source code is governed by a MIT license that can be found in the LICENSE file at the root of the repository.
     5  
     6  */
     7  
     8  package funcs
     9  
    10  import (
    11  	"context"
    12  	"encoding/json"
    13  	"fmt"
    14  
    15  	"github.com/wcharczuk/go-chart/v2"
    16  	"github.com/wcharczuk/go-chart/v2/drawing"
    17  	"github.com/wcharczuk/go-incr"
    18  )
    19  
    20  // ChartConfig is a set of configurable options for charts.
    21  type ChartConfig struct {
    22  	Height                  int    `json:"height"`
    23  	Width                   int    `json:"width"`
    24  	CanvasColor             string `json:"canvas_color"`
    25  	BackgroundColor         string `json:"background_color"`
    26  	SeriesFillColor         string `json:"series_fill_color"`
    27  	SeriesStrokeColor       string `json:"series_stroke_color"`
    28  	RegressionStrokeColor   string `json:"regression_stroke_color"`
    29  	RegressionFillColor     string `json:"regression_fill_color"`
    30  	HideAxisLabels          bool   `json:"hide_axis_labels"`
    31  	HideSeries              bool   `json:"hide_series"`
    32  	HideSeriesLastValue     bool   `json:"hide_series_last_value"`
    33  	HideRegression          bool   `json:"hide_regression"`
    34  	HideRegressionLastValue bool   `json:"hide_regression_last_value"`
    35  }
    36  
    37  // ParseChartConfig parses a chart config from an expression.
    38  func ParseChartConfig(expression string) (output ChartConfig) {
    39  	_ = json.Unmarshal([]byte(expression), &output)
    40  	return
    41  }
    42  
    43  // ApplyChart applies the chart config to the chart itself.
    44  func (cc ChartConfig) ApplyChart(target *chart.Chart) {
    45  	if cc.Height > 0 {
    46  		target.Height = cc.Height
    47  	}
    48  	if cc.Width > 0 {
    49  		target.Width = cc.Width
    50  	}
    51  	if cc.HideAxisLabels {
    52  		target.XAxis.Style = chart.Hidden()
    53  		target.YAxis.Style = chart.Hidden()
    54  		target.YAxisSecondary.Style = chart.Hidden()
    55  	}
    56  	if cc.BackgroundColor != "" {
    57  		target.Background.FillColor = drawing.ParseColor(cc.BackgroundColor)
    58  	}
    59  	if cc.CanvasColor != "" {
    60  		target.Canvas.FillColor = drawing.ParseColor(cc.CanvasColor)
    61  	}
    62  }
    63  
    64  // ApplyChartThumbnail applies the chart config to the chart itself.
    65  func (cc ChartConfig) ApplyChartThumbnail(target *chart.Chart) {
    66  	if cc.BackgroundColor != "" {
    67  		target.Background.FillColor = drawing.ParseColor(cc.BackgroundColor)
    68  	}
    69  	if cc.CanvasColor != "" {
    70  		target.Canvas.FillColor = drawing.ParseColor(cc.CanvasColor)
    71  	}
    72  }
    73  
    74  // ApplyPrimarySeries applies the chart config to the primary series.
    75  func (cc ChartConfig) ApplyPrimaryTimeSeries(primarySeries *chart.TimeSeries) {
    76  	if cc.SeriesStrokeColor != "" {
    77  		primarySeries.Style.StrokeColor = drawing.ParseColor(cc.SeriesStrokeColor)
    78  	}
    79  	if cc.SeriesFillColor != "" {
    80  		primarySeries.Style.FillColor = drawing.ParseColor(cc.SeriesFillColor)
    81  	}
    82  }
    83  
    84  // ApplyPrimaryContinuousSeries applies the chart config to the primary series.
    85  func (cc ChartConfig) ApplyPrimaryContinuousSeries(primarySeries *chart.ContinuousSeries) {
    86  	if cc.SeriesFillColor != "" {
    87  		primarySeries.Style.FillColor = drawing.ParseColor(cc.SeriesFillColor)
    88  	}
    89  	if cc.SeriesStrokeColor != "" {
    90  		primarySeries.Style.StrokeColor = drawing.ParseColor(cc.SeriesStrokeColor)
    91  	}
    92  }
    93  
    94  // ApplyRegressionSeries applies the chart config to the primary series.
    95  func (cc ChartConfig) ApplyRegressionSeries(regressionSeries *chart.LinearRegressionSeries) {
    96  	if cc.SeriesFillColor != "" {
    97  		regressionSeries.Style.FillColor = drawing.ParseColor(cc.RegressionFillColor)
    98  	}
    99  	if cc.SeriesStrokeColor != "" {
   100  		regressionSeries.Style.StrokeColor = drawing.ParseColor(cc.RegressionStrokeColor)
   101  	}
   102  }
   103  
   104  func getChartLoggerForContext(ctx context.Context) chart.Logger {
   105  	if tracer := incr.GetTracer(ctx); tracer != nil {
   106  		return &chartLogger{Tracer: tracer}
   107  	}
   108  	return nil
   109  }
   110  
   111  type chartLogger struct {
   112  	Tracer incr.Tracer
   113  }
   114  
   115  func (cl chartLogger) Info(args ...any) {
   116  	cl.Tracer.Print(append([]any{"chart.info"}, args...)...)
   117  }
   118  
   119  func (cl chartLogger) Infof(format string, args ...any) {
   120  	cl.Tracer.Print("chart.info: ", fmt.Sprintf(format, args...))
   121  }
   122  
   123  func (cl chartLogger) Debug(args ...any) {
   124  	cl.Tracer.Print(append([]any{"chart.debug: "}, args...)...)
   125  }
   126  
   127  func (cl chartLogger) Debugf(format string, args ...any) {
   128  	cl.Tracer.Print("chart.debug: ", fmt.Sprintf(format, args...))
   129  }
   130  
   131  func (cl chartLogger) Err(err error) {
   132  	if err != nil {
   133  		cl.Tracer.Error(append([]any{"chart.error: "}, err)...)
   134  	}
   135  }
   136  
   137  func (cl chartLogger) FatalErr(err error) {
   138  	if err != nil {
   139  		cl.Tracer.Error(append([]any{"chart.fatal: "}, err)...)
   140  	}
   141  }
   142  
   143  func (cl chartLogger) Error(args ...any) {
   144  	cl.Tracer.Error(append([]any{"chart.error: "}, args...)...)
   145  }
   146  
   147  func (cl chartLogger) Errorf(format string, args ...any) {
   148  	cl.Tracer.Print("chart.error: ", fmt.Sprintf(format, args...))
   149  }