chartjs-plugin-zoom icon indicating copy to clipboard operation
chartjs-plugin-zoom copied to clipboard

Add mode to force zoom to use whole numbers for new min/max values

Open DevidCIC opened this issue 5 years ago • 14 comments

How can I configure the zoom level on mouse wheel? I would like to zoom by 1 or by 1/10 everytime the weel is moved. In the gif below I have a chart with two Y Axes and there is a problem that one axes moves when I zoom in or out.

Alt Text

DevidCIC avatar Mar 07 '19 14:03 DevidCIC

Perhaps https://github.com/chartjs/chartjs-plugin-zoom/pull/215 will help you some?

But it seems like you also are concerned that only one of the two axes is being zoomed?

benmccann avatar Mar 07 '19 14:03 benmccann

Perhaps #215 will help you some?

But it seems like you also are concerned that only one of the two axes is being zoomed?

Thanks. But as you see the y axes starts at 0 and ends at 300. When I zoom in I get something like 258.2342353453 . But I want to zoom in in Integer number like 258, 256, 254, 252 and so on .....

Yes the second problem is that the zoom is just zooming at one axes instead of both y axes. I will open a new ticket for this

DevidCIC avatar Mar 13 '19 09:03 DevidCIC

But as you see the y axes starts at 0 and ends at 300. When I zoom in I get something like 258.2342353453 . But I want to zoom in in Integer number like 258, 256, 254, 252 and so on .....

@DevidCIC Mmh, not sure how to handle this, since it depends on your range (if your axis have values between 0 and 1 for instance, you'd still want labels with decimals)... Probably depends on the size of your chart, too.

jledentu avatar Mar 15 '19 09:03 jledentu

You could add an afterUpdate hook, where you round the scale min/max

benmccann avatar Mar 15 '19 14:03 benmccann

@DevidCIC Mmh, not sure how to handle this, since it depends on your range (if your axis have values between 0 and 1 for instance, you'd still want labels with decimals)... Probably depends on the size of your chart, too.

Yes but when I zoom in I don't want values like 0,0000123 → 0,0000125 → ...... → 0,0001023. But instead it makes more sense to configure the steps of the zoom and then you get something like 0,1 → 0,3 → 0,5 → ... → 0,9

You could add an afterUpdate hook, where you round the scale min/max

Ok I am interested, do you maybe have a small example for me ?

DevidCIC avatar Mar 18 '19 09:03 DevidCIC

See here: https://www.chartjs.org/docs/latest/developers/plugins.html

benmccann avatar Mar 18 '19 16:03 benmccann

@DevidCIC Did you solve this issue ? I have the same problem.

hstemplewski avatar Jul 01 '19 13:07 hstemplewski

I solve this issue using y(x)Axes config in chartjs. @DevidCIC

options: {
  scales: {
    yAxes: [{
      ticks: {
        callback: function(value) {
          if (Math.floor(value) === value) {
            return value
          }
        }
      }
     }]
   }
}

hstemplewski avatar Jul 02 '19 06:07 hstemplewski

I solve this issue using y(x)Axes config in chartjs. @DevidCIC

options: {
  scales: {
    yAxes: [{
      ticks: {
        callback: function(value) {
          if (Math.floor(value) === value) {
            return value
          }
        }
      }
     }]
   }
}

you saved a life

annezao avatar Jul 26 '19 04:07 annezao

I solve this issue using y(x)Axes config in chartjs. @DevidCIC

options: {
  scales: {
    yAxes: [{
      ticks: {
        callback: function(value) {
          if (Math.floor(value) === value) {
            return value
          }
        }
      }
     }]
   }
}

Perfect. Thanks very much. It works exactly as I want it too

DevidCIC avatar Jul 29 '19 14:07 DevidCIC

I solve this issue using y(x)Axes config in chartjs. @DevidCIC

options: {
  scales: {
    yAxes: [{
      ticks: {
        callback: function(value) {
          if (Math.floor(value) === value) {
            return value
          }
        }
      }
     }]
   }
}

Thanks this worked for me, exact thing what i searched for!

flowwdelapro avatar Mar 10 '20 09:03 flowwdelapro

I solve this issue using y(x)Axes config in chartjs. @DevidCIC

options: {
  scales: {
    yAxes: [{
      ticks: {
        callback: function(value) {
          if (Math.floor(value) === value) {
            return value
          }
        }
      }
     }]
   }
}

Thanks!

mehadihn avatar Oct 13 '21 05:10 mehadihn

I solve this issue using y(x)Axes config in chartjs. @DevidCIC

options: {
  scales: {
    yAxes: [{
      ticks: {
        callback: function(value) {
          if (Math.floor(value) === value) {
            return value
          }
        }
      }
     }]
   }
}

This was causing min and max values to display "undefined".

Had to completely hide the min and max value when they had some decimals in them by:

if (Math.floor(value) === value) {
     return value;
    } else {
     return "";
}

Baribj avatar Mar 12 '22 19:03 Baribj

I solve this issue using y(x)Axes config in chartjs. @DevidCIC

options: {
  scales: {
    yAxes: [{
      ticks: {
        callback: function(value) {
          if (Math.floor(value) === value) {
            return value
          }
        }
      }
     }]
   }
}

This was causing min and max values to display "undefined".

Had to completely hide the min and max value when they had some decimals in them by:

if (Math.floor(value) === value) {
     return value;
    } else {
     return "";
}

Still imperfect if you want to show ticks for high zoom levels (like values between "0" and "1" - "0.2", "0.4", "0.6" etc.)

Improved with following code:

ticks: {
  // for valid ticks in the middle
  precision: 2,
  // Hides min and max value that has too many decimal points during zoom/pan
  callback: (value, i, ticks) => {
    return  (i === 0 || i === ticks.length - 1) && Math.floor(+value) !== +value ? "" : value;
  },
},

shanovskyi-b avatar Nov 22 '23 11:11 shanovskyi-b