Unfortunately, there is no way to set an image instead of a String or Double at the value above the bar.
but yes we can access the value above bar please visit the mentioned link.
https://github.com/danielgindi/Charts/issues/2126
class ViewController: UIViewController {
var chartData = BarChartData()
var months: [String]!
var unitsSold = [Double]()
weak var valueFormatter: IValueFormatter?
@IBOutlet var viewForChart: BarChartView!
override func viewDidLoad() {
super.viewDidLoad()
valueFormatter = self
months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]
unitsSold = [20.0, 4.0, 6.0, 3.0, 12.0, 16.0, 4.0, 18.0, 2.0, 4.0, 5.0, 4.0]
setChart(dataEntryX: months, dataEntryY: unitsSold)
}
func setChart(dataEntryX forX:[String],dataEntryY forY: [Double]) {
viewForChart.noDataText = "You need to provide data for the chart."
var dataEntries:[BarChartDataEntry] = []
for i in 0..<forX.count{
let dataEntry = BarChartDataEntry(x: Double(i), y: Double(forY[i]) , data: months as AnyObject?)
dataEntries.append(dataEntry)
}
let chartDataSet = BarChartDataSet(values: dataEntries, label: "Units Sold")
chartDataSet.setColor(UIColor.brown, alpha: 0.30)
chartData = BarChartData(dataSet: chartDataSet)
chartData.barWidth = 0.75
chartData.setDrawValues(true)
viewForChart.data = chartData
viewForChart.xAxis.valueFormatter = IndexAxisValueFormatter(values: months)
viewForChart.drawBordersEnabled = false
viewForChart.data?.setValueFormatter(valueFormatter)
}
}
add the extension above or beyond the class
extension ViewController: IValueFormatter {
func stringForValue(_ value: Double, entry: ChartDataEntry, dataSetIndex: Int, viewPortHandler: ViewPortHandler?) -> String {
print(value)
//logic to print string at required point or value
switch value {
case 20.0: return "??"
default: return ""
}
}
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…