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

Calculating space (in px) between two points #548

Closed
jibax opened this issue Nov 12, 2015 · 14 comments
Closed

Calculating space (in px) between two points #548

jibax opened this issue Nov 12, 2015 · 14 comments

Comments

@jibax
Copy link

jibax commented Nov 12, 2015

Hi,

I would love to display the value of the chart like below (instead of value). The thing is, I don't know how to deal with the coordinate of each point (xAxis). I searched a lot to get the space between two points (spaceBetweenLabel doesn't fit because it isn't a px value) in order to make a calculation. The label Value change when it goes over a point.

All I have is :

  • contentOffset of the UIScrollView
  • Number of entries

Is there a way to know the coordinate of each point, based of the UIView where the LineChart is displayed? Which value should I need to have to perform this ?

The red square is my lineChartView, the green one an UIScrollView where the user can scroll to display the chart.

Thanks a lot
charts

@liuxuan30
Copy link
Member

Here's the code I took from internal func drawLabels(context context: CGContext, pos: CGFloat, anchor: CGPoint) from x axis renderer:

        let valueToPixelMatrix = transformer.valueToPixelMatrix
        for (var i = _minX, maxX = min(_maxX + 1, _xAxis.values.count); i < maxX; i += _xAxis.axisLabelModulus)
        {
            let label = _xAxis.values[i]
            if (label == nil)
            {
                continue
            }

            position.x = CGFloat(i)
            position.y = 0.0
            position = CGPointApplyAffineTransform(position, valueToPixelMatrix)

We use valueToPixelMatrix and other functions in ChartViewPortHandler to convert data value to px. So you should be able to calculate the space using these functions.

@liuxuan30
Copy link
Member

But be careful, if you also enable the zooming feature, because the spaces can be amplified.

@jibax
Copy link
Author

jibax commented Nov 16, 2015

Thank you @liuxuan30 , I finally managed this. However, is there a way to have the "position" variable of xAxisRenderer from outside the library (like a getter or something else) ? It would be the best way to have to coordinate of all the CGPoint.

@liuxuan30
Copy link
Member

@jibax I'm afraid there is no such way to get it right now. Since this is not everyone asked to have. I do need the coordinate as well, but I write a delegate to get it. So if people are keeping asking for this, it can be implemented. But for now you have to write your own code to get it.

@jibax
Copy link
Author

jibax commented Nov 17, 2015

Oh ok I get it. Thanks for the informations

@Julien-Abulfeda
Copy link

Hello @liuxuan30 and @jibax, I found this topic closed but I need the same coordinate as well for my app. I don’t want to ask you to commit but would it be possible to share the delegate you wrote here ? Thank you very much

@liuxuan30
Copy link
Member

It depends on if you need the highlighted coordinates or all the points? There are several ways to do it, but needs you add some new functions and override some methods.

For me I only need the highlighted one, so you can write a function called internal func getHighlightPoint, and take a look at how we get the highlighted point in drawHighlighted. You can also add properties to store the points after the library calculates it

The point is to know when you get the correct coordinates no matter in drawHighlighted or drawDataSet

I add a delegate called optional func chartDidMovedToPoint(chartView: ChartViewBase, point: CGPoint) so when I get the highlight point I will call this to do something.

@jibax
Copy link
Author

jibax commented Nov 19, 2015

Hi @liuxuan30 I did a delegate and it works pretty well. But the thing is I modify a Pod file ( ChartXAxisRenderer ) , it means it is gonna be delete on the next update. What should I do to keep this protocol for the next update? (I'm not used to modify pod) Thank you

@liuxuan30
Copy link
Member

I am no expert of pod, @petester42 can help on this.
But looks like you have to sub class rather than change the library?

@pmairoldi
Copy link
Collaborator

You should never change the library cause it will overwrite your changes when you run pod install. Subclass is the way to go. Otherwise if it makes sense to be in the library then create a pull request with your work.

@jibax
Copy link
Author

jibax commented Nov 20, 2015

Hi @petester42 Thank you for your answer. Problem is , I cannot subclass ChartXAxisRenderer because I got the following error "Multiple Inheritance from classes 'UIViewController' and 'ChartXAxisRender' which is pretty bad for me. So the only way to do this is to create a pull request right ?

@pmairoldi
Copy link
Collaborator

No. You can make a separate object that does only the chart axis rendering and then use that in your view controller.

@jibax
Copy link
Author

jibax commented Nov 20, 2015

The thing is, I need to call (or even better override) the drawlabels function which has an internal accessibilty : so I'm not able to perform myChartAxisRenderingObject.drawlabels in my View controller. How can I do ?

@liuxuan30
Copy link
Member

drawLabel is not called outside, it is called in chart view's drawRect. You can subclass the renderer, override drawLabels, and that's it. You don't have to call it.

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