Debian Codegraph @

examples/stacked_bar_labels/main.go

raw symbols SCIP
1 package main
2
3 import (
4 "os"
5
6 "github.com/wcharczuk/go-chart/v2"
7 "github.com/wcharczuk/go-chart/v2/drawing"
8 )
9
10 func main() {
11 chart.DefaultBackgroundColor = chart.ColorTransparent
12 chart.DefaultCanvasColor = chart.ColorTransparent
13
14 barWidth := 120
15
16 var (
17 colorWhite = drawing.Color{R: 241, G: 241, B: 241, A: 255}
18 colorMariner = drawing.Color{R: 60, G: 100, B: 148, A: 255}
19 colorLightSteelBlue = drawing.Color{R: 182, G: 195, B: 220, A: 255}
20 colorPoloBlue = drawing.Color{R: 126, G: 155, B: 200, A: 255}
21 colorSteelBlue = drawing.Color{R: 73, G: 120, B: 177, A: 255}
22 )
23
24 stackedBarChart := chart.StackedBarChart{
25 Title: "Quarterly Sales",
26 TitleStyle: chart.Shown(),
27 Background: chart.Style{
28 Padding: chart.Box{
29 Top: 100,
30 },
31 },
32 Width: 810,
33 Height: 500,
34 XAxis: chart.Shown(),
35 YAxis: chart.Shown(),
36 BarSpacing: 50,
37 Bars: []chart.StackedBar{
38 {
39 Name: "Q1",
40 Width: barWidth,
41 Values: []chart.Value{
42 {
43 Label: "32K",
44 Value: 32,
45 Style: chart.Style{
46 StrokeWidth: .01,
47 FillColor: colorMariner,
48 FontColor: colorWhite,
49 },
50 },
51 {
52 Label: "46K",
53 Value: 46,
54 Style: chart.Style{
55 StrokeWidth: .01,
56 FillColor: colorLightSteelBlue,
57 FontColor: colorWhite,
58 },
59 },
60 {
61 Label: "48K",
62 Value: 48,
63 Style: chart.Style{
64 StrokeWidth: .01,
65 FillColor: colorPoloBlue,
66 FontColor: colorWhite,
67 },
68 },
69 {
70 Label: "42K",
71 Value: 42,
72 Style: chart.Style{
73 StrokeWidth: .01,
74 FillColor: colorSteelBlue,
75 FontColor: colorWhite,
76 },
77 },
78 },
79 },
80 {
81 Name: "Q2",
82 Width: barWidth,
83 Values: []chart.Value{
84 {
85 Label: "45K",
86 Value: 45,
87 Style: chart.Style{
88 StrokeWidth: .01,
89 FillColor: colorMariner,
90 FontColor: colorWhite,
91 },
92 },
93 {
94 Label: "60K",
95 Value: 60,
96 Style: chart.Style{
97 StrokeWidth: .01,
98 FillColor: colorLightSteelBlue,
99 FontColor: colorWhite,
100 },
101 },
102 {
103 Label: "62K",
104 Value: 62,
105 Style: chart.Style{
106 StrokeWidth: .01,
107 FillColor: colorPoloBlue,
108 FontColor: colorWhite,
109 },
110 },
111 {
112 Label: "53K",
113 Value: 53,
114 Style: chart.Style{
115 StrokeWidth: .01,
116 FillColor: colorSteelBlue,
117 FontColor: colorWhite,
118 },
119 },
120 },
121 },
122 {
123 Name: "Q3",
124 Width: barWidth,
125 Values: []chart.Value{
126 {
127 Label: "54K",
128 Value: 54,
129 Style: chart.Style{
130 StrokeWidth: .01,
131 FillColor: colorMariner,
132 FontColor: colorWhite,
133 },
134 },
135 {
136 Label: "58K",
137 Value: 58,
138 Style: chart.Style{
139 StrokeWidth: .01,
140 FillColor: colorLightSteelBlue,
141 FontColor: colorWhite,
142 },
143 },
144 {
145 Label: "55K",
146 Value: 55,
147 Style: chart.Style{
148 StrokeWidth: .01,
149 FillColor: colorPoloBlue,
150 FontColor: colorWhite,
151 },
152 },
153 {
154 Label: "47K",
155 Value: 47,
156 Style: chart.Style{
157 StrokeWidth: .01,
158 FillColor: colorSteelBlue,
159 FontColor: colorWhite,
160 },
161 },
162 },
163 },
164 {
165 Name: "Q4",
166 Width: barWidth,
167 Values: []chart.Value{
168 {
169 Label: "46K",
170 Value: 46,
171 Style: chart.Style{
172 StrokeWidth: .01,
173 FillColor: colorMariner,
174 FontColor: colorWhite,
175 },
176 },
177 {
178 Label: "70K",
179 Value: 70,
180 Style: chart.Style{
181 StrokeWidth: .01,
182 FillColor: colorLightSteelBlue,
183 FontColor: colorWhite,
184 },
185 },
186 {
187 Label: "74K",
188 Value: 74,
189 Style: chart.Style{
190 StrokeWidth: .01,
191 FillColor: colorPoloBlue,
192 FontColor: colorWhite,
193 },
194 },
195 {
196 Label: "60K",
197 Value: 60,
198 Style: chart.Style{
199 StrokeWidth: .01,
200 FillColor: colorSteelBlue,
201 FontColor: colorWhite,
202 },
203 },
204 },
205 },
206 },
207 }
208
209 pngFile, err := os.Create("output.png")
210 if err != nil {
211 panic(err)
212 }
213
214 if err := stackedBarChart.Render(chart.PNG, pngFile); err != nil {
215 panic(err)
216 }
217
218 if err := pngFile.Close(); err != nil {
219 panic(err)
220 }
221 }