diff --git a/spec/assets/util.js b/spec/assets/util.js
index f2689939e..607991507 100644
--- a/spec/assets/util.js
+++ b/spec/assets/util.js
@@ -37,7 +37,7 @@ const generate = args => {
args.bindto = "#chart";
}
- initDom(args);
+ initDom(args.bindto);
// when touch param is set, make to be 'touch' input mode
if (args.interaction && args.interaction.inputType && args.interaction.inputType.touch) {
diff --git a/spec/legend-spec.js b/spec/legend-spec.js
index 197bb96a2..e4d69288a 100644
--- a/spec/legend-spec.js
+++ b/spec/legend-spec.js
@@ -5,6 +5,7 @@
/* eslint-disable */
/* global describe, beforeEach, it, expect */
import util from "./assets/util";
+import CLASS from "../src/config/classes";
describe("LEGEND", () => {
let chart;
@@ -230,7 +231,7 @@ describe("LEGEND", () => {
});
});
- describe("custom legend size", () => {
+ describe("custom legend settings", () => {
before(() => {
args = {
data: {
@@ -259,20 +260,10 @@ describe("LEGEND", () => {
expect(tileWidth).to.be.equal(args.legend.item.tile.width);
});
});
- });
- describe("custom legend padding", () => {
- before(() => {
- args = {
- data: {
- columns: [
- ["padded1", 30, 200, 100, 400, 150, 250],
- ["padded2", 130, 100, 200, 100, 250, 150]
- ]
- },
- legend: {
- padding: 10
- }
+ it("set options legend.padding=10", () => {
+ args.legend = {
+ padding: 10
};
});
@@ -289,4 +280,75 @@ describe("LEGEND", () => {
});
});
});
+
+ describe("set legend using template", () => {
+ before(() => {
+ sandbox("legend-wrapper").innerHTML = "
";
+
+ args = {
+ data: {
+ columns: [
+ ["data1", 30, 200, 100, 400, 150, 250],
+ ["data2", 130, 100, 200, 100, 250, 150]
+ ],
+ colors: {
+ data1: "rgb(42, 208, 255)",
+ data2: "rgb(250, 113, 113)"
+ }
+ },
+ legend: {
+ contents: {
+ bindto: "#legend",
+ template: "{=TITLE}"
+ }
+ }
+ };
+ });
+
+ it("check for legend template setting with template string", () => {
+ const legend = d3.select("#legend");
+ const items = legend.selectAll(`.${CLASS.legendItem}`);
+
+ expect(legend.html()).not.to.be.null;
+
+ items.each(function(v) {
+ const item = d3.select(this);
+
+ expect(item.html()).to.be.equal(v);
+ expect(item.style("background-color")).to.be.equal(chart.color(v));
+
+ // check for event bind
+ expect(item.on("click")).not.be.null;
+ });
+
+ expect(items.size()).to.be.equal(2);
+ });
+
+ it("set options legend.content.template as function", () => {
+ args.legend.contents.template = function(title, color) {
+ if (title !== "data1") {
+ return `${title}`;
+ }
+ }
+ });
+
+ it("check for legend template setting with template function callback", () => {
+ const legend = d3.select("#legend");
+ const items = legend.selectAll(`.${CLASS.legendItem}`);
+
+ expect(legend.html()).not.to.be.null;
+
+ items.each(function(v) {
+ const item = d3.select(this);
+
+ expect(item.html()).to.be.equal(v);
+ expect(item.style("background-color")).to.be.equal(chart.color(v));
+
+ // check for event bind
+ expect(item.on("click")).not.be.null;
+ });
+
+ expect(items.size()).to.be.equal(1);
+ });
+ });
});
diff --git a/src/api/api.show.js b/src/api/api.show.js
index 895d4604c..2da6d732d 100644
--- a/src/api/api.show.js
+++ b/src/api/api.show.js
@@ -59,8 +59,7 @@ extend(Chart.prototype, {
targets.style("opacity", null).style("opacity", "0");
});
- options.withLegend &&
- $$.hideLegend(targetIds);
+ options.withLegend && $$.hideLegend(targetIds);
$$.redraw({
withUpdateOrgXDomain: true,
diff --git a/src/config/Options.js b/src/config/Options.js
index 9b766f812..f8ef93d56 100644
--- a/src/config/Options.js
+++ b/src/config/Options.js
@@ -991,6 +991,11 @@ export default class Options {
* @property {Boolean} [legend.show=true] Show or hide legend.
* @property {Boolean} [legend.hide=false] Hide legend
* 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.
+ * 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
* @property {String} [legend.position=bottom] Change the position of legend.
* 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.
@@ -1015,6 +1020,20 @@ export default class Options {
* hide: true,
* //or hide: "data1"
* //or hide: ["data1", "data2"]
+ * contents: {
+ * bindto: "#legend", //
+ *
+ * // will be as: data1
+ * template: "{=TITLE}"
+ *
+ * // or using function
+ * template: function(title, color) {
+ * // if you want omit some legend, return falsy value
+ * if (title !== "data1") {
+ * return "