Browse Source

Fix event rect when x max/min specified - #171

pull/183/head
Masayuki Tanaka 11 years ago
parent
commit
a009c7d756
  1. 36
      c3.js
  2. 6
      c3.min.js

36
c3.js

@ -545,9 +545,11 @@
return getAxisClipHeight(__axis_rotated); return getAxisClipHeight(__axis_rotated);
} }
function getEventRectWidth() { function getEventRectWidth() {
var base = __axis_rotated ? height : width, var target = getMaxDataCountTarget(c3.data.targets),
firstData = target.values[0], lastData = target.values[target.values.length - 1],
base = x(lastData.x) - x(firstData.x),
maxDataCount = getMaxDataCount(), maxDataCount = getMaxDataCount(),
ratio = getXDomainRatio() * (hasBarType(c3.data.targets) ? (maxDataCount - (isCategorized ? 0.25 : 1)) / maxDataCount : 0.98); ratio = (hasBarType(c3.data.targets) ? (maxDataCount - (isCategorized ? 0.25 : 1)) / maxDataCount : 1);
return maxDataCount > 1 ? (base * ratio) / (maxDataCount - 1) : base; return maxDataCount > 1 ? (base * ratio) / (maxDataCount - 1) : base;
} }
function updateLegendStep(step) { function updateLegendStep(step) {
@ -658,8 +660,8 @@
} }
} else { } else {
axis.tickOffset = function () { axis.tickOffset = function () {
var base = __axis_rotated ? height : width; var edgeX = getEdgeX(c3.data.targets), base = x(edgeX[1]) - x(edgeX[0]);
return ((base * getXDomainRatio()) / getMaxDataCount()) / 2; return (base / getMaxDataCount()) / 2;
}; };
} }
@ -1258,18 +1260,15 @@
} }
return [yDomainMin - padding_bottom, yDomainMax + padding_top]; return [yDomainMin - padding_bottom, yDomainMax + padding_top];
} }
function getXDomainRatio(isSub) {
var orgDiff = diffDomain(orgXDomain), currentDiff = diffDomain(x.domain());
return isSub || currentDiff === 0 ? 1 : orgDiff / currentDiff;
}
function getXDomainMin(targets) { function getXDomainMin(targets) {
return __axis_x_min ? (isTimeSeries ? parseDate(__axis_x_min) : __axis_x_min) : d3.min(targets, function (t) { return d3.min(t.values, function (v) { return v.x; }); }); return __axis_x_min ? (isTimeSeries ? parseDate(__axis_x_min) : __axis_x_min) : d3.min(targets, function (t) { return d3.min(t.values, function (v) { return v.x; }); });
} }
function getXDomainMax(targets) { function getXDomainMax(targets) {
return __axis_x_max ? (isTimeSeries ? parseDate(__axis_x_max) : __axis_x_max) : d3.max(targets, function (t) { return d3.max(t.values, function (v) { return v.x; }); }); return __axis_x_max ? (isTimeSeries ? parseDate(__axis_x_max) : __axis_x_max) : d3.max(targets, function (t) { return d3.max(t.values, function (v) { return v.x; }); });
} }
function getXDomainPadding(targets, domain) { function getXDomainPadding(targets) {
var firstX = domain[0], lastX = domain[1], diff = Math.abs(firstX - lastX), maxDataCount, padding, paddingLeft, paddingRight; var edgeX = getEdgeX(targets), diff = edgeX[1] - edgeX[0],
maxDataCount, padding, paddingLeft, paddingRight;
if (isCategorized) { if (isCategorized) {
padding = 0; padding = 0;
} else if (hasBarType(targets)) { } else if (hasBarType(targets)) {
@ -1291,7 +1290,7 @@
function getXDomain(targets) { function getXDomain(targets) {
var xDomain = [getXDomainMin(targets), getXDomainMax(targets)], var xDomain = [getXDomainMin(targets), getXDomainMax(targets)],
firstX = xDomain[0], lastX = xDomain[1], firstX = xDomain[0], lastX = xDomain[1],
padding = getXDomainPadding(targets, xDomain), padding = getXDomainPadding(targets),
min = 0, max = 0; min = 0, max = 0;
if (firstX || firstX === 0) { if (firstX || firstX === 0) {
min = isTimeSeries ? new Date(firstX.getTime() - padding.left) : firstX - padding.left; min = isTimeSeries ? new Date(firstX.getTime() - padding.left) : firstX - padding.left;
@ -1558,20 +1557,25 @@
function getMaxDataCount() { function getMaxDataCount() {
return d3.max(c3.data.targets, function (t) { return t.values.length; }); return d3.max(c3.data.targets, function (t) { return t.values.length; });
} }
function getMaxDataCountTarget() { function getMaxDataCountTarget(targets) {
var length = c3.data.targets.length, max = 0, maxTarget; var length = targets.length, max = 0, maxTarget;
if (length > 1) { if (length > 1) {
c3.data.targets.forEach(function (t) { targets.forEach(function (t) {
if (t.values.length > max) { if (t.values.length > max) {
maxTarget = t; maxTarget = t;
max = t.values.length; max = t.values.length;
} }
}); });
} else { } else {
maxTarget = length ? c3.data.targets[0] : null; maxTarget = length ? targets[0] : null;
} }
return maxTarget; return maxTarget;
} }
function getEdgeX(targets) {
var target = getMaxDataCountTarget(targets),
firstData = target.values[0], lastData = target.values[target.values.length - 1];
return [firstData.x, lastData.x];
}
function mapToIds(targets) { function mapToIds(targets) {
return targets.map(function (d) { return d.id; }); return targets.map(function (d) { return d.id; });
} }
@ -3483,7 +3487,7 @@
}; };
} }
// Set data // Set data
maxDataCountTarget = getMaxDataCountTarget(); maxDataCountTarget = getMaxDataCountTarget(c3.data.targets);
main.select('.' + CLASS.eventRects) main.select('.' + CLASS.eventRects)
.datum(maxDataCountTarget ? maxDataCountTarget.values : []); .datum(maxDataCountTarget ? maxDataCountTarget.values : []);
// Update rects // Update rects

6
c3.min.js vendored

File diff suppressed because one or more lines are too long
Loading…
Cancel
Save