-
-
Save horlabyc/dbfb16366a7056b89db4047ab95f6c98 to your computer and use it in GitHub Desktop.
Create bar chart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"encoding/csv" | |
"log" | |
"os" | |
"sort" | |
"strconv" | |
"github.com/go-echarts/go-echarts/v2/charts" | |
"github.com/go-echarts/go-echarts/v2/opts" | |
) | |
func createChart(sortedData DataList) { | |
bar := charts.NewBar() | |
bar.SetGlobalOptions(charts.WithTitleOpts(opts.Title{ | |
Title: "PC Games Sales", | |
Subtitle: "Best selling PC games", | |
})) | |
bar.SetXAxis([]string{ | |
sortedData[0].Key[:4], sortedData[1].Key[:4], sortedData[2].Key[:4], sortedData[3].Key[:4], | |
sortedData[4].Key[:4], sortedData[5].Key[:4], sortedData[6].Key[:4], | |
sortedData[7].Key[:4], sortedData[8].Key[:4], sortedData[9].Key[:4], | |
}).AddSeries("Values", generateBarItems(sortedData)) | |
f, _ := os.Create("games.html") | |
bar.Render(f) | |
} | |
func generateBarItems(data DataList) []opts.BarData { | |
barData := []float64{} | |
items := make([]opts.BarData, 0) | |
for i := 0; i < 10; i++ { | |
barData = append(barData, data[i].Value) | |
} | |
for _, v := range barData { | |
items = append(items, opts.BarData{Value: v}) | |
} | |
return items | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment