简体   繁体   中英

iOS Charts - Grouped Bar chart not index properly

I'm populating a Combined Chart having a Line Chart and a Grouped Bar Chart BUT the bars are not coming up as expected. They're not in sync with xAxis indexes. Any help will be appreciated.

Combined Chart Image: Line + Grouped Bar

For the image I've attached, bars should be in sync with each month but they are not.

Here's the code:

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
}

Probably you are using a library to create your graphic. This bibliotca have this problem, the items are limited in the x-axis. The graph divides them so that the User can understand even without some data. You will realize that it always starts on the first index and ends on the last index of the array so it takes some data from the middle of the x-axis. Thus the User can understand the graph and the graph is not overloaded. There is no right solution in the library for this but you can try to zoom the graph or the labels on the x axis. Increasing the width of the graph should also help.

set those in class level

 var barCartDataSet : [IChartDataSet] = []

  var colNamesArray : [String]!

//call this function as many time as you needed means how much bars you need in one group

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

//After that at last call this

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 = " "



    }

sorry for bad English. Hope it helps :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM