Charts
Charts copied to clipboard
HorizontalBarChartView value labels are absent if axisMinimum is not set to zero
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.
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..
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
:
With uncommented
barChartView.leftAxis.axisMinimum = 0
:
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
Tried with barChartView.data = barChartData
on the last line. No change. The same for manual call of notifyDataSetChanged()
in my case.
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.
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.
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.
Hello,
I am using HorizontalBarChart and facing same issue.
Any update on Showing values on BarChart ?
Thanks.
Same issue here!

If it can help If someone has an idea
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
Same issue, I have to use
barChartView.leftAxis.axisMinimum = 0
I have the same issue. Have to use barChartView.leftAxis.axisMinimum = 0
I am using scatter chart in 3.2.2, some icons or labels are not shown. leftAxis.axisMinimum = 0 does not work.
Okay, maybe my problem is different, because you say all are gone, but mine is just 30% are gone.
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
Any update on this bug? barChart.leftAxis.axisMinimum = 0 barChart.leftAxis.axisMaximum = 100
both are not working in my case
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