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

How to achieve 0 spacing between bars? #365

Closed
kexoth opened this issue Sep 16, 2015 · 7 comments
Closed

How to achieve 0 spacing between bars? #365

kexoth opened this issue Sep 16, 2015 · 7 comments

Comments

@kexoth
Copy link

kexoth commented Sep 16, 2015

I want to make a bar chart with 0 space between the bars, but couldn't manage to do that so far.

simulator screen shot sep 16 2015 6 23 37 am

This is my current code for that matter:

    self.chartView.delegate = self
    self.chartView.descriptionText = ""
    self.chartView.noDataTextDescription = "No data to show at the moment."
    self.chartView.scaleYEnabled = false

    self.chartView.leftAxis.customAxisMin = 0
    self.chartView.leftAxis.customAxisMax = 100
    self.chartView.leftAxis.labelCount = 10
    self.chartView.leftAxis.startAtZeroEnabled = true

    let yNumbersFormatter = NSNumberFormatter()
    yNumbersFormatter.allowsFloats = false
    yNumbersFormatter.positiveSuffix = "%"
    self.chartView.leftAxis.valueFormatter = yNumbersFormatter
    self.chartView.leftAxis.labelFont = UIFont(name: "HelveticaNeue-Light", size: 8)!

    self.chartView.xAxis.labelPosition = ChartXAxis.XAxisLabelPosition.Bottom
    self.chartView.xAxis.spaceBetweenLabels = 0
    self.chartView.xAxis.drawGridLinesEnabled = false

    self.chartView.rightAxis.enabled = false

    let xValues = [String](count: 100, repeatedValue: "")

    var onValues = [BarChartDataEntry]()
    var offValues = [BarChartDataEntry]()

    for i in 0..<100 {

        let entry = BarChartDataEntry(value: Double(index), xIndex: index, data: nil)

        if random() % 2 == 1 {

            onValues.append(entry)
        }
        else {

            offValues.append(entry)
        }
    }

    let onDataSet = BarChartDataSet(yVals: onValues, label: "ON")
    onDataSet.barSpace = 0
    onDataSet.colors = [UIColor.greenColor()]
    onDataSet.drawValuesEnabled = false

    let offDataSet = BarChartDataSet(yVals: offValues, label: "OFF")
    offDataSet.barSpace = 0
    offDataSet.colors = [UIColor.grayColor()]
    offDataSet.drawValuesEnabled = false

    let data = BarChartData(xVals: xValues, dataSets: [onDataSet, offDataSet])
    data.groupSpace = 0
    self.chartView.data = data

No matter how I arrange the bars, only if I have gray after green I get the desired effect.

The code is with Swift 2.0 & I'm using your swift-2.0 branch.

@liuxuan30
Copy link
Member

Seems the space is there because some of your green/grey bars are missing, and ios-charts need to make a place holder for every bar, even it is not there.

@kexoth
Copy link
Author

kexoth commented Sep 16, 2015

@liuxuan30 but there shouldn't be any bars missing missing, take a look at the code, I have a range 0..<100, yValue & xIndex have the same value while iterating in the range.

@liuxuan30
Copy link
Member

Here's the thing:
you have two dataSets, which means every xIndex should have two bars; however you add bars randomly into each dataSet exclusively, which means each xIndex would only have 1 bar, and the other is missing, but the space is reserved.

@kexoth
Copy link
Author

kexoth commented Sep 17, 2015

@liuxuan30 I'm sorry, but now I understand my mistake 😥

Apparently I did it the wrong way trying to cover with 2 datasets. But then what will be the correct approach to achieve it?

Should I put them all in the same dataset & create an array of colors for each item & assign it to the dataset & then customize the legend to follow that convention?

@liuxuan30
Copy link
Member

@kexoth it first depends on what you want your chart looks like. 1 dataSet means one bar, remember that first. Puting more than one y value inside one dataSet will gives you a stacked bar chart. Check them out the ChartsDemo. There are multiple bar charts and stacked bar charts.

For those spacing issues, if ios-charts does not support it yet, then you may need to override the renderer to get what you want, but this will get complicated and requires you understand how the bars is positioned and rendered.

@kexoth
Copy link
Author

kexoth commented Sep 17, 2015

I managed to achieve the mentioned with 1 dataSet, assigned it an array of colors for every entry & created a custom labels & colors for the legend.

Thanks anyway, I'll close the thread ✌️

@kexoth kexoth closed this as completed Sep 17, 2015
@AwaisFayyaz
Copy link

Can i now remove spacing between Y bars in BarChartView ?

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

3 participants