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

Create Simple Bar Chart #2749

Closed
martheli opened this issue Aug 25, 2017 · 8 comments
Closed

Create Simple Bar Chart #2749

martheli opened this issue Aug 25, 2017 · 8 comments

Comments

@martheli
Copy link

I have been looking all over the place and theres no where where theres an example of how to create a simple bar graph in Swift 3. So far I have this but am getting nil errors. Does anyone have any documentation on how to make simple bar graphs with Swift 3?

func setChart() {

        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 {
            
            let dataEntry = BarChartDataEntry(x: Double(test[i]), y: Double(unitsSold[i]))
            
            dataEntries.append(dataEntry)
        }
        
        let chartDataSet = BarChartDataSet(values: dataEntries, label: "Visitor count")
        
        let chartData = BarChartData(dataSet: chartDataSet)
        
        barChartView.data = chartData
        
    }
@seenoevo
Copy link

seenoevo commented Aug 25, 2017

Not sure how you're getting a nil, I copied your code into my demo and it works just fine.

class BarChartViewController: UIViewController
{
    @IBOutlet var barChartView: BarChartView!
    
    var months: [String]!

    override func viewDidLoad()
    {
        super.viewDidLoad()
        months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]

       setChart()
    }

    func setChart()
    { 
        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 
       {
            let dataEntry = BarChartDataEntry(x: Double(test[i]), y: Double(unitsSold[i]))
            
            dataEntries.append(dataEntry)
        }
        
        let chartDataSet = BarChartDataSet(values: dataEntries, label: "Visitor count")
        let chartData = BarChartData(dataSet: chartDataSet)
        
        barChartView.data = chartData  
    }
}

simulator screen shot aug 24 2017 7 55 23 pm

You must be doing something wrong...recheck your code

@martheli
Copy link
Author

your graph doesn't show the months on the x-axis.

@seenoevo
Copy link

seenoevo commented Aug 25, 2017

@martheli: that was just an example based on the code you showed, I can only go based off what you have shown

if you are receiving a nil somewhere else, show me the whole function where it's receiving a nil

@seenoevo
Copy link

@martheli glad you figured it out 👍

@martheli
Copy link
Author

do you know if there is a simple way to remove the Y axis on the right hand side of the graph? id like the Y axis labels to only show up on the left side of the graph.

@seenoevo
Copy link

seenoevo commented Aug 25, 2017

BarChartViewController.m shows the following:

    ChartYAxis *rightAxis = _chartView.rightAxis;
    rightAxis.enabled = YES;

Now use that as reference and apply it to your swift code.

If you want to change things, play around with the Charts demo and you'll start to figure out how the charts are displayed the way they are.

@muhammadawais790
Copy link

It is not working for me it shows no chart data available. please help me out. I am very worry about my final year project and it is totally graph projects.

@froymetal
Copy link

How you delete the numbers above each bar?

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

4 participants