6

I know that on MPAndroidChart is possible to handle events on the charts. On its documentation are all well documented.

Nevertheless I am not able to find any documentation about the same events on ios-chart. I know that its creator tells that we should follow the documentaiton of MPAndroidChart for his library also but I am not able to handle that events on Swift 3.0.

I also could not find any examples handle these events for ios-chart library.

so, Is it possible to handle tap event on ios-chart library?

EDIT: According to the feedback of @AdamM I am going to put here the function in which I set the data to the chart.

func enterData(valuesChart: [BarChartDataEntry]){
    let chartDataSet = BarChartDataSet(values: valuesChart, label: "Total Values")

    let charData = BarChartData(dataSets: [chartDataSet])
    barChartView?.data = charData
    barChartView?.animate(xAxisDuration: 2.0, yAxisDuration: 2.0)
}

Thanks in advance!

Francisco Romero
  • 12,787
  • 22
  • 92
  • 167

2 Answers2

6

Yeah, this functionality is built in to the charting library. Make sure your class conforms to the ChartViewDelegate protocol.

Edit full code sample

import UIKit
import Charts

@objc(BarChartFormatter)
public class BarChartFormatter: NSObject, IAxisValueFormatter
{
    var months: [String]! = ["Jan", "Feb", "Mar"]
    public func stringForValue(_ value: Double, axis: AxisBase?) -> String {

        return months[Int(value)]
    }
}


class ViewController: UIViewController,ChartViewDelegate
{
    @IBOutlet var barChart : BarChartView!
     let dataArray = ["Jan", "Feb", "Mar"]
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

        self.title = "Testing"

        let values = [5.2,10.5,15.3]
        setBarChart(values: values)
        barChart.delegate = self
    }

    //MARK: Chart delegate methods
    func chartValueSelected(_ chartView: ChartViewBase, entry: ChartDataEntry, highlight: Highlight) {

        let pos = NSInteger(entry.x)
        print(dataArray[pos])
    }

    func chartValueNothingSelected(_ chartView: ChartViewBase)
    {

    }
    //MARK: Set chart
    func setBarChart(values: [Double])
    {
        var dataEntries: [BarChartDataEntry] = []

        for i in 0..<values.count {
            let dataEntry = BarChartDataEntry(x: Double(i), y: values[i])

            dataEntries.append(dataEntry)
        }
        let dataSet = BarChartDataSet(values: dataEntries, label: "")
        var dataSets : [IChartDataSet] = [IChartDataSet]()
        dataSets.append(dataSet)

        let format:BarChartFormatter = BarChartFormatter()
        let xAxis = barChart.xAxis
        xAxis.granularity = 1.0
        xAxis.labelPosition = .bottom
        xAxis.valueFormatter = format


        let chartData = BarChartData(dataSets: dataSets)
        barChart?.data = chartData

    }
}
AdamM
  • 4,400
  • 5
  • 49
  • 95
  • Hi @AdamM, Thank you for answer but I am not able to make it to work. Where should I add `barChart?.delegate = self`. Anything inside these functions does not seem to be executed after I click on any column of my CombinedChart. – Francisco Romero Nov 02 '16 at 16:58
  • Check edit. If you have any more issues, I can put up complete code sample tomorrow morning when I have my macbook again – AdamM Nov 02 '16 at 21:28
  • I will try tomorrow when I will have my macbook again also and I will reply you if it worked or not. Thank you again :) – Francisco Romero Nov 02 '16 at 21:40
  • Ok I have tried it but it only works for the method `chartValueNothingSelected` when I make double click (select the column and deselect it) but it never enters when I tap the column (1 click) on the method `chartValueSelected`. – Francisco Romero Nov 03 '16 at 08:09
  • Updated my answer will full code sample. Just tested it there, seems to be working fine – AdamM Nov 03 '16 at 09:01
  • Ok, the only thing that I have different with you is that I have a method `setChart()` in which I retrieve the data from a database with Alamofire. When it finish I call another method called `enterData(values)` in which I set the values to the chart but as I can see you also use a function to create the chart. Also, before you updated the answer, I had `@IBOutlet var barChart : BarChartView!` instead of `@IBOutlet var barChart : BarChartView?`. Now I have changed it but I do not know if it could be the problem but I do not think so. But I still cannot get tap event when the chart is clicked. – Francisco Romero Nov 03 '16 at 16:08
  • The ? or ! won't matter too much, that just means it is either wrapped or unwrapped value. Regardless of where you get the data, as long as you have the chart setup and assigned the delegate, it should work. Can you update your question with some source code so I can have a look? – AdamM Nov 03 '16 at 16:14
  • Would it be helpful if I add all my code without how I retrieve the data? I mean, how the JSON comes to my program. For security reasons I cannot provide it online. – Francisco Romero Nov 03 '16 at 16:19
  • Yeah, I don't need to know about how you retrieve the code, I just want to see what you do with the data once you receive it. – AdamM Nov 03 '16 at 16:22
  • I have edited my question. I have omitted the lines about the configuration of the chart like color, text-size, etc because I think it does not have anything related with the problem. – Francisco Romero Nov 03 '16 at 16:38
  • Ok see in that method there, after you have called that method, then call barChartView.delegate = self – AdamM Nov 03 '16 at 16:43
  • So I call it two times? one on viewDidLoad and the other after executing that method from Alamofire? – Francisco Romero Nov 03 '16 at 16:52
  • I have tried it on both places and only one after calling that method but the behaviour is the same. – Francisco Romero Nov 03 '16 at 16:56
  • Finally as the other user suggested the problem was the dataSetIndex. – Francisco Romero Nov 04 '16 at 07:23
  • You only need to call it once, after you have set the chart data, then set the delegate. Have you tried putting a breakpoint in your code? I would need to see more of your source code to see what is wrong. Can you confirm you have setup the chart to allow user interaction? Does the chart highlight when you click it? – AdamM Nov 04 '16 at 09:01
  • Now it works when I select the chart with the code provided by @user2986119 but the problem now is that I do not know how to access to the id of each column. And I cannot find any documentation about this method. – Francisco Romero Nov 04 '16 at 09:02
  • 1
    I just realized my version is out of date, give me 20 mins will update my code base, then update my answer – AdamM Nov 04 '16 at 09:42
  • Updated answer with new code. I did not realize the chart library I was using was so out of date. The above code should work fine now and included code to show you how you would access ID of each column – AdamM Nov 04 '16 at 10:15
  • Thank you very much. Now it is what I wanted :). I will wait to give you the bounty until the last day of the bounty so you can get more rep if the users come to this question. Happy coding :) – Francisco Romero Nov 04 '16 at 12:43
2

For IOS Charts 3. Omit the dataSetIndex!

func chartValueSelected(_ chartView: ChartViewBase, entry: ChartDataEntry, highlight: Highlight) 
{...}
Slarty Bartfast
  • 635
  • 7
  • 13