Using this library "https://github.com/wcharczuk/go-chart" I want to generate pie chart using json data.
Code
pie := chart.PieChart{
Width: 512,
Height: 512,
Values: []chart.Value{
{Value: 5, Label: "Blue"},
{Value: 5, Label: "Green"},
{Value: 4, Label: "Gray"},
},
}
My Json
"reaction_summary": {
"ANGRY": 7,
"HAHA": 40,
"LIKE": 161,
"LOVE": 56,
"SAD": 26,
"SHOCK": 6
}
What I really want to achieve is this
data = Parse/map json as the content of chart.Value
pie := chart.PieChart{
Width: 512,
Height: 512,
Values: []chart.Value{
data
},
}
Something along the lines of
raw := `{"reaction_summary": {"ANGRY": 7,"HAHA": 40,"LIKE": 161,"LOVE": 56,"SAD": 26,"SHOCK": 6}}`
// Parse JSON
data := struct {
ReactionSummary map[string]int `json:"reaction_summary"`
}{}
if err := json.Unmarshal([]byte(raw), &data); err != nil {
log.Fatal(err)
}
// Populate a slice of chart values
var values []chart.Value
for l, v := range data.ReactionSummary {
values = append(values, chart.Value{Label: l, Value: float64(v)})
}
// Initialize the chart
pie := chart.PieChart{
Width: 512,
Height: 512,
Values: values,
}
Result: