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 bar overlap bug when tick fit is false #2306

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
82 changes: 69 additions & 13 deletions spec/shape.bar-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,6 @@ describe('c3 chart shape bar', function () {
});
});
});

});

describe('internal.isWithinBar', function () {
Expand Down Expand Up @@ -174,6 +173,18 @@ describe('c3 chart shape bar', function () {

});

var getBBox = function(selector) {
return d3.select(selector).node().getBBox();
};

var getBarBBox = function(name, idx) {
return getBBox('.c3-target-' + name + ' .c3-bar-' + (idx || 0));
};

var getBarWidth = function(name, idx) {
return parseInt(getBarBBox(name, idx).width);
};

describe('bar spacing', function() {

var createArgs = function(spacing) {
Expand All @@ -199,10 +210,6 @@ describe('c3 chart shape bar', function () {
};
};

var getBBox = function(selector) {
return d3.select(selector).node().getBBox();
};

var getBarContainerWidth = function() {
return parseInt(getBBox('.c3-chart-bars').width);
};
Expand All @@ -211,14 +218,6 @@ describe('c3 chart shape bar', function () {
return parseInt(getBBox('.c3-chart-bars').x);
};

var getBarBBox = function(name, idx) {
return getBBox('.c3-target-' + name + ' .c3-bar-' + (idx || 0));
};

var getBarWidth = function(name, idx) {
return parseInt(getBarBBox(name, idx).width);
};

var getBarOffset = function(name1, name2, idx) {
var bbox1 = getBarBBox(name1, idx);
var bbox2 = getBarBBox(name2, idx);
Expand Down Expand Up @@ -326,4 +325,61 @@ describe('c3 chart shape bar', function () {
expect(getBarContainerOffset()).toEqual(31 + parseInt(15 / 2));
});
});

describe('bar width', function () {
var createArgs = function(dataColumns) {
return {
size: {
width: 500
},
data: {
x: 'x',
columns: dataColumns,
groups: [
['data1', 'data2']
],
type: 'bar',
},
axis: {
x: {
type: 'timeseries',
tick: {
fit: false,
format: '%Y-%m-%d'
}
}
}
};
};

it('should set arguments with fewer unique x values than fit false ticks', function () {
var data = [
['x', '2016-01-05', '2016-01-06', '2016-01-10'],
['data1', 50, 56, 75],
['data2', 2, 2, 2]
];
args = createArgs(data);
expect(true).toBeTruthy();
});

it('should calculate bar width using tick count when there are more ticks than x axis values', function () {
expect(getBarWidth('data2', '0')).toEqual(34);
});

it('should set arguments with more unique x values than fit false ticks', function () {
var data = [
['x', '2016-01-05', '2016-01-06','2016-01-07', '2016-01-08', '2016-01-09',
'2016-01-10', '2016-01-11','2016-01-12', '2016-01-13', '2016-01-14',
'2016-01-15', '2016-01-16', '2016-01-17','2016-01-18', '2016-01-19'],
['data1', 50, 56, 75, null, null, null, 50, 56, 75, 50, 56, 75, 50, 56, 75],
['data2', 2, 2, 2, null, null, null, 2, 2, 2, 50, 56, 75, 2, 2, 2]
];
args = createArgs(data);
expect(true).toBeTruthy();
});

it('should calculate bar width using unique x value count when there are more x values than ticks', function () {
expect(getBarWidth('data2', '0')).toEqual(18);
});
});
});
6 changes: 4 additions & 2 deletions src/axis.js
Original file line number Diff line number Diff line change
Expand Up @@ -330,14 +330,16 @@ c3_axis_internal_fn.generateAxis = function () {
axis.tickOffset = function () {
return internal.tickOffset;
};
axis.tickInterval = function () {
axis.tickInterval = function (maxUniqueXValues) {
var interval, length;
if (params.isCategory) {
interval = internal.tickOffset * 2;
}
else {
length = axis.g.select('path.domain').node().getTotalLength() - internal.outerTickSize * 2;
interval = length / axis.g.selectAll('line').size();
var tickCount = axis.g.selectAll('line').size();
var intervalDivisor = Math.max(maxUniqueXValues, tickCount) || tickCount;
interval = length / intervalDivisor;
}
return interval === Infinity ? 0 : interval;
};
Expand Down
10 changes: 8 additions & 2 deletions src/shape.bar.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,14 @@ c3_chart_internal_fn.redrawBar = function (drawBar, withTransition) {
];
};
c3_chart_internal_fn.getBarW = function (axis, barTargetsNum) {
var $$ = this, config = $$.config,
w = typeof config.bar_width === 'number' ? config.bar_width : barTargetsNum ? (axis.tickInterval() * config.bar_width_ratio) / barTargetsNum : 0;
var $$ = this, config = $$.config, maxUniqueXValues;
if ($$.data.targets.length > 0) {
maxUniqueXValues = $$.data.targets.reduce(function returnMaxLength(maxLength, target) {
var targetLength = target.values.length;
return Math.max(maxLength, targetLength);
}, 0);
}
var w = typeof config.bar_width === 'number' ? config.bar_width : barTargetsNum ? (axis.tickInterval(maxUniqueXValues) * config.bar_width_ratio) / barTargetsNum : 0;
return config.bar_width_max && w > config.bar_width_max ? config.bar_width_max : w;
};
c3_chart_internal_fn.getBars = function (i, id) {
Expand Down