Sturnment Sturnment - 2 months ago 17
Swift Question

How to Show Another ViewController by ChartValueSelected from using IOSChart?

I'm trying to develop a simple active chart. My intention was to have the specific bar selected in the chart to show another view. The main chart was intended to be a dashboard style, so for in-depth discovery or details, user may select the specific bar to learn more [supposedly will show the specific viewcontroller].

func chartValueSelected(chartView: ChartViewBase, entry: ChartDataEntry, dataSetIndex: Int, highlight: ChartHighlight) {
println("\(entry.value) in \(months[entry.xIndex])")

var storyboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)

var vc: NewViewController = storyboard.instantiateViewControllerWithIdentifier("newView") as! NewViewController

self.presentViewController(vc, animated: true, completion: nil)

}


For each month will have their own views because I am going to put another charts in that specific view. From what I understand, for each month will have the specific function command; so I think of using IF ELSE inside the function but I totally have no clue how to start. I'm also not sure if there are another simpler way to achieve this, as I'm still learning.

I'm following the tutorial for using iOSCharts http://www.appcoda.com/ios-charts-api-tutorial/
and custom show viewcontroller from https://www.youtube.com/watch?v=0I4HX6NDDak.

Answer
func chartValueSelected(chartView: ChartViewBase, entry: ChartDataEntry, dataSetIndex: Int, highlight: ChartHighlight) {
    print("\(entry.value) in \(months[entry.xIndex])")

    let strSelectedMonth : String = months[entry.xIndex] as! String

    if strSelectedMonth == "April"
    {
        print(strSelectedMonth)

        // Now push another view controller and display bar chart for April month
    }     
}

Dont forget to implement barChartView.delegate = self and ChartViewDelegate

This solution is best use for showing year, months, days and hours with different view controller and catered data on chart.

Comments