I am new to charts, I have to implement horizontal bar charts,It always have two horizontal bar and 3 x-axis value and four y-axis value. I am able to draw horizontal bar, but not able to add labels for x-axis and y-axis.
@IBOutlet weak var horizontalBarChartContainnerView: HorizontalBarChartView!
var xValueValue = ["10x Bonus", "20x Bonus"]
var xValue = [25.0, 50.0]
var dollarValue = ["$25", "$80", "$75", "$100"]
override func viewDidLoad() {
super.viewDidLoad()
horizontalBarChartContainnerView.drawGridBackgroundEnabled = true
horizontalBarChartContainnerView.gridBackgroundColor = hexStringToUIColor(hex: "#0C2C43")//.systemBlue
horizontalBarChartContainnerView.xAxis.valueFormatter = IndexAxisValueFormatter(values: dollarValue)
print(horizontalBarChartContainnerView.rightAxis.labelCount)
print(horizontalBarChartContainnerView.xAxis.labelCount)
let xAxis = horizontalBarChartContainnerView.xAxis
xAxis.labelPosition = .bottom
xAxis.drawAxisLineEnabled = false
xAxis.drawGridLinesEnabled = false
xAxis.granularityEnabled = true
xAxis.granularity = 1.0
let leftAxis = horizontalBarChartContainnerView.leftAxis;
leftAxis.drawAxisLineEnabled = true;
leftAxis.drawGridLinesEnabled = true;
leftAxis.axisMinimum = 0; // this replaces startAtZero = YES
let rightAxis = horizontalBarChartContainnerView.rightAxis
rightAxis.enabled = true;
rightAxis.drawAxisLineEnabled = true;
rightAxis.drawGridLinesEnabled = false;
rightAxis.granularity = 1
rightAxis.axisMinimum = 0; // this replaces startAtZero = YES
let l = horizontalBarChartContainnerView.legend
l.enabled = false
horizontalBarChartContainnerView.fitBars = true;
updateCharts()
}
func updateCharts() {
var chartDataEntry = [BarChartDataEntry]()
let spaceForBar = 7.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 = [hexStringToUIColor(hex: "#ffffff"), hexStringToUIColor(hex: "#D6D3C4")]
let chartMain = BarChartData(dataSet: chartDataSet)
chartMain.barWidth = 5.0
horizontalBarChartContainnerView.data = chartMain
}
Note:- The horizontal bar should be like below image and at 10X label horizontal bar can we show UIImage(custom image)