Charts icon indicating copy to clipboard operation
Charts copied to clipboard

HorizontalBarChartView value labels are absent if axisMinimum is not set to zero

Open ashumilov opened this issue 7 years ago • 17 comments

It seems to be a strange behavior in HorizontalBarCharRenderer. Without setting barChartView.leftAxis.axisMinimum = 0 value labels are not shown. I've investigated a bit and what I've found so far in my case. _contentRect.origin.x is 10. prepareMatrixValuePx() is called with 0.1 for chartXMin, what leads to _matrixValueToPx.tx = -6 and barRect.origin.x = 4 after transformation. And this all fails in drawValues() at if !viewPortHandler.isInBoundsX(rect.origin.x) , because _contentRect.origin.x > rect.origin.x + 1 The check's change to if !viewPortHandler.isInBoundsX(rect.origin.x + rect.size.width) also helps Please advise whether this is a bug or just misusage of the library.

ashumilov avatar Jan 11 '17 12:01 ashumilov

you have to explain 'value labels are not shown.' All labels gone or just one of them? Is this barRect showing in contentRect fully or just part of it? From your saying I just feel it calculates a bound and the label is just out of bound..

liuxuan30 avatar Jan 12 '17 00:01 liuxuan30

All labels are gone, bars are shown fully. Yes, labels are out of bound, but what a reason for it?

   let statsVC = UIViewController()
   let barChartView = HorizontalBarChartView()
   statsVC.view.addSubview(barChartView)
    barChartView.snp.makeConstraints { (make) -> Void in
        make.center.equalToSuperview()
        make.size.equalToSuperview()
    }
   
    var dataEntries: [BarChartDataEntry] = []
    for i in 1...10 {
        let dataEntry = BarChartDataEntry(x: Double(i), y: Double(i))
        dataEntries.append(dataEntry)
    }
    let barChartDataSet = BarChartDataSet(values: dataEntries, label: nil)
    let barChartData = BarChartData(dataSet: barChartDataSet)
    
    barChartView.data = barChartData
    
    barChartView.legend.enabled = false
    barChartView.xAxis.enabled = false
    barChartView.leftAxis.enabled = false
    barChartView.rightAxis.enabled = false
    barChartView.chartDescription?.enabled = false
    barChartView.drawValueAboveBarEnabled = false
    
    // barChartView.leftAxis.axisMinimum = 0

    present(statsVC, animated: false, completion: nil)

With commented // barChartView.leftAxis.axisMinimum = 0: no_labels With uncommented barChartView.leftAxis.axisMinimum = 0: with_labels

ashumilov avatar Jan 12 '17 05:01 ashumilov

put barChartView.data = barChartData at last line and retry. Remember to set chart data after you configure the chart or manually call notifyDataSetChanged(). data setter will call it

liuxuan30 avatar Jan 16 '17 01:01 liuxuan30

Tried with barChartView.data = barChartData on the last line. No change. The same for manual call of notifyDataSetChanged() in my case.

ashumilov avatar Jan 16 '17 07:01 ashumilov

OK, then I need you to reproduce it with ChartsDemo, so I can try as well. Or a demo project. It also will be better if you can debug drawValues() to see where it stops drawing the labels.

liuxuan30 avatar Jan 17 '17 02:01 liuxuan30

Hello, I am facing the same problem. Value labels are not shown unless I set the axisMinimum to 0. I was able to reproduce this with the demo project by thexande/IOS_Charts_Swift_3_demo. I modified it to use horizontal bar chart. Please see the zipped project below.

IOS_Charts_Swift_3_demo.zip

I also noticed another thing; when I change the value range to be big enough, for example 0...500 (Double(arc4random_uniform(1000) + 1) / 2), then the values are shown. But if I use 10 as a seed for randomiser then no values.

Could you please study what could be the reason for this kind of behaviour. Thanks.

vvuorio avatar Jan 23 '17 18:01 vvuorio

Hello,

I am using HorizontalBarChart and facing same issue.

Any update on Showing values on BarChart ?

Thanks.

dhaneshgosai avatar Mar 16 '17 07:03 dhaneshgosai

Same issue here!

rafaellage avatar Apr 26 '17 18:04 rafaellage

capture d ecran 2017-04-27 a 09 10 30

If it can help If someone has an idea

thierryH91200 avatar Apr 27 '17 07:04 thierryH91200

in BarChartRenderer

open override func drawValues(context: CGContext)

.......

                               if !viewPortHandler.isInBoundsY(y) || !viewPortHandler.isInBoundsLeft(x)
                                {
                                    continue
                                }

```.......


in HorizontalBarChartRenderer
    open override func drawValues(context: CGContext)
......
                    if viewPortHandler.isInBoundsX(rect.origin.x)   **<-------- miss ! i have try tis is fine**
                    {
                        continue
                    }
                    
                    if !viewPortHandler.isInBoundsBottom(rect.origin.y)
                    {
                        continue
                    }

and a PR a bit later

thierryH91200 avatar May 16 '17 08:05 thierryH91200

Same issue, I have to use
barChartView.leftAxis.axisMinimum = 0

Schabaani avatar Oct 07 '17 07:10 Schabaani

I have the same issue. Have to use barChartView.leftAxis.axisMinimum = 0

eolianWhisper avatar May 30 '19 15:05 eolianWhisper

I am using scatter chart in 3.2.2, some icons or labels are not shown. leftAxis.axisMinimum = 0 does not work.

Andy1984 avatar Jul 19 '19 06:07 Andy1984

Okay, maybe my problem is different, because you say all are gone, but mine is just 30% are gone.

Andy1984 avatar Jul 19 '19 06:07 Andy1984

For me I had to set both the axis min and max value for the labels to show up.

barChart.leftAxis.axisMinimum = 0
barChart.leftAxis.axisMaximum = 100

rysady avatar Aug 02 '19 19:08 rysady

Any update on this bug? barChart.leftAxis.axisMinimum = 0 barChart.leftAxis.axisMaximum = 100

both are not working in my case

mudassir-abbas-brainx avatar Dec 14 '21 16:12 mudassir-abbas-brainx

I am also having this issue, I really just want to be able to draw a partial line chart & this is the only way I could figure out how.

Setting both values won't draw the line for me chart.leftAxis.axisMinimum = 0 chart.leftAxis.axisMaximum = 100

quintonpryce avatar Aug 17 '22 19:08 quintonpryce