gpt4 book ai didi

go - 添加自定义颜色去图表 slice

转载 作者:数据小太阳 更新时间:2023-10-29 03:21:29 27 4
gpt4 key购买 nike

我无法在饼图 slice 中添加自定义颜色。我正在使用 go 图表库,但不确定如何在我的图表中动态添加自定义颜色。

这里我正在使用这段代码绘制图表,我知道我们有一个图表样式选项可以使用 drawing.Color{R,G,B,A} 分配颜色,但这只会具体说明所有 slice 的一种颜色。我想为每个饼图 slice 动态指定我自己的自定义颜色

   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,
}

Pie chart here

最佳答案

请看这个例子:https://github.com/wcharczuk/go-chart/blob/master/_examples/custom_styles/main.go

对于每个 chart.Value{Label: l, Value: float64(v)},您可以提供一个 Style 属性:https://godoc.org/github.com/wcharczuk/go-chart#Value

style 属性可以带颜色、笔触和其他东西:

Style: chart.Style{
Show: true, //note; if we set ANY other properties, we must set this to true.
StrokeColor: drawing.ColorRed, // will supercede defaults
FillColor: drawing.ColorRed.WithAlpha(64), // will supercede defaults
},

此处提供更多选项:https://godoc.org/github.com/wcharczuk/go-chart#Style

请确保链接到您将来使用的库。

如果您想要 JSON 中的自定义颜色,请使用 json 字符串中的 RGB 值并将它们解析为结构,或者在代码中定义结构中的颜色并使用传递的值。

关于go - 添加自定义颜色去图表 slice ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53051930/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com