1

Image with following Code with SetLabel Count

with the following code

barchart.xAxis.labelTextColor = UIColor.white

barchart.xAxis.labelPosition = .topInside

barChart.xAxis.setLabelCount(7, force: true)

Issue: x axis grid get destructed Image with following Code without setLabelCount

barchart.xAxis.labelTextColor = UIColor.white

barchart.xAxis.labelPosition = .topInside

Issue: Right side Label gets hidden

Solution Required: Need Following to be fixed:

*Grid should be aligned

*Label should be viewed

my code is :

barChart.legend.enabled = false

barChart.xAxis.labelTextColor = UIColor.white
barChart.xAxis.labelPosition = .topInside

barChart.leftAxis.drawAxisLineEnabled = true
barChart.leftAxis.enabled = true
barChart.leftAxis.drawGridLinesEnabled = false
barChart.rightAxis.drawAxisLineEnabled = false

barChart.rightAxis.drawGridLinesEnabled = true
barChart.rightAxis.drawLabelsEnabled = true
barChart.rightAxis.labelFont =  NSUIFont.systemFont(ofSize: 13.0)
barChart.xAxis.labelFont = NSUIFont.systemFont(ofSize: 16.0)
barChart.rightAxis.labelTextColor =  UIColor.white

barChart.leftAxis.axisMinimum = 0.0
barChart.rightAxis.axisMinimum = 0.0
barChart.rightAxis.drawLabelsEnabled = false
    var dataEntries: [ChartDataEntry] = []

    for i in 0..<dataPoints.count {
        let dataEntry = BarChartDataEntry(x: Double(i),yValues: [values[i]])
        dataEntries.append(dataEntry)
    }
   let chartDataSet = BarChartDataSet(values: dataEntries, label: "HB2")
    let chartData = BarChartData(dataSet: chartDataSet)
    chartData.setValueFormatter(DefaultValueFormatter(formatter: formatter))
    barChart.xAxis.valueFormatter = IndexAxisValueFormatter(values:dataPoints)
    barChart.data = chartData
Marios
  • 509
  • 1
  • 7
  • 23

2 Answers2

0

First of all label position should be barchart.xAxis.labelPosition = .top and I do not understand why grids are not aligned but if you use following code it should be right aligned it worked for me. chartView.legend.enabled = false

   // chartView.xAxis.labelTextColor = UIColor.white
    chartView.xAxis.labelPosition = .top

    chartView.leftAxis.drawAxisLineEnabled = true
    chartView.leftAxis.enabled = true
    chartView.leftAxis.drawGridLinesEnabled = false
    chartView.rightAxis.drawAxisLineEnabled = false

    chartView.rightAxis.drawGridLinesEnabled = true
    chartView.rightAxis.drawLabelsEnabled = true
    chartView.rightAxis.labelFont =  NSUIFont.systemFont(ofSize: 13.0)
    chartView.xAxis.labelFont = NSUIFont.systemFont(ofSize: 16.0)
    chartView.rightAxis.labelTextColor =  UIColor.white

    chartView.leftAxis.axisMinimum = 0.0
    chartView.rightAxis.axisMinimum = 0.0
    chartView.rightAxis.drawLabelsEnabled = false
    var dataEntries: [ChartDataEntry] = []

    for i in 0..<unitsSold.count {
        let dataEntry = BarChartDataEntry(x: Double(i),yValues: [unitsSold[i]])
        dataEntries.append(dataEntry)
    }
    let chartDataSet = BarChartDataSet(values: dataEntries, label: "HB2")
    let chartData = BarChartData(dataSet: chartDataSet)
    chartView.xAxis.valueFormatter = IndexAxisValueFormatter(values: months)
    chartView.data = chartData

    chartView.animate(yAxisDuration: 2.5)

I have removed your first formatter I do not understand why you used it but it gave me following chart.

Horizontal bar chart

atalayasa
  • 3,310
  • 25
  • 42
  • thanks for the response.. one more issue that right side label should be inside the graph.and that grid alignment is still have an issue. – Marios May 15 '18 at 12:48
  • Thanks for the answer, see my graph that grid lines are boxed with horizontal graph, and right axis label are inside the grid, your solution its outside the grid and horizontal graph lines shows middle of the graph – Marios May 16 '18 at 10:05
  • @Marios actually I could not understand what is your problem and what do you need for alignment if you have any picture of what you need it could be helpful. – atalayasa May 21 '18 at 06:04
  • @atalayasa, could you please tell which property is for showing value at the top bar ? I used your code and tried, but all good other than those labels on top of the Bar. – yogi Oct 26 '20 at 09:07
  • @yogi it's been very long time since i do not deal with charts. However when you add `BarChartDataEntry(x: Double(i),yValues: [unitsSold[i]])` `yValues` needs to be worked. Are you sure `yValues` is not empty? – atalayasa Oct 27 '20 at 10:57
  • @atalayasa, Hey I got it, chartData.setDrawValues(true) worked for me with current code i have implemented. But thanks for post overall. – yogi Oct 27 '20 at 12:36
0

as per my exprience you need to add lableCount in your chart configuration like below so that it will calculate proper xAxis and show it on your chart

horiBarChartView.xAxis.labelCount = <yourXAxisArray>.count 

If we are not setting this count some time it will not able to generate proper xAxis position on chart.

So try this once and let me know its working or not.

CodeChanger
  • 7,953
  • 5
  • 49
  • 80
  • This solved only one problem boss.. grid line make the problem in alignment. – Marios May 15 '18 at 12:58
  • its totally depend on view size and we can't control it and if you check it properly that last line of chart and chart border messing this and that not make any issue as its default. – CodeChanger May 16 '18 at 05:58