使用这个库"https://github.com/wcharczuk/go-chart“,我希望使用json数据生成饼图。
码
pie := chart.PieChart{
Width: 512,
Height: 512,
Values: []chart.Value{
{Value: 5, Label: "Blue"},
{Value: 5, Label: "Green"},
{Value: 4, Label: "Gray"},
},
}我的Json
"reaction_summary": {
"ANGRY": 7,
"HAHA": 40,
"LIKE": 161,
"LOVE": 56,
"SAD": 26,
"SHOCK": 6
}我真正想要实现的是
data = Parse/map json作为chart.Value的内容
pie := chart.PieChart{
Width: 512,
Height: 512,
Values: []chart.Value{
data
},
}发布于 2018-10-26 20:23:46
与…有关的东西
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,
}结果:

https://stackoverflow.com/questions/53008816
复制相似问题