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

I can not set one label on y axis. #3630

Closed
pridra opened this issue Sep 9, 2018 · 1 comment
Closed

I can not set one label on y axis. #3630

pridra opened this issue Sep 9, 2018 · 1 comment

Comments

@pridra
Copy link

pridra commented Sep 9, 2018

Hi, first of all thanks for this awesome library.
We are using CombinedChartView.

What did you do?

I set 1 to the setLabelCount of the YAxis class.

What did you expect to happen?

I can not set one label on y axis.

What happened instead?

I set 0 or 1 to setLabelCount or labelCount, but
they are converted to 2, because
the bottom line (base line) and the top line (ceiling line) are always drawn.

Corresponding code

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
    @objc open var labelCount: Int
    {
        get
        {
            return _labelCount
        }
        set
        {
            _labelCount = newValue
            
            if _labelCount > 25
            {
                _labelCount = 25
            }
            if _labelCount < 2
            {
                _labelCount = 2
            }
            
            forceLabelsEnabled = false
        }
    }

AxisRendererBase.swift

    /// Sets up the axis values. Computes the desired number of labels between the two given extremes.
    @objc open func computeAxisValues(min: Double, max: Double)
    {
        guard let axis = self.axis else { return }
        
        let yMin = min
        let yMax = max
        
        let labelCount = axis.labelCount
        let range = abs(yMax - yMin)
        
        if labelCount == 0 || range <= 0 || range.isInfinite
        {
            axis.entries = [Double]()
            axis.centeredEntries = [Double]()
            return
        }

YAxisRenderer.swift

    /// draws the y-labels on the specified x-position
    @objc internal func drawYLabels(
        context: CGContext,
        fixedPosition: CGFloat,
        positions: [CGPoint],
        offset: CGFloat,
        textAlign: NSTextAlignment)
    {
        guard
            let yAxis = self.axis as? YAxis
            else { return }
        
        let labelFont = yAxis.labelFont
        let labelTextColor = yAxis.labelTextColor
        
        let from = yAxis.isDrawBottomYLabelEntryEnabled ? 0 : 1
        let to = yAxis.isDrawTopYLabelEntryEnabled ? yAxis.entryCount : (yAxis.entryCount - 1)
        
        for i in stride(from: from, to: to, by: 1)
        {

We only need bottom line and bottom label.

Charts Environment

Charts version/Branch/Commit Number:
3.0.4
Xcode version:
9.1
Swift version:
4.0.2
macOS version running Xcode:
10.13.6

Thanks again!

@liuxuan30
Copy link
Member

have you tried forceLabelsEnabled? Otherwise, override computeAxisValues and make axis.entries contains only one data

        // force label count
        if axis.isForceLabelsEnabled
        {
            let step = Double(range) / Double(labelCount - 1)
            
            // Ensure stops contains at least n elements.
            axis.entries.removeAll(keepingCapacity: true)
            axis.entries.reserveCapacity(labelCount)
            
            var v = yMin
            
            for _ in 0 ..< labelCount
            {
                axis.entries.append(v)
                v += step
            }
            
            n = labelCount
        }

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

2 participants