Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to set LabelCount bigger than 25 in HorizatalBarView #2085

Closed
Archerlly opened this issue Jan 17, 2017 · 12 comments
Closed

How to set LabelCount bigger than 25 in HorizatalBarView #2085

Archerlly opened this issue Jan 17, 2017 · 12 comments

Comments

@Archerlly
Copy link

No description provided.

@Archerlly Archerlly changed the title How to set LabelCount bigger How to set LabelCount bigger than 25 in HorizatalBarView Jan 17, 2017
@Archerlly
Copy link
Author

like this in 2.X
image

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

@desnyki
Copy link

desnyki commented Jan 17, 2017

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.

@liuxuan30
Copy link
Member

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.

@Archerlly
Copy link
Author

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

@4np
Copy link
Contributor

4np commented Jan 23, 2017

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.

@mat2e
Copy link

mat2e commented Mar 13, 2017

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
    }

@Archerlly
Copy link
Author

Archerlly commented Mar 13, 2017

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
}

@mat2e
Copy link

mat2e commented Mar 13, 2017

@Archerlly That´s even better!

@cuddle-sujeet
Copy link

@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
    }

@Archerlly
Copy link
Author

@cuddle-sujeet Is there enough to draw the skipping label?

@cuddle-sujeet
Copy link

@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.

@Archerlly
Copy link
Author

@cuddle-sujeet sorry, i don't very clear about your question. My solution is that:

  1. change the source code
  2. use func setLabelCount(_ count: Int, force: Bool), but the count is the real count + 1, barChart is draw from -0.5 to real count + 0.5

Hope this can help u

igzrobertoestrada added a commit to igzrobertoestrada/Charts that referenced this issue Oct 13, 2017
This change adds the ability to users to override the imposed limitation of at least 2 labels per axis and at most 25 labels per axis at their own risk. By default these customizable limits are hardcoded to the previous limits, at least 2 labels, at most 25.
philipengberg pushed a commit to tonsser/Charts that referenced this issue Jan 19, 2018
…hartsOrg#2894)

* Give the users customizable axis label limits (Fixes ChartsOrg#2085)

This change adds the ability to users to override the imposed limitation of at least 2 labels per axis and at most 25 labels per axis at their own risk. By default these customizable limits are hardcoded to the previous limits, at least 2 labels, at most 25.

* Type inference was enough to declare 'axisMinLabels' and 'axisMaxLabels' properties
FreddyZeng added a commit to FreddyZeng/Charts that referenced this issue Jan 20, 2018
* 'master' of https://github.com/danielgindi/Charts: (34 commits)
  Fixed X-Axis Labels Not Showing (ChartsOrg#3154) (ChartsOrg#3174)
  fix programatical unhighlighting for BarCharView (ChartsOrg#3159)
  Give the users customizable axis label limits (Fixes ChartsOrg#2085) (ChartsOrg#2894)
  bump pod version
  chart views now use open legend renderer property instead of internal one (ChartsOrg#3149)
  Fix axis label disappear when zooming in deep enough (ChartsOrg#3132)
  added DataApproximator+N extension (ChartsOrg#2848)
  Minor cleanup to Highlighter types (ChartsOrg#3003)
  Refactored ChartUtils method into CGPoint extension (ChartsOrg#3087)
  Update ViewPortHandler.swift (ChartsOrg#3143)
  add option to build demo projects unit tests on iOS (ChartsOrg#3121)
  Replaced relevant `ChartUtils` methods with `Double` extensions (ChartsOrg#2994)
  Update 4.0.0 with master (ChartsOrg#3135)
  Removed redundant ivars in BarLineChartViewBase (ChartsOrg#3043)
  fix ChartsOrg#1830. credit from ChartsOrg#2049 (ChartsOrg#2874)
  Makes ChartsDemo compiling again (ChartsOrg#3117)
  Fixed using wrong axis (Issue ChartsOrg#2257)
  Removed methods and properties deprecated in 1.0 (ChartsOrg#2996)
  for ChartsOrg#3061 revert animationUpdate() and animationEnd() not trigger crash if subclass does nothing
  The backing var is not necessary. (ChartsOrg#3000)
  ...

# Conflicts:
#	Source/Charts/Data/Implementations/Standard/LineChartDataSet.swift
#	Source/Charts/Highlight/BarHighlighter.swift
#	Source/Charts/Renderers/BarChartRenderer.swift
kzyryanov pushed a commit to fishbrain/Charts that referenced this issue Feb 20, 2018
…hartsOrg#2894)

* Give the users customizable axis label limits (Fixes ChartsOrg#2085)

This change adds the ability to users to override the imposed limitation of at least 2 labels per axis and at most 25 labels per axis at their own risk. By default these customizable limits are hardcoded to the previous limits, at least 2 labels, at most 25.

* Type inference was enough to declare 'axisMinLabels' and 'axisMaxLabels' properties
kalmurzayev pushed a commit to kalmurzayev/Charts that referenced this issue Feb 26, 2018
…hartsOrg#2894)

* Give the users customizable axis label limits (Fixes ChartsOrg#2085)

This change adds the ability to users to override the imposed limitation of at least 2 labels per axis and at most 25 labels per axis at their own risk. By default these customizable limits are hardcoded to the previous limits, at least 2 labels, at most 25.

* Type inference was enough to declare 'axisMinLabels' and 'axisMaxLabels' properties
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants