iOS图表 - 分组条形图未正确索引

时间:2016-08-29 15:28:54

标签: ios swift charts bar-chart ios-charts

我填充了具有折线图和分组条形图的组合图表但是 酒吧没有像预期的那样出现。它们与xAxis索引不同步。任何帮助将不胜感激。

Combined Chart Image: Line + Grouped Bar

对于我附上的图片,酒吧应该与每个月保持同步但不是。

以下是代码:

private func setLineChartWith(data: [Double]) {

    var lineChartEntries: [ChartDataEntry] = []

    for i in 0..<data.count {

        let lineChartEntry = ChartDataEntry(value: data[i], xIndex: i)
        lineChartEntries.append(lineChartEntry)
    }

    let lineDataSet = LineChartDataSet(yVals: lineChartEntries, label: "Line")
    lineDataSet.lineWidth = 3
    lineDataSet.circleRadius = 4
    lineDataSet.mode = .CubicBezier
    lineDataSet.highlightEnabled = true

    let lineChartData = LineChartData(xVals: datesData, dataSets: [lineDataSet])
    chartData.lineData = lineChartData
}

    // MARK: Bar Chart Code
    func setBarChartWith(data: [Double], secondaryData: [Double]?) {

    var barChartEntries1: [BarChartDataEntry] = []
    var barChartEntries2: [BarChartDataEntry] = []
    for i in 0..<data.count {

        barChartEntries1.append(BarChartDataEntry(value: data[i], xIndex: i))
        barChartEntries2.append(BarChartDataEntry(value: data[i], xIndex: i))
    }

    let barDataSet = BarChartDataSet(yVals: barChartEntries1, label: "Bar1")
    barDataSet.barSpace = 0.5
    barDataSet.highlightEnabled = true
    barDataSet.barShadowColor = UIColor.clearColor()

    let barDataSet2 = BarChartDataSet(yVals: barChartEntries1, label: "Bar2")
    barDataSet2.barSpace = 0.5
    barDataSet2.highlightEnabled = true
    barDataSet2.barShadowColor = UIColor.clearColor()

    var dataSets : [BarChartDataSet] = [BarChartDataSet]()
    dataSets.append(barDataSet)
    dataSets.append(barDataSet2)
    let barChartData = BarChartData(xVals: datesData, dataSets: dataSets)
    chartData.barData = barChartData
}

2 个答案:

答案 0 :(得分:0)

可能您正在使用库来创建图形。这个bibliotca有这个问题,项目在x轴上受到限制。图表将它们分开,以便即使没有一些数据,用户也可以理解。您将意识到它始终从第一个索引开始,并以数组的最后一个索引结束,因此它从x轴的中间获取一些数据。因此,用户可以理解图形并且图形不会过载。库中没有正确的解决方案,但您可以尝试缩放图表或x轴上的标签。增加图表的宽度也应该有所帮助。

答案 1 :(得分:0)

在课程级别设置

 var barCartDataSet : [IChartDataSet] = []

  var colNamesArray : [String]!

//根据需要多次调用此函数意味着在一个组中需要多少条形

setChartData("Test Lable", values: [2.0,3.0]  , color: UIColor.darkGrayColor())

//最后调用此

setChartDataSet(colNamesArray, chartDataSet: barCartDataSet)

// Functios

   func setChartData(lableName: String, values: [Double] , color : UIColor) {


        var dataEntries: [BarChartDataEntry] = []


        for i in 0..<values.count {
            let dataEntry = BarChartDataEntry(value: values[i], xIndex: i)
            dataEntries.append(dataEntry)
        }


        let chartDataSet = BarChartDataSet(yVals: dataEntries, label: lableName)

        barCartDataSet.append(chartDataSet)


        //changing color
        chartDataSet.colors = [color]   
    }

    func setChartDataSet(dataPoints: [String], chartDataSet : [IChartDataSet]) {

        BarChartDrawingView.noDataText = "You need to provide data for the chart."


        let chartData = BarChartData(xVals: dataPoints, dataSets: chartDataSet)

        chartData.groupSpace = 0.8

        BarChartDrawingView.data = chartData

        BarChartDrawingView.xAxis.labelPosition = .Bottom

        //Description Text
        BarChartDrawingView.descriptionText = " "



    }
抱歉英语不好。希望它有所帮助:)