2

I am following the tutorial here:

https://www.scala-js.org/doc/project/dependencies.html

  1. First things first, I have my project set up like this:

https://github.com/scala-js/scalajs-cross-compile-example

Without making any changes, this runs as expected when I pass in the following commands:

sbt> fooJS/run
sbt> fooJVM/run
  1. Now I want to import this library:

  2. I want to run the following function:

    Plotly.newPlot('myDiv', data);

How can I do this?

My Main.scala file inside the js folder looks like this:

package example

object Main extends App {
  println(s"Using Scala.js version ${System.getProperty("java.vm.version")}")
}

I know a facade for this library already exists, but I would like to be able to create my own facades for future projects, and am using this as an example. I read the tutorial here:

https://www.scala-js.org/doc/interoperability/facade-types.html

But in all honesty I do not follow those steps coming from a different language ecosystem.

finite_diffidence
  • 893
  • 2
  • 11
  • 20
  • 3
    "However this fails". How does it fail? What did you try? What command did you run where? What did you expect and what happened instead? Help us helping you. Further reading: https://stackoverflow.com/help/how-to-ask – sjrd Jun 24 '20 at 16:09
  • Apologies I should have been clearer - I will update the original post – finite_diffidence Jun 24 '20 at 16:27
  • @sjrd I have made the question simpler and more concrete using a more realistic example, it unclear please let me know. – finite_diffidence Jun 24 '20 at 16:50

1 Answers1

2

“I do not follow those steps” is not a useful way to describe the problems you're having.

It looks fairly obvious what you need to do. There's a global Object called Plotly, which has a method called newPlot, which takes a String and an array of objects containing the data. So you need something like this:

@js.native
@JSGlobal
object Plotly extends js.Object {
  def newPlot(id: String, data: js.Array[PlotData]) = js.native
}

Now that we have that, we also need to specify what PlotData is supposed to look like. Unlike the type of the Plotly object, where we merely specify the interface and the actual object is implemented in JS, this type is going to be implemented in Scala, so you need to follow this guide.

For a scatter type plot, it could look like so:

case class PlotData(
  x: js.Array[Double],
  y: js.Array[Double]
) extends js.Object {
  def type: String = "scatter"
}
Matthias Berndt
  • 4,387
  • 1
  • 11
  • 25