如何创建分组条形图

时间:2017-06-01 10:56:09

标签: arrays swift swift3 swiftcharts

我正在尝试将数组作为groupsData传递。以下是i-schuetz的组图表示例。

fileprivate var chart: Chart?

fileprivate func barsChart(horizontal: Bool) -> Chart {
    let labelSettings = ChartLabelSettings(font: ExamplesDefaults.labelFont)


    var groupsData: [(title: String, [(min: Double, max: Double)])] = [
        ("A", [
            (0, 40),
            (0, 50),
            (0, 35)
            ]),
        ("B", [
            (0, 20),
            (0, 30),
            (0, 25)
            ]),
        ("C", [
            (0, 30),
            (0, 50),
            (0, 5)
            ]),
        ("D", [
            (0, 55),
            (0, 30),
            (0, 25),(0, 25)
            ])
    ]

    var colors: [UIColor] = []

    for _ in 0..<groupsData.count {
        let red = Double(arc4random_uniform(256))
        let green = Double(arc4random_uniform(256))
        let blue = Double(arc4random_uniform(256))
        let color = UIColor(red: CGFloat(red/255), green: CGFloat(green/255), blue: CGFloat(blue/255), alpha: 1)
        colors.append(color)
    }

    let groups: [ChartPointsBarGroup] = groupsData.enumerated().map {index, entry in
        let constant = ChartAxisValueDouble(index)
        let bars = entry.1.enumerated().map {index, tuple in
            ChartBarModel(constant: constant, axisValue1: ChartAxisValueDouble(5), axisValue2: ChartAxisValueDouble(tuple.max), bgColor: colors[index])
        }
        return ChartPointsBarGroup(constant: constant, bars: bars)
    }

我正在尝试将此数组转换为Chart而不是gropsData。该组可能包含n个条形码。

    let months = ["Jan", "Feb", "Mar", "Apr", "May"]
    let unitsSold = [20.0, 4.0, 6.0, 3.0, 12.0]
    let unitsBought = [10.0, 14.0, 60.0, 13.0, 2.0]

我该怎么做?

谢谢!

0 个答案:

没有答案