How to change the grid distance in Javascript

Sander Tacker shared this question 3 years ago

Hi GeoGebra friends,

I am a teacher and I implemented a GeoGebra file to practise drawing linear functions. There are different levels of difficulty. All works really well, but now I need to change the grid distance via Javascript. In the Preferences Menu (Tab "Grid") I can set the values for the grid distance manually for x- and y-direction (see attachment), but I don't want to change these values permanently from the start, instead I need to set these values via Javascript as soon as a certain level of difficulty is reached.

I found the following methods to manipulate the coordinate system, but they don't do the job I need:

"setAxisSteps" - changes the steps of the numbers on the axes, but not the steps of grid. I want to set both steps independently.

"setCoordSystem" - also effects only the numbers, but not the grid

"setAxesRatio" - stretches the ymin/ymax-values corresponding to the given ratio

Is there a Javascript method to change the grid distance?

Thanks for your help!


Comments (2)


Dear Sander, I've just seen your question... I'm sorry I've seen it just now... 10 months after you've asked a question

So, it's possible to change not just the grid distance but so many different things with GeoGebra XML format.

Here's a function for changing grid distance with arbitrary values - put this function in Scripting > Global JavaScript

function changeGridStep(distX,distY) {
   var prop='<grid distX="'+distX+'" distY="'+distY+'"/>'
   var tagName = "grid";
   var xml = ggbApplet.getXML();
   var lines = xml.split("\n");
   var xmlDef = '';
   for (var i = 0; i < lines.length; i++) {
     if (lines[i].indexOf('<' + tagName) !== -1)
       xmlDef += prop + "\n";
       xmlDef += lines[i] + "\n";   }

After that just create a button and call a changeGridStep(distX,distY) function as On Click script. See the image below.


Hope this helps



amvukovic this is great. Do you have a javascript that does the same, but with tic-distance? @amvukovic

© 2020 International GeoGebra Institute