skip to Main Content

I have to show [0, 25, 50, 75, 100] with dollar sign in Charts using swift iOS

enter image description here

 var xValue = [25.0, 50.0]
var dollarValue = ["$25", "$50", "$75", "$100"]

Added in view didLoad

  rightAxis.valueFormatter = IndexAxisValueFormatter(values:     dollarValue)
 func updateCharts() {
    var chartDataEntry = [BarChartDataEntry]()
    let spaceForBar =  10.0;
    for i in 0..<xValue.count {
        let value = BarChartDataEntry(x: Double(i)  * spaceForBar, y: xValue[i])
        chartDataEntry.append(value)
    }

    let chartDataSet = BarChartDataSet(entries: chartDataEntry, label: "10x In-Store ")
    chartDataSet.colors = ChartColorTemplates.material()

    let chartMain = BarChartData(dataSet: chartDataSet)
    chartMain.barWidth = 5.0
    
    horizontalBarChartContainnerView.animate(yAxisDuration: 0.5)
    horizontalBarChartContainnerView.data = chartMain
}

I wanted to show 0 $25 $50 $75 $100 instead of 0 10 20 30 40 50

2

Answers


  1. Chosen as BEST ANSWER
    This code is working fine
    
    public class CustomAxisValueFormatter: AxisValueFormatter {
    var labels: [String] = []
    public func stringForValue(_ value: Double, axis: AxisBase?) -> String {
        let count = self.labels.count
        guard let axis = axis, count > 0 else {
            return ""
        }
        let factor = axis.axisMaximum / Double(count)
        let index = Int((value / factor).rounded())
        if index >= 0 && index < count {
            return self.labels[index]
        }
        return ""
    }
     }
     
    

    Add inside of viewDidLoad()

     rightAxis.axisMaximum = 4
     let customAxisValueFormatter  = CustomAxisValueFormatter()
     customAxisValueFormatter.labels =  ["$25", "$80", "$75", "$100"]
    

  2. You need to create a custom formatter

    public class CustomAxisValueFormatter: NSObject, AxisValueFormatter {
        public func stringForValue(_ value: Double, axis: AxisBase?) -> String {
            return "$(Int(value))"
        } }
    

    and set the axis value formatter to your custom formatter

    xAxis.valueFormatter = CustomAxisValueFormatter()
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search