react-native-chart-kit icon indicating copy to clipboard operation
react-native-chart-kit copied to clipboard

setting min & max ranges for line charts

Open ikoazoulay opened this issue 4 years ago • 18 comments

Is it possible to set the min & max which are not from the data itself?

Sat my data is [20,30] and I would still like to see the Y axis from 10-80. Is it possible?

If not I believe it would be great to add it.

Would be happy to add it myself

ikoazoulay avatar Dec 03 '20 15:12 ikoazoulay

As a hack i set a 2nd dataset with desired [min, max] and render a transparent line. Not ideal but worked as a temp fix for me.

GeoffAbbott avatar Feb 22 '21 18:02 GeoffAbbott

Thanks !

On Mon, Feb 22, 2021, 20:04 Geoff Abbott [email protected] wrote:

As a hack i set a 2nd dataset with desired [min, max] and render a transparent line. Not ideal but worked as a temp fix for me.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/indiespirit/react-native-chart-kit/issues/447#issuecomment-783561969, or unsubscribe https://github.com/notifications/unsubscribe-auth/AO6CAQRVBBCJGKRPMPODZQDTAKMBTANCNFSM4UMDDR2A .

ikoazoulay avatar Feb 22 '21 18:02 ikoazoulay

Good method!

versa-dev avatar Apr 12 '21 21:04 versa-dev

As a hack i set a 2nd dataset with desired [min, max] and render a transparent line. Not ideal but worked as a temp fix for me.

Nice hack! Any way to set withShadow={false} just for that hacky line?

ftzi avatar Apr 28 '21 00:04 ftzi

I used this datasets: [ { data, }, { data: [min, max], color: () => 'transparent', strokeWidth: 0, withDots: false, }, ],

BishoyBishai avatar May 06 '21 11:05 BishoyBishai

@BishoyBishai This worked thank you!

PhillipFraThailand avatar Jul 06 '21 16:07 PhillipFraThailand

As a hack i set a 2nd dataset with desired [min, max] and render a transparent line. Not ideal but worked as a temp fix for me.

Nice hack! Any way to set withShadow={false} just for that hacky line?

The method @BishoyBishai proposed causes the single data point to get rendered halfway through the chart, which gives that shadow. The way to hide it is to fill the whole length of your data with the min, max:

// this assumes that all datasets[i].data are the same length and that datasets[0].data is defined
datasets: [
  ...datasets,
  {
    data: new Array(datasets[0].data.length).fill(min),
    color: () => 'transparent',
    strokeWidth: 0,
    withDots: false,
  },
  {
    data: new Array(datasets[0].data.length).fill(max),
    color: () => 'transparent',
    strokeWidth: 0,
    withDots: false,
  },
],

SaltedBlowfish avatar Jul 08 '21 16:07 SaltedBlowfish

I used this datasets: [ { data, }, { data: [min, max], color: () => 'transparent', strokeWidth: 0, withDots: false, }, ],

Thanks, @BishoyBishai !

Looking at the line chart code, strokeWidth is not used so we can simplify our dummy dataset. If strokeWidth is zero (falsy) on the dataset, it falls back to the value for the chart, and if that is falsy, it defaults to the magic number 3.

https://github.com/indiespirit/react-native-chart-kit/blob/304116394d20729b53216ba024684eaf30fc85b7/src/line-chart/LineChart.tsx#L235

This looks like the same issue as #259. It would be great to make this a feature, to set y-axis min and max values in the chart config. There is already a fromZero option, so the effort to do this might not be too terrible.

mnebuerquo avatar Dec 16 '21 18:12 mnebuerquo

Good news for our very hacky solution. As @BishoyBishai taught us above, we can add a new dataset to our list to push the y-axis range higher or lower (this can only extend the range, not constrict it).

The dataset was made invisible by setting color function to transparent. This wasn't convenient for me, so I used svg dashes to hide the dummy dataset:

{
      key: 'dummy-range-padding',
      data: [min, max],
      strokeDashArray: [0, 1000],
      withDots: false,
    }

The first of the strokeDashArray is the width of the dash. Zero make it not draw one. The second value there is the width of the space. 1000 is just an arbitrary non-zero value.

The withDots field tells the graph not to draw all the points, just the line (which we suppressed with the strokeDashArray).

Then min and max are the new graph extents, which you can calculate to nicely frame your data.

The key field is just an identifier, not used by the graph.

mnebuerquo avatar Dec 16 '21 18:12 mnebuerquo

As a hack i set a 2nd dataset with desired [min, max] and render a transparent line. Not ideal but worked as a temp fix for me.

Nice hack! Any way to set withShadow={false} just for that hacky line?

This is possible with the useShadowColorFromDataset chart config.

          datasets: [
            {
              data: actualData,
              color: () => 'red', // Color of your real data shadow.
            },
            {
              key: 'dummy-range-padding',
              data: [0, 100],
              color: () => 'rgba0, 0, 0, 0',
              strokeDashArray: [0, 1000],
              withDots: false,
            },
          ],

cjhines avatar Mar 09 '22 11:03 cjhines

you can use fromNumber to set the max value for Y axes

QzLP2P avatar Jun 29 '22 10:06 QzLP2P

you can use fromNumber to set the max value for Y axes

This is what worked for me!! Thank you so much

alexhrao avatar Sep 04 '22 14:09 alexhrao

you can use fromNumber to set the max value for Y axes

Perfect answer thank you

tx-dynamics avatar Oct 14 '22 11:10 tx-dynamics

@QzLP2P thanks a ton mate, worked for me

AmitP1392 avatar Nov 03 '22 08:11 AmitP1392

you can use fromNumber to set the max value for Y axes

Mark this as the PERFECT answer! This worked for me too

rahulTPatil avatar Dec 18 '22 09:12 rahulTPatil

It's 2023 still has no max min props!

And using fromNumber props will cause some with y axis issue when the data only has length one.

qaws5503 avatar May 10 '23 15:05 qaws5503

Why is there a fromNumber but no toNumber..

GNUGradyn avatar Dec 15 '23 17:12 GNUGradyn