atalayasa
atalayasa

Reputation: 3480

Multiple charts in same view IOS Charts

I have been trying to develop a view controller with multiple charts(bar chart, line chart, pie chart). I created a table view and custom table view cell. There is a UIView inside custom table view cell. However, when I am trying to cast that UIView to BarchartView it gives me an error

Could not cast value of type 'UIView' (0x10a8e7f40) to 'Charts.LineChartView' (0x1074f63a0).

How can I achieve that multiple charts in same table view? Thanks in advance.

cellForRowAt indexPath:

let cell = myTableView.dequeueReusableCell(withIdentifier: "chart") as? GraphicCell 
var lineChart:LineChartView = cell?.chart as! LineChartView
lineChart.noDataText = "A" 
return cell! 

the view outlet that I have created in GraphicCell is UIView type

The charts which are shown depends on the user choice. User can select one bar chart and two line chart or only two line chart without bar chart. I do not understand completely how to achieve this. I have added my project to GitHub project link

Upvotes: 2

Views: 2297

Answers (2)

AlexSmet
AlexSmet

Reputation: 2161

You need to create prototype cells for each of the types of charts, which you want to use in your TableView. In each prototype cell you need to put UIView and then change the class of UIView to LineChartView, BarChartView, etc.

Set class for view

Also you need to define your own class for each prototype cell, e.g:

class MyLineChartCell: UITableViewCell {

    @IBOutlet weak var lineChartView: LineChartView!

    func configure (/* ... */) {
        lineChartView.noDataText = "You have no data"
        // ...
    }

} 

.

Use this classes for you prototype cells: Set class for cell

Then in func func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell you could choose which prototype will be used at the moment.

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    if indexPath.row == 0 {
        let cell = tableView.dequeueReusableCell(withIdentifier: "MyLineChartCellIdentifier") as! MyLineChartCell
        cell.configure(/* Data, Colors, etc. */)
    }
    if indexPath.row == 1 {
        let cell = tableView.dequeueReusableCell(withIdentifier: "MyBarChartCellIdentifier") as! MyBarChartCell
        cell.configure(/* Data, Colors, etc. */)
    }
    //... some other types of cells

    return cell
}

Upvotes: 1

DevB2F
DevB2F

Reputation: 5075

The chartView can´t be of type UIView, it has to have the correct type when you declare it. You can have 3 different views inside the tableView cell, like this:

@IBOutlet weak var barChart: BarChartView!
@IBOutlet weak var pieChart: PieChartView!
@IBOutlet weak var lineChart: LineChartView!

and then use the one you need, depending on which graph type you want. If you are using Storyboard, you also need to choose class for each view as BarChartView, LineChartView or PieChartView in the Storyboard.

Upvotes: 1

Related Questions