Chathuranga
Chathuranga

Reputation: 316

show the value on middle of the bar in IOS chart using swift

my created chart is look like this,

enter image description here

I need to add another data to the bar like this, but didn't find a way to to that

enter image description here

here is my current code to setup the chart,

func setupBarChart  (xAxisChart: [String], _values: [Double], limit: Double) {
    
    barChart.clear()
    
    
    let barColor = UIColor.init(red: 0.15, green: 0.29, blue: 0.45, alpha: 1.0)
    let limitorColour = UIColor(red: 1.00, green: 1.00, blue: 1.00, alpha: 0.2) // 20%
    
    // set data
    var dataEntriesSensor: [BarChartDataEntry] = []
    
    for (index,_data ) in xAxisChart.enumerated() {
        dataEntriesSensor.append(BarChartDataEntry.init(x: Double(index), y: _values[index]))
    }
    
    let dataSet = BarChartDataSet(entries: dataEntriesSensor, label: "")
    dataSet.colors = [barColor]
    dataSet.highlightAlpha = 1.0
    dataSet.drawValuesEnabled = false
    
    
    let data = BarChartData(dataSet: dataSet)
    data.setValueTextColor(.white)
    data.setValueFont(UIFont(name: "SF Pro Text", size: 15) ?? UIFont.systemFont(ofSize: 15))
    data.setValueFormatter(StringValueFormatter(customPrices: priceDataSet))
    
    if priceDataSet.count < 30 {
        data.barWidth = 0.45//Double(priceDataSet.count)
    } else {
        data.barWidth = 0.6 // Set bar width
        
    }
    
    barChart.data = data
    
    // Customize x-axis to show all labels without overcrowding
    let xAxis = barChart.xAxis
    xAxis.labelPosition = .bottom
    xAxis.drawGridLinesEnabled = false
    xAxis.granularity = 1
    xAxis.labelCount = dataEntriesSensor.count
    xAxis.labelRotationAngle = 0
    xAxis.valueFormatter = IndexAxisValueFormatter(values: xAxisChart)
    xAxis.labelTextColor = .white
    
    // Customize y-axis left side
    let leftAxis = barChart.leftAxis
    leftAxis.axisMinimum = 0
    leftAxis.drawGridLinesEnabled = false
    leftAxis.drawAxisLineEnabled = false
    leftAxis.drawLabelsEnabled = false        
    
    // Customize y-axis right side
    let rightAxis = barChart.rightAxis
    rightAxis.drawGridLinesEnabled = false
    rightAxis.drawAxisLineEnabled = false
    rightAxis.drawLabelsEnabled = false
    
    // barchart setting
    barChart.pinchZoomEnabled = false
    barChart.setVisibleXRangeMaximum(7)
    barChart.dragEnabled = true
    barChart.setScaleEnabled(false)
    barChart.doubleTapToZoomEnabled = false
    barChart.pinchZoomEnabled = false
    barChart.legend.enabled = false
   

    // Add a horizontal crossing line for limit
    let yLimitLine = ChartLimitLine(limit: limit, label: "")
    yLimitLine.lineColor = limitorColour
    yLimitLine.valueTextColor = .black
    yLimitLine.valueFont = .systemFont(ofSize: 10)
    yLimitLine.lineDashLengths = [4, 4] // Dashed line
    
    barChart.leftAxis.addLimitLine(yLimitLine)
    
    // Refresh the chart
    barChart.notifyDataSetChanged()
}

these function for get the top value when user click on the bar

// Custom value formatter for strings
class StringValueFormatter: NSObject, IValueFormatter {
let customPrices: [Double]

init(customPrices: [Double]) {
    self.customPrices = customPrices
}


func stringForValue(_ value: Double, entry: ChartDataEntry, dataSetIndex: Int, viewPortHandler: ViewPortHandler?) -> String {
    let index = Int(entry.x)
    return "$\(String(format: "%.2f", customPrices[index]))" // Customize this to display your desired string
}


}

// Custom value formatter for a single value
class CustomSingleValueFormatter: NSObject, IValueFormatter {
let customPrices: [Double]
let index: Int

init(customPrices: [Double], index: Int) {
    self.customPrices = customPrices
    self.index = index
}

this is for add the custom string with value

func stringForValue(_ value: Double, entry: ChartDataEntry, dataSetIndex: Int, viewPortHandler: ViewPortHandler?) -> String {
    if index == Int(entry.x) {
        return "$\(String(format: "%.2f", customPrices[index]))"
    } else {
        return ""
    }
    
}
}

Looking for idea to customise like this.

Upvotes: 0

Views: 32

Answers (0)

Related Questions