Browse Source

Add axis.x.tick.culling and axis.x.tick.count option and fix jshint - #45

pull/51/head
Masayuki Tanaka 11 years ago
parent
commit
b622010667
  1. 92
      c3.js

92
c3.js

@ -81,10 +81,13 @@
__legend_item_onclick = getConfig(['legend', 'item', 'onclick'], function () {}); __legend_item_onclick = getConfig(['legend', 'item', 'onclick'], function () {});
// axis // axis
var __axis_x_type = getConfig(['axis', 'x', 'type'], 'indexed'), var __axis_rotated = getConfig(['axis', 'rotated'], false),
__axis_x_type = getConfig(['axis', 'x', 'type'], 'indexed'),
__axis_x_categories = getConfig(['axis', 'x', 'categories'], []), __axis_x_categories = getConfig(['axis', 'x', 'categories'], []),
__axis_x_tick_centered = getConfig(['axis', 'x', 'tick', 'centered'], false), __axis_x_tick_centered = getConfig(['axis', 'x', 'tick', 'centered'], false),
__axis_x_tick_format = getConfig(['axis', 'x', 'tick', 'format'], null), __axis_x_tick_format = getConfig(['axis', 'x', 'tick', 'format'], null),
__axis_x_tick_culling = getConfig(['axis', 'x', 'tick', 'culling'], __axis_rotated || __axis_x_type === 'categorized' ? false : true),
__axis_x_tick_count = getConfig(['axis', 'x', 'tick', 'count'], 10),
__axis_x_default = getConfig(['axis', 'x', 'default'], null), __axis_x_default = getConfig(['axis', 'x', 'default'], null),
__axis_x_label = getConfig(['axis', 'x', 'label'], null), __axis_x_label = getConfig(['axis', 'x', 'label'], null),
__axis_y_max = getConfig(['axis', 'y', 'max'], null), __axis_y_max = getConfig(['axis', 'y', 'max'], null),
@ -104,8 +107,7 @@
__axis_y2_inner = getConfig(['axis', 'y2', 'inner'], false), __axis_y2_inner = getConfig(['axis', 'y2', 'inner'], false),
__axis_y2_tick_format = getConfig(['axis', 'y2', 'tick', 'format'], null), __axis_y2_tick_format = getConfig(['axis', 'y2', 'tick', 'format'], null),
__axis_y2_padding = getConfig(['axis', 'y2', 'padding'], null), __axis_y2_padding = getConfig(['axis', 'y2', 'padding'], null),
__axis_y2_ticks = getConfig(['axis', 'y2', 'ticks'], 10), __axis_y2_ticks = getConfig(['axis', 'y2', 'ticks'], 10);
__axis_rotated = getConfig(['axis', 'rotated'], false);
// grid // grid
var __grid_x_show = getConfig(['grid', 'x', 'show'], false), var __grid_x_show = getConfig(['grid', 'x', 'show'], false),
@ -396,10 +398,8 @@
// Set tick // Set tick
axis.tickFormat(tickFormat).ticks(ticks); axis.tickFormat(tickFormat).ticks(ticks);
// Set categories
if (isCategorized) { if (isCategorized) {
axis.categories(__axis_x_categories).tickCentered(__axis_x_tick_centered); axis.tickCentered(__axis_x_tick_centered);
} else { } else {
axis.tickOffset = function () { axis.tickOffset = function () {
var base = __axis_rotated ? height : width; var base = __axis_rotated ? height : width;
@ -407,6 +407,11 @@
}; };
} }
// Set categories
if (isCategorized) {
axis.categories(__axis_x_categories);
}
return axis; return axis;
} }
function getYAxis(scale, orient, tickFormat, ticks) { function getYAxis(scale, orient, tickFormat, ticks) {
@ -428,7 +433,7 @@
} }
function getXAxisTicks() { function getXAxisTicks() {
var maxDataCount = getMaxDataCount(); var maxDataCount = getMaxDataCount();
return maxDataCount < 10 ? maxDataCount : 10; return __axis_x_tick_culling && maxDataCount > __axis_x_tick_count ? __axis_x_tick_count : maxDataCount;
} }
//-- Arc --// //-- Arc --//
@ -1386,7 +1391,7 @@
}; };
//showValues on Bar - added 23.2.2014 - fiery- //showValues on Bar - added 23.2.2014 - fiery-
var textVal = function (barIndices, isSub_, inIsXY) { var textVal = function (barIndices, isSub_, inIsXY) {
var barTargetsNum = barIndices.__max__ + 1, var barTargetsNum = barIndices.__max__ + 1,
isSub = arguments.length > 1 ? isSub_ : true, isSub = arguments.length > 1 ? isSub_ : true,
barW = getBarW(xAxis, barTargetsNum, !!isSub), barW = getBarW(xAxis, barTargetsNum, !!isSub),
@ -1411,8 +1416,8 @@
var indexX = __axis_rotated ? 1 : 0; var indexX = __axis_rotated ? 1 : 0;
var indexY = __axis_rotated ? 0 : 1; var indexY = __axis_rotated ? 0 : 1;
if(inIsXY == 'X') return d.value < 0 ? points[2][indexX] - 4 : points[2][indexX] + 4; if (inIsXY === 'X') { return d.value < 0 ? points[2][indexX] - 4 : points[2][indexX] + 4; }
return (points[0][indexY] + points[2][indexY])/2; return (points[0][indexY] + points[2][indexY]) / 2;
}; };
}; };
@ -2217,8 +2222,6 @@
.style("opacity", initialOpacity) .style("opacity", initialOpacity)
.transition().duration(duration) .transition().duration(duration)
.attr('d', drawBar(barIndices, false)) .attr('d', drawBar(barIndices, false))
// .style("fill", function (d) { return color(d.id); })
// .attr("class", classBar)
.style("opacity", 1); .style("opacity", 1);
mainBar.exit().transition().duration(duration) mainBar.exit().transition().duration(duration)
.style('opacity', 0) .style('opacity', 0)
@ -2226,28 +2229,28 @@
//showValues on Bar - added 23.2.2014 - fiery- //showValues on Bar - added 23.2.2014 - fiery-
if (showValues) { if (showValues) {
mainBarTxt = main.selectAll('.-bars').selectAll('.-bartxt') mainBarTxt = main.selectAll('.-bars').selectAll('.-bartxt')
.data(barData); .data(barData);
mainBarTxt.enter().append('text') mainBarTxt.enter().append('text')
.attr('text-anchor', function(d,i) { return d.value < 0 ? 'end' : 'start' ; }) .attr('text-anchor', function (d) { return d.value < 0 ? 'end' : 'start'; })
.attr('y', textVal(barIndices, false, 'Y')) .attr('y', textVal(barIndices, false, 'Y'))
.attr('x', textVal(barIndices, false, 'X')) .attr('x', textVal(barIndices, false, 'X'))
.attr('dy', '.32em') .attr('dy', '.32em')
.style("stroke", 'none') .style("stroke", 'none')
.style("opacity", 0) .style("opacity", 0)
.text(function(d,i) { return d3.format(',.2f')(d.value); }) .text(function (d) { return d3.format(',.2f')(d.value); })
.attr("class", classTextBar); .attr("class", classTextBar);
mainBarTxt mainBarTxt
.style("opacity", initialOpacity) .style("opacity", initialOpacity)
.transition().duration(duration) .transition().duration(duration)
.attr('y', textVal(barIndices, false, 'Y')) .attr('y', textVal(barIndices, false, 'Y'))
.attr('x', textVal(barIndices, false, 'X')) .attr('x', textVal(barIndices, false, 'X'))
.style("opacity", 1); .style("opacity", 1);
mainBarTxt.exit() mainBarTxt.exit()
.transition().duration(duration) .transition().duration(duration)
.style('opacity', 0) .style('opacity', 0)
.remove(); .remove();
} }
// lines and cricles // lines and cricles
main.selectAll('.-line') main.selectAll('.-line')
@ -3020,7 +3023,7 @@
function categoryAxis() { function categoryAxis() {
var scale = d3.scale.linear(), orient = "bottom"; var scale = d3.scale.linear(), orient = "bottom";
var tickMajorSize = 6, /*tickMinorSize = 6,*/ tickEndSize = 6, tickPadding = 3, tickCentered = false, tickTextNum = 10, tickOffset = 0, tickFormat = null; var tickMajorSize = 6, /*tickMinorSize = 6,*/ tickEndSize = 6, tickPadding = 3, tickCentered = false, tickTextNum = 10, tickOffset = 0, tickFormat = null, tickCulling = true;
var categories = []; var categories = [];
function axisX(selection, x) { function axisX(selection, x) {
selection.attr("transform", function (d) { selection.attr("transform", function (d) {
@ -3047,7 +3050,8 @@
return ticks; return ticks;
} }
function shouldShowTickText(ticks, i) { function shouldShowTickText(ticks, i) {
return true || ticks.length < tickTextNum || i % Math.ceil(ticks.length / tickTextNum) === 0; var length = ticks.length - 1;
return length <= tickTextNum || i % Math.ceil(length / tickTextNum) === 0;
} }
function category(i) { function category(i) {
return i < categories.length ? categories[i] : i; return i < categories.length ? categories[i] : i;
@ -3168,11 +3172,6 @@
tickCentered = x; tickCentered = x;
return axis; return axis;
}; };
axis.tickTextNum = function (x) {
if (!arguments.length) { return tickTextNum; }
tickTextNum = x;
return axis;
};
axis.tickFormat = function (format) { axis.tickFormat = function (format) {
if (!arguments.length) { return tickFormat; } if (!arguments.length) { return tickFormat; }
tickFormat = format; tickFormat = format;
@ -3181,8 +3180,15 @@
axis.tickOffset = function () { axis.tickOffset = function () {
return tickOffset; return tickOffset;
}; };
axis.ticks = function () { axis.ticks = function (n) {
return; // TODO: implement if (!arguments.length) { return tickTextNum; }
tickTextNum = n;
return axis;
};
axis.tickCulling = function (culling) {
if (!arguments.length) { return tickCulling; }
tickCulling = culling;
return axis;
}; };
return axis; return axis;
} }

Loading…
Cancel
Save