Browse Source

Fix show/hide API and refactor target ids

pull/155/head
Masayuki Tanaka 11 years ago
parent
commit
0a3333bd1e
  1. 54
      c3.js
  2. 4
      c3.min.js

54
c3.js

@ -1085,7 +1085,7 @@
//-- Domain --// //-- Domain --//
function getYDomainMin(targets) { function getYDomainMin(targets) {
var ids = getTargetIds(targets), ys = getValuesAsIdKeyed(targets), j, k, baseId, idsInGroup, id, hasNegativeValue; var ids = mapToIds(targets), ys = getValuesAsIdKeyed(targets), j, k, baseId, idsInGroup, id, hasNegativeValue;
if (__data_groups.length > 0) { if (__data_groups.length > 0) {
hasNegativeValue = hasNegativeValueInTargets(targets); hasNegativeValue = hasNegativeValueInTargets(targets);
for (j = 0; j < __data_groups.length; j++) { for (j = 0; j < __data_groups.length; j++) {
@ -1114,7 +1114,7 @@
return d3.min(Object.keys(ys).map(function (key) { return d3.min(ys[key]); })); return d3.min(Object.keys(ys).map(function (key) { return d3.min(ys[key]); }));
} }
function getYDomainMax(targets) { function getYDomainMax(targets) {
var ids = getTargetIds(targets), ys = getValuesAsIdKeyed(targets), j, k, baseId, idsInGroup, id, hasPositiveValue; var ids = mapToIds(targets), ys = getValuesAsIdKeyed(targets), j, k, baseId, idsInGroup, id, hasPositiveValue;
if (__data_groups.length > 0) { if (__data_groups.length > 0) {
hasPositiveValue = hasPositiveValueInTargets(targets); hasPositiveValue = hasPositiveValueInTargets(targets);
for (j = 0; j < __data_groups.length; j++) { for (j = 0; j < __data_groups.length; j++) {
@ -1397,7 +1397,7 @@
// set target types // set target types
if (__data_type) { if (__data_type) {
setTargetType(getTargetIds(targets).filter(function (id) { return ! (id in __data_types); }), __data_type); setTargetType(mapToIds(targets).filter(function (id) { return ! (id in __data_types); }), __data_type);
} }
// cache as original id keyed // cache as original id keyed
@ -1439,12 +1439,14 @@
} }
return maxTarget; return maxTarget;
} }
function getTargetIds(targets) { function mapToIds(targets) {
targets = isUndefined(targets) ? c3.data.targets : targets;
return targets.map(function (d) { return d.id; }); return targets.map(function (d) { return d.id; });
} }
function hasTarget(id) { function mapToTargetIds(ids) {
var ids = getTargetIds(), i; return ids ? (typeof ids === 'string' ? [ids] : ids) : mapToIds(c3.data.targets);
}
function hasTarget(targets, id) {
var ids = mapToIds(targets), i;
for (i = 0; i < ids.length; i++) { for (i = 0; i < ids.length; i++) {
if (ids[i] === id) { if (ids[i] === id) {
return true; return true;
@ -1549,6 +1551,7 @@
return targetId || targetId === 0 ? '-' + (targetId.replace ? targetId.replace(/([^a-zA-Z0-9-_])/g, '-') : targetId) : ''; return targetId || targetId === 0 ? '-' + (targetId.replace ? targetId.replace(/([^a-zA-Z0-9-_])/g, '-') : targetId) : '';
} }
function selectorTarget(id) { return '.' + CLASS.target + getTargetSelectorSuffix(id); } function selectorTarget(id) { return '.' + CLASS.target + getTargetSelectorSuffix(id); }
function selectorTargets(ids) { return ids.map(function (id) { return selectorTarget(id); }); }
function initialOpacity(d) { function initialOpacity(d) {
return d.value !== null && withoutFadeIn[d.id] ? 1 : 0; return d.value !== null && withoutFadeIn[d.id] ? 1 : 0;
@ -1840,11 +1843,10 @@
//-- Type --// //-- Type --//
function setTargetType(targetIds, type) { function setTargetType(targetIds, type) {
var i, ids = targetIds ? (typeof targetIds === 'string' ? [targetIds] : targetIds) : getTargetIds(); mapToTargetIds(targetIds).forEach(function (id) {
for (i = 0; i < ids.length; i++) { withoutFadeIn[id] = (type === __data_types[id]);
withoutFadeIn[ids[i]] = (type === __data_types[ids[i]]); __data_types[id] = type;
__data_types[ids[i]] = type; });
}
} }
function hasType(targets, type) { function hasType(targets, type) {
var has = false; var has = false;
@ -3288,7 +3290,7 @@
.remove(); .remove();
// update fadein condition // update fadein condition
getTargetIds().forEach(function (id) { mapToIds(c3.data.targets).forEach(function (id) {
withoutFadeIn[id] = true; withoutFadeIn[id] = true;
}); });
} }
@ -3545,7 +3547,7 @@
done = function () {}; done = function () {};
} }
// filter existing target // filter existing target
targetIds = targetIds.filter(function (id) { return hasTarget(id); }); targetIds = targetIds.filter(function (id) { return hasTarget(c3.data.targets, id); });
// If no target, call done and return // If no target, call done and return
if (!targetIds || targetIds.length === 0) { if (!targetIds || targetIds.length === 0) {
done(); done();
@ -3595,7 +3597,7 @@
} }
function updateLegend(targets, options) { function updateLegend(targets, options) {
var ids = getTargetIds(targets), l; var ids = mapToIds(targets), l;
var xForLegend, xForLegendText, xForLegendRect, yForLegend, yForLegendText, yForLegendRect; var xForLegend, xForLegendText, xForLegendRect, yForLegend, yForLegendText, yForLegendRect;
var paddingTop = 4, paddingRight = 26, maxWidth = 0, maxHeight = 0, posMin = 10; var paddingTop = 4, paddingRight = 26, maxWidth = 0, maxHeight = 0, posMin = 10;
var totalLength = 0, offsets = {}, widths = {}, heights = {}, margins = {}, steps = {}, step = 0; var totalLength = 0, offsets = {}, widths = {}, heights = {}, margins = {}, steps = {}, step = 0;
@ -3743,10 +3745,10 @@
/*-- Event Handling --*/ /*-- Event Handling --*/
function isNoneArc(d) { function isNoneArc(d) {
return hasTarget(d.id); return hasTarget(c3.data.targets, d.id);
} }
function isArc(d) { function isArc(d) {
return 'data' in d && hasTarget(d.data.id); return 'data' in d && hasTarget(c3.data.targets, d.data.id);
} }
function getGridFilter(params) { function getGridFilter(params) {
var value = params && params.value ? params.value : null, var value = params && params.value ? params.value : null,
@ -3807,17 +3809,19 @@
revertLegend(); revertLegend();
}; };
c3.show = function (targetId) { c3.show = function (targetIds) {
removeHiddenTargetIds(targetId); targetIds = mapToTargetIds(targetIds);
svg.selectAll(selectorTarget(targetId)) removeHiddenTargetIds(targetIds);
svg.selectAll(selectorTargets(targetIds))
.transition() .transition()
.style('opacity', 1); .style('opacity', 1);
redraw({withUpdateOrgXDomain: true, withUpdateXDomain: true, withLegend: false}); redraw({withUpdateOrgXDomain: true, withUpdateXDomain: true, withLegend: false});
}; };
c3.hide = function (targetId) { c3.hide = function (targetIds) {
addHiddenTargetIds(targetId); targetIds = mapToTargetIds(targetIds);
svg.selectAll(selectorTarget(targetId)) addHiddenTargetIds(targetIds);
svg.selectAll(selectorTargets(targetIds))
.transition() .transition()
.style('opacity', 0); .style('opacity', 0);
redraw({withUpdateOrgXDomain: true, withUpdateXDomain: true, withLegend: false}); redraw({withUpdateOrgXDomain: true, withUpdateXDomain: true, withLegend: false});
@ -3850,7 +3854,7 @@
// unload if needed // unload if needed
if ('unload' in args) { if ('unload' in args) {
// TODO: do not unload if target will load (included in url/rows/columns) // TODO: do not unload if target will load (included in url/rows/columns)
unload(typeof args.unload === 'string' ? [args.unload] : typeof args.unload === 'boolean' && args.unload ? getTargetIds() : args.unload, function () { unload(mapToTargetIds((typeof args.unload === 'boolean' && args.unload) ? null : args.unload), function () {
loadFromArgs(args); loadFromArgs(args);
}); });
} else { } else {
@ -3859,7 +3863,7 @@
}; };
c3.unload = function (targetIds) { c3.unload = function (targetIds) {
unload(targetIds ? typeof targetIds === 'string' ? [targetIds] : targetIds : getTargetIds(), function () { unload(mapToTargetIds(targetIds), function () {
redraw({withUpdateOrgXDomain: true, withUpdateXDomain: true, withLegend: true}); redraw({withUpdateOrgXDomain: true, withUpdateXDomain: true, withLegend: true});
}); });
}; };

4
c3.min.js vendored

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