github.com/kotovmak/go-admin@v1.1.1/template/chartjs/bar.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 BarChart struct { 13 *Chart 14 15 JsContent BarJsContent 16 } 17 18 type BarJsContent struct { 19 JsContent 20 21 Data BarAttributes `json:"data"` 22 } 23 24 type BarAttributes struct { 25 Attributes 26 27 DataSets BarDataSets `json:"datasets"` 28 } 29 30 type BarDataSets []*BarDataSet 31 32 func (l BarDataSets) Add(ds *BarDataSet) BarDataSets { 33 return append(l, ds) 34 } 35 36 type BarDataSet 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 44 BorderSkipped string `json:"borderSkipped,omitempty"` 45 BorderWidth float64 `json:"borderWidth,omitempty"` 46 47 HoverBackgroundColor Color `json:"hoverBackgroundColor,omitempty"` 48 HoverBorderColor Color `json:"hoverBorderColor,omitempty"` 49 HoverBorderWidth float64 `json:"hoverBorderWidth,omitempty"` 50 51 Order float64 `json:"order,omitempty"` 52 XAxisID string `json:"xAxisID,omitempty"` 53 YAxisID string `json:"yAxisID,omitempty"` 54 } 55 56 func (l *BarDataSet) SetLabel(label string) *BarDataSet { 57 l.Label = label 58 return l 59 } 60 61 func (l *BarDataSet) SetData(data []float64) *BarDataSet { 62 l.Data = data 63 return l 64 } 65 66 func (l *BarDataSet) SetType(t string) *BarDataSet { 67 l.Type = t 68 return l 69 } 70 71 func (l *BarDataSet) SetBackgroundColor(backgroundColor Color) *BarDataSet { 72 l.BackgroundColor = backgroundColor 73 return l 74 } 75 76 func (l *BarDataSet) SetBorderCapStyle(borderCapStyle string) *BarDataSet { 77 l.BorderCapStyle = borderCapStyle 78 return l 79 } 80 81 func (l *BarDataSet) SetBorderColor(borderColor Color) *BarDataSet { 82 l.BorderColor = borderColor 83 return l 84 } 85 86 func (l *BarDataSet) SetBorderWidth(borderWidth float64) *BarDataSet { 87 l.BorderWidth = borderWidth 88 return l 89 } 90 91 func (l *BarDataSet) SetBorderSkipped(skip string) *BarDataSet { 92 l.BorderSkipped = skip 93 return l 94 } 95 96 func (l *BarDataSet) SetHoverBackgroundColor(hoverBackgroundColor Color) *BarDataSet { 97 l.HoverBackgroundColor = hoverBackgroundColor 98 return l 99 } 100 101 func (l *BarDataSet) SetHoverBorderColor(hoverBorderColor Color) *BarDataSet { 102 l.HoverBorderColor = hoverBorderColor 103 return l 104 } 105 106 func (l *BarDataSet) SetHoverBorderWidth(hoverBorderWidth float64) *BarDataSet { 107 l.HoverBorderWidth = hoverBorderWidth 108 return l 109 } 110 111 func (l *BarDataSet) SetOrder(order float64) *BarDataSet { 112 l.Order = order 113 return l 114 } 115 116 func (l *BarDataSet) SetXAxisID(xAxisID string) *BarDataSet { 117 l.XAxisID = xAxisID 118 return l 119 } 120 121 func (l *BarDataSet) SetYAxisID(yAxisID string) *BarDataSet { 122 l.YAxisID = yAxisID 123 return l 124 } 125 126 func Bar() *BarChart { 127 return &BarChart{ 128 Chart: &Chart{ 129 BaseComponent: &template2.BaseComponent{ 130 Name: "chartjs", 131 HTMLData: List["chartjs"], 132 }, 133 dataSetIndex: -1, 134 }, 135 JsContent: BarJsContent{ 136 JsContent: JsContent{ 137 Type: "bar", 138 }, 139 Data: BarAttributes{ 140 Attributes: Attributes{ 141 Labels: make([]string, 0), 142 }, 143 DataSets: make(BarDataSets, 0), 144 }, 145 }, 146 } 147 } 148 149 func (l *BarChart) SetID(s string) *BarChart { 150 l.ID = s 151 return l 152 } 153 154 func (l *BarChart) SetTitle(s template.HTML) *BarChart { 155 l.Title = s 156 return l 157 } 158 159 func (l *BarChart) SetHeight(s int) *BarChart { 160 l.Height = s 161 return l 162 } 163 164 func (l *BarChart) SetLabels(s []string) *BarChart { 165 l.JsContent.Data.Labels = s 166 return l 167 } 168 169 func (l *BarChart) AddDataSet(s string) *BarChart { 170 l.dataSetIndex++ 171 l.JsContent.Data.DataSets = l.JsContent.Data.DataSets.Add(&BarDataSet{ 172 Type: "bar", 173 Label: s, 174 }) 175 return l 176 } 177 178 func (l *BarChart) DSLabel(s string) *BarChart { 179 l.JsContent.Data.DataSets[l.dataSetIndex].SetLabel(s) 180 return l 181 } 182 183 func (l *BarChart) DSData(data []float64) *BarChart { 184 l.JsContent.Data.DataSets[l.dataSetIndex].SetData(data) 185 return l 186 } 187 188 func (l *BarChart) DSType(t string) *BarChart { 189 l.JsContent.Data.DataSets[l.dataSetIndex].SetType(t) 190 return l 191 } 192 193 func (l *BarChart) DSBackgroundColor(backgroundColor Color) *BarChart { 194 l.JsContent.Data.DataSets[l.dataSetIndex].SetBackgroundColor(backgroundColor) 195 return l 196 } 197 198 func (l *BarChart) DSBorderCapStyle(borderCapStyle string) *BarChart { 199 l.JsContent.Data.DataSets[l.dataSetIndex].SetBorderCapStyle(borderCapStyle) 200 return l 201 } 202 203 func (l *BarChart) DSBorderSkipped(skip string) *BarChart { 204 l.JsContent.Data.DataSets[l.dataSetIndex].SetBorderSkipped(skip) 205 return l 206 } 207 208 func (l *BarChart) DSBorderColor(borderColor Color) *BarChart { 209 l.JsContent.Data.DataSets[l.dataSetIndex].SetBorderColor(borderColor) 210 return l 211 } 212 213 func (l *BarChart) DSBorderWidth(borderWidth float64) *BarChart { 214 l.JsContent.Data.DataSets[l.dataSetIndex].SetBorderWidth(borderWidth) 215 return l 216 } 217 218 func (l *BarChart) DSHoverBackgroundColor(hoverBackgroundColor Color) *BarChart { 219 l.JsContent.Data.DataSets[l.dataSetIndex].SetHoverBackgroundColor(hoverBackgroundColor) 220 return l 221 } 222 223 func (l *BarChart) DSHoverBorderColor(hoverBorderColor Color) *BarChart { 224 l.JsContent.Data.DataSets[l.dataSetIndex].SetHoverBorderColor(hoverBorderColor) 225 return l 226 } 227 228 func (l *BarChart) DSHoverBorderWidth(hoverBorderWidth float64) *BarChart { 229 l.JsContent.Data.DataSets[l.dataSetIndex].SetHoverBorderWidth(hoverBorderWidth) 230 return l 231 } 232 233 func (l *BarChart) DSOrder(order float64) *BarChart { 234 l.JsContent.Data.DataSets[l.dataSetIndex].SetOrder(order) 235 return l 236 } 237 238 func (l *BarChart) DSXAxisID(xAxisID string) *BarChart { 239 l.JsContent.Data.DataSets[l.dataSetIndex].SetXAxisID(xAxisID) 240 return l 241 } 242 243 func (l *BarChart) DSYAxisID(yAxisID string) *BarChart { 244 l.JsContent.Data.DataSets[l.dataSetIndex].SetYAxisID(yAxisID) 245 return l 246 } 247 248 func (l *BarChart) GetContent() template.HTML { 249 buffer := new(bytes.Buffer) 250 tmpl, defineName := l.GetTemplate() 251 252 if l.JsContentOptions != nil { 253 l.JsContent.Options = l.JsContentOptions 254 } 255 256 jsonByte, _ := json.Marshal(l.JsContent) 257 l.Js = template.JS(string(jsonByte)) 258 259 err := tmpl.ExecuteTemplate(buffer, defineName, l) 260 if err != nil { 261 fmt.Println("ComposeHtml Error:", err) 262 } 263 return template.HTML(buffer.String()) 264 }