4

I am in mid of developing small practice IOS app using Swift 3. Inside that app, I would like to have a Pie chart. To build a Pie chart, I followed this link and pie chart is getting generated.

Now I want to add one more functionality. When user tap on one of the area of Pie chart like any month, then it should open a new controller and display details of that month only.

I managed to add gesture recognition functionality. But when I tap any portion of the screen, it opens up the new screen. I want to limit that tap functionality to Pie chart section only and that Pie chart month should be captured. The code is

let gestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(self.handleTap))

pieChartView.addGestureRecognizer(gestureRecognizer)

And handle tap code is

func handleTap(gestureRecognizer: UIGestureRecognizer) {
let monthDetail = storyboard?.instantiateViewController(withIdentifier: "MonthTableView") as! MonthDetailsTableViewController
monthDetail = month  //Should be the month value of Tap area
navigationController?.pushViewController(monthDetail, animated: true)

}

Appreciate any help!!

==========================

Updated Code

import UIKit
import Charts
import CoreData

class ExpenseViewController: UIViewController, ChartViewDelegate {

@IBOutlet weak var pieChartView: PieChartView!

    let months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun"]
    var unitsSold = [10.0, 20.0, 10.0, 30.0, 10.0, 10.0]

     override func viewDidLoad() {
        super.viewDidLoad()

        setChart(dataPoints: months, values: unitsSold)

        pieChartView.delegate = self
     }

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

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


        let pieChartDataSet = PieChartDataSet(values: dataEntries, label: "Units Sold")
        let pieChartData = PieChartData(dataSet: pieChartDataSet)

        pieChartView.data = pieChartData

      }

     func chartValueSelected(chartView: ChartViewBase, entry: ChartDataEntry, dataSetIndex: Int, highlight: ChartHighlighter) {
        //print("\(entry.value) in \(months[entry.xIndex])")
        print("Y = \(entry.y) ")
        print("X = \(entry.x) ")
    }

}
NGR
  • 1,230
  • 2
  • 15
  • 44

2 Answers2

5

Check the Touch Events part in your Appcoda tutorial, you find the ChartViewDelegate method chartValueSelected, so you need to use that method instead of UITapGestureRecognizer.

func chartValueSelected(_ chartView: ChartViewBase, entry: ChartDataEntry, highlight: Highlight) {
     print("\(entry.value) in \(yourArray[entry.xIndex])")
}
Nirav D
  • 71,513
  • 12
  • 161
  • 183
  • Hi Nirav. Thanks for a reply. But even after following those steps provided in link, its not calling chartValueSelected function. I wonder why? I extended the class with ChartViewDeleage, added pieChartView.delegate = self, still not working. – NGR Mar 14 '17 at 17:34
  • @NGR Please edit your post with current code and check that delegate is properly set or not. – Nirav D Mar 14 '17 at 17:42
  • 1
    @NGR You are working with Swift 3 and syntax of delegate may changed, so if you have copy pasted method from my answer then try to remove and retype by yourself Xcode will suggest you the correct one. – Nirav D Mar 14 '17 at 18:23
  • Yes. You are right. Modified the method and its get called on tap. Now having trouble in getting the index of Months. – NGR Mar 14 '17 at 18:28
  • 1
    Got it. Using highlight.x, I can fetch the index of month. Thanks for your help. – NGR Mar 14 '17 at 18:57
  • @NGR Welcome mate :) – Nirav D Mar 15 '17 at 03:28
  • Hi Nirav, Any idea about how to hide text which is visible inside pie chart? I want to display only percentage/value numbers and not the text associated with it. – NGR Apr 22 '17 at 04:54
  • @NGR Don't about it, may be you need to ask new question – Nirav D Apr 22 '17 at 05:38
0

In handleTap, use the gesture recognizer's locationInView to figure out what part of the chart (if any) the tap is in, and respond accordingly.

matt
  • 515,959
  • 87
  • 875
  • 1,141