github.com/kotovmak/go-admin@v1.1.1/template/chartjs/pie.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 PieChart struct { 13 *Chart 14 15 JsContent PieJsContent 16 } 17 18 type PieJsContent struct { 19 JsContent 20 21 Data PieAttributes `json:"data"` 22 } 23 24 type PieAttributes struct { 25 Attributes 26 27 DataSets PieDataSets `json:"datasets"` 28 } 29 30 type PieDataSets []*PieDataSet 31 32 func (l PieDataSets) Add(ds *PieDataSet) PieDataSets { 33 return append(l, ds) 34 } 35 36 type PieDataSet struct { 37 Label string `json:"label"` 38 Data []float64 `json:"data"` 39 Type string `json:"type,omitempty"` 40 BackgroundColor []Color `json:"backgroundColor,omitempty"` 41 BorderColor Color `json:"borderColor,omitempty"` 42 43 BorderWidth float64 `json:"borderWidth,omitempty"` 44 BorderAlign string `json:"borderAlign,omitempty"` 45 46 HoverBackgroundColor Color `json:"hoverBackgroundColor,omitempty"` 47 HoverBorderColor Color `json:"hoverBorderColor,omitempty"` 48 HoverBorderWidth float64 `json:"hoverBorderWidth,omitempty"` 49 50 Weight int `json:"weight,omitempty"` 51 } 52 53 func (l *PieDataSet) SetLabel(label string) *PieDataSet { 54 l.Label = label 55 return l 56 } 57 58 func (l *PieDataSet) SetData(data []float64) *PieDataSet { 59 l.Data = data 60 return l 61 } 62 63 func (l *PieDataSet) SetType(t string) *PieDataSet { 64 l.Type = t 65 return l 66 } 67 68 func (l *PieDataSet) SetBackgroundColor(backgroundColor []Color) *PieDataSet { 69 l.BackgroundColor = backgroundColor 70 return l 71 } 72 73 func (l *PieDataSet) SetBorderAlign(align string) *PieDataSet { 74 l.BorderAlign = align 75 return l 76 } 77 78 func (l *PieDataSet) SetBorderColor(borderColor Color) *PieDataSet { 79 l.BorderColor = borderColor 80 return l 81 } 82 83 func (l *PieDataSet) SetBorderWidth(borderWidth float64) *PieDataSet { 84 l.BorderWidth = borderWidth 85 return l 86 } 87 88 func (l *PieDataSet) SetWeight(weight int) *PieDataSet { 89 l.Weight = weight 90 return l 91 } 92 93 func (l *PieDataSet) SetHoverBackgroundColor(hoverBackgroundColor Color) *PieDataSet { 94 l.HoverBackgroundColor = hoverBackgroundColor 95 return l 96 } 97 98 func (l *PieDataSet) SetHoverBorderColor(hoverBorderColor Color) *PieDataSet { 99 l.HoverBorderColor = hoverBorderColor 100 return l 101 } 102 103 func (l *PieDataSet) SetHoverBorderWidth(hoverBorderWidth float64) *PieDataSet { 104 l.HoverBorderWidth = hoverBorderWidth 105 return l 106 } 107 108 func Pie() *PieChart { 109 return &PieChart{ 110 Chart: &Chart{ 111 BaseComponent: &template2.BaseComponent{ 112 Name: "chartjs", 113 HTMLData: List["chartjs"], 114 }, 115 dataSetIndex: -1, 116 }, 117 JsContent: PieJsContent{ 118 JsContent: JsContent{ 119 Type: "pie", 120 }, 121 Data: PieAttributes{ 122 Attributes: Attributes{ 123 Labels: make([]string, 0), 124 }, 125 DataSets: make(PieDataSets, 0), 126 }, 127 }, 128 } 129 } 130 131 func (l *PieChart) SetID(s string) *PieChart { 132 l.ID = s 133 return l 134 } 135 136 func (l *PieChart) SetTitle(s template.HTML) *PieChart { 137 l.Title = s 138 return l 139 } 140 141 func (l *PieChart) SetHeight(s int) *PieChart { 142 l.Height = s 143 return l 144 } 145 146 func (l *PieChart) SetLabels(s []string) *PieChart { 147 l.JsContent.Data.Labels = s 148 return l 149 } 150 151 func (l *PieChart) AddDataSet(s string) *PieChart { 152 l.dataSetIndex++ 153 l.JsContent.Data.DataSets = l.JsContent.Data.DataSets.Add(&PieDataSet{ 154 Type: "pie", 155 Label: s, 156 }) 157 return l 158 } 159 160 func (l *PieChart) DSLabel(s string) *PieChart { 161 l.JsContent.Data.DataSets[l.dataSetIndex].SetLabel(s) 162 return l 163 } 164 165 func (l *PieChart) DSData(data []float64) *PieChart { 166 l.JsContent.Data.DataSets[l.dataSetIndex].SetData(data) 167 return l 168 } 169 170 func (l *PieChart) DSType(t string) *PieChart { 171 l.JsContent.Data.DataSets[l.dataSetIndex].SetType(t) 172 return l 173 } 174 175 func (l *PieChart) DSBackgroundColor(backgroundColor []Color) *PieChart { 176 l.JsContent.Data.DataSets[l.dataSetIndex].SetBackgroundColor(backgroundColor) 177 return l 178 } 179 180 func (l *PieChart) DSBorderColor(borderColor Color) *PieChart { 181 l.JsContent.Data.DataSets[l.dataSetIndex].SetBorderColor(borderColor) 182 return l 183 } 184 185 func (l *PieChart) DSBorderWidth(borderWidth float64) *PieChart { 186 l.JsContent.Data.DataSets[l.dataSetIndex].SetBorderWidth(borderWidth) 187 return l 188 } 189 190 func (l *PieChart) DSWeight(weight int) *PieChart { 191 l.JsContent.Data.DataSets[l.dataSetIndex].SetWeight(weight) 192 return l 193 } 194 195 func (l *PieChart) DSHoverBackgroundColor(hoverBackgroundColor Color) *PieChart { 196 l.JsContent.Data.DataSets[l.dataSetIndex].SetHoverBackgroundColor(hoverBackgroundColor) 197 return l 198 } 199 200 func (l *PieChart) DSHoverBorderColor(hoverBorderColor Color) *PieChart { 201 l.JsContent.Data.DataSets[l.dataSetIndex].SetHoverBorderColor(hoverBorderColor) 202 return l 203 } 204 205 func (l *PieChart) DSHoverBorderWidth(hoverBorderWidth float64) *PieChart { 206 l.JsContent.Data.DataSets[l.dataSetIndex].SetHoverBorderWidth(hoverBorderWidth) 207 return l 208 } 209 210 func (l *PieChart) GetContent() template.HTML { 211 buffer := new(bytes.Buffer) 212 tmpl, defineName := l.GetTemplate() 213 214 if l.JsContentOptions != nil { 215 l.JsContent.Options = l.JsContentOptions 216 } 217 218 jsonByte, _ := json.Marshal(l.JsContent) 219 l.Js = template.JS(string(jsonByte)) 220 221 err := tmpl.ExecuteTemplate(buffer, defineName, l) 222 if err != nil { 223 fmt.Println("ComposeHtml Error:", err) 224 } 225 return template.HTML(buffer.String()) 226 }