How to change the grid distance in Javascript

Sander Tacker shared this question 6 years ago
Answered

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!

Sander

Comments (4)

photo
1

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";
     else
       xmlDef += lines[i] + "\n";   }
    ggbApplet.setXML(xmlDef);
}


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

ea0a3c976b8e4deee2b25dc0eee41d8d

Hope this helps


Alex

photo
1

Dear amvukovic,

thanks for your suggestion, which I found right now :-)


Your function changeGripSteps using setXML does work, but there are two problems:

1. When executing setXML, the name of the GeoGebra file is deleted (it gets an unsaved file). How can I prevent this problem? (see attached file)


Because setXML is really slow for big files (since the whole construction string has to be reset), I tried to use evalXML instead which allows to change properties of single geogebra objects.


2. Whereas evalXML works well for changing properties of geogebra objects (for example the color of a point), it does not work for properties of the view like distX. How can I get evalXML working? (see attached file)


Thanks for your help!

photo
1

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

photo
1

Dear amvukovic,

thanks for your suggestion, which I found right now :-)


Your function changeGripSteps using setXML does work, but there are two problems:

1. When executing setXML, the name of the GeoGebra file is deleted (it gets an unsaved file). How can I prevent this problem? (see attached file)


Because setXML is really slow for big files (since the whole construction string has to be reset), I tried to use evalXML instead which allows to change properties of single geogebra objects.


2. Whereas evalXML works well for changing properties of geogebra objects (for example the color of a point), it does not work for properties of the view like distX. How can I get evalXML working? (see attached file)


Thanks for your help!

Comments have been locked on this page!

© 2023 International GeoGebra Institute