Browse Source

Add x and xs API - #82

pull/104/head
Masayuki Tanaka 11 years ago
parent
commit
437f1b346b
  1. 107
      c3.js
  2. 4
      c3.min.js

107
c3.js

@ -1235,6 +1235,34 @@
return data; return data;
} }
function updateTargetX(targets, x) {
targets.forEach(function (t) {
t.values.filter(function (v) { return v.value !== null; }).forEach(function (v, i) {
v.x = generateTargetX(x[i], t.id, i);
});
c3.data.x[t.id] = x;
});
}
function updateTargetXs(targets, xs) {
targets.forEach(function (t) {
if (xs[t.id]) {
updateTargetX([t], xs[t.id]);
}
});
}
function generateTargetX(rawX, id, index) {
var x;
if (isTimeSeries) {
x = rawX ? rawX instanceof Date ? rawX : parseDate(rawX) : null;
}
else if (isCustomX && !isCategorized) {
x = rawX ? +rawX : getXValue(id, index);
}
else {
x = index;
}
return x;
}
function convertRowsToData(rows) { function convertRowsToData(rows) {
var keys = rows[0], new_row = {}, new_rows = [], i, j; var keys = rows[0], new_row = {}, new_rows = [], i, j;
for (i = 1; i < rows.length; i++) { for (i = 1; i < rows.length; i++) {
@ -1267,17 +1295,25 @@
throw new Error('data.x or data.xs must be specified when axis.x.type == "timeseries"'); throw new Error('data.x or data.xs must be specified when axis.x.type == "timeseries"');
} }
// save x for update data by load // save x for update data by load when custom x and c3.x API
if (isCustomX) { ids.forEach(function (id) {
ids.forEach(function (id) { var xKey = getXKey(id);
var xKey = getXKey(id); if (isCustomX) {
if (xs.indexOf(xKey) >= 0) { if (xs.indexOf(xKey) >= 0) {
c3.data.x[id] = data.map(function (d) { return d[xKey]; }); c3.data.x[id] = data.map(function (d) { return d[xKey]; });
} else { // if no x included, use same x of current will be used
c3.data.x[id] = c3.data.x[Object.keys(c3.data.x)[0]];
} }
}); // MEMO: if no x included, use same x of current will be used
} } else {
c3.data.x[id] = data.map(function (d, i) { return i; });
}
});
// check x is defined
ids.forEach(function (id) {
if (!c3.data.x[id]) {
throw new Error('x is not defined for id = "' + id + '".');
}
});
// convert to target // convert to target
targets = ids.map(function (id, index) { targets = ids.map(function (id, index) {
@ -1286,26 +1322,12 @@
id: convertedId, id: convertedId,
id_org: id, id_org: id,
values: data.map(function (d, i) { values: data.map(function (d, i) {
var x, xKey = getXKey(id); var xKey = getXKey(id), rawX = d[xKey], x = generateTargetX(rawX, id, i);
// use x as categories if custom x and categorized
if (isTimeSeries) { if (isCustomX && isCategorized && index === 0 && rawX) {
x = d[xKey] ? d[xKey] instanceof Date ? d[xKey] : parseDate(d[xKey]) : null; if (i === 0) { __axis_x_categories = []; }
} __axis_x_categories.push(rawX);
else if (isCustomX && !isCategorized) {
x = d[xKey] ? +d[xKey] : getXValue(id, i);
}
else if (isCustomX && isCategorized) {
x = i;
if (index === 0 && d[xKey]) {
if (i === 0) { __axis_x_categories = []; }
__axis_x_categories.push(d[xKey]);
}
} }
else {
x = i;
}
d.x = x; // used by event-rect
return {x: x, value: d[id] !== null && !isNaN(d[id]) ? +d[id] : null, id: convertedId}; return {x: x, value: d[id] !== null && !isNaN(d[id]) ? +d[id] : null, id: convertedId};
}) })
}; };
@ -3085,16 +3107,18 @@
} else { } else {
if (isCustomX && !isCategorized) { if (isCustomX && !isCategorized) {
rectW = function (d, i) { rectW = function (d, i) {
var prevX = getPrevX(i), nextX = getNextX(i); var prevX = getPrevX(i), nextX = getNextX(i), dx = c3.data.x[d.id][i];
return (x(nextX ? nextX : d.x + 50) - x(prevX ? prevX : d.x - 50)) / 2; return (x(nextX ? nextX : dx + 50) - x(prevX ? prevX : dx - 50)) / 2;
}; };
rectX = function (d, i) { rectX = function (d, i) {
var prevX = getPrevX(i); var prevX = getPrevX(i), dx = c3.data.x[d.id][i];
return (x(d.x) + x(prevX ? prevX : d.x - 50)) / 2; return (x(dx) + x(prevX ? prevX : dx - 50)) / 2;
}; };
} else { } else {
rectW = getEventRectWidth(); rectW = getEventRectWidth();
rectX = function (d) { return x(d.x) - (rectW / 2); }; rectX = function (d) {
return x(d.x) - (rectW / 2);
};
} }
// Set data // Set data
maxDataCountTarget = getMaxDataCountTarget(); maxDataCountTarget = getMaxDataCountTarget();
@ -3261,8 +3285,8 @@
.style("cursor", function (d) { return __data_selection_isselectable(d) ? "pointer" : null; }); .style("cursor", function (d) { return __data_selection_isselectable(d) ? "pointer" : null; });
// Update date for selected circles // Update date for selected circles
targets.forEach(function (t) { targets.forEach(function (t) {
main.selectAll('.' + CLASS.selectedCircles + getTargetSelectorSuffix(t.id)).selectAll('.' + CLASS.selectedCircle).each(function (d) { main.selectAll('.' + CLASS.selectedCircles + getTargetSelectorSuffix(t.id)).selectAll('.' + CLASS.selectedCircle).each(function (d, i) {
d.value = t.values[d.x].value; d.value = t.values[i].value;
}); });
}); });
// MEMO: can not keep same color... // MEMO: can not keep same color...
@ -3847,6 +3871,21 @@
return __data_names; return __data_names;
}; };
c3.x = function (x) {
if (arguments.length) {
updateTargetX(c3.data.targets, x);
redraw({withUpdateOrgXDomain: true, withUpdateXDomain: true});
}
return c3.data.x;
};
c3.xs = function (xs) {
if (arguments.length) {
updateTargetXs(c3.data.targets, xs);
redraw({withUpdateOrgXDomain: true, withUpdateXDomain: true});
}
return c3.data.x;
};
c3.resize = function (size) { c3.resize = function (size) {
__size_width = size ? size.width : null; __size_width = size ? size.width : null;
__size_height = size ? size.height : null; __size_height = size ? size.height : null;

4
c3.min.js vendored

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