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

Bars in BarChartView become invisible if the x-value in DataEntry is of certain values #1716

Closed
alanlo-hkej opened this issue Oct 26, 2016 · 5 comments

Comments

@alanlo-hkej
Copy link

alanlo-hkej commented Oct 26, 2016

When the input x-values for the data entries of bar chart are set to certain values, the bars in the chart will become invisible, while the value labels for corresponding bar are showing. I've made a sample project reproducing this issue and hope to see if I am missing something or is there something wrong.

Sample project:
ChartTest.zip

In the sample project, commenting one of the lines initiating xVal in the following method:

- (void)setChartData {
    NSMutableArray *arr_tempEntry = [NSMutableArray array];
    for (int i = 0 ; i < 28 ; i++) {

        // comment out one of the following lines for xVal
        double xVal = 86400.0 * (i + 1);
        //double xVal = i + 1;
        NSInteger yVal = arc4random_uniform(15000);

        BarChartDataEntry *dataEntry = [[BarChartDataEntry alloc] initWithX:xVal y:yVal];
        [arr_tempEntry addObject:dataEntry];
        xVal++;
    }

    BarChartDataSet *dataSet = nil;
    if (self.barChartView.data.dataSetCount > 0) {
        dataSet = (BarChartDataSet *)[self.barChartView.data.dataSets firstObject];
        dataSet.values = arr_tempEntry;
        [self.barChartView.data notifyDataChanged];
        [self.barChartView notifyDataSetChanged];
    } else {
        dataSet = [[BarChartDataSet alloc] initWithValues:arr_tempEntry];
        [dataSet setColor:[UIColor colorWithRed:113/255.f green:200/255.f blue:177/255.f alpha:1.f]];
        //dataSet.drawValuesEnabled = FALSE;

        NSMutableArray *dataSets = [NSMutableArray array];
        [dataSets addObject:dataSet];

        BarChartData *data = [[BarChartData alloc] initWithDataSet:dataSet];
        data.barWidth = 0.8f;
        self.barChartView.data = data;
    }
}

If the line double xVal = i + 1; is used, the bar chart has no problem at all, which gives the following result:
simulator screen shot 26 oct 2016 12 20 58 pm
However, if the line double xVal = 86400.0 * (i + 1); is used, the bar chart will give the following result:
simulator screen shot 26 oct 2016 12 15 30 pm
The x-values are setting these large as I am using it to plot chart showing data with respect to date, which those are in fact NSTimeInterval. I am kind of stuck here and have been trying some other settings like setting granularity to different values like 0.0, 1.0 as well as 86400.0, adjusting bar width, etc, but they don't work at all.

Thanks for any help!

@avitus
Copy link

avitus commented Oct 31, 2016

Yes, if I divide the timeInterval by 10,000 (which obviously gives the wrong date) then the bars in the graph are visible:

let timeIntervalForDate: TimeInterval = i.entryDate.timeIntervalSince1970 / 10000

Otherwise, they are invisible.

@sakuraehikaru
Copy link

Digging into the source code, I think the issue is in the computeAxisValues(min:max:) function in AxisRendererBase.swift

I haven't had the time to figure everything out yet, but it looks like the code between line 176 and 194 is rounding up my time intervals for the x-axis, and therefore setting an array of incorrect doubles for the x-axis' entries property. I thought the entries array in my x-axis is supposed to match the x values I've passed in when I called addEntry:

chartDataSet.addEntry(
    BarChartDataEntry(x: timeInterval, y: value)
)
// Ensure stops contains at least n elements.
axis.entries.removeAll(keepingCapacity: true)
axis.entries.reserveCapacity(labelCount)

var f = first
var i = 0
while i < n
{
    if f == 0.0
    {
        // Fix for IEEE negative zero case (Where value == -0.0, and 0.0 == -0.0)
        f = 0.0
    }

    axis.entries.append(Double(f))

    f += interval
    i += 1
}

@liuxuan30
Copy link
Member

liuxuan30 commented Nov 2, 2016

No, axis.entries is the calculated x value labels appearing on the x axis. It won't match your data entry in most cases. I don't have time looking into this.. but will try

@liuxuan30
Copy link
Member

I think this is a dup to #1742. closing.

@kabouzeid
Copy link

kabouzeid commented Sep 9, 2018

I could fix it by sorting the values from smallest to largest x first. Very strange bug.

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

No branches or pull requests

5 participants