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

Following the tutorial creates an empty/blank view #16

Closed
zkhalapyan opened this issue Feb 26, 2014 · 17 comments
Closed

Following the tutorial creates an empty/blank view #16

zkhalapyan opened this issue Feb 26, 2014 · 17 comments

Comments

@zkhalapyan
Copy link

I followed the tutorial to create the bar chart, and the result was an empty screen. I am assuming the problem is in the initialization code:

JBBarChartView *barChartView = [[JBBarChartView alloc] init];

Can someone double check this and see if they can reproduce the issue on an empty project?

@terryworona
Copy link
Collaborator

Did you set the datasource and delegate? Add the barChartView as a subview?

Check out the demo project for a complete example.

@zkhalapyan
Copy link
Author

Yes, followed the tutorial completely - the graph works only when initialized with initWithFrame. The following code does not work:

JBBarChartView *barChartView = [[JBBarChartView alloc] init];
barChartView.delegate = self;
barChartView.dataSource = self;
barChartView.backgroundColor = [UIColor grayColor];
[self.view addSubview:barChartView];

[barChartView reloadData];

Used the following mock delegate functions:

- (NSInteger)numberOfBarsInBarChartView:(JBBarChartView *)barChartView
{
    return 10;
}

- (CGFloat)barChartView:(JBBarChartView *)barChartView heightForBarViewAtAtIndex:(NSInteger)index
{
    return 100;
}

@yeahdongcn
Copy link

Try this.

  • (CGFloat)barChartView:(JBBarChartView *)barChartView heightForBarViewAtAtIndex:(NSInteger)index
    {
    return arc4random() % 20;
    }

@terryworona
Copy link
Collaborator

It is recommended that you initialize any JB* chart view with initWithFrame:... even if it is with CGRectZero.

As well, you need to supply each bar with a height (random or otherwise) as suggested by @yeahdongcn.

@zkhalapyan
Copy link
Author

@yeahdongcn I tried with the random bar chart heights, but the resulting view is still empty.

@terryworona If the library is not going to work with the empty initializers, should we update the tutorial?

Thanks for the support guys!

@terryworona
Copy link
Collaborator

@zkhalapyan I added default init functions for both line and bar charts.

You can now initialize JBLineChartView and JBBarChartView with init as well as initWithFrame:.

Note: you must set a frame if you start supplying headers and footers, otherwise an assert will catch the mis-matched layout constraints.

I also updated the (demo) line & bar chart controllers to use base -init to demonstrate the change.

Hope this helps.

@yeahdongcn
Copy link

This code works for me.
JBBarChartView *barChartView = [[JBBarChartView alloc] initWithFrame:CGRectMake(10, 64, 300, 200)];
barChartView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
barChartView.layer.borderWidth = 1;

@simonbromberg
Copy link

In view did load for my view controller have the following :
JBBarChartView* chartView = [[JBBarChartView alloc] init]; //also tried intiWithFrame
chartView.delegate = self;
chartView.dataSource = self;
[self.view addSubview:chartView];

Nothing loads in the view and the delegate/datasource methods don't get called...

@terryworona
Copy link
Collaborator

@simonbromberg you have to call reloadData to invoke the drawing.

@simonbromberg
Copy link

Ah, thanks, that fixed it. I know it's kind of implied, since you're following the UITableView structure, but you should consider mentioning that in the readme.

Another
It's also shifted down vertically for some reason, even though its frame matches the container view's frame exactly. Know why it's doing this? Is it because it's expecting a header?

Now I have: (in viewDidLoad)

JBChartView* chartView = [[JBBarChartView alloc] initWithFrame:self.graphContainer.frame];       
chartView.layer.borderWidth = 1;
chartView.delegate = self;
chartView.dataSource = self;
[self.graphContainer addSubview:chartView];
[chartView reloadData];

The grey area is the background for the container view.
screen shot 2014-03-14 at 6 11 05 pm

@terryworona
Copy link
Collaborator

Trying initializing the chartView with the container's bounds?

JBChartView* chartView = [[JBBarChartView alloc] initWithFrame:self.graphContainer.bounds];       

@simonbromberg
Copy link

Thanks, that fixed it.

@ridzkurniawan
Copy link

it gives me a blankbar on the last one, for example i set number of bar 4 then it will display 3 with a single large space for the fourth one
how could I fix this?

here is the code

  • (void)viewDidLoad
    {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    barChartView = [[JBBarChartView alloc] init];
    barChartView.dataSource = self;
    barChartView.delegate = self;
    [self.view addSubview:barChartView];

    barChartView.backgroundColor = [UIColor grayColor];

    barChartView.frame = CGRectMake(60, 100, 200, 200);
    [barChartView reloadData];
    }

  • (void)didReceiveMemoryWarning
    {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
    }

-(CGFloat)barChartView:(JBBarChartView *)barChartView heightForBarViewAtIndex:(NSUInteger)index
{
switch (index) {
case 0:
return 1.0;
break;
case 1:
return 0.9;
break;
case 2:
return 0.7;
break;

    default:return 0.2;
        break;
}
//return 5; // height of bar at index

}

  • (NSUInteger)numberOfBarsInBarChartView:(JBBarChartView *)barChartView
    {
    return 4; // number of bars in chart
    }

  • (UIColor *)barChartView:(JBBarChartView *)barChartView colorForBarViewAtIndex:(NSUInteger)index{
    switch (index) {
    case 0:
    return [UIColor redColor];
    break;
    case 1:
    return [UIColor yellowColor];
    break;
    case 2:
    return [UIColor greenColor];
    break;

    default: return [UIColor blueColor];
        break;
    

    }
    }

  • (UIColor *)barSelectionColorForBarChartView:(JBBarChartView *)barChartView
    {
    return [UIColor whiteColor]; // color of selection view
    }

@EnD

thanks

@terryworona
Copy link
Collaborator

@ridzkurniawan try setting the chart's minimumValue to 0.

@ridzkurniawan
Copy link

@terryworona Thanks, it works now

@MVakas
Copy link

MVakas commented Nov 10, 2014

How can I set the label of a bar?

@terryworona
Copy link
Collaborator

@MVakas the bar's do not have labels (by default); the demo project uses a footer subview to label each bar.

You can create your own custom bar views via:

- (UIView *)barChartView:(JBBarChartView *)barChartView barViewAtIndex:(NSUInteger)index

From there, you can place your own label on a bar and manage it yourself.

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

6 participants