JsxGraph from Dart using Dart JS interop

Issue

I am trying to use the plotting library JSXGraph from Dart using dart js but I have problems.

This is working javascript code

var board = JXG.JSXGraph.initBoard(
    'box',
    {
      boundingbox: [-10, 10, 2, -1],
      axis: true,
      keepaspectratio: false,
      showNavigation: false
    }
);
var graph = board.create(
    'functiongraph',
    [function(x){ return 3*x*x + 5*x + 1;}, -10, 10],
    {strokeColor: '#ffff00'}
);

You can try it here on jsFiddle.

I want to do basically the identicall thing but from dart. This is what I have come up with so far:

JsObject jsxGraph = context['JXG']['JSXGraph'];
var board = jsxGraph.callMethod('initBoard', [
  'box',
  new JsObject.jsify({
    'boundingbox': [-10, 10, 2, -1],
    'axis': true,
    'keepaspectratio': false,
    'showNavigation': false
  })
]);
var graph = board.callMethod('create', [
  'functiongraph',
  [(x){return 3*x*x + 5*x + 1;}, -10, 10],
  new JsObject.jsify({'strokeColor': '#ffff00'})
]);

I get the plot area and axes but I can’t get the actual plot line. Instead, there is an error on the console: Uncaught TypeError: this.Y is not a function.

Could someone, please, point me to what I could be doing wrong? I suspect it is something about the callback function but I really don’t know what.

Solution

There’s a jsify missing in your code:

JsObject jsxGraph = context['JXG']['JSXGraph'];
var board = jsxGraph.callMethod('initBoard', [
  'box',
  new JsObject.jsify({
    'boundingbox': [-10, 10, 2, -1],
    'axis': true,
    'keepaspectratio': false,
    'showNavigation': false
  })
]);
var graph = board.callMethod('create', [
  'functiongraph',
  new JsObject.jsify([(x){return 3*x*x + 5*x + 1;}, -10, 10]},
  new JsObject.jsify({'strokeColor': '#ffff00'})
]);

Answered By – Alexandre Ardhuin

Answer Checked By – David Marino (FlutterFixes Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *