like this in 2.X

bug in 3.X, you limit the max labelCount of Xasix
public var labelCount: Int
{
get
{
return _labelCount
}
set
{
setLabelCount(newValue, force: false);
}
}
so i change labelCount granularity and forceLabelsEnabled, this doesn't work
I need you help, THX
The labels are horribly optimized, The chart will try to redraw all your labels every time you call notifyDataChanged even if your labels are static, hence the 25 max limit. You should try to clone the repository and change the max value from 25 to whatever label count you need in AxisBase.swift. If your labels are static, i would recommend creating a view with the labels and overlay that view on the graph.
label count is set to max 25 all the time, not just 3.x.
You can modify it of course.. but you have to think about do you really need that in your chart. Displaying too many labels also looks horrible.
Thanks for your answer @liuxuan30 @desnyki
In 2.x, label count is set to max 25 that is only in ChartYAxis, so the XAxis's label count is not limited
It is fixed to 25 (and 2) in AxisBase.swift:
/// the number of label entries the axis should have
/// max = 25,
/// min = 2,
/// default = 6,
/// be aware that this number is not fixed and can only be approximated
open var labelCount: Int
{
get
{
return _labelCount
}
set
{
_labelCount = newValue
if _labelCount > 25
{
_labelCount = 25
}
if _labelCount < 2
{
_labelCount = 2
}
forceLabelsEnabled = false
}
}
Unfortunately you cannot create an extension to override this behaviour as _labelCount is fileprivate and extension may not contain stored properties.
I wonder why the code does not use something like this to create a read-only variable:
open var maxLabelCount = 25
open var minLabelCount = 2
open private(set) var labelCount: Int {
didSet {
guard minLabelCount <= maxLabelCount else {
return
}
if labelCount > maxLabelCount {
labelCount = maxLabelCount
} else if labelCount < minLabelCount {
labelCount = minLabelCount
}
}
}
Implementing a read-only variable using a _variableName is in my opinion a legacy pattern from the Objective-C days. Using private(set) is a much more Swifty solution and moving those hardcoded limits to variables makes it customisable for the end user.
As sometimes you really really want more than 25 labels, I would suggest to honor the force-Flag of the setter like so:
open var labelCount: Int
{
get
{
return _labelCount
}
set
{
_labelCount = newValue
// honor the force-Flag to override the max of 25 Labels
if _labelCount > 25 && !forceLabelsEnabled
{
_labelCount = 25
}
if _labelCount < 2
{
_labelCount = 2
}
forceLabelsEnabled = false
}
}
open func setLabelCount(_ count: Int, force: Bool)
{
forceLabelsEnabled = force // Flag is needed in the setter but will be reset ...
self.labelCount = count
forceLabelsEnabled = force // ... so need to set it twice
}
THX @mat2e
I have changed source code like this
open func setLabelCount(_ count: Int, force: Bool)
{
if force {
_labelCount = count
} else {
self.labelCount = count
}
forceLabelsEnabled = force
}
@Archerlly That麓s even better!
@Archerlly Have tried that as well but still the labels are not drawn properly in my case, its still skipping one label after drawing one..that is skipping label on odd positions.
chart_X_Axis.valueFormatter = IndexAxisValueFormatter(values:axisLabels!.reversed())
let labelCount = axisLabels?.count ?? 0
chart_X_Axis.setLabelCount(labelCount, force: false)
// if labelCount > 25 {
// chart_X_Axis.setLabelCount(labelCount, force: true)
// if forced -true, then only have of the last labels are drawn, but the first half is not
// }
chart_X_Axis.granularityEnabled = true
chart_X_Axis.granularity = 1.0
chart_X_Axis.avoidFirstLastClippingEnabled = true
if dataSetCount! > 1 {
let groupSpace = groupSpacePercent
let barSpace = barSpacePercent / Double(dataSetCount!)
let max = 0.0 + (chartData?.groupWidth(groupSpace: groupSpace, barSpace: barSpace))! * Double((axisLabels?.count)!)
chart_X_Axis.axisMinimum = 0.0
chart_X_Axis.axisMaximum = max
horizontalGraphView.groupBars(fromX: 0.0, groupSpace: groupSpace, barSpace: barSpace)
chart_X_Axis.centerAxisLabelsEnabled = true
horizontalGraphView.fitBars = true
}else {
horizontalGraphView.fitBars = true
}
@cuddle-sujeet Is there enough to draw the skipping label?
@Archerlly by enough if u mean space then, there is enough space.. what I have noticed that for label count greater than 25 this drawing breaks else it draws perfectly fine as expected. I have also noticed that When I try to zoom in I can see the skipped labels and they are at the correct position.
@cuddle-sujeet sorry, i don't very clear about your question. My solution is that:
func setLabelCount(_ count: Int, force: Bool), but the count is the real count + 1, barChart is draw from -0.5 to real count + 0.5Hope this can help u
Most helpful comment
THX @mat2e
I have changed source code like this