'How to update the Chart view when button is touched

I have a Chart that shows the Euro price for each candle and m3 candles, so I want to change it when the user touch the button for example if its in EURO to change it to m3

Library used: danielgindi/Charts

so at chartImage below:

  1. the user clicks the button
  2. the data needs to change from EURO to m3 and
  3. the label needs to change from EURO to m3.

enter image description here

The code implemented so far:

extension dashboardViewController: AxisValueFormatter {
   AxisValueFormatter {

func stringForValue(_ value: Double, axis: AxisBase?) -> String {
    print(Int(value))
    return months[Int(value)]
    }
}

@IBAction func euroANDm3(_ sender: Any) {
    euroANDm3.addTarget(self, action: #selector(changeValue), for: .touchUpInside)
}


@objc func changeValue() {
    uniteuro = uniteurotest
    barChart.reloadInputViews()
}


override func viewDidLayoutSubviews() {
    super.viewDidLoad()
    axisFormatDelegate = self
    
    months = ["Jan",
              "Feb",
              "Mar",
              "Apr",
              "May",
              "Jun",
              "Jul",
              "Aug",
              "Sep",
              "Oct",
              "Nov",
              "Dec"
    ]
    uniteuro = [43,34,52,39,36,33,55,39,51,45,38,44]
    uniteurotest = [41,41,41,41,41,41,41,39,51,45,38,44]
    
    setChart(dataPoints: months, values: uniteuro)
}


func setChart(dataPoints: [String], values: [Double]) {
    barChart.noDataText = "NO DATA"
    
    var dataEntries = [BarChartDataEntry]()
    
    for i in 0..<dataPoints.count {
        let dataEntry = BarChartDataEntry(x: Double(i), y: values[i], data: dataPoints)
        
        dataEntries.append(dataEntry)
    }
    
    let chartDataSet = BarChartDataSet(entries: dataEntries, label: "EUR")
    let chartData = BarChartData(dataSet: chartDataSet)
    
    barChart.data = chartData
    
    barChart.xAxis.valueFormatter = axisFormatDelegate
}


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source