question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. ItĀ collects links to all the places you might be looking at while hunting down a tough bug.

And, if youā€™re still stuck at the end, weā€™re happy to hop on a call to see how we can help out.

Get the Y value for a given X value

See original GitHub issue

What is the problem?

Hope Iā€™m not being too active on these issues right now šŸ¤ž This is more like an idea / feature: Getting the Y value for a given X value. When the XAxis is linear (e.g time), it would be nice to have a function like x() and y() but not for the coordinates, but for the values. For example, if you have the following input:

[
 { value: 10, timestamp: new Date(2018, 06, 25) },
 // One day missing
 { value: 20, timestamp: new Date(2018, 06, 27) }
]

The graph is now going to be painted as if the values were like so:

[
 { value: 10, timestamp: new Date(2018, 06, 25) },
 { value: 15, timestamp: new Date(2018, 06, 26) },
 { value: 20, timestamp: new Date(2018, 06, 27) }
]

So, by providing the x value, in this case time, it should be possible to find out not only what coordinate the graph has plotted the Y value on, but what the Y value actually is ā€” by providing yValueForX(new Date(2018, 06, 26)) we should be able to get 15 back, as thatā€™s already plotted on the graph?

TL;DR: It should be possible to retrieve the ā€œin-betweenā€ values, given an X-value within the domain for the graph

Issue Analytics

  • State:open
  • Created 5 years ago
  • Reactions:3
  • Comments:9 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
JesperLeklandcommented, Jun 28, 2018

I hear you. Iā€™ll have to look into if d3.js have that option anywhere, since thatā€™s what Iā€™m using to generate the paths.

1reaction
wrsullivcommented, Jan 12, 2021

For anyone having this issue, hereā€™s my solution:

Step I: Get the SVG String

First, I bring the SVG string generated internally by area-chart.js into my React componentā€™s state. I do this by mounting this extractor component as a child of the chart:

const LineExtractor = ({ line }) => {
  this.setState({ line });
  return null;
};

The line prop comes from chart.js, which passes extraProps to all chart child components. You could also use this prop directly if youā€™re building a child component.

Step II: Iterate the SVG to find the Y Value

Next, I get the Y value from the extracted SVG. To do that, I found a browser script which uses binary search (instead of the bisect function): https://www.freecodecamp.org/news/how-to-build-react-native-charts-with-dynamic-tooltips-64aefc550c95/

Unfortunately, this script requires the getPointAtLength, and getTotalLength browser API functions. After some research, I found a pure JS alternative called svg-path-properties: https://www.npmjs.com/package/svg-path-properties

The final script to calculate the Y-Value in React Native then looks like this:

//  REFERENCE:  This code is an adaptation of the browser-based solution found here (https://www.freecodecamp.org/news/how-to-build-react-native-charts-with-dynamic-tooltips-64aefc550c95/)

import * as path from 'svg-path-properties';

function findY(p, x) {
  const properties = path.svgPathProperties(p);
  var pathLength = properties.getTotalLength()
  var start = 0
  var end = pathLength
  var target = (start + end) / 2

  // Ensure that x is within the range of the path
  x = Math.max(x, properties.getPointAtLength(0).x)
  x = Math.min(x, properties.getPointAtLength(pathLength).x)

  // Walk along the path using binary search 
  // to locate the point with the supplied x value
  while (target >= start && target <= pathLength) {
    var pos = properties.getPointAtLength(target)

    // use a threshold instead of strict equality 
    // to handle javascript floating point precision
    if (Math.abs(pos.x - x) < 0.001) {
      return pos.y
    } else if (pos.x > x) {
      end = target
    } else {
      start = target
    }
    target = (start + end) / 2
  }
}

Within my React component, I get the Y-Value with:

findY(this.state.line, x);

It should also possible to do this with bisect by first iterating through the SVG and creating an array of values in the x domain.

Itā€™s a little messy but it works for now =)

Read more comments on GitHub >

github_iconTop Results From Across the Web

Determine the y value based on the given x value - YouTube
Determine a y value based on a given x value.
Read more >
Find x and y values from a graph - YouTube
Find 100's more videos linked to the Australia Senior Maths Curriculum at http://mathsvideosaustralia.com/There are videos for:Queensland:Ā ...
Read more >
Tutorial Video Finding x values given y value with Ti84
Tutorial Video Finding x values given y value with Ti84. 53K views Ā· 5 years ago ...more. Peter Gorkiewicz. 105. Subscribe. 373. Share....
Read more >
2.4: Find y given x and the Equation of a Line - Math LibreTexts
Find the value of y when x is 5. Solution. Just replace the variable x with the number 5 in the equation and...
Read more >
How to find value of Y if i know X value from grap - MathWorks
I want to know the Y value from my graph example X = 10 to 11, till 10 to 11 in y axis...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found