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!
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
}
}