ios-charts xaxis标签没有排队

时间:2017-09-11 14:13:57

标签: swift ios-charts

我创建了一个具有非常简单值的分组条形图。我有5个标签,我想沿着x轴和3个条组展开,以与每个xaxis标签重合。

图表现在显示1个x轴标签和1个条形图。 (它们可能不合时宜,很难说。)我需要每个案例与正确的msArrayAxis标签同步。

以下是代码:

 var msArrayXAxis = ["0-20", "20-50", "50-100", "100-200", "<200"]


 barchartView.isUserInteractionEnabled = false
    barchartView.noDataText = "You need to provide data for the chart."
    barchartView.legend.enabled = false
    barchartView.chartDescription?.text = ""

    barchartView.xAxis.labelPosition = XAxis.LabelPosition.bottom
    barchartView.xAxis.valueFormatter = IndexAxisValueFormatter(values: msArrayXAxis)

    barchartView.xAxis.axisMinimum = 0

    barchartView.xAxis.granularity = 2
    barchartView.xAxis.setLabelCount(5, force: true)

    barchartView.xAxis.spaceMin = 0.3
    //        barchartView.extraBottomOffset = 1
    barchartView.xAxis.labelWidth = 1
    barchartView.xAxis.avoidFirstLastClippingEnabled = true
    barchartView.rightAxis.drawGridLinesEnabled = false
    barchartView.rightAxis.drawAxisLineEnabled = false
    barchartView.rightAxis.drawLabelsEnabled = false

func parsedArrayCount(case1: Array<Double>,case2: Array<Double>,case3: Array<Double>,case4: Array<Double>, case5: Array<Double>) -> [BarChartDataEntry] {

    var dataEntries1: [BarChartDataEntry] = []

    NSLog("Parsing array count")

    let dataEntry = BarChartDataEntry(x: Double(0), y:Double(case1.count))
    dataEntries1.append(dataEntry)

    let dataEntry2 = BarChartDataEntry(x: Double(1), y:Double(case2.count))
    dataEntries1.append(dataEntry2)

    let dataEntry3 = BarChartDataEntry(x: Double(2), y:Double(case3.count))
    dataEntries1.append(dataEntry3)

    let dataEntry4 = BarChartDataEntry(x: Double(3), y:Double(case4.count))
    dataEntries1.append(dataEntry4)

    let dataEntry5 = BarChartDataEntry(x: Double(1), y:Double(case5.count))
    dataEntries1.append(dataEntry5)

    return dataEntries1
}

func updateGraph(array: Array<name>?) {

    var dataEntries1: [BarChartDataEntry] = []
    var dataEntries2: [BarChartDataEntry] = []
    var dataEntries3: [BarChartDataEntry] = []

    barchartView.clear()
    rArray.removeAll()
    nameArray.removeAll()

    if array != nil && array!.count > 0 {

        for i in 0..<array!.count {
            rArray.append(array![i].r)
            nameArray.append(array![i].name)
        }

        NSLog("updating graph..")

        let name  = Analyzer.sharedInstance.nameArray(names: rArray.map{String($0)}, Name: nameArray)

        let cData = parserArrays(rArray: name.first.map {Double($0)!})
        let wData = parserArrays(rArray: name.last.map {Double($0)!})
        let vData = parserArrays(rArray: name.middle.map {Double($0)!})

        dataEntries1 = parsedArrayCount(case1: cData.case1, case2: cData.case2, case3: cData.case3, case4: cData.case4, case5: cData.case5)
        dataEntries2 = parsedArrayCount(case1: wData.case1, case2: wData.case2, case3: wData.case3, case4: wData.case4, case5: wData.case5)
        dataEntries3 = parsedArrayCount(case1: vData.case1, case2: vData.case2, case3: vData.case3, case4: vData.case4, case5: vData.case5)

        let chartDataSet = BarChartDataSet(values: dataEntries1, label: "")
        chartDataSet.colors = [UIColor.Blue()]

        let chartDataSet1 = BarChartDataSet(values: dataEntries2, label: "")
        chartDataSet1.colors = [UIColor.Green()]

        let chartDataSet2 = BarChartDataSet(values: dataEntries3, label: "")
        chartDataSet2.colors = [UIColor.Orange()]

        NSLog("Data set1 \(dataEntries1)")

        let groupSpace = 1.0
        let barSpace = 0.05
        let barWidth = 0.1

      /*  let xAxis = XAxis()
        xAxis.valueFormatter = self
        xAxis.drawGridLinesEnabled = true
        xAxis.labelPosition = .bottom
        xAxis.centerAxisLabelsEnabled = true
        barchartView.xAxis.valueFormatter = xAxis.valueFormatter*/

        let dataSets: [BarChartDataSet] = [chartDataSet,chartDataSet1]
        let chartData = BarChartData(dataSets: dataSets)
        chartData.setDrawValues(false)
        chartData.barWidth = barWidth
        chartData.groupBars(fromX:0.3, groupSpace: groupSpace, barSpace: barSpace)
       // let gg = chartData.groupWidth(groupSpace: groupSpace, barSpace: barSpace)
       // barchartView.xAxis.axisMaximum = Double(0) + gg * Double(msArrayXAxis.count)

        barchartView.data = chartData
        barchartView.animate(yAxisDuration: 0.5)
    }
}

1 个答案:

答案 0 :(得分:0)

请检查:

parentForm