elm-d3

Elm 绑定用于 D3.js。(Elm bindings for D3.js.)

  • Owner: seliopou/elm-d3
  • Platform: Linux, Mac, Windows
  • License:: Other
  • Category::
  • Topic:
    d3
  • Like:
    0
      Compare:

Github stars Tracking Chart

elm-d3

elm-d3 为 d3.js 提供 Elm 绑定。 它使您能够使用 HTML、SVG 和 CSS 创建类型安全、可组合的小部件。 D3 充当库的概念基础,也是 Elm 的替代渲染器。

安装

首先确保安装了 node.js,以及 Elm 编译器。 安装这些依赖项后,克隆 elm-d3 存储库并从根目录运行以下命令:

elm-make

这将在本地安装 smash 实用程序并构建库。

要在浏览器中编译并运行示例,请使用以下命令:

elm-make examples/Circles.elm --output circles.html

然后,您必须编辑 HTML 文件并添加将加载 D3.js 库的 <script> 标记。 (不幸的是,不再可以使用 Elm 编译器控制外部 JavaScript 代码的链接,因此必须执行此手动步骤。)然后,在浏览器中加载它:

open build/examples/circles.html

如果您使用的不是 OS X,则最后一个命令将不起作用。 在这种情况下,您可以在操作系统中正常打开 circle .html。打开页面后,将鼠标左右移动来添加和删除圆圈,并上下移动鼠标来更改圆圈的亮度。

Main metrics

Overview
Name With Ownerseliopou/elm-d3
Primary LanguageJavaScript
Program languageElm (Language Count: 2)
PlatformLinux, Mac, Windows
License:Other
所有者活动
Created At2013-09-13 19:18:56
Pushed At2016-07-29 03:42:08
Last Commit At2015-12-08 14:21:06
Release Count9
Last Release Namev0.4.2 (Posted on )
First Release Namev0.1.0 (Posted on )
用户参与
Stargazers Count336
Watchers Count21
Fork Count36
Commits Count119
Has Issues Enabled
Issues Count13
Issue Open Count4
Pull Requests Count9
Pull Requests Open Count1
Pull Requests Close Count11
项目设置
Has Wiki Enabled
Is Archived
Is Fork
Is Locked
Is Mirror
Is Private

elm-d3

elm-d3 provides Elm bindings for d3.js. It enables you to create
type-safe, composable widgets using HTML, SVG, and CSS. D3 acts as a conceptual
basis for the library, as well as an alternative renderer for Elm.

Installation

First make sure that you have node.js installed, as well as the
Elm compiler. Once you've installed those dependencies, clone the elm-d3
repository and run the following command from the root directory:

elm-make

This will locally install the smash utility and build the library.

To get an example compiled and running in your browser, use the following
command:

elm-make examples/Circles.elm --output circles.html

You must then edit the HTML file and add a <script> tag that will load the
D3.js library. (Unfortunately, it is no longer possible to control linking of
external JavaScript code using the Elm compiler, so this manual step is
necessary.) Then, load it up on your browser:

open build/examples/circles.html

If you're not using OS X, the last command won't work. In that case open
circles.html however you normally would in your operating system. Once the
page is open, move your mouse left and right to add and remove circles, and
move your mouse up and down to change their brightness.

Conceptual Prerequisites

In order to effectively use elm-d3, you should be familiar with the core
concepts of D3.js, including data joins, nested selections, and reusable
charts. The following series of blog posts by @mbostock introduce these
concepts quite nicely, and independently of elm-d3 should be read by anybody
that is interested in developing their D3.js skills:

Usage

elm-d3 is designed to be a very literal interpretation of the D3.js API. In
fact, any D3.js code that uses only the Core Selection API should be
fairly straightforward to port over to elm-d3. For example, here's a fragment
of code taken from the Voronoi Diagram example (original D3.js
version
):

voronoi : D3 [D3.Voronoi.Point] [D3.Voronoi.Point]
voronoi =
  selectAll "path", . bind cells, - enter <.> append "path", - update, . fun attr "d" (\ps _ -> path ps), . fun attr "class" (\_ i -> "q" ++ (show ((%) i 9)) ++ "-9")

Operations such as selectAll, enter, and attr have the same behavior as
their D3.js counterparts. The bind operation is equivalent to the data()
operator from D3.js, though it requires its argument to be a function.
Similarly, attr also requires a function as its second argument, which takes
the data bound to the element and the element's index in the selection. Another
difference is that elm-d3 replaces method chaining with the , . operator. For
example,

selectAll "path", . bind cells

is equivalent to

d3.selectAll("path")
  .data(cells)

Sequencing is another operation that's slightly different in elm-d3. In Javascript,
there's a common pattern where you apply a data bound to a selection, assign it
to a variable, and then apply enter(), update, and exit() operations to the
variable. In place of sequencing, you would use the , - infix operator. Its
use is illustrated in the example above. You can see the equivalent code in JavaScript below.

var path = d3.selectAll('path')
    .bind(function(d) { ... });

path.enter()
  .append('path');

path
    .attr('d', function(d) { ... })
    .attr('class', function(d) { ... });

Rendering

Creating a selection such as voronoi above does not actually draw anything to
the screen. Rather, it defines a computation that the runtime knows how to draw
to the screen. To do this, you use the render function. Its first two
arguments are the height and width of the drawing area. The third is the D3 a b that will be rendered. The final argument is the datum of type a that will
be bound to the selection. The result is an Element that the Elm runtime
knows what to do with:

main : Element
main = render 800 600 voronoi [{x: 200, y: 200}, {x: 320, y:100}]

Further documentation

For more information about the API, the source file in src/D3.elm.
Each function is preceded by a comment describing the equivalent expression in
JavaScript. Types are also very instructive.

License

BSD3, see LICENSE file for its text.