简体   繁体   English

为图表生成 Colors 的梯度

[英]Generate Gradient of Colors for chart

I'm making a small pie chart, and I'd like to have each entry in the chart get lighter and lighter the smaller the pie slices get.我正在制作一个小饼图,我希望图表中的每个条目越小,饼片越小。 I have to generate an array of UIColor to color the chart.我必须生成一个UIColor数组来为图表着色。

Here's what I have right now:这是我现在拥有的:

for i in 0..<entries.count {
    let percent = 1.0 - (CGFloat(i - 1)/CGFloat(entries.count))
    dataSet.colors.append(UIColor.systemPurple.withAlphaComponent(percent))
}

And it generates this:它会生成这个: 饼图的坏例子

I'd like the colors in the chart to be more like this: (sorry I just quick made it in google sheets as an example)我希望图表中的 colors 更像这样:(对不起,我只是在谷歌表格中快速制作了它作为示例) 好的饼图

Any ideas?有任何想法吗?

Try with this solution.试试这个解决方案。

var initialAlpha = 1.0

for i in 0..<entries.count { 
    dataSet.colors.append(UIColor.systemPurple.withAlphaComponent(initialAlpha))
    initialAlpha -= 0.1
}

You can also change the difference value of 0.1 to any fraction value let's say 0.15 or 0.2 etc. according to your requirement.您还可以根据您的要求将 0.1 的差值更改为任何分数值,例如 0.15 或 0.2 等。

You might want to use a different color model like HSB, and vary one or more of those settings between colors.您可能想要使用不同的颜色 model(如 HSB),并在 colors 之间更改其中的一个或多个设置。 In your case it looks like you're using a bright purple and want to vary the saturation from 100% to 0%.在您的情况下,您似乎正在使用亮紫色并希望将饱和度从 100% 更改为 0%。

You could then create your colors using the UIColor initializer init(hue:saturation:brightness:alpha:)然后,您可以使用 UIColor 初始化程序init(hue:saturation:brightness:alpha:)创建 colors

In tinkering with it a little bit, colors with a hue of 300, 100% brightness, and saturation values of 100%, 80%, 60%, 40%, 20% and 0% looked decent.稍微修改一下,colors 的色调为 300、100% 亮度,饱和度值为 100%、80%、60%、40%、20% 和 0%,看起来不错。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM