Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(api): Fix regions.remove() error #579

Merged
merged 1 commit into from
Sep 5, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
189 changes: 122 additions & 67 deletions spec/api/api.region-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,32 +7,39 @@ import util from "../assets/util";

describe("API region", function() {
let chart;
let args;

beforeEach(() => {
chart = util.generate(args);
});

describe("regions()", () => {
chart = util.generate({
data: {
columns: [
["data1", 30, 200, 100, 400, 150, 250]
]
},
regions: [
{
axis: "y",
start: 300,
end: 400,
class: "green",
before(() => {
args = {
data: {
columns: [
["data1", 30, 200, 100, 400, 150, 250]
]
},
{
axis: "y",
start: 0,
end: 100,
class: "green",
}
]
regions: [
{
axis: "y",
start: 300,
end: 400,
class: "green",
},
{
axis: "y",
start: 0,
end: 100,
class: "green",
}
]
}
});

it("should update regions", done => {
const main = chart.internal.main;
const main = chart.$.main;
const expectedRegions = [
{
axis: "y",
Expand Down Expand Up @@ -83,30 +90,32 @@ describe("API region", function() {
});

describe("Add regions using regions()", () => {
chart = util.generate({
data: {
columns: [
["data1", 30, 200, 100, 400, 150, 250],
]
},
regions: [
{
axis: "y",
start: 300,
end: 400,
class: "green",
before(() => {
args = {
data: {
columns: [
["data1", 30, 200, 100, 400, 150, 250],
]
},
{
axis: "y",
start: 0,
end: 100,
class: "green",
}
]
regions: [
{
axis: "y",
start: 300,
end: 400,
class: "green",
},
{
axis: "y",
start: 0,
end: 100,
class: "green",
}
]
}
});

it("should add regions", done => {
const main = chart.internal.main;
const main = chart.$.main;
const expectedRegions = [
{
axis: "y",
Expand Down Expand Up @@ -173,36 +182,37 @@ describe("API region", function() {
});

describe("Remove regions using regions()", () => {
chart = util.generate({
data: {
columns: [
["data1", 30, 200, 100, 400, 150, 250],
]
},
regions: [
{
axis: "y",
start: 300,
end: 400,
class: "green",
},
{
axis: "y",
start: 0,
end: 100,
class: "green",
before(() =>
args = {
data: {
columns: [
["data1", 30, 200, 100, 400, 150, 250],
]
},
{
axis: "y",
start: 250,
end: 350,
class: "red"
},
]
});
regions: [
{
axis: "y",
start: 300,
end: 400,
class: "green",
},
{
axis: "y",
start: 0,
end: 100,
class: "green",
},
{
axis: "y",
start: 250,
end: 350,
class: "red"
},
]
});

it("should remove regions", done => {
const main = chart.internal.main;
const main = chart.$.main;
const expectedRegions = [
{
axis: "y",
Expand Down Expand Up @@ -244,4 +254,49 @@ describe("API region", function() {
});

// regions.add / remove
describe("regions()", () => {
before(() =>
args ={
data: {
columns: [
["data1", 30, 200, 100, 400, 150, 250]
]
}
});

it(".add() / .remove()", () => {
const regions = [
{
axis: "y",
start: 300,
end: 400,
class: "class-a"
},
{
axis: "y",
start: 0,
end: 100,
class: "class-b"
}
];

// when
chart.regions.add(regions);

expect(chart.regions()).to.deep.equal(regions);

// when
const removed = chart.regions.remove({
classes: ["class-a"]
});

expect(chart.regions().length).to.be.equal(1);
expect(removed[0]).to.deep.equal(regions[1]);

// when remove all
chart.regions.remove();

expect(chart.regions().length).to.be.equal(0);
});
});
});
42 changes: 25 additions & 17 deletions src/api/api.region.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
*/
import Chart from "../internals/Chart";
import CLASS from "../config/classes";
import {extend} from "../internals/util";
import {getOption, extend} from "../internals/util";

/**
* Update regions.
Expand All @@ -31,7 +31,7 @@ const regions = function(regions) {
config.regions = regions;
$$.redrawWithoutRescale();

return config.regions;
return regions;
};

extend(regions, {
Expand Down Expand Up @@ -76,7 +76,7 @@ extend(regions, {
* @instance
* @memberOf Chart
* @param {Object} regions This argument should include classes. If classes is given, the regions that have one of the specified classes will be removed. If args is not given, all of regions will be removed.
* @return {Array} regions
* @return {Array} regions Removed regions
* @example
* // regions that have 'region-A' or 'region-B' will be removed.
* chart.regions.remove({
Expand All @@ -93,32 +93,40 @@ extend(regions, {
const config = $$.config;

const options = optionsValue || {};
const duration = $$.getOption(options, "duration", config.transition_duration);
const classes = $$.getOption(options, "classes", [CLASS.region]);
const regions = $$.main.select(`.${CLASS.regions}`)
const duration = getOption(options, "duration", config.transition_duration);
const classes = getOption(options, "classes", [CLASS.region]);
let regions = $$.main.select(`.${CLASS.regions}`)
.selectAll(classes.map(c => `.${c}`));

(duration ? regions.transition().duration(duration) : regions)
.style("opacity", "0")
.remove();

config.regions = config.regions.filter(region => {
let found = false;
regions = config.regions;

if (!region.class) {
return true;
}
if (Object.keys(options).length) {
regions = regions.filter(region => {
let found = false;

region.class.split(" ").forEach(c => {
if (classes.indexOf(c) >= 0) {
found = true;
if (!region.class) {
return true;
}

region.class.split(" ").forEach(c => {
if (classes.indexOf(c) >= 0) {
found = true;
}
});

return !found;
});

return !found;
});
config.regions = regions;
} else {
config.regions = [];
}

return config.regions;
return regions;
}
});

Expand Down