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

need Swift implementation details and example #80

Closed
NicolaZordan opened this issue Aug 6, 2014 · 10 comments
Closed

need Swift implementation details and example #80

NicolaZordan opened this issue Aug 6, 2014 · 10 comments

Comments

@NicolaZordan
Copy link

Hi, I am trying to use it in a swift project.
however I am having a hard time getting it to work.

will it be possible to show an example
or
list step by step documentation instructions to make it work on a swift project

@DirkLXX
Copy link

DirkLXX commented Sep 26, 2014

I do agree.
It's such a great piece of work, it deserves to be used by more people. I finally got it to work but it's tough for beginners like myself. A simple example (using Storyboards) that shows all capabilities would be great.

@mkuliszkiewicz
Copy link

The easiest way to use this lib with Swift is to add it via Cocoapods to your project.
Then you create the file called "Bridging-Header.h", and add there the following imports:

#import "JBChartView/JBChartView.h"
#import "JBChartView/JBBarChartView.h"
#import "JBChartView/JBLineChartView.h" 

Then you need to specify the bridging header path in your target configuration. It's under the Build Settings tab, section "Swift Compiler - Code Generation"

Hope it helps

@nsimonson
Copy link

Since most of the delegate functions return UInt you'll need to explicitly covert values in Swift. For instance, setting up the basic functions for a line chart might look like this:

func numberOfLinesInLineChartView(lineChartView: JBLineChartView!) -> UInt {
    return 1 //number of lines in chart
}

func lineChartView(lineChartView: JBLineChartView!, numberOfVerticalValuesAtLineIndex lineIndex: UInt) -> UInt {
    return UInt(chartData.count) //number of values for a line
}

func lineChartView(lineChartView: JBLineChartView!, verticalValueForHorizontalIndex horizontalIndex: UInt, atLineIndex lineIndex: UInt) -> CGFloat {
    return CGFloat(chartData[Int(horizontalIndex)]) //y-position (y-axis) of point at horizontalIndex (x-axis)
}

@gregorifaroux
Copy link

@DirkLXX @mbanasiewicz @nsimonson - could you share a complete sample code? I tried a simple barchart in swift and I see the area's background, but no bars:

import UIKit

class ViewController: UIViewController, JBBarChartViewDelegate, JBBarChartViewDataSource {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        var label = UILabel(frame: CGRectMake(0, 0, 200, 21))
        label.center = CGPointMake(160, 284)
        label.textAlignment = NSTextAlignment.Center
        label.text = "I'am a test label"
        self.view.addSubview(label)

        let barChartView = JBBarChartView();
        barChartView.dataSource = self;
        barChartView.delegate = self;
        barChartView.backgroundColor = UIColor.darkGrayColor();
        barChartView.frame = CGRectMake(0, 0, 320, 200);
        barChartView.reloadData();
        self.view.addSubview(barChartView);
        println("Launched");
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    func numberOfBarsInBarChartView(barChartView: JBBarChartView!) -> UInt {
        println("numberOfBarsInBarChartView");
        return 2 //number of lines in chart
    }

     func barChartView(barChartView: JBBarChartView, heightForBarViewAtIndex index: UInt) -> CGFloat {
        println("barChartView", index);
        return 100.0
    }
}

I used println to make sure the methods were called; I get the following results:

numberOfBarsInBarChartView
(barChartView, 0)
(barChartView, 1)
Launched

I set the frame and called the reloadData after setting up the frame; any ideas why I do not see any bars?

Thanks,
Gregori

@nsimonson
Copy link

@gregorifaroux You have it set up correctly. The minimum and maximum values of the chart are equal to the min and max values of the data source. In your case the min and max are both being set to 100. You can override the minimum and maximum values of the chart view by setting them on your barChartView before you call reloadData(). Or add some other values for the data source.

@gregorifaroux
Copy link

@nsimonson Thank you! I replaced my fixed value with random numbers and it is working:

     func barChartView(barChartView: JBBarChartView, heightForBarViewAtIndex index: UInt) -> CGFloat {
        println("barChartView", index);

        return CGFloat(arc4random_uniform(100));
    }

Here is the complete code if someone is looking for a Swift example:


import UIKit

class ViewController: UIViewController, JBBarChartViewDelegate, JBBarChartViewDataSource {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        var label = UILabel(frame: CGRectMake(0, 0, 200, 21))
        label.center = CGPointMake(160, 284)
        label.textAlignment = NSTextAlignment.Center
        label.text = "I'm a test label"
        self.view.addSubview(label)

        let barChartView = JBBarChartView();
        barChartView.dataSource = self;
        barChartView.delegate = self;
        barChartView.backgroundColor = UIColor.darkGrayColor();
        barChartView.frame = CGRectMake(0, 20, 320, 200);
        barChartView.reloadData();
        self.view.addSubview(barChartView);
        println("Launched");
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    func numberOfBarsInBarChartView(barChartView: JBBarChartView!) -> UInt {
        println("numberOfBarsInBarChartView");
        return 10 //number of lines in chart
    }

     func barChartView(barChartView: JBBarChartView, heightForBarViewAtIndex index: UInt) -> CGFloat {
        println("barChartView", index);

        return CGFloat(arc4random_uniform(100));
    }
}

@gregorifaroux
Copy link

Here is a full example using swift and a JSON REST Web Service to populate the data: https://github.com/gregorifaroux/swift-JBBarChartView-RESTAPI-JSON

@shameer49
Copy link

@gregorifaroux Hi , I tried your code but still i have no bar chart, Can you help me ?

@gregorifaroux
Copy link

@shameer49 Hi, Sorry about this.

Did you try the github link posted? Another trick is to set minimumValue and maximumValue.... For instance to 0 and 100 if your values are between 0 and 100.

If you could post your code, I can have a look. Or if you create a git repository so I can look at the full code.

Take care,
Grégori

Sent from my iPhone

On Nov 28, 2014, at 1:55 AM, Shameerjan notifications@github.com wrote:

@gregorifaroux Hi , I tried your code but still i have no bar chart, Can you help me ?


Reply to this email directly or view it on GitHub.

@terryworona
Copy link
Collaborator

Closing this. Please re-open if you are still having issues.

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

7 participants