mkraska / meclib

JSXGraph-based dynamic and interactive images in Moodle STACK questions without even touching JavaScript
9 stars 3 forks source link

Adjust Meclib behaviour to STACK setting of decimal separator #51

Open mkraska opened 1 month ago

mkraska commented 1 month ago

STACK 4.6 has a question-wide option "decimal separator", which can be set to "." or ",".

If it is set to , then the comma becomes the decimal separator and the semicolon ; becomes the argument separator in functions and lists.

The required modifications are:

Detect the separator setting in the question

The brute force option is to add a constant in the `[[JSXGraph]] block and set it manually to the value of the separator option in the question.

const decsep = ",";

This must be defined manually according to the question setting, unless there is a CAS variable in STACK which reflects that setting.

Format of the names input field

Adjustment of the output format is done in the Meclib function update().

  for (m of objects) {
    dfield.push(m.data());
    if (names != "[") { names = names.concat(",") }
    names = names.concat(m.name()); }
  names = names.concat("]");
  // adjust separators 
  if (decsep == ",") {
    names = names.replace(/,/g, ';');
    names = names.replace(/\./g, ',');}

This adjustment is required as long as STACK doesn't provide a forgiving mode, where both versions are accepted

Format of the Infobox

The Infobox is customized using the function

board.highlightInfobox = function(x, y , el) {
    let ref = [0,0];
    let scale = [xscale,yscale];
    let dp = [dpx,dpy];
    let lbl = '';
    if (typeof (el.ref) == 'function') {ref = el.ref()} 
    else if (typeof(el.ref) != 'undefined') {ref = el.ref}
    if (typeof (el.scale) != 'undefined') {scale = el.scale}
    if (typeof (el.dp) != 'undefined') {dp = el.dp}
    if (typeof (el.infoboxlabel) == 'string') {lbl = el.infoboxlabel}
    this.infobox.setText( 
        lbl+'('+((parseFloat(x)-ref[0])*scale[0]).toFixed(dp[0]) + ', ' + ((parseFloat(y)-ref[1])*scale[1]).toFixed(dp[1])+ ')')
};

The modification could probably be done using JS native localization methods.

Format of the tick labels

The tick labels are also already customized in the [["grid"]] object.

  this.xaxis = board.create('axis', [[0, 0], [1,0]], 
    {name:toTEX(data[1]), withLabel: true, label: labelopt, layer:0,
    ticks: { label:{layer:3}, generateLabelValue:function(p1,p2) {
      return ((p1.usrCoords[1]-p2.usrCoords[1])*fx).toFixed(Math.max(...[0,dpx-1]))}} });}

The modification could probably be done using JS native localization methods.

mkraska commented 1 month ago

Formatting of the output is tested in TM2 03 T02 V2 Mohr's circle (stress) (Meclib, localized)

mkraska commented 1 month ago

Here https://jsfiddle.net/7k0a4jbv/3/ you can find a meclib version with german locale. Infobox and axes tick labels are adjusted.