0

I am trying to recreate the streamgraph ObjectiveC example from the examples page, using swift. First thing I noticed is that I can't.. and hopefully don't need to, initialize the PlotOptions style:

    let plotoptions = HIPlotOptions()
    plotoptions.series = HISeries()
    plotoptions.series.label = HILabel()
    plotoptions.series.label.minFontSize = 5
    plotoptions.series.label.maxFontSize = 15
    //plotoptions.series.label.style = HIStyle()
    plotoptions.series.label.style.color = "rgba(255,255,255,0.75)"

because Cannot assign value of type 'HIStyle' to type 'HICSSObject?'

But more importantly, the example shows colors being assigned directly as HIColors:

HIColor *color5 = [[HIColor alloc] initWithHexValue:@"1aadce"];

HIColor *color6 = [[HIColor alloc] initWithRGBA:73 green:41 blue:112 alpha:.2];
HIColor *color7 = [[HIColor alloc] initWithRGBA:73 green:41 blue:112 alpha:.1];

But doing the equivalent, I get:

    let colors: [HIColor] = [
//...
        HIColor(hexValue:"1aadce"),
        HIColor(rgba: 73, green: 41, blue: 112, alpha: 0.2),
        HIColor(rgba: 73, green: 41, blue: 112, alpha: 0.1),
//...

Cannot assign value of type '[HIColor]' to type '[String]?'

For many of these values, its no worries .. I could just use "1aadce" .. but for the RGBA ones, how do I initialize this?

edit

I had it in reverse in the sense that it is the hex values that it cannot use, but the rgba() values produce strings that are accepted.

roberto tomás
  • 4,435
  • 5
  • 42
  • 71

1 Answers1

0

so, the answer to the style part is just plotoptions.series.label.style = HICSSObject(). The colors have to be manually converted (thanks to stack overflow for the resources to find these):

    let colors: [HIColor] = [
        HIColor(hexValue:"2f7ed8"),
        HIColor(hexValue:"0d233a"),
        HIColor(hexValue:"8bbc21"),
        HIColor(hexValue:"910000"),
        HIColor(hexValue:"1aadce"),
        HIColor(hexValue:"1aadce"),
        HIColor(rgba: 73, green: 41, blue: 112, alpha: 0.2),
        HIColor(rgba: 73, green: 41, blue: 112, alpha: 0.1),
        HIColor(hexValue:"492970"),
        HIColor(hexValue:"f28f43"),
        HIColor(hexValue:"77a1e5"),
        HIColor(hexValue:"c42525"),
        HIColor(hexValue:"a6c96a"),
        HIColor(hexValue:"2f7ed8"),
        HIColor(hexValue:"0d233a"),
        HIColor(hexValue:"910000"),
        HIColor(rgba: 139, green: 188, blue: 33, alpha: 0.1),
        HIColor(rgba: 139, green: 188, blue: 33, alpha: 0.2),
        HIColor(rgba: 139, green: 188, blue: 33, alpha: 0.3),
    ]

    options.colors = colors.map{ color in
        let colorSpec = color.getData().debugDescription
            .replacingOccurrences(of: "Optional(", with: "")
            .replacingOccurrences(of: ")", with: "")
        if(colorSpec.hasPrefix("#")) {
            let index = colorSpec.index(colorSpec.startIndex, offsetBy: 1)
            // src: https://stackoverflow.com/questions/24263007/how-to-use-hex-color-values
            let pre = colorSpec
                .trimmingCharacters(in: CharacterSet.whitespacesAndNewlines)
                .uppercased()
            let cString = pre[index...]

            let rString = (cString as NSString).substring(to: 2)
            let gString = ((cString as NSString).substring(from: 2) as NSString).substring(to: 2)
            let bString = ((cString as NSString).substring(from: 4) as NSString).substring(to: 2)

            return "rgba(\(Int(strtoul(rString, nil, 16))), \(Int(strtoul(bString, nil, 16))), \(Int(strtoul(gString, nil, 16))), \(1.000000))"
        }

        return color.getData().debugDescription
    }
roberto tomás
  • 4,435
  • 5
  • 42
  • 71