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 Chart remove x if y is 0 #2747

Closed
MathiasVang opened this issue Aug 24, 2017 · 4 comments
Closed

Bar Chart remove x if y is 0 #2747

MathiasVang opened this issue Aug 24, 2017 · 4 comments

Comments

@MathiasVang
Copy link

I have been working with the framework quite a bit, but the bar chart with actual numbers in them are not positioned as I want them because some data might be "0". Is it possible to ignore these values when rendering the chart?

Screenshot:

skaermbillede 2017-08-24 kl 15 10 49

The column 2nd from the left is the one with "0" data in this case.

@seenoevo
Copy link

@MathiasVang I don't understand what you mean by The column 2nd from the left is the one with "0" data in this case.

I see the 2nd column from the left with 55,55 kr. That shows data to me.

Are you talking about the 2nd column from the right?

@MathiasVang
Copy link
Author

@seenoevo That was a mistake. I am talking about the 2nd column from the the right.

@seenoevo
Copy link

seenoevo commented Aug 26, 2017

@MathiasVang sorry for the late reply

In playing around with your issue, I haven't found a way to shift x-axis data to left by one if there is an empty BarChartDataEntry, i.e. shift the gray bar to the left to fill in the empty spot.

The x double value that is passed into BarChartDataEntry(x: Double, y: Double) has to be in sequence order, i.e. 0, 1, 2, 3, 4..etc, and not skip any numbers in between, at least from my understanding.

Therefore you can't do something like this:

let unitsSold = [20.0, 4.0, 6.0, 3.0, 12.0, 16.0, 4.0, 18.0, 2.0, 4.0, 5.0, 4.0]

let test = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]

var dataEntries: [BarChartDataEntry] = []

for i in 0..<months.count
{
    if i != 2
    {
        let dataEntry = BarChartDataEntry(x: Double(test[i]), y: Double(unitsSold[i]))
    
        dataEntries.append(dataEntry)
    }
}

let chartDataSet = BarChartDataSet(values: dataEntries, label: "Test")

let chartData = BarChartData(dataSet: chartDataSet)

barChartView.data = chartData

This will produce a BarChartDatEntry object with the following, where the x value is skipped at 3.0:

[ChartDataEntry, x: 1.0, y 20.0,
 ChartDataEntry, x: 2.0, y 4.0, 
 ChartDataEntry, x: 4.0, y 3.0, 
 ChartDataEntry, x: 5.0, y 12.0, 
 ChartDataEntry, x: 6.0, y 16.0, 
 ChartDataEntry, x: 7.0, y 4.0, 
 ChartDataEntry, x: 8.0, y 18.0, 
 ChartDataEntry, x: 9.0, y 2.0, 
 ChartDataEntry,  x: 10.0, y 4.0, 
 ChartDataEntry, x: 11.0, y 5.0, 
 ChartDataEntry, x: 12.0, y 4.0]

The only way I found to remedy this is that you have to either update your array or store it in a new array with the removed data element, then pass that into BarChartDataEntry(x: Double, y: Double).

This way, when you call dataEntries.append(dataEntry), the x value will contain every number in sequential order, and not skip any numbers.

@MathiasVang
Copy link
Author

@seenoevo Thank you for the thorough response! I used your suggestion in the end of your post and broke out of the loop if the value was 0.

I am closing this issue.

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