El_Loco
El_Loco

Reputation: 1866

BarChart does not fit on display Iphone

This is probably very simple, but I am not used to Iphone programming and Swift yet. I have only worked with Android. I want to use iosChart in my application. I have followed this tutorial to create a test bar chart.

The problem is that the chart does not fit the screen, but extends outside the display in the emulator. There is probably something easy to fix in the storyboard, but I have not found anything.

Here is how it looks like:

enter image description here

The x-axis should extend to December, but it is not visible.

Here is the code:

import UIKit
import Charts

class DetailedScoreController: UIViewController {
var results : Results

var months: [String]!

// MARK: Initialization
@IBOutlet weak var detailedChart: BarChartView!

required init?(coder aDecoder: NSCoder) {
    self.results = Results()!
    super.init(coder: aDecoder)
}

override func viewDidLoad() {
    super.viewDidLoad()
    print("DetailedResultController Created!");
    self.results.printResult()
    self.results.printDetailedResults()

    months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]
    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]

    setChart(months, values: unitsSold)
}

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

// MARK: Set up barchart

func setChart(dataPoints: [String], values: [Double]) {
    var dataEntries: [BarChartDataEntry] = []

    for i in 0..<dataPoints.count {
        let dataEntry = BarChartDataEntry(value: values[i], xIndex: i)
        dataEntries.append(dataEntry)
    }

    let chartDataSet = BarChartDataSet(yVals: dataEntries, label: "Units Sold")
    let chartData = BarChartData(xVals: months, dataSet: chartDataSet)
    detailedChart.data = chartData
}

}

Here is how the storyboard looks like, the marked is the bar chart:

enter image description here

To me it looks like the entire chart should lie in the display.

Upvotes: 0

Views: 410

Answers (2)

Jess Murray
Jess Murray

Reputation: 1339

Click on your view and select the pin at the bottom right hand corner to add constraints.

Set the top, bottom, left and right to 10 and click 'Add constraints'. It should now show the entire bar chart as expected.

enter image description here

Upvotes: 2

Ali Beadle
Ali Beadle

Reputation: 4526

Add constraints to your view in storyboard to make it change size to fit the screen: About Auto Layout and Layout Constraints

Upvotes: 1

Related Questions