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

Wrong values from pixelForValues #3895

Closed
shukiAnavi opened this issue Mar 11, 2019 · 2 comments
Closed

Wrong values from pixelForValues #3895

shukiAnavi opened this issue Mar 11, 2019 · 2 comments

Comments

@shukiAnavi
Copy link

Hello!

I'm getting weird results while I'm trying to get pixel values for a specific entry that displayed on the chart view.

Convert entry to pixel:
let optionalEntry = dataSet.entryForIndex(13) // yValue = 0.69
guard let entry = optionalEntry else {continue}
let entryPixel = chartView.getTransformer(forAxis: .left).pixelForValues(x: entry.x, y: entry.y)

Draw the pixel on the chartView:
let view = UIView(frame: CGRect(x: entryPixel.x, y: entryPixel.y, width: 18, height: 18))
let circlePath = UIBezierPath(arcCenter: CGPoint(x: 9, y: 9), radius: CGFloat(4.5), startAngle: CGFloat(0), endAngle: CGFloat(Double.pi * 2), clockwise: true)
let shapeLayer = CAShapeLayer()
shapeLayer.path = circlePath.cgPath
shapeLayer.fillColor = UIColor.white.cgColor
shapeLayer.strokeColor = UIColor.blue.cgColor
shapeLayer.lineWidth = 3.0
view.layer.addSublayer(shapeLayer)
view.backgroundColor = UIColor.yellow
self.chartView.addSubview(view)
The subview is drawn in the wrong position:

image

Are there any ideas about what's wrong with the code?

@liuxuan30
Copy link
Member

liuxuan30 commented Mar 21, 2019

I don't see the line chart is wrong. if you are asking the sub view you manually created, that's because your axis coordinate system is not the same. ask this on stack overflow - as this is a UIKit or CoreGraphics topic. Apple has docs that explained pretty well.

short answer: you should use CoreGraphics to draw the shape you want. Not by a sub view. This library has some similar methods did so, like ChartMarker, drawHighlighted, you can refer those

@shukiAnavi
Copy link
Author

The question was about the wrong coordinate values, when I’m adding circles for each entry and tap on one of the circles (that represents entry) I’m getting the right coordinates.
Maybe my question was unclear but I expected that the method ‘entryForIndex’ will return the same coordinate values like as I tapped on an entry.
Anyway, I managed to solve it in another way (tapping on entry to get the real coordinates as I described above and then adding a subview in the right position).

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