Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to handle tap events on a column of BarChart?

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!

like image 986
Francisco Romero Avatar asked Oct 27 '16 12:10

Francisco Romero


People also ask

Is a bar chart a graph?

A bar chart or bar graph is a chart or graph that presents categorical data with rectangular bars with heights or lengths proportional to the values that they represent. The bars can be plotted vertically or horizontally. A vertical bar chart is sometimes called a column chart.

How do I change the color of a bar graph in Google?

To change the colors assigned to data series in a specific chart: Select that chart, then on the right, open the STYLE tab. In the Color by section, select Series order, Bar order, or Slice order, depending on the type of chart. Click a color box to set the color for each series.


2 Answers

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

    }
}
like image 165
AdamM Avatar answered Sep 28 '22 08:09

AdamM


For IOS Charts 3. Omit the dataSetIndex!

func chartValueSelected(_ chartView: ChartViewBase, entry: ChartDataEntry, highlight: Highlight) 
{...}
like image 43
Slarty Bartfast Avatar answered Sep 28 '22 08:09

Slarty Bartfast