Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bar Width issue after reset the data #3801

Closed
jayeshkanzariya opened this issue Dec 27, 2018 · 3 comments
Closed

Bar Width issue after reset the data #3801

jayeshkanzariya opened this issue Dec 27, 2018 · 3 comments

Comments

@jayeshkanzariya
Copy link

screen shot 2018-12-27 at 5 07 21 pm

i am displaying here 12 months data after that i am changing the data to 3 months then bar width is changed and display like below image.

screen shot 2018-12-27 at 5 09 32 pm

Here is the code which i am using for set data in bar chart.

func setDataInChart(chartView : BarChartView,dataArray : [ChartDataModel]){
    
    chartView.clear()
    chartView.clearValues()
   
    
    let grouppedData = dataArray.group(by: {$0.name})
    print(dataArray.group { $0.name })
    
    var xArray = [String]()
    for (_,obj) in grouppedData.enumerated(){
        xArray = obj.value.sorted(by: {$0.monthNo < $1.monthNo}).map({$0.month})
        break
    }
    
    
    let formatter = CustomLabelsXAxisValueFormatter()
    formatter.labels = xArray
    chartView.xAxis.valueFormatter = formatter
    
    
    var dataEntries = [String : [BarChartDataEntry]]()
    for (_,obj) in grouppedData.enumerated(){
        var dataEntry = [BarChartDataEntry]()
        for (index,data) in obj.value.sorted(by: {$0.monthNo < $1.monthNo}).enumerated(){
            dataEntry.append(BarChartDataEntry(x: Double(index), y: data.value, icon: nil))
        }
        dataEntries[obj.key] = dataEntry
    }
    
    var datasetArray = [BarChartDataSet]()
    
    for (index,entry) in dataEntries.enumerated(){
        let set = BarChartDataSet(values: entry.value, label: entry.key)
        set.drawIconsEnabled = false
        if index == 0{
            set.colors = [#colorLiteral(red: 0.3176470588, green: 0.8, blue: 0.5843137255, alpha: 1)]
        }
        else if index == 1{
            set.colors = [#colorLiteral(red: 0.3921568627, green: 0.8784313725, blue: 0.662745098, alpha: 1)]
        }
        else if index == 2{
            set.colors = [#colorLiteral(red: 0.4509803922, green: 0.937254902, blue: 0.7215686275, alpha: 1)]
        }
        else if index == 3{
            set.colors = [#colorLiteral(red: 0.6470588235, green: 0.968627451, blue: 0.8235294118, alpha: 1)]
        }
        
        set.valueTextColor = UIColor.black
        set.valueFont = UIFont(name: "Lato-Regular", size: 10)!
        datasetArray.append(set)
    }
    
    let groupSpace = 0.4
    let barSpace = 0.03
    let barWidth = 0.3



    
    let data = BarChartData(dataSets: datasetArray)
    data.barWidth = barWidth

// chartView.xAxis.axisMinimum = 0.0

    chartView.xAxis.resetCustomAxisMin()
    chartView.xAxis.resetCustomAxisMax()
    chartView.rightAxis.resetCustomAxisMin()
    chartView.rightAxis.resetCustomAxisMax()
    
    if datasetArray.count > 1{
        chartView.xAxis.centerAxisLabelsEnabled = true
        chartView.xAxis.axisMaximum = 0.0 + data.groupWidth(groupSpace: groupSpace, barSpace: barSpace) * Double(xArray.count)
        data.groupBars(fromX: 0.0, groupSpace: groupSpace, barSpace: barSpace)
        chartView.xAxis.granularity = chartView.xAxis.axisMaximum / Double(xArray.count)
    }
    
    else{
        chartView.xAxis.centerAxisLabelsEnabled = false
        chartView.xAxis.granularity = 1.0
    }

    chartView.data = data
    data.notifyDataChanged()
    chartView.notifyDataSetChanged()
    chartView.setVisibleXRangeMaximum(7)

}

@liuxuan30
Copy link
Member

liuxuan30 commented Jan 3, 2019

seems like by design; the more bars you have, the thinner you get; and vice versa.
if you want thin bars in your 3 months chart, you have to override a few methods to do so. or modify the bar width. do not use grouped bar space if you are not using multiple bar chart.

@jayeshkanzariya
Copy link
Author

if datasetArray.count > 1{
chartView.xAxis.centerAxisLabelsEnabled = true
chartView.xAxis.axisMaximum = 0.0 + data.groupWidth(groupSpace: groupSpace, barSpace: barSpace) * Double(xArray.count)
data.groupBars(fromX: 0.0, groupSpace: groupSpace, barSpace: barSpace)
chartView.xAxis.granularity = chartView.xAxis.axisMaximum / Double(xArray.count)
}

else{
    chartView.xAxis.centerAxisLabelsEnabled = false
    chartView.xAxis.granularity = 1.0
}

this is the condition i am not using groupspace on single bar chart.
please guide me which method i need to override..

@liuxuan30
Copy link
Member

take a look at bar chart renderer; the drawDataSet is where it begins.
warning: if you are using other features as well, I suggest you review your design. just imagine how do you draw the bars if only 1 or two on a canvas: more space or more bar width.

I don't know what your problem is, but the image just looks fine.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants