wuxudong / react-native-charts-wrapper

a react native charts wrapper (support android & iOS)
2.44k stars 657 forks source link

Wrong plot when using dates #988

Closed bearkillerPT closed 5 months ago

bearkillerPT commented 5 months ago

Hey! I'm trying to create a line plot with a few data points. The y values represent power while the x axis represents time (passed in epoche milliseconds).

Expected Behavior

6 data points, one per minute.

Actual Behavior

I can only see 4 points and at least 2 of them are grouped in the same minute.

Screenshots

Data and config

Specifications

bearkillerPT commented 5 months ago

After looking through a bunch of issues it seems that the "correct" way of doing it is by using the since prop (with the epoch time, in milliseconds, of the first point) on the xAxis object and then to subctact this initial date to every point. Something like this:

// Assuming dataArray is ordered in time!
const firstPointTimestamp = dataArray[0]?.timestamp;
const datasetValues = dataArray.map((data, i) => ({
  y: data.y,
  x: data.timestamp - firstPointTimestamp,
}));

// dataSets array
[
  {
    values: datasetValues,
    ...
  }
]

// xAxis object
{
  valueFormatter: "date",
  valueFormatterPattern: "HH:mm",
  since: firstPointTimestamp,
  ...
}

EDIT: The issue that I mentioned is Issue #726. TL:DR It has to do with the type used on Android being a float (on iOS it's a double). I've no idea why this happens tho... the value of the current date in epoch milliseconds is well withing the range of a float from my quick math!