具有渐变填充的iOS Swift Highcharts饼为黑色

问题描述 投票:0回答:1

我正在使用此示例在iOS上创建饼图:https://www.highcharts.com/ios/demo/pie-gradient

饼形图效果很好,但渐变填充仅为黑色。我将示例中的代码转换为Swift,如下所示:

let colors = [
        HIColor(radialGradient: ["cx": 0.5, "cy": 0.3,"r": 0.7],
            stops: [[ 0, "#7cb5ec" ],[1, "rgb(48,105,160)"]]),
        HIColor(radialGradient: ["cx": 0.5, "cy": 0.3,"r": 0.7],
                stops: [[ 0, "#434348" ],[1, "rgb(0,0,0)"]])!,
        HIColor(radialGradient: ["cx": 0.5, "cy": 0.3,"r": 0.7],
                stops: [[ 0, "#90ed7d" ],[1, "rgb(68,161,49)"]]),
        HIColor(radialGradient: ["cx": 0.5, "cy": 0.3,"r": 0.7],
                stops: [[ 0, "#f7a35c" ],[1, "rgb(171,87,16)"]]),
        HIColor(radialGradient: ["cx": 0.5, "cy": 0.3,"r": 0.7],
                stops: [[ 0, "#8085e9" ],[1, "rgb(52,57,157)"]]),
        HIColor(radialGradient: ["cx": 0.5, "cy": 0.3,"r": 0.7],
                stops: [[ 0, "#f15c80" ],[1, "rgb(165,16,52)"]]),
        HIColor(radialGradient: ["cx": 0.5, "cy": 0.3,"r": 0.7],
                stops: [[ 0, "#e4d354" ],[1, "rgb(152,135,8)"]]),
        HIColor(radialGradient: ["cx": 0.5, "cy": 0.3,"r": 0.7],
                stops: [[ 0, "#2b908f" ],[1, "rgb(0,68,67)"]]),
        HIColor(radialGradient: ["cx": 0.5, "cy": 0.3,"r": 0.7],
                stops: [[ 0, "#f45b5b" ],[1, "rgb(168,15,15)"]]),
        HIColor(radialGradient: ["cx": 0.5, "cy": 0.3,"r": 0.7],
                stops: [[ 0, "#91e8e1" ],[1, "rgb(69,156,149)"]])
    ]

在给定的示例中,他们已经将colors数组分配给options.colors,但是它仅接受String数组,而不接受HIColor数组。这是我得到的错误:error.png

为了修复错误,这是我尝试过的代码修改,它给出了黑色的馅饼:

let colors_str = colors.map{
        (color: HIColor!) -> String  in

        let c = color.getData().debugDescription
        .replacingOccurrences(of: "Optional(", with: "")
        .replacingOccurrences(of: "\n", with: "")
        .replacingOccurrences(of: "\"", with: "")
        .dropLast()

        let value = String(c)
        return value
    } 

options.colors = colors_str

black-pie-chart

任何帮助将不胜感激。

ios swift highcharts pie-chart radial-gradients
1个回答
0
投票

我找到了解决此问题的方法。自从我花了一天的时间后就想发布它,以防其他人遇到相同的问题。因此,Highcarts示例上的iOS代码不正确。我参考了同一个饼图的Javascript代码,发现将颜色分配给绘图选项饼图颜色。这是swift的示例代码:

 let plot_options = HIPlotOptions()
 plot_options.pie = HIPie()
 plot_options.pie.colors = colors as? [HIColor]
© www.soinside.com 2019 - 2024. All rights reserved.