github.com/kotovmak/go-admin@v1.1.1/template/chartjs/line.go (about)

     1  package chartjs
     2  
     3  import (
     4  	"bytes"
     5  	"encoding/json"
     6  	"fmt"
     7  	"html/template"
     8  
     9  	template2 "github.com/kotovmak/go-admin/template"
    10  )
    11  
    12  type LineChart struct {
    13  	*Chart
    14  
    15  	JsContent LineJsContent
    16  }
    17  
    18  type LineJsContent struct {
    19  	JsContent
    20  
    21  	Data LineAttributes `json:"data"`
    22  }
    23  
    24  type LineAttributes struct {
    25  	Attributes
    26  
    27  	DataSets LineDataSets `json:"datasets"`
    28  }
    29  
    30  type LineDataSets []*LineDataSet
    31  
    32  func (l LineDataSets) Add(ds *LineDataSet) LineDataSets {
    33  	return append(l, ds)
    34  }
    35  
    36  type LineDataSet struct {
    37  	Label                     string    `json:"label"`
    38  	Data                      []float64 `json:"data"`
    39  	Type                      string    `json:"type,omitempty"`
    40  	BackgroundColor           Color     `json:"backgroundColor,omitempty"`
    41  	BorderCapStyle            string    `json:"borderCapStyle,omitempty"`
    42  	BorderColor               Color     `json:"borderColor,omitempty"`
    43  	BorderDash                []int     `json:"borderDash,omitempty"`
    44  	BorderDashOffset          float64   `json:"borderDashOffset,omitempty"`
    45  	BorderJoinStyle           string    `json:"borderJoinStyle,omitempty"`
    46  	BorderWidth               float64   `json:"borderWidth,omitempty"`
    47  	CubicInterpolationMode    string    `json:"cubicInterpolationMode,omitempty"`
    48  	Fill                      bool      `json:"fill"`
    49  	HoverBackgroundColor      Color     `json:"hoverBackgroundColor,omitempty"`
    50  	HoverBorderCapStyle       string    `json:"hoverBorderCapStyle,omitempty"`
    51  	HoverBorderColor          Color     `json:"hoverBorderColor,omitempty"`
    52  	HoverBorderDash           float64   `json:"hoverBorderDash,omitempty"`
    53  	HoverBorderDashOffset     float64   `json:"hoverBorderDashOffset,omitempty"`
    54  	HoverBorderJoinStyle      string    `json:"hoverBorderJoinStyle,omitempty"`
    55  	HoverBorderWidth          float64   `json:"hoverBorderWidth,omitempty"`
    56  	LineTension               float64   `json:"lineTension,omitempty"`
    57  	Order                     float64   `json:"order,omitempty"`
    58  	PointBackgroundColor      Color     `json:"pointBackgroundColor,omitempty"`
    59  	PointBorderColor          Color     `json:"pointBorderColor,omitempty"`
    60  	PointBorderWidth          float64   `json:"pointBorderWidth,omitempty"`
    61  	PointHitRadius            float64   `json:"pointHitRadius,omitempty"`
    62  	PointHoverBackgroundColor Color     `json:"pointHoverBackgroundColor,omitempty"`
    63  	PointHoverBorderColor     Color     `json:"pointHoverBorderColor,omitempty"`
    64  	PointHoverBorderWidth     float64   `json:"pointHoverBorderWidth,omitempty"`
    65  	PointHoverRadius          float64   `json:"pointHoverRadius,omitempty"`
    66  	PointRadius               float64   `json:"pointRadius,omitempty"`
    67  	PointRotation             float64   `json:"pointRotation,omitempty"`
    68  	PointStyle                string    `json:"pointStyle,omitempty"`
    69  	ShowLine                  bool      `json:"showLine,omitempty"`
    70  	SpanGaps                  bool      `json:"spanGaps,omitempty"`
    71  	SteppedLine               bool      `json:"steppedLine,omitempty"`
    72  	XAxisID                   string    `json:"xAxisID,omitempty"`
    73  	YAxisID                   string    `json:"yAxisID,omitempty"`
    74  }
    75  
    76  func (l *LineDataSet) SetLabel(label string) *LineDataSet {
    77  	l.Label = label
    78  	return l
    79  }
    80  
    81  func (l *LineDataSet) SetData(data []float64) *LineDataSet {
    82  	l.Data = data
    83  	return l
    84  }
    85  
    86  func (l *LineDataSet) SetType(t string) *LineDataSet {
    87  	l.Type = t
    88  	return l
    89  }
    90  
    91  func (l *LineDataSet) SetBackgroundColor(backgroundColor Color) *LineDataSet {
    92  	l.BackgroundColor = backgroundColor
    93  	return l
    94  }
    95  
    96  func (l *LineDataSet) SetBorderCapStyle(borderCapStyle string) *LineDataSet {
    97  	l.BorderCapStyle = borderCapStyle
    98  	return l
    99  }
   100  
   101  func (l *LineDataSet) SetBorderColor(borderColor Color) *LineDataSet {
   102  	l.BorderColor = borderColor
   103  	return l
   104  }
   105  
   106  func (l *LineDataSet) SetBorderDash(borderDash []int) *LineDataSet {
   107  	l.BorderDash = borderDash
   108  	return l
   109  }
   110  
   111  func (l *LineDataSet) SetBorderDashOffset(borderDashOffset float64) *LineDataSet {
   112  	l.BorderDashOffset = borderDashOffset
   113  	return l
   114  }
   115  
   116  func (l *LineDataSet) SetBorderJoinStyle(borderJoinStyle string) *LineDataSet {
   117  	l.BorderJoinStyle = borderJoinStyle
   118  	return l
   119  }
   120  
   121  func (l *LineDataSet) SetBorderWidth(borderWidth float64) *LineDataSet {
   122  	l.BorderWidth = borderWidth
   123  	return l
   124  }
   125  
   126  func (l *LineDataSet) SetCubicInterpolationMode(cubicInterpolationMode string) *LineDataSet {
   127  	l.CubicInterpolationMode = cubicInterpolationMode
   128  	return l
   129  }
   130  
   131  func (l *LineDataSet) SetFill(fill bool) *LineDataSet {
   132  	l.Fill = fill
   133  	return l
   134  }
   135  
   136  func (l *LineDataSet) SetHoverBackgroundColor(hoverBackgroundColor Color) *LineDataSet {
   137  	l.HoverBackgroundColor = hoverBackgroundColor
   138  	return l
   139  }
   140  
   141  func (l *LineDataSet) SetHoverBorderCapStyle(hoverBorderCapStyle string) *LineDataSet {
   142  	l.HoverBorderCapStyle = hoverBorderCapStyle
   143  	return l
   144  }
   145  
   146  func (l *LineDataSet) SetHoverBorderColor(hoverBorderColor Color) *LineDataSet {
   147  	l.HoverBorderColor = hoverBorderColor
   148  	return l
   149  }
   150  
   151  func (l *LineDataSet) SetHoverBorderDash(hoverBorderDash float64) *LineDataSet {
   152  	l.HoverBorderDash = hoverBorderDash
   153  	return l
   154  }
   155  
   156  func (l *LineDataSet) SetHoverBorderDashOffset(hoverBorderDashOffset float64) *LineDataSet {
   157  	l.HoverBorderDashOffset = hoverBorderDashOffset
   158  	return l
   159  }
   160  
   161  func (l *LineDataSet) SetHoverBorderJoinStyle(hoverBorderJoinStyle string) *LineDataSet {
   162  	l.HoverBorderJoinStyle = hoverBorderJoinStyle
   163  	return l
   164  }
   165  
   166  func (l *LineDataSet) SetHoverBorderWidth(hoverBorderWidth float64) *LineDataSet {
   167  	l.HoverBorderWidth = hoverBorderWidth
   168  	return l
   169  }
   170  
   171  func (l *LineDataSet) SetLineTension(lineTension float64) *LineDataSet {
   172  	l.LineTension = lineTension
   173  	return l
   174  }
   175  
   176  func (l *LineDataSet) SetOrder(order float64) *LineDataSet {
   177  	l.Order = order
   178  	return l
   179  }
   180  
   181  func (l *LineDataSet) SetPointBackgroundColor(pointBackgroundColor Color) *LineDataSet {
   182  	l.PointBackgroundColor = pointBackgroundColor
   183  	return l
   184  }
   185  
   186  func (l *LineDataSet) SetPointBorderColor(pointBorderColor Color) *LineDataSet {
   187  	l.PointBorderColor = pointBorderColor
   188  	return l
   189  }
   190  
   191  func (l *LineDataSet) SetPointBorderWidth(pointBorderWidth float64) *LineDataSet {
   192  	l.PointBorderWidth = pointBorderWidth
   193  	return l
   194  }
   195  
   196  func (l *LineDataSet) SetPointHitRadius(pointHitRadius float64) *LineDataSet {
   197  	l.PointHitRadius = pointHitRadius
   198  	return l
   199  }
   200  
   201  func (l *LineDataSet) SetPointHoverBackgroundColor(pointHoverBackgroundColor Color) *LineDataSet {
   202  	l.PointHoverBackgroundColor = pointHoverBackgroundColor
   203  	return l
   204  }
   205  
   206  func (l *LineDataSet) SetPointHoverBorderColor(pointHoverBorderColor Color) *LineDataSet {
   207  	l.PointHoverBorderColor = pointHoverBorderColor
   208  	return l
   209  }
   210  
   211  func (l *LineDataSet) SetPointHoverBorderWidth(pointHoverBorderWidth float64) *LineDataSet {
   212  	l.PointHoverBorderWidth = pointHoverBorderWidth
   213  	return l
   214  }
   215  
   216  func (l *LineDataSet) SetPointHoverRadius(pointHoverRadius float64) *LineDataSet {
   217  	l.PointHoverRadius = pointHoverRadius
   218  	return l
   219  }
   220  
   221  func (l *LineDataSet) SetPointRadius(pointRadius float64) *LineDataSet {
   222  	l.PointRadius = pointRadius
   223  	return l
   224  }
   225  
   226  func (l *LineDataSet) SetPointRotation(pointRotation float64) *LineDataSet {
   227  	l.PointRotation = pointRotation
   228  	return l
   229  }
   230  
   231  func (l *LineDataSet) SetPointStyle(pointStyle string) *LineDataSet {
   232  	l.PointStyle = pointStyle
   233  	return l
   234  }
   235  
   236  func (l *LineDataSet) SetShowLine(showLine bool) *LineDataSet {
   237  	l.ShowLine = showLine
   238  	return l
   239  }
   240  
   241  func (l *LineDataSet) SetSpanGaps(spanGaps bool) *LineDataSet {
   242  	l.SpanGaps = spanGaps
   243  	return l
   244  }
   245  
   246  func (l *LineDataSet) SetSteppedLine(steppedLine bool) *LineDataSet {
   247  	l.SteppedLine = steppedLine
   248  	return l
   249  }
   250  
   251  func (l *LineDataSet) SetXAxisID(xAxisID string) *LineDataSet {
   252  	l.XAxisID = xAxisID
   253  	return l
   254  }
   255  
   256  func (l *LineDataSet) SetYAxisID(yAxisID string) *LineDataSet {
   257  	l.YAxisID = yAxisID
   258  	return l
   259  }
   260  
   261  func Line() *LineChart {
   262  	return &LineChart{
   263  		Chart: &Chart{
   264  			BaseComponent: &template2.BaseComponent{
   265  				Name:     "chartjs",
   266  				HTMLData: List["chartjs"],
   267  			},
   268  			dataSetIndex: -1,
   269  		},
   270  		JsContent: LineJsContent{
   271  			JsContent: JsContent{
   272  				Type: "line",
   273  			},
   274  			Data: LineAttributes{
   275  				Attributes: Attributes{
   276  					Labels: make([]string, 0),
   277  				},
   278  				DataSets: make(LineDataSets, 0),
   279  			},
   280  		},
   281  	}
   282  }
   283  
   284  func (l *LineChart) SetID(s string) *LineChart {
   285  	l.ID = s
   286  	return l
   287  }
   288  
   289  func (l *LineChart) SetTitle(s template.HTML) *LineChart {
   290  	l.Title = s
   291  	return l
   292  }
   293  
   294  func (l *LineChart) SetHeight(s int) *LineChart {
   295  	l.Height = s
   296  	return l
   297  }
   298  
   299  func (l *LineChart) SetLabels(s []string) *LineChart {
   300  	l.JsContent.Data.Labels = s
   301  	return l
   302  }
   303  
   304  func (l *LineChart) AddDataSet(s string) *LineChart {
   305  	l.dataSetIndex++
   306  	l.JsContent.Data.DataSets = l.JsContent.Data.DataSets.Add(&LineDataSet{
   307  		Type:  "line",
   308  		Label: s,
   309  	})
   310  	return l
   311  }
   312  
   313  func (l *LineChart) DSLabel(s string) *LineChart {
   314  	l.JsContent.Data.DataSets[l.dataSetIndex].SetLabel(s)
   315  	return l
   316  }
   317  
   318  func (l *LineChart) DSData(data []float64) *LineChart {
   319  	l.JsContent.Data.DataSets[l.dataSetIndex].SetData(data)
   320  	return l
   321  }
   322  
   323  func (l *LineChart) DSType(t string) *LineChart {
   324  	l.JsContent.Data.DataSets[l.dataSetIndex].SetType(t)
   325  	return l
   326  }
   327  
   328  func (l *LineChart) DSBackgroundColor(backgroundColor Color) *LineChart {
   329  	l.JsContent.Data.DataSets[l.dataSetIndex].SetBackgroundColor(backgroundColor)
   330  	return l
   331  }
   332  
   333  func (l *LineChart) DSBorderCapStyle(borderCapStyle string) *LineChart {
   334  	l.JsContent.Data.DataSets[l.dataSetIndex].SetBorderCapStyle(borderCapStyle)
   335  	return l
   336  }
   337  
   338  func (l *LineChart) DSBorderColor(borderColor Color) *LineChart {
   339  	l.JsContent.Data.DataSets[l.dataSetIndex].SetBorderColor(borderColor)
   340  	return l
   341  }
   342  
   343  func (l *LineChart) DSBorderDash(borderDash []int) *LineChart {
   344  	l.JsContent.Data.DataSets[l.dataSetIndex].SetBorderDash(borderDash)
   345  	return l
   346  }
   347  
   348  func (l *LineChart) DSBorderDashOffset(borderDashOffset float64) *LineChart {
   349  	l.JsContent.Data.DataSets[l.dataSetIndex].SetBorderDashOffset(borderDashOffset)
   350  	return l
   351  }
   352  
   353  func (l *LineChart) DSBorderJoinStyle(borderJoinStyle string) *LineChart {
   354  	l.JsContent.Data.DataSets[l.dataSetIndex].SetBorderJoinStyle(borderJoinStyle)
   355  	return l
   356  }
   357  
   358  func (l *LineChart) DSBorderWidth(borderWidth float64) *LineChart {
   359  	l.JsContent.Data.DataSets[l.dataSetIndex].SetBorderWidth(borderWidth)
   360  	return l
   361  }
   362  
   363  func (l *LineChart) DSCubicInterpolationMode(cubicInterpolationMode string) *LineChart {
   364  	l.JsContent.Data.DataSets[l.dataSetIndex].SetCubicInterpolationMode(cubicInterpolationMode)
   365  	return l
   366  }
   367  
   368  func (l *LineChart) DSFill(fill bool) *LineChart {
   369  	l.JsContent.Data.DataSets[l.dataSetIndex].SetFill(fill)
   370  	return l
   371  }
   372  
   373  func (l *LineChart) DSHoverBackgroundColor(hoverBackgroundColor Color) *LineChart {
   374  	l.JsContent.Data.DataSets[l.dataSetIndex].SetHoverBackgroundColor(hoverBackgroundColor)
   375  	return l
   376  }
   377  
   378  func (l *LineChart) DSHoverBorderCapStyle(hoverBorderCapStyle string) *LineChart {
   379  	l.JsContent.Data.DataSets[l.dataSetIndex].SetHoverBorderCapStyle(hoverBorderCapStyle)
   380  	return l
   381  }
   382  
   383  func (l *LineChart) DSHoverBorderColor(hoverBorderColor Color) *LineChart {
   384  	l.JsContent.Data.DataSets[l.dataSetIndex].SetHoverBorderColor(hoverBorderColor)
   385  	return l
   386  }
   387  
   388  func (l *LineChart) DSHoverBorderDash(hoverBorderDash float64) *LineChart {
   389  	l.JsContent.Data.DataSets[l.dataSetIndex].SetHoverBorderDash(hoverBorderDash)
   390  	return l
   391  }
   392  
   393  func (l *LineChart) DSHoverBorderDashOffset(hoverBorderDashOffset float64) *LineChart {
   394  	l.JsContent.Data.DataSets[l.dataSetIndex].SetHoverBorderDashOffset(hoverBorderDashOffset)
   395  	return l
   396  }
   397  
   398  func (l *LineChart) DSHoverBorderJoinStyle(hoverBorderJoinStyle string) *LineChart {
   399  	l.JsContent.Data.DataSets[l.dataSetIndex].SetHoverBorderJoinStyle(hoverBorderJoinStyle)
   400  	return l
   401  }
   402  
   403  func (l *LineChart) DSHoverBorderWidth(hoverBorderWidth float64) *LineChart {
   404  	l.JsContent.Data.DataSets[l.dataSetIndex].SetHoverBorderWidth(hoverBorderWidth)
   405  	return l
   406  }
   407  
   408  func (l *LineChart) DSLineTension(lineTension float64) *LineChart {
   409  	l.JsContent.Data.DataSets[l.dataSetIndex].SetLineTension(lineTension)
   410  	return l
   411  }
   412  
   413  func (l *LineChart) DSOrder(order float64) *LineChart {
   414  	l.JsContent.Data.DataSets[l.dataSetIndex].SetOrder(order)
   415  	return l
   416  }
   417  
   418  func (l *LineChart) DSPointBackgroundColor(pointBackgroundColor Color) *LineChart {
   419  	l.JsContent.Data.DataSets[l.dataSetIndex].SetPointBackgroundColor(pointBackgroundColor)
   420  	return l
   421  }
   422  
   423  func (l *LineChart) DSPointBorderColor(pointBorderColor Color) *LineChart {
   424  	l.JsContent.Data.DataSets[l.dataSetIndex].SetPointBorderColor(pointBorderColor)
   425  	return l
   426  }
   427  
   428  func (l *LineChart) DSPointBorderWidth(pointBorderWidth float64) *LineChart {
   429  	l.JsContent.Data.DataSets[l.dataSetIndex].SetPointBorderWidth(pointBorderWidth)
   430  	return l
   431  }
   432  
   433  func (l *LineChart) DSPointHitRadius(pointHitRadius float64) *LineChart {
   434  	l.JsContent.Data.DataSets[l.dataSetIndex].SetPointHitRadius(pointHitRadius)
   435  	return l
   436  }
   437  
   438  func (l *LineChart) DSPointHoverBackgroundColor(pointHoverBackgroundColor Color) *LineChart {
   439  	l.JsContent.Data.DataSets[l.dataSetIndex].SetPointHoverBackgroundColor(pointHoverBackgroundColor)
   440  	return l
   441  }
   442  
   443  func (l *LineChart) DSPointHoverBorderColor(pointHoverBorderColor Color) *LineChart {
   444  	l.JsContent.Data.DataSets[l.dataSetIndex].SetPointHoverBorderColor(pointHoverBorderColor)
   445  	return l
   446  }
   447  
   448  func (l *LineChart) DSPointHoverBorderWidth(pointHoverBorderWidth float64) *LineChart {
   449  	l.JsContent.Data.DataSets[l.dataSetIndex].SetPointHoverBorderWidth(pointHoverBorderWidth)
   450  	return l
   451  }
   452  
   453  func (l *LineChart) DSPointHoverRadius(pointHoverRadius float64) *LineChart {
   454  	l.JsContent.Data.DataSets[l.dataSetIndex].SetPointHoverRadius(pointHoverRadius)
   455  	return l
   456  }
   457  
   458  func (l *LineChart) DSPointRadius(pointRadius float64) *LineChart {
   459  	l.JsContent.Data.DataSets[l.dataSetIndex].SetPointRadius(pointRadius)
   460  	return l
   461  }
   462  
   463  func (l *LineChart) DSPointRotation(pointRotation float64) *LineChart {
   464  	l.JsContent.Data.DataSets[l.dataSetIndex].SetPointRotation(pointRotation)
   465  	return l
   466  }
   467  
   468  func (l *LineChart) DSPointStyle(pointStyle string) *LineChart {
   469  	l.JsContent.Data.DataSets[l.dataSetIndex].SetPointStyle(pointStyle)
   470  	return l
   471  }
   472  
   473  func (l *LineChart) DSShowLine(showLine bool) *LineChart {
   474  	l.JsContent.Data.DataSets[l.dataSetIndex].SetShowLine(showLine)
   475  	return l
   476  }
   477  
   478  func (l *LineChart) DSSpanGaps(spanGaps bool) *LineChart {
   479  	l.JsContent.Data.DataSets[l.dataSetIndex].SetSpanGaps(spanGaps)
   480  	return l
   481  }
   482  
   483  func (l *LineChart) DSSteppedLine(steppedLine bool) *LineChart {
   484  	l.JsContent.Data.DataSets[l.dataSetIndex].SetSteppedLine(steppedLine)
   485  	return l
   486  }
   487  
   488  func (l *LineChart) DSXAxisID(xAxisID string) *LineChart {
   489  	l.JsContent.Data.DataSets[l.dataSetIndex].SetXAxisID(xAxisID)
   490  	return l
   491  }
   492  
   493  func (l *LineChart) DSYAxisID(yAxisID string) *LineChart {
   494  	l.JsContent.Data.DataSets[l.dataSetIndex].SetYAxisID(yAxisID)
   495  	return l
   496  }
   497  
   498  func (l *LineChart) GetContent() template.HTML {
   499  	buffer := new(bytes.Buffer)
   500  	tmpl, defineName := l.GetTemplate()
   501  
   502  	if l.JsContentOptions != nil {
   503  		l.JsContent.Options = l.JsContentOptions
   504  	}
   505  
   506  	jsonByte, _ := json.Marshal(l.JsContent)
   507  	l.Js = template.JS(string(jsonByte))
   508  
   509  	err := tmpl.ExecuteTemplate(buffer, defineName, l)
   510  	if err != nil {
   511  		fmt.Println("ComposeHtml Error:", err)
   512  	}
   513  	return template.HTML(buffer.String())
   514  }