Jawbone / JBChartView

iOS-based charting library for both line and bar graphs.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

JBLineChartView: wrong width on iPhone6/6+

va1da opened this issue · comments

I have JBLineChartView inside UITableViewCell which is inserted in UITableView after the touch on another UITableViewCell.
When the cell is displayed for the first time it has wrong width on iPhone6/6+. Later it look OK.
If I put ordinary UIView instead of JBLineChartView everything works fine.

Any idea what could be the reason?

Thanks.

bad
ok

I'm guessing the width of the chart is incorrect when it's displayed the first time.

As an experiment, keep an instance around of the chart and call reloadData on viewDidAppear of your table controller. See what happens...

Unfortunately I can't because the big cell with the chart is made visible only after I select some row :-(

I've created a small sample which demonstrates the problem: https://dl.dropboxusercontent.com/u/102474509/ChartCellTest.zip

I'm not super familiar with auto layout and constraints, but it seems that the chart's width is equal to 600 when the table is first loaded.

The property is found in IB:

screen shot 2015-07-13 at 9 55 56 am

AL constraints are set correctly, so it should work.
If you replace JBLineChartView with UIView it works OK.
So I think there must be something about JBLineChartView.

Print out the chart's frame on reloadData and it's grabbing 600.

Subsequent call have it set to 375.

screen shot 2015-07-13 at 10 12 22 am

The chart is doing it's thing, it's just that reloadData is being called at a time where the chart's size isn't correctly set.

Why is it 600 to begin with? Any way to correct this and have it equal the width of the container?

I thought that AL should do it.
But I don't know how to make AL perform its calculations before reloadData is called :-(
I've tried to call layoutIfNeeded and layoutSubviews but it doesn't work.

OK finally I figured it out. I had to implement layoutSubviews:

- (void)layoutSubviews
{
    [super layoutSubviews];

    CGRect frame = self.contentView.frame;
    CGRect chartFrame = self.lineChartView.frame;
    chartFrame.size.width = frame.size.width;
    self.lineChartView.frame = chartFrame;
    [self.lineChartView reloadData];
}

Thanks a lot for your help.
JBChartView library is very nice!

Isn't it viewWillLayoutSubviews