| 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 := 80 |
| 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: 75, |
| 30 | }, |
| 31 | }, |
| 32 | Width: 800, |
| 33 | Height: 600, |
| 34 | XAxis: chart.Shown(), |
| 35 | YAxis: chart.Shown(), |
| 36 | BarSpacing: 40, |
| 37 | IsHorizontal: true, |
| 38 | Bars: []chart.StackedBar{ |
| 39 | { |
| 40 | Name: "Q1", |
| 41 | Width: barWidth, |
| 42 | Values: []chart.Value{ |
| 43 | { |
| 44 | Label: "32K", |
| 45 | Value: 32, |
| 46 | Style: chart.Style{ |
| 47 | StrokeWidth: .01, |
| 48 | FillColor: colorMariner, |
| 49 | FontColor: colorWhite, |
| 50 | }, |
| 51 | }, |
| 52 | { |
| 53 | Label: "46K", |
| 54 | Value: 46, |
| 55 | Style: chart.Style{ |
| 56 | StrokeWidth: .01, |
| 57 | FillColor: colorLightSteelBlue, |
| 58 | FontColor: colorWhite, |
| 59 | }, |
| 60 | }, |
| 61 | { |
| 62 | Label: "48K", |
| 63 | Value: 48, |
| 64 | Style: chart.Style{ |
| 65 | StrokeWidth: .01, |
| 66 | FillColor: colorPoloBlue, |
| 67 | FontColor: colorWhite, |
| 68 | }, |
| 69 | }, |
| 70 | { |
| 71 | Label: "42K", |
| 72 | Value: 42, |
| 73 | Style: chart.Style{ |
| 74 | StrokeWidth: .01, |
| 75 | FillColor: colorSteelBlue, |
| 76 | FontColor: colorWhite, |
| 77 | }, |
| 78 | }, |
| 79 | }, |
| 80 | }, |
| 81 | { |
| 82 | Name: "Q2", |
| 83 | Width: barWidth, |
| 84 | Values: []chart.Value{ |
| 85 | { |
| 86 | Label: "45K", |
| 87 | Value: 45, |
| 88 | Style: chart.Style{ |
| 89 | StrokeWidth: .01, |
| 90 | FillColor: colorMariner, |
| 91 | FontColor: colorWhite, |
| 92 | }, |
| 93 | }, |
| 94 | { |
| 95 | Label: "60K", |
| 96 | Value: 60, |
| 97 | Style: chart.Style{ |
| 98 | StrokeWidth: .01, |
| 99 | FillColor: colorLightSteelBlue, |
| 100 | FontColor: colorWhite, |
| 101 | }, |
| 102 | }, |
| 103 | { |
| 104 | Label: "62K", |
| 105 | Value: 62, |
| 106 | Style: chart.Style{ |
| 107 | StrokeWidth: .01, |
| 108 | FillColor: colorPoloBlue, |
| 109 | FontColor: colorWhite, |
| 110 | }, |
| 111 | }, |
| 112 | { |
| 113 | Label: "53K", |
| 114 | Value: 53, |
| 115 | Style: chart.Style{ |
| 116 | StrokeWidth: .01, |
| 117 | FillColor: colorSteelBlue, |
| 118 | FontColor: colorWhite, |
| 119 | }, |
| 120 | }, |
| 121 | }, |
| 122 | }, |
| 123 | { |
| 124 | Name: "Q3", |
| 125 | Width: barWidth, |
| 126 | Values: []chart.Value{ |
| 127 | { |
| 128 | Label: "54K", |
| 129 | Value: 54, |
| 130 | Style: chart.Style{ |
| 131 | StrokeWidth: .01, |
| 132 | FillColor: colorMariner, |
| 133 | FontColor: colorWhite, |
| 134 | }, |
| 135 | }, |
| 136 | { |
| 137 | Label: "58K", |
| 138 | Value: 58, |
| 139 | Style: chart.Style{ |
| 140 | StrokeWidth: .01, |
| 141 | FillColor: colorLightSteelBlue, |
| 142 | FontColor: colorWhite, |
| 143 | }, |
| 144 | }, |
| 145 | { |
| 146 | Label: "55K", |
| 147 | Value: 55, |
| 148 | Style: chart.Style{ |
| 149 | StrokeWidth: .01, |
| 150 | FillColor: colorPoloBlue, |
| 151 | FontColor: colorWhite, |
| 152 | }, |
| 153 | }, |
| 154 | { |
| 155 | Label: "47K", |
| 156 | Value: 47, |
| 157 | Style: chart.Style{ |
| 158 | StrokeWidth: .01, |
| 159 | FillColor: colorSteelBlue, |
| 160 | FontColor: colorWhite, |
| 161 | }, |
| 162 | }, |
| 163 | }, |
| 164 | }, |
| 165 | { |
| 166 | Name: "Q4", |
| 167 | Width: barWidth, |
| 168 | Values: []chart.Value{ |
| 169 | { |
| 170 | Label: "46K", |
| 171 | Value: 46, |
| 172 | Style: chart.Style{ |
| 173 | StrokeWidth: .01, |
| 174 | FillColor: colorMariner, |
| 175 | FontColor: colorWhite, |
| 176 | }, |
| 177 | }, |
| 178 | { |
| 179 | Label: "70K", |
| 180 | Value: 70, |
| 181 | Style: chart.Style{ |
| 182 | StrokeWidth: .01, |
| 183 | FillColor: colorLightSteelBlue, |
| 184 | FontColor: colorWhite, |
| 185 | }, |
| 186 | }, |
| 187 | { |
| 188 | Label: "74K", |
| 189 | Value: 74, |
| 190 | Style: chart.Style{ |
| 191 | StrokeWidth: .01, |
| 192 | FillColor: colorPoloBlue, |
| 193 | FontColor: colorWhite, |
| 194 | }, |
| 195 | }, |
| 196 | { |
| 197 | Label: "60K", |
| 198 | Value: 60, |
| 199 | Style: chart.Style{ |
| 200 | StrokeWidth: .01, |
| 201 | FillColor: colorSteelBlue, |
| 202 | FontColor: colorWhite, |
| 203 | }, |
| 204 | }, |
| 205 | }, |
| 206 | }, |
| 207 | }, |
| 208 | } |
| 209 | |
| 210 | pngFile, err := os.Create("output.png") |
| 211 | if err != nil { |
| 212 | panic(err) |
| 213 | } |
| 214 | |
| 215 | if err := stackedBarChart.Render(chart.PNG, pngFile); err != nil { |
| 216 | panic(err) |
| 217 | } |
| 218 | |
| 219 | if err := pngFile.Close(); err != nil { |
| 220 | panic(err) |
| 221 | } |
| 222 | } |