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

Values on X axis issue #3304

Closed
milosmilakovic-oviva opened this issue Feb 28, 2018 · 4 comments
Closed

Values on X axis issue #3304

milosmilakovic-oviva opened this issue Feb 28, 2018 · 4 comments

Comments

@milosmilakovic-oviva
Copy link

milosmilakovic-oviva commented Feb 28, 2018

I use ScatterChart. On the x axis labels should be hours (00:00, 06:00, 12:00, 18:00). But, If I put minimum = 0 and maximum = 1440 (minutes in day) and and label count to 5 I get graph like this:

graph

If I set labelCount to 4, labels on xAxis are: 00:00, 06:40 (400), 13:20 (800), 20:00 (1200).
If labelCount = 3 axis are 00:00, 08:20 (500), 16:40 (1000)

This is my setup:
vwChart.xAxis.axisMinimum = 0 vwChart.xAxis.axisMaximum = 1440 vwChart.setVisibleXRangeMaximum(1440) vwChart.setVisibleXRangeMinimum(1440) vwChart.xAxis.labelCount = 4 // 5, 6, 3, 2

Is there a way to specify interval between labels, so I can display each 6h?

@liuxuan30
Copy link
Member

check computeAxisValues(), where it generate the x axis labels. You can override it with your own

@JCMcLovin
Copy link
Contributor

@liuxuan30 - does the demo project have an example that overrides computeAxisValues()? I can't seem to find one that does.

@liuxuan30
Copy link
Member

no, it's default one. The logic is simple, just do math or none

@tianli91
Copy link

tianli91 commented Sep 18, 2019

So when we need to manually override the computeAxisValues() for the XAxisRenderer. Here is an example @JCMcLovin :

class NonUniformXAxisRenderer: XAxisRenderer {
    override func computeAxisValues(min: Double, max: Double) {
        super.computeAxisValues(min: min, max: max)
 
        guard let axis = self.axis else { return }
         
        let labelCount = axis.labelCount
        let interval = 86400000.0
         
       // Ensure stops contains at least n elements.
        axis.entries.removeAll(keepingCapacity: true)
        axis.entries.reserveCapacity(labelCount)
        axis.entries.append(min)
        var v = min + 26423301
        
        for _ in 0 ..< labelCount - 2
        {
            axis.entries.append(v)
            v += interval
        }
        axis.entries.append(max)
         
    }
}
let transformer = chartView.getTransformer(forAxis:.left)
let viewPortHandler = chartView.xAxisRenderer.viewPortHandler
chartView.xAxisRenderer = NonUniformXAxisRenderer(viewPortHandler: viewPortHandler, xAxis: chartView.xAxis, transformer: transformer)

Take note you cannot use your own transformer and viewPortHandler instance.
This matrix transforms the dataset x values to the pixels on screen. the default transformer re-calculate the mapping matrix when viewPort changes

ChartXAxis Value Not Displayed
So first check xAxis.entries in computeAxisValues(), and then drawLabels()
You can try debug xAxis.drawLabels() at if viewPortHandler.isInBoundsX(position.x) to see if it meets the condition.

@liuxuan30 the logic is simple but it did take me hours to figure out why the axis label not showing. I used my own transformer and viewPortHandler instance and read all the related source code to know why it does not work.

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

4 participants