Skip to content

Commit

Permalink
feat(legend): Add argument to legend template
Browse files Browse the repository at this point in the history
Pass data argument for template function

Fix #302
Close #322
  • Loading branch information
Julien Castelain authored and netil committed Mar 7, 2018
1 parent 67ff3c6 commit 2ecab0d
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 10 deletions.
6 changes: 3 additions & 3 deletions spec/internals/legend-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -319,9 +319,9 @@ describe("LEGEND", () => {
});

it("set options legend.content.template as function", () => {
args.legend.contents.template = function(title, color) {
args.legend.contents.template = function(title, color, data) {
if (title !== "data1") {
return `<li style='background-color:${color}'>${title}</li>`;
return `<li style='background-color:${color}'>${title}-${data[0].value}</li>`;
}
}
});
Expand All @@ -335,7 +335,7 @@ describe("LEGEND", () => {
items.each(function(v) {
const item = d3.select(this);

expect(item.html()).to.be.equal(v);
expect(item.html()).to.be.equal(`${v}-${chart.data.values(v)[0]}`);
expect(item.style("background-color")).to.be.equal(chart.color(v));

// check for event bind
Expand Down
12 changes: 8 additions & 4 deletions src/config/Options.js
Original file line number Diff line number Diff line change
Expand Up @@ -1077,9 +1077,13 @@ export default class Options {
* If true given, all legend will be hidden. If string or array given, only the legend that has the id will be hidden.
* @property {String|HTMLElement} [legend.contents.bindto=undefined] Set CSS selector or element reference to bind legend items.
* @property {String|Function} [legend.contents.template=undefined] Set item's template.<br>
* If set string value, within template the 'color' and 'title' can be replaced using template-like syntax string:
* - {=COLOR}: data color value
* - {=TITLE}: data title value
* - If set `string` value, within template the 'color' and 'title' can be replaced using template-like syntax string:
* - {=COLOR}: data color value
* - {=TITLE}: data title value
* - If set `function` value, will pass following arguments to the given function:
* - title {String}: data's id value
* - color {String}: color string
* - data {Array}: data array
* @property {String} [legend.position=bottom] Change the position of legend.<br>
* Available values are: `bottom`, `right` and `inset` are supported.
* @property {Object} [legend.inset={anchor: 'top-left',x: 10,y: 0,step: undefined}] Change inset legend attributes.<br>
Expand Down Expand Up @@ -1114,7 +1118,7 @@ export default class Options {
* template: "<li style='background-color:{=COLOR}'>{=TITLE}</li>"
*
* // or using function
* template: function(title, color) {
* template: function(id, color, data) {
* // if you want omit some legend, return falsy value
* if (title !== "data1") {
* return "<li style='background-color:"+ color +">"+ title +"</li>";
Expand Down
6 changes: 3 additions & 3 deletions src/internals/legend.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,13 @@ extend(ChartInternal.prototype, {
const template = config.legend_contents_template;

if (!wrapper.empty()) {
const targets = $$.mapToIds($$.data.targets);
const targets = $$.data.targets;
const ids = [];
let html = "";

targets.forEach(v => {
$$.mapToIds(targets).forEach(v => {
const content = isFunction(template) ?
template.call($$, v, $$.color(v)) :
template.call($$, v, $$.color(v), $$.api.data(v)[0].values) :
template
.replace(/{=COLOR}/g, $$.color(v))
.replace(/{=TITLE}/g, v);
Expand Down

0 comments on commit 2ecab0d

Please sign in to comment.