Skip to content

Commit

Permalink
adds Legend image and label examples
Browse files Browse the repository at this point in the history
  • Loading branch information
davelandry committed Aug 31, 2017
1 parent 5a1ec7d commit 418b3b2
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 0 deletions.
24 changes: 24 additions & 0 deletions example/legend-image.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
[width]: 400
[height]: 200

# Images in Legends

Each pair of square and text in a Legend is just an extension of the [Shape](http://d3plus.org/docs/#Shape) class. In order to override how they are rendered, pass a valid [config](http://d3plus.org/docs/#BaseClass.config) object to [.shapeConfig( )](http://d3plus.org/docs/#Shape.shapeConfig). In this example, we are providing an accessor function to the [backgroundImage](http://d3plus.org/docs/#Shape.backgroundImage) method of [Shape](http://d3plus.org/docs/#Shape), which returns an image path from each data point.

```js
var data = [
{id: "Apple", color: "orange", image: "https://datausa.io/static/img/attrs/thing_apple.png"},
{id: "Fish", color: "blue", image: "https://datausa.io/static/img/attrs/thing_fish.png"},
{id: "Tomato", color: "red", image: "https://datausa.io/static/img/attrs/thing_tomato.png"}
];

var legend = new d3plus.Legend()
.data(data)
.shapeConfig({
backgroundImage: function(d) { return d.image; },
fill: function(d) { return d.color; },
height: 25,
width: 25
})
.render();
```
Binary file added example/legend-image.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
25 changes: 25 additions & 0 deletions example/legend-remove-label.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
[width]: 400
[height]: 200

# Removing Legend Labels

The text labels in a legend can be removed/disabled by setting the [.label( )](http://d3plus.org/docs/#Legend.label) method to `false`. This is common practice when using images, as in the [Images in Legends](http://d3plus.org/examples/d3plus-legend/legend-image/) example (which this code is based on).

```js
var data = [
{id: "Apple", color: "orange", image: "https://datausa.io/static/img/attrs/thing_apple.png"},
{id: "Fish", color: "blue", image: "https://datausa.io/static/img/attrs/thing_fish.png"},
{id: "Tomato", color: "red", image: "https://datausa.io/static/img/attrs/thing_tomato.png"}
];

var legend = new d3plus.Legend()
.data(data)
.label(false)
.shapeConfig({
backgroundImage: function(d) { return d.image; },
fill: function(d) { return d.color; },
height: 25,
width: 25
})
.render();
```
Binary file added example/legend-remove-label.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 418b3b2

Please sign in to comment.