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

Line chart: fixed the check dataColors options array for emptyness #47

Closed
wants to merge 2 commits into from
Closed
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
13 changes: 8 additions & 5 deletions src/Line.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,11 @@ import addLegend from './utils/addLegend';
import addFont from './utils/addFont';
import addFilter from './utils/addFilter';
import colors from './utils/colors';
import arrayUtils from './utils/arrayUtils';
import config from './config';

const { isEmptyArray } = arrayUtils;

const margin = {
top: 50, right: 30, bottom: 50, left: 50,
};
Expand Down Expand Up @@ -127,7 +130,7 @@ class Line {
.attr('class', 'xkcd-chart-line')
.attr('d', (d) => theLine(d.data))
.attr('fill', 'none')
.attr('stroke', (d, i) => (this.options.dataColors
.attr('stroke', (d, i) => (!isEmptyArray(this.options.dataColors)
Copy link
Owner

@timqian timqian Nov 9, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if this.options is undefined, looks the error still exists as we are we are still trying to get a property of undefined here
this.options.dataColors.

May be check this.options && this.options.dataColors here?

Not 100 present sure as I dont get a computer nearby to play with the code

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@timqian Thanks for getting back.
Correct, let me do that check and update this PR

? this.options.dataColors[i]
: colors[i]))
.attr('filter', this.filter);
Expand All @@ -145,8 +148,8 @@ class Line {

const circles = this.data.datasets.map((dataset, i) => graphPart
.append('circle')
.style('stroke', this.options.dataColors ? this.options.dataColors[i] : colors[i])
.style('fill', this.options.dataColors ? this.options.dataColors[i] : colors[i])
.style('stroke', !isEmptyArray(this.options.dataColors) ? this.options.dataColors[i] : colors[i])
.style('fill', !isEmptyArray(this.options.dataColors) ? this.options.dataColors[i] : colors[i])
.attr('r', 3.5)
.style('visibility', 'hidden'));

Expand Down Expand Up @@ -187,7 +190,7 @@ class Line {
});

const tooltipItems = this.data.datasets.map((dataset, j) => ({
color: this.options.dataColors ? this.options.dataColors[j] : colors[j],
color: !isEmptyArray(this.options.dataColors) ? this.options.dataColors[j] : colors[j],
text: `${this.data.datasets[j].label || ''}: ${this.data.datasets[j].data[mostNearLabelIndex]}`,
}));

Expand All @@ -214,7 +217,7 @@ class Line {
// Legend
const legendItems = this.data.datasets
.map((dataset, i) => ({
color: this.options.dataColors ? this.options.dataColors[i] : colors[i],
color: !isEmptyArray(this.options.dataColors) ? this.options.dataColors[i] : colors[i],
text: dataset.label,
}));

Expand Down
2 changes: 1 addition & 1 deletion src/components/Tooltip.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ class Tooltip {

return g;
}

_getBackgroundWidth() {
const maxItemLength = this.items.reduce(
(pre, cur) => (pre > cur.text.length ? pre : cur.text.length), 0,
Expand Down
10 changes: 10 additions & 0 deletions src/utils/arrayUtils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@

const isEmptyArray = (data) => {
if (!data) {
return true;
}

return !(Array.isArray(data) && data.length);
};

export default { isEmptyArray };