Digital Deception
Digital Deception

Reputation: 2967

Dart D3 line using Package JS

We want to call D3 (v4) line functions directly (among others) from Dart, as we're trying to use it with Angular 2. We've tried various things such as

But this explodes and is very ugly.

So we got to:

   Line line = new Line();
   line.x(allowInterop((blah) { return 10;}));
   line.y(allowInterop((blah) { return 10;}));
   line.apply([[10, 20]]); //What goes here?

What is the recommended way of doing this? Also, do you have any guides for using Angular 2 + D3 + Dart? I've only seen type-script, and translating between them is not the easiest thing in the world.

Upvotes: 1

Views: 344

Answers (1)

Digital Deception
Digital Deception

Reputation: 2967

I managed to get this working using Js.dart. The important part is to have a call function. This allows you to call the function directly to get your result, as D3 does.

The Dart wrapper:

@JS('d3')
library d3;

import 'dart:js';
import "package:js/js.dart";

typedef num D3AccessorFunction(List<num> d, num i, List<List<num>> data);
Function d3AccessorFunction(D3AccessorFunction function) {
  return allowInterop(function);
}
@JS('line')
class Line {
  external Line();
  external String call (List<List<num>> data);
  external Line x(D3AccessorFunction function);
  external Line y(D3AccessorFunction function);
}

@JS("arc")
class Arc {
  external innerRadius([num innerRadius]);
  external outerRadius([num outerRadius]);
  external startAngle([num startAngleInRadians]);
  external endAngle([num endAngleInRadians]);
  external Arc();
  external String call ();
}

The function:

Line line = new Line();
return line([[fromTo.x, fromTo.y], [endTo.x, endTo.y]]);

Upvotes: 1

Related Questions