Browse Source

Introduce AxisInternal (#2099)

* Introduce AxisInternal

* Fix c3.axis import

* Fix lint

* Fix axis tick multiline

* Fix spec for axis tick multiline

* Change 23023$ to internal

* Use class and extend instead of inherit
pull/2152/head
Masayuki Tanaka 7 years ago committed by Yoshiya Hinosawa
parent
commit
67324e4bef
  1. 642
      c3.js
  2. 2
      c3.min.js
  3. 20
      spec/axis-spec.js
  4. 8
      spec/data-spec.js
  5. 471
      src/axis.js
  6. 330
      src/c3.axis.js
  7. 23
      src/core.js
  8. 1
      src/index.js

642
c3.js

File diff suppressed because it is too large Load Diff

2
c3.min.js vendored

File diff suppressed because one or more lines are too long

20
spec/axis-spec.js

@ -235,7 +235,7 @@ describe('c3 chart axis', function () {
it('should split x axis tick text to multiple lines', function () {
var ticks = chart.internal.main.select('.c3-axis-x').selectAll('g.tick'),
expectedTexts = ['very long tick text', 'on x axis'],
expectedTexts = ['very long tick text on x', 'axis'],
expectedX = '0';
expect(ticks.size()).toBe(6);
ticks.each(function () {
@ -315,7 +315,7 @@ describe('c3 chart axis', function () {
it('should split x axis tick text to multiple lines', function () {
var ticks = chart.internal.main.select('.c3-axis-x').selectAll('g.tick'),
expectedTexts = ['very long tick', 'text on x axis'],
expectedTexts = ['very long tick text on', 'x axis'],
expectedX = '-9';
expect(ticks.size()).toBe(6);
ticks.each(function () {
@ -406,9 +406,9 @@ describe('c3 chart axis', function () {
var tick = chart.internal.main.select('.c3-axis-x').select('g.tick'),
tspans = tick.selectAll('tspan'),
expectedTickTexts = [
'this is a very',
'long tick text',
'on category axis'
'this is a very long',
'tick text on category',
'axis'
],
expectedX = '0';
expect(tspans.size()).toBe(3);
@ -453,9 +453,9 @@ describe('c3 chart axis', function () {
var tick = chart.internal.main.select('.c3-axis-x').select('g.tick'),
tspans = tick.selectAll('tspan'),
expectedTickTexts = [
'this is a very',
'long tick text on',
'category axis'
'this is a very long',
'tick text on category',
'axis'
],
expectedX = '-9';
expect(tspans.size()).toBe(3);
@ -504,8 +504,8 @@ describe('c3 chart axis', function () {
var tick = chart.internal.main.select('.c3-axis-x').select('g.tick'),
tspans = tick.selectAll('tspan'),
expectedTickTexts = [
'this is a very long tick',
'text on category axis'
'this is a very long tick text on',
'category axis'
],
expectedX = '-9';
expect(tspans.size()).toBe(2);

8
spec/data-spec.js

@ -1094,7 +1094,7 @@ describe('c3 chart data', function () {
it('should have y domain with proper padding', function () {
var domain = chart.internal.y.domain();
expect(domain[0]).toBeCloseTo(-259, -1);
expect(domain[0]).toBeCloseTo(-253, -1);
expect(domain[1]).toBeCloseTo(260, -1);
});
@ -1117,7 +1117,7 @@ describe('c3 chart data', function () {
it('should have y domain with proper padding', function () {
var domain = chart.internal.y.domain();
expect(domain[0]).toBeCloseTo(-259, -1);
expect(domain[0]).toBeCloseTo(-253, -1);
expect(domain[1]).toBeCloseTo(260, -1);
});
@ -1349,8 +1349,8 @@ describe('c3 chart data', function () {
it('should have y domain with proper padding', function () {
var domain = chart.internal.y.domain();
expect(domain[0]).toBeCloseTo(-899, -1);
expect(domain[1]).toBeCloseTo(101, -1.2);
expect(domain[0]).toBeCloseTo(-893, -1);
expect(domain[1]).toBeCloseTo(93, -1);
});
it('should locate labels above each data point', function () {

471
src/axis.js

@ -1,16 +1,387 @@
import CLASS from './class';
import { inherit, API } from './core';
import { Component } from './core';
import { isValue, isFunction, isString, isEmpty } from './util';
import c3_axis from './c3.axis';
export default function Axis(owner) {
API.call(this, owner);
export var c3_axis_fn;
export var c3_axis_internal_fn;
function AxisInternal(component, params) {
var internal = this;
internal.component = component;
internal.params = params || {};
internal.d3 = component.d3;
internal.scale = internal.d3.scale.linear();
internal.range;
internal.orient = "bottom";
internal.innerTickSize = 6;
internal.outerTickSize = this.params.withOuterTick ? 6 : 0;
internal.tickPadding = 3;
internal.tickValues = null;
internal.tickFormat;
internal.tickArguments;
internal.tickOffset = 0;
internal.tickCulling = true;
internal.tickCentered;
internal.tickTextCharSize;
internal.tickTextRotate = internal.params.tickTextRotate;
internal.tickLength;
internal.axis = internal.generateAxis();
}
c3_axis_internal_fn = AxisInternal.prototype;
c3_axis_internal_fn.axisX = function (selection, x, tickOffset) {
selection.attr("transform", function (d) {
return "translate(" + Math.ceil(x(d) + tickOffset) + ", 0)";
});
};
c3_axis_internal_fn.axisY = function (selection, y) {
selection.attr("transform", function (d) {
return "translate(0," + Math.ceil(y(d)) + ")";
});
};
c3_axis_internal_fn.scaleExtent = function (domain) {
var start = domain[0], stop = domain[domain.length - 1];
return start < stop ? [ start, stop ] : [ stop, start ];
};
c3_axis_internal_fn.generateTicks = function (scale) {
var internal = this;
var i, domain, ticks = [];
if (scale.ticks) {
return scale.ticks.apply(scale, internal.tickArguments);
}
domain = scale.domain();
for (i = Math.ceil(domain[0]); i < domain[1]; i++) {
ticks.push(i);
}
if (ticks.length > 0 && ticks[0] > 0) {
ticks.unshift(ticks[0] - (ticks[1] - ticks[0]));
}
return ticks;
};
c3_axis_internal_fn.copyScale = function () {
var internal = this;
var newScale = internal.scale.copy(), domain;
if (internal.params.isCategory) {
domain = internal.scale.domain();
newScale.domain([domain[0], domain[1] - 1]);
}
return newScale;
};
c3_axis_internal_fn.textFormatted = function (v) {
var internal = this,
formatted = internal.tickFormat ? internal.tickFormat(v) : v;
return typeof formatted !== 'undefined' ? formatted : '';
};
c3_axis_internal_fn.updateRange = function () {
var internal = this;
internal.range = internal.scale.rangeExtent ? internal.scale.rangeExtent() : internal.scaleExtent(internal.scale.range());
return internal.range;
};
c3_axis_internal_fn.updateTickTextCharSize = function (tick) {
var internal = this;
if (internal.tickTextCharSize) {
return internal.tickTextCharSize;
}
var size = {
h: 11.5,
w: 5.5
};
tick.select('text').text(function(d) { return internal.textFormatted(d); }).each(function (d) {
var box = this.getBoundingClientRect(),
text = internal.textFormatted(d),
h = box.height,
w = text ? (box.width / text.length) : undefined;
if (h && w) {
size.h = h;
size.w = w;
}
}).text('');
internal.tickTextCharSize = size;
return size;
};
c3_axis_internal_fn.transitionise = function (selection) {
return this.params.withoutTransition ? selection : this.d3.transition(selection);
};
c3_axis_internal_fn.isVertical = function () {
return this.orient === 'left' || this.orient === 'right';
};
c3_axis_internal_fn.tspanData = function (d, i, ticks, scale) {
var internal = this;
var splitted = internal.params.tickMultiline ? internal.splitTickText(d, ticks, scale) : [].concat(internal.textFormatted(d));
return splitted.map(function (s) {
return { index: i, splitted: s, length: splitted.length };
});
};
c3_axis_internal_fn.splitTickText = function (d, ticks, scale) {
var internal = this,
tickText = internal.textFormatted(d),
maxWidth = internal.params.tickWidth,
subtext, spaceIndex, textWidth, splitted = [];
if (Object.prototype.toString.call(tickText) === "[object Array]") {
return tickText;
}
if (!maxWidth || maxWidth <= 0) {
maxWidth = internal.isVertical() ? 95 : internal.params.isCategory ? (Math.ceil(scale(ticks[1]) - scale(ticks[0])) - 12) : 110;
}
function split(splitted, text) {
spaceIndex = undefined;
for (var i = 1; i < text.length; i++) {
if (text.charAt(i) === ' ') {
spaceIndex = i;
}
subtext = text.substr(0, i + 1);
textWidth = internal.tickTextCharSize.w * subtext.length;
// if text width gets over tick width, split by space index or crrent index
if (maxWidth < textWidth) {
return split(
splitted.concat(text.substr(0, spaceIndex ? spaceIndex : i)),
text.slice(spaceIndex ? spaceIndex + 1 : i)
);
}
}
return splitted.concat(text);
}
return split(splitted, tickText + "");
};
c3_axis_internal_fn.updateTickLength = function () {
var internal = this;
internal.tickLength = Math.max(internal.innerTickSize, 0) + internal.tickPadding;
};
c3_axis_internal_fn.lineY2 = function (d) {
var internal = this,
tickPosition = internal.scale(d) + (internal.tickCentered ? 0 : internal.tickOffset);
return internal.range[0] < tickPosition && tickPosition < internal.range[1] ? internal.innerTickSize : 0;
};
c3_axis_internal_fn.textY = function (){
var internal = this, rotate = internal.tickTextRotate;
return rotate ? 11.5 - 2.5 * (rotate / 15) * (rotate > 0 ? 1 : -1) : internal.tickLength;
};
c3_axis_internal_fn.textTransform = function () {
var internal = this, rotate = internal.tickTextRotate;
return rotate ? "rotate(" + rotate + ")" : "";
};
c3_axis_internal_fn.textTextAnchor = function () {
var internal = this, rotate = internal.tickTextRotate;
return rotate ? (rotate > 0 ? "start" : "end") : "middle";
};
c3_axis_internal_fn.tspanDx = function () {
var internal = this, rotate = internal.tickTextRotate;
return rotate ? 8 * Math.sin(Math.PI * (rotate / 180)) : 0;
};
c3_axis_internal_fn.tspanDy = function (d, i) {
var internal = this,
dy = internal.tickTextCharSize.h;
if (i === 0) {
if (internal.isVertical()) {
dy = -((d.length - 1) * (internal.tickTextCharSize.h / 2) - 3);
} else {
dy = ".71em";
}
}
return dy;
};
c3_axis_internal_fn.generateAxis = function () {
var internal = this, d3 = internal.d3, params = internal.params;
function axis(g) {
g.each(function () {
var g = axis.g = d3.select(this);
var scale0 = this.__chart__ || internal.scale,
scale1 = this.__chart__ = internal.copyScale();
var ticks = internal.tickValues ? internal.tickValues : internal.generateTicks(scale1),
tick = g.selectAll(".tick").data(ticks, scale1),
tickEnter = tick.enter().insert("g", ".domain").attr("class", "tick").style("opacity", 1e-6),
// MEMO: No exit transition. The reason is this transition affects max tick width calculation because old tick will be included in the ticks.
tickExit = tick.exit().remove(),
tickUpdate = internal.transitionise(tick).style("opacity", 1),
tickTransform, tickX, tickY;
if (params.isCategory) {
internal.tickOffset = Math.ceil((scale1(1) - scale1(0)) / 2);
tickX = internal.tickCentered ? 0 : internal.tickOffset;
tickY = internal.tickCentered ? internal.tickOffset : 0;
} else {
internal.tickOffset = tickX = 0;
}
inherit(API, Axis);
tickEnter.append("line");
tickEnter.append("text");
Axis.prototype.init = function init() {
internal.updateRange();
internal.updateTickLength();
internal.updateTickTextCharSize(g.select('.tick'));
var lineUpdate = tickUpdate.select("line"),
textUpdate = tickUpdate.select("text"),
tspanUpdate = tick.select("text").selectAll('tspan')
.data(function (d, i) { return internal.tspanData(d, i, ticks, scale1); });
tspanUpdate.enter().append('tspan');
tspanUpdate.exit().remove();
tspanUpdate.text(function (d) { return d.splitted; });
var path = g.selectAll(".domain").data([ 0 ]),
pathUpdate = (path.enter().append("path").attr("class", "domain"), internal.transitionise(path));
// TODO: each attr should be one function and change its behavior by internal.orient, probably
switch (internal.orient) {
case "bottom":
{
tickTransform = internal.axisX;
lineUpdate.attr("x1", tickX)
.attr("x2", tickX)
.attr("y2", function (d, i) { return internal.lineY2(d, i); });
textUpdate.attr("x", 0)
.attr("y", function (d, i) { return internal.textY(d, i); })
.attr("transform", function (d, i) { return internal.textTransform(d, i); })
.style("text-anchor", function (d, i) { return internal.textTextAnchor(d, i); });
tspanUpdate.attr('x', 0)
.attr("dy", function (d, i) { return internal.tspanDy(d, i); })
.attr('dx', function (d, i) { return internal.tspanDx(d, i); });
pathUpdate.attr("d", "M" + internal.range[0] + "," + internal.outerTickSize + "V0H" + internal.range[1] + "V" + internal.outerTickSize);
break;
}
case "top":
{
// TODO: rotated tick text
tickTransform = internal.axisX;
lineUpdate.attr("x2", 0)
.attr("y2", -internal.innerTickSize);
textUpdate.attr("x", 0)
.attr("y", -internal.tickLength)
.style("text-anchor", "middle");
tspanUpdate.attr('x', 0)
.attr("dy", "0em");
pathUpdate.attr("d", "M" + internal.range[0] + "," + -internal.outerTickSize + "V0H" + internal.range[1] + "V" + -internal.outerTickSize);
break;
}
case "left":
{
tickTransform = internal.axisY;
lineUpdate.attr("x2", -internal.innerTickSize)
.attr("y1", tickY)
.attr("y2", tickY);
textUpdate.attr("x", -internal.tickLength)
.attr("y", internal.tickOffset)
.style("text-anchor", "end");
tspanUpdate.attr('x', -internal.tickLength)
.attr("dy", function (d, i) { return internal.tspanDy(d, i); });
pathUpdate.attr("d", "M" + -internal.outerTickSize + "," + internal.range[0] + "H0V" + internal.range[1] + "H" + -internal.outerTickSize);
break;
}
case "right":
{
tickTransform = internal.axisY;
lineUpdate.attr("x2", internal.innerTickSize)
.attr("y2", 0);
textUpdate.attr("x", internal.tickLength)
.attr("y", 0)
.style("text-anchor", "start");
tspanUpdate.attr('x', internal.tickLength)
.attr("dy", function (d, i) { return internal.tspanDy(d, i); });
pathUpdate.attr("d", "M" + internal.outerTickSize + "," + internal.range[0] + "H0V" + internal.range[1] + "H" + internal.outerTickSize);
break;
}
}
if (scale1.rangeBand) {
var x = scale1, dx = x.rangeBand() / 2;
scale0 = scale1 = function (d) {
return x(d) + dx;
};
} else if (scale0.rangeBand) {
scale0 = scale1;
} else {
tickExit.call(tickTransform, scale1, internal.tickOffset);
}
tickEnter.call(tickTransform, scale0, internal.tickOffset);
tickUpdate.call(tickTransform, scale1, internal.tickOffset);
});
}
axis.scale = function (x) {
if (!arguments.length) { return internal.scale; }
internal.scale = x;
return axis;
};
axis.orient = function (x) {
if (!arguments.length) { return internal.orient; }
internal.orient = x in {top: 1, right: 1, bottom: 1, left: 1} ? x + "" : "bottom";
return axis;
};
axis.tickFormat = function (format) {
if (!arguments.length) { return internal.tickFormat; }
internal.tickFormat = format;
return axis;
};
axis.tickCentered = function (isCentered) {
if (!arguments.length) { return internal.tickCentered; }
internal.tickCentered = isCentered;
return axis;
};
axis.tickOffset = function () {
return internal.tickOffset;
};
axis.tickInterval = function () {
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();
}
return interval === Infinity ? 0 : interval;
};
axis.ticks = function () {
if (!arguments.length) { return internal.tickArguments; }
internal.tickArguments = arguments;
return axis;
};
axis.tickCulling = function (culling) {
if (!arguments.length) { return internal.tickCulling; }
internal.tickCulling = culling;
return axis;
};
axis.tickValues = function (x) {
if (typeof x === 'function') {
internal.tickValues = function () {
return x(internal.scale.domain());
};
}
else {
if (!arguments.length) { return internal.tickValues; }
internal.tickValues = x;
}
return axis;
};
return axis;
};
export default class Axis extends Component {
constructor (owner) {
var fn = {
fn: c3_axis_fn,
internal: {
fn: c3_axis_internal_fn
}
};
super(owner, 'axis', fn);
this.d3 = owner.d3;
this.internal = AxisInternal;
}
}
c3_axis_fn = Axis.prototype;
c3_axis_fn.init = function init() {
var $$ = this.owner, config = $$.config, main = $$.main;
$$.axes.x = main.append("g")
.attr("class", CLASS.axis + ' ' + CLASS.axisX)
@ -41,7 +412,7 @@ Axis.prototype.init = function init() {
.attr("transform", config.axis_rotated ? "" : "rotate(-90)")
.style("text-anchor", this.textAnchorForY2AxisLabel.bind(this));
};
Axis.prototype.getXAxis = function getXAxis(scale, orient, tickFormat, tickValues, withOuterTick, withoutTransition, withoutRotateTickText) {
c3_axis_fn.getXAxis = function getXAxis(scale, orient, tickFormat, tickValues, withOuterTick, withoutTransition, withoutRotateTickText) {
var $$ = this.owner, config = $$.config,
axisParams = {
isCategory: $$.isCategorized(),
@ -51,7 +422,7 @@ Axis.prototype.getXAxis = function getXAxis(scale, orient, tickFormat, tickValue
tickTextRotate: withoutRotateTickText ? 0 : config.axis_x_tick_rotate,
withoutTransition: withoutTransition,
},
axis = c3_axis($$.d3, axisParams).scale(scale).orient(orient);
axis = new this.internal(this, axisParams).axis.scale(scale).orient(orient);
if ($$.isTimeSeries() && tickValues && typeof tickValues !== "function") {
tickValues = tickValues.map(function (v) { return $$.parseDate(v); });
@ -68,7 +439,7 @@ Axis.prototype.getXAxis = function getXAxis(scale, orient, tickFormat, tickValue
return axis;
};
Axis.prototype.updateXAxisTickValues = function updateXAxisTickValues(targets, axis) {
c3_axis_fn.updateXAxisTickValues = function updateXAxisTickValues(targets, axis) {
var $$ = this.owner, config = $$.config, tickValues;
if (config.axis_x_tick_fit || config.axis_x_tick_count) {
tickValues = this.generateTickValues($$.mapTargetsToUniqueXs(targets), config.axis_x_tick_count, $$.isTimeSeries());
@ -81,14 +452,14 @@ Axis.prototype.updateXAxisTickValues = function updateXAxisTickValues(targets, a
}
return tickValues;
};
Axis.prototype.getYAxis = function getYAxis(scale, orient, tickFormat, tickValues, withOuterTick, withoutTransition, withoutRotateTickText) {
c3_axis_fn.getYAxis = function getYAxis(scale, orient, tickFormat, tickValues, withOuterTick, withoutTransition, withoutRotateTickText) {
var $$ = this.owner, config = $$.config,
axisParams = {
withOuterTick: withOuterTick,
withoutTransition: withoutTransition,
tickTextRotate: withoutRotateTickText ? 0 : config.axis_y_tick_rotate
},
axis = c3_axis($$.d3, axisParams).scale(scale).orient(orient).tickFormat(tickFormat);
axis = new this.internal(this, axisParams).axis.scale(scale).orient(orient).tickFormat(tickFormat);
if ($$.isTimeSeriesY()) {
axis.ticks($$.d3.time[config.axis_y_tick_time_value], config.axis_y_tick_time_interval);
} else {
@ -96,11 +467,11 @@ Axis.prototype.getYAxis = function getYAxis(scale, orient, tickFormat, tickValue
}
return axis;
};
Axis.prototype.getId = function getId(id) {
c3_axis_fn.getId = function getId(id) {
var config = this.owner.config;
return id in config.data_axes ? config.data_axes[id] : 'y';
};
Axis.prototype.getXAxisTickFormat = function getXAxisTickFormat() {
c3_axis_fn.getXAxisTickFormat = function getXAxisTickFormat() {
var $$ = this.owner, config = $$.config,
format = $$.isTimeSeries() ? $$.defaultAxisTimeFormat : $$.isCategorized() ? $$.categoryName : function (v) { return v < 0 ? v.toFixed(0) : v; };
if (config.axis_x_tick_format) {
@ -114,19 +485,19 @@ Axis.prototype.getXAxisTickFormat = function getXAxisTickFormat() {
}
return isFunction(format) ? function (v) { return format.call($$, v); } : format;
};
Axis.prototype.getTickValues = function getTickValues(tickValues, axis) {
c3_axis_fn.getTickValues = function getTickValues(tickValues, axis) {
return tickValues ? tickValues : axis ? axis.tickValues() : undefined;
};
Axis.prototype.getXAxisTickValues = function getXAxisTickValues() {
c3_axis_fn.getXAxisTickValues = function getXAxisTickValues() {
return this.getTickValues(this.owner.config.axis_x_tick_values, this.owner.xAxis);
};
Axis.prototype.getYAxisTickValues = function getYAxisTickValues() {
c3_axis_fn.getYAxisTickValues = function getYAxisTickValues() {
return this.getTickValues(this.owner.config.axis_y_tick_values, this.owner.yAxis);
};
Axis.prototype.getY2AxisTickValues = function getY2AxisTickValues() {
c3_axis_fn.getY2AxisTickValues = function getY2AxisTickValues() {
return this.getTickValues(this.owner.config.axis_y2_tick_values, this.owner.y2Axis);
};
Axis.prototype.getLabelOptionByAxisId = function getLabelOptionByAxisId(axisId) {
c3_axis_fn.getLabelOptionByAxisId = function getLabelOptionByAxisId(axisId) {
var $$ = this.owner, config = $$.config, option;
if (axisId === 'y') {
option = config.axis_y_label;
@ -137,11 +508,11 @@ Axis.prototype.getLabelOptionByAxisId = function getLabelOptionByAxisId(axisId)
}
return option;
};
Axis.prototype.getLabelText = function getLabelText(axisId) {
c3_axis_fn.getLabelText = function getLabelText(axisId) {
var option = this.getLabelOptionByAxisId(axisId);
return isString(option) ? option : option ? option.text : null;
};
Axis.prototype.setLabelText = function setLabelText(axisId, text) {
c3_axis_fn.setLabelText = function setLabelText(axisId, text) {
var $$ = this.owner, config = $$.config,
option = this.getLabelOptionByAxisId(axisId);
if (isString(option)) {
@ -156,7 +527,7 @@ Axis.prototype.setLabelText = function setLabelText(axisId, text) {
option.text = text;
}
};
Axis.prototype.getLabelPosition = function getLabelPosition(axisId, defaultPosition) {
c3_axis_fn.getLabelPosition = function getLabelPosition(axisId, defaultPosition) {
var option = this.getLabelOptionByAxisId(axisId),
position = (option && typeof option === 'object' && option.position) ? option.position : defaultPosition;
return {
@ -170,28 +541,28 @@ Axis.prototype.getLabelPosition = function getLabelPosition(axisId, defaultPosit
isBottom: position.indexOf('bottom') >= 0
};
};
Axis.prototype.getXAxisLabelPosition = function getXAxisLabelPosition() {
c3_axis_fn.getXAxisLabelPosition = function getXAxisLabelPosition() {
return this.getLabelPosition('x', this.owner.config.axis_rotated ? 'inner-top' : 'inner-right');
};
Axis.prototype.getYAxisLabelPosition = function getYAxisLabelPosition() {
c3_axis_fn.getYAxisLabelPosition = function getYAxisLabelPosition() {
return this.getLabelPosition('y', this.owner.config.axis_rotated ? 'inner-right' : 'inner-top');
};
Axis.prototype.getY2AxisLabelPosition = function getY2AxisLabelPosition() {
c3_axis_fn.getY2AxisLabelPosition = function getY2AxisLabelPosition() {
return this.getLabelPosition('y2', this.owner.config.axis_rotated ? 'inner-right' : 'inner-top');
};
Axis.prototype.getLabelPositionById = function getLabelPositionById(id) {
c3_axis_fn.getLabelPositionById = function getLabelPositionById(id) {
return id === 'y2' ? this.getY2AxisLabelPosition() : id === 'y' ? this.getYAxisLabelPosition() : this.getXAxisLabelPosition();
};
Axis.prototype.textForXAxisLabel = function textForXAxisLabel() {
c3_axis_fn.textForXAxisLabel = function textForXAxisLabel() {
return this.getLabelText('x');
};
Axis.prototype.textForYAxisLabel = function textForYAxisLabel() {
c3_axis_fn.textForYAxisLabel = function textForYAxisLabel() {
return this.getLabelText('y');
};
Axis.prototype.textForY2AxisLabel = function textForY2AxisLabel() {
c3_axis_fn.textForY2AxisLabel = function textForY2AxisLabel() {
return this.getLabelText('y2');
};
Axis.prototype.xForAxisLabel = function xForAxisLabel(forHorizontal, position) {
c3_axis_fn.xForAxisLabel = function xForAxisLabel(forHorizontal, position) {
var $$ = this.owner;
if (forHorizontal) {
return position.isLeft ? 0 : position.isCenter ? $$.width / 2 : $$.width;
@ -199,39 +570,39 @@ Axis.prototype.xForAxisLabel = function xForAxisLabel(forHorizontal, position) {
return position.isBottom ? -$$.height : position.isMiddle ? -$$.height / 2 : 0;
}
};
Axis.prototype.dxForAxisLabel = function dxForAxisLabel(forHorizontal, position) {
c3_axis_fn.dxForAxisLabel = function dxForAxisLabel(forHorizontal, position) {
if (forHorizontal) {
return position.isLeft ? "0.5em" : position.isRight ? "-0.5em" : "0";
} else {
return position.isTop ? "-0.5em" : position.isBottom ? "0.5em" : "0";
}
};
Axis.prototype.textAnchorForAxisLabel = function textAnchorForAxisLabel(forHorizontal, position) {
c3_axis_fn.textAnchorForAxisLabel = function textAnchorForAxisLabel(forHorizontal, position) {
if (forHorizontal) {
return position.isLeft ? 'start' : position.isCenter ? 'middle' : 'end';
} else {
return position.isBottom ? 'start' : position.isMiddle ? 'middle' : 'end';
}
};
Axis.prototype.xForXAxisLabel = function xForXAxisLabel() {
c3_axis_fn.xForXAxisLabel = function xForXAxisLabel() {
return this.xForAxisLabel(!this.owner.config.axis_rotated, this.getXAxisLabelPosition());
};
Axis.prototype.xForYAxisLabel = function xForYAxisLabel() {
c3_axis_fn.xForYAxisLabel = function xForYAxisLabel() {
return this.xForAxisLabel(this.owner.config.axis_rotated, this.getYAxisLabelPosition());
};
Axis.prototype.xForY2AxisLabel = function xForY2AxisLabel() {
c3_axis_fn.xForY2AxisLabel = function xForY2AxisLabel() {
return this.xForAxisLabel(this.owner.config.axis_rotated, this.getY2AxisLabelPosition());
};
Axis.prototype.dxForXAxisLabel = function dxForXAxisLabel() {
c3_axis_fn.dxForXAxisLabel = function dxForXAxisLabel() {
return this.dxForAxisLabel(!this.owner.config.axis_rotated, this.getXAxisLabelPosition());
};
Axis.prototype.dxForYAxisLabel = function dxForYAxisLabel() {
c3_axis_fn.dxForYAxisLabel = function dxForYAxisLabel() {
return this.dxForAxisLabel(this.owner.config.axis_rotated, this.getYAxisLabelPosition());
};
Axis.prototype.dxForY2AxisLabel = function dxForY2AxisLabel() {
c3_axis_fn.dxForY2AxisLabel = function dxForY2AxisLabel() {
return this.dxForAxisLabel(this.owner.config.axis_rotated, this.getY2AxisLabelPosition());
};
Axis.prototype.dyForXAxisLabel = function dyForXAxisLabel() {
c3_axis_fn.dyForXAxisLabel = function dyForXAxisLabel() {
var $$ = this.owner, config = $$.config,
position = this.getXAxisLabelPosition();
if (config.axis_rotated) {
@ -240,7 +611,7 @@ Axis.prototype.dyForXAxisLabel = function dyForXAxisLabel() {
return position.isInner ? "-0.5em" : config.axis_x_height ? config.axis_x_height - 10 : "3em";
}
};
Axis.prototype.dyForYAxisLabel = function dyForYAxisLabel() {
c3_axis_fn.dyForYAxisLabel = function dyForYAxisLabel() {
var $$ = this.owner,
position = this.getYAxisLabelPosition();
if ($$.config.axis_rotated) {
@ -249,7 +620,7 @@ Axis.prototype.dyForYAxisLabel = function dyForYAxisLabel() {
return position.isInner ? "1.2em" : -10 - ($$.config.axis_y_inner ? 0 : (this.getMaxTickWidth('y') + 10));
}
};
Axis.prototype.dyForY2AxisLabel = function dyForY2AxisLabel() {
c3_axis_fn.dyForY2AxisLabel = function dyForY2AxisLabel() {
var $$ = this.owner,
position = this.getY2AxisLabelPosition();
if ($$.config.axis_rotated) {
@ -258,19 +629,19 @@ Axis.prototype.dyForY2AxisLabel = function dyForY2AxisLabel() {
return position.isInner ? "-0.5em" : 15 + ($$.config.axis_y2_inner ? 0 : (this.getMaxTickWidth('y2') + 15));
}
};
Axis.prototype.textAnchorForXAxisLabel = function textAnchorForXAxisLabel() {
c3_axis_fn.textAnchorForXAxisLabel = function textAnchorForXAxisLabel() {
var $$ = this.owner;
return this.textAnchorForAxisLabel(!$$.config.axis_rotated, this.getXAxisLabelPosition());
};
Axis.prototype.textAnchorForYAxisLabel = function textAnchorForYAxisLabel() {
c3_axis_fn.textAnchorForYAxisLabel = function textAnchorForYAxisLabel() {
var $$ = this.owner;
return this.textAnchorForAxisLabel($$.config.axis_rotated, this.getYAxisLabelPosition());
};
Axis.prototype.textAnchorForY2AxisLabel = function textAnchorForY2AxisLabel() {
c3_axis_fn.textAnchorForY2AxisLabel = function textAnchorForY2AxisLabel() {
var $$ = this.owner;
return this.textAnchorForAxisLabel($$.config.axis_rotated, this.getY2AxisLabelPosition());
};
Axis.prototype.getMaxTickWidth = function getMaxTickWidth(id, withoutRecompute) {
c3_axis_fn.getMaxTickWidth = function getMaxTickWidth(id, withoutRecompute) {
var $$ = this.owner, config = $$.config,
maxWidth = 0, targetsToShow, scale, axis, dummy, svg;
if (withoutRecompute && $$.currentMaxTickWidths[id]) {
@ -303,7 +674,7 @@ Axis.prototype.getMaxTickWidth = function getMaxTickWidth(id, withoutRecompute)
return $$.currentMaxTickWidths[id];
};
Axis.prototype.updateLabels = function updateLabels(withTransition) {
c3_axis_fn.updateLabels = function updateLabels(withTransition) {
var $$ = this.owner;
var axisXLabel = $$.main.select('.' + CLASS.axisX + ' .' + CLASS.axisXLabel),
axisYLabel = $$.main.select('.' + CLASS.axisY + ' .' + CLASS.axisYLabel),
@ -324,7 +695,7 @@ Axis.prototype.updateLabels = function updateLabels(withTransition) {
.attr("dy", this.dyForY2AxisLabel.bind(this))
.text(this.textForY2AxisLabel.bind(this));
};
Axis.prototype.getPadding = function getPadding(padding, key, defaultValue, domainLength) {
c3_axis_fn.getPadding = function getPadding(padding, key, defaultValue, domainLength) {
var p = typeof padding === 'number' ? padding : padding[key];
if (!isValue(p)) {
return defaultValue;
@ -335,12 +706,12 @@ Axis.prototype.getPadding = function getPadding(padding, key, defaultValue, doma
// assume padding is pixels if unit is not specified
return this.convertPixelsToAxisPadding(p, domainLength);
};
Axis.prototype.convertPixelsToAxisPadding = function convertPixelsToAxisPadding(pixels, domainLength) {
c3_axis_fn.convertPixelsToAxisPadding = function convertPixelsToAxisPadding(pixels, domainLength) {
var $$ = this.owner,
length = $$.config.axis_rotated ? $$.width : $$.height;
return domainLength * (pixels / length);
};
Axis.prototype.generateTickValues = function generateTickValues(values, tickCount, forTimeSeries) {
c3_axis_fn.generateTickValues = function generateTickValues(values, tickCount, forTimeSeries) {
var tickValues = values, targetCount, start, end, count, interval, i, tickValue;
if (tickCount) {
targetCount = isFunction(tickCount) ? tickCount() : tickCount;
@ -366,7 +737,7 @@ Axis.prototype.generateTickValues = function generateTickValues(values, tickCoun
if (!forTimeSeries) { tickValues = tickValues.sort(function (a, b) { return a - b; }); }
return tickValues;
};
Axis.prototype.generateTransitions = function generateTransitions(duration) {
c3_axis_fn.generateTransitions = function generateTransitions(duration) {
var $$ = this.owner, axes = $$.axes;
return {
axisX: duration ? axes.x.transition().duration(duration) : axes.x,
@ -375,7 +746,7 @@ Axis.prototype.generateTransitions = function generateTransitions(duration) {
axisSubX: duration ? axes.subx.transition().duration(duration) : axes.subx
};
};
Axis.prototype.redraw = function redraw(transitions, isHidden) {
c3_axis_fn.redraw = function redraw(transitions, isHidden) {
var $$ = this.owner;
$$.axes.x.style("opacity", isHidden ? 0 : 1);
$$.axes.y.style("opacity", isHidden ? 0 : 1);

330
src/c3.axis.js

@ -1,330 +0,0 @@
// Features:
// 1. category axis
// 2. ceil values of translate/x/y to int for half pixel antialiasing
// 3. multiline tick text
var tickTextCharSize;
export default function c3_axis(d3, params) {
var scale = d3.scale.linear(), orient = "bottom", innerTickSize = 6, outerTickSize, tickPadding = 3, tickValues = null, tickFormat, tickArguments;
var tickOffset = 0, tickCulling = true, tickCentered;
params = params || {};
outerTickSize = params.withOuterTick ? 6 : 0;
function axisX(selection, x) {
selection.attr("transform", function (d) {
return "translate(" + Math.ceil(x(d) + tickOffset) + ", 0)";
});
}
function axisY(selection, y) {
selection.attr("transform", function (d) {
return "translate(0," + Math.ceil(y(d)) + ")";
});
}
function scaleExtent(domain) {
var start = domain[0], stop = domain[domain.length - 1];
return start < stop ? [ start, stop ] : [ stop, start ];
}
function generateTicks(scale) {
var i, domain, ticks = [];
if (scale.ticks) {
return scale.ticks.apply(scale, tickArguments);
}
domain = scale.domain();
for (i = Math.ceil(domain[0]); i < domain[1]; i++) {
ticks.push(i);
}
if (ticks.length > 0 && ticks[0] > 0) {
ticks.unshift(ticks[0] - (ticks[1] - ticks[0]));
}
return ticks;
}
function copyScale() {
var newScale = scale.copy(), domain;
if (params.isCategory) {
domain = scale.domain();
newScale.domain([domain[0], domain[1] - 1]);
}
return newScale;
}
function textFormatted(v) {
var formatted = tickFormat ? tickFormat(v) : v;
return typeof formatted !== 'undefined' ? formatted : '';
}
function getSizeFor1Char(tick) {
if (tickTextCharSize) {
return tickTextCharSize;
}
var size = {
h: 11.5,
w: 5.5
};
tick.select('text').text(textFormatted).each(function (d) {
var box = this.getBoundingClientRect(),
text = textFormatted(d),
h = box.height,
w = text ? (box.width / text.length) : undefined;
if (h && w) {
size.h = h;
size.w = w;
}
}).text('');
tickTextCharSize = size;
return size;
}
function transitionise(selection) {
return params.withoutTransition ? selection : d3.transition(selection);
}
function axis(g) {
g.each(function () {
var g = axis.g = d3.select(this);
var scale0 = this.__chart__ || scale, scale1 = this.__chart__ = copyScale();
var ticks = tickValues ? tickValues : generateTicks(scale1),
tick = g.selectAll(".tick").data(ticks, scale1),
tickEnter = tick.enter().insert("g", ".domain").attr("class", "tick").style("opacity", 1e-6),
// MEMO: No exit transition. The reason is this transition affects max tick width calculation because old tick will be included in the ticks.
tickExit = tick.exit().remove(),
tickUpdate = transitionise(tick).style("opacity", 1),
tickTransform, tickX, tickY;
var range = scale.rangeExtent ? scale.rangeExtent() : scaleExtent(scale.range()),
path = g.selectAll(".domain").data([ 0 ]),
pathUpdate = (path.enter().append("path").attr("class", "domain"), transitionise(path));
tickEnter.append("line");
tickEnter.append("text");
var lineEnter = tickEnter.select("line"),
lineUpdate = tickUpdate.select("line"),
textEnter = tickEnter.select("text"),
textUpdate = tickUpdate.select("text");
if (params.isCategory) {
tickOffset = Math.ceil((scale1(1) - scale1(0)) / 2);
tickX = tickCentered ? 0 : tickOffset;
tickY = tickCentered ? tickOffset : 0;
} else {
tickOffset = tickX = 0;
}
var text, tspan, sizeFor1Char = getSizeFor1Char(g.select('.tick')), counts = [];
var tickLength = Math.max(innerTickSize, 0) + tickPadding,
isVertical = orient === 'left' || orient === 'right';
// this should be called only when category axis
function splitTickText(d, maxWidth) {
var tickText = textFormatted(d),
subtext, spaceIndex, textWidth, splitted = [];
if (Object.prototype.toString.call(tickText) === "[object Array]") {
return tickText;
}
if (!maxWidth || maxWidth <= 0) {
maxWidth = isVertical ? 95 : params.isCategory ? (Math.ceil(scale1(ticks[1]) - scale1(ticks[0])) - 12) : 110;
}
function split(splitted, text) {
spaceIndex = undefined;
for (var i = 1; i < text.length; i++) {
if (text.charAt(i) === ' ') {
spaceIndex = i;
}
subtext = text.substr(0, i + 1);
textWidth = sizeFor1Char.w * subtext.length;
// if text width gets over tick width, split by space index or crrent index
if (maxWidth < textWidth) {
return split(
splitted.concat(text.substr(0, spaceIndex ? spaceIndex : i)),
text.slice(spaceIndex ? spaceIndex + 1 : i)
);
}
}
return splitted.concat(text);
}
return split(splitted, tickText + "");
}
function tspanDy(d, i) {
var dy = sizeFor1Char.h;
if (i === 0) {
if (orient === 'left' || orient === 'right') {
dy = -((counts[d.index] - 1) * (sizeFor1Char.h / 2) - 3);
} else {
dy = ".71em";
}
}
return dy;
}
function tickSize(d) {
var tickPosition = scale(d) + (tickCentered ? 0 : tickOffset);
return range[0] < tickPosition && tickPosition < range[1] ? innerTickSize : 0;
}
text = tick.select("text");
tspan = text.selectAll('tspan')
.data(function (d, i) {
var splitted = params.tickMultiline ? splitTickText(d, params.tickWidth) : [].concat(textFormatted(d));
counts[i] = splitted.length;
return splitted.map(function (s) {
return { index: i, splitted: s };
});
});
tspan.enter().append('tspan');
tspan.exit().remove();
tspan.text(function (d) { return d.splitted; });
var rotate = params.tickTextRotate;
function textAnchorForText(rotate) {
if (!rotate) {
return 'middle';
}
return rotate > 0 ? "start" : "end";
}
function textTransform(rotate) {
if (!rotate) {
return '';
}
return "rotate(" + rotate + ")";
}
function dxForText(rotate) {
if (!rotate) {
return 0;
}
return 8 * Math.sin(Math.PI * (rotate / 180));
}
function yForText(rotate) {
if (!rotate) {
return tickLength;
}
return 11.5 - 2.5 * (rotate / 15) * (rotate > 0 ? 1 : -1);
}
switch (orient) {
case "bottom":
{
tickTransform = axisX;
lineEnter.attr("y2", innerTickSize);
textEnter.attr("y", tickLength);
lineUpdate.attr("x1", tickX).attr("x2", tickX).attr("y2", tickSize);
textUpdate.attr("x", 0).attr("y", yForText(rotate))
.style("text-anchor", textAnchorForText(rotate))
.attr("transform", textTransform(rotate));
tspan.attr('x', 0).attr("dy", tspanDy).attr('dx', dxForText(rotate));
pathUpdate.attr("d", "M" + range[0] + "," + outerTickSize + "V0H" + range[1] + "V" + outerTickSize);
break;
}
case "top":
{
// TODO: rotated tick text
tickTransform = axisX;
lineEnter.attr("y2", -innerTickSize);
textEnter.attr("y", -tickLength);
lineUpdate.attr("x2", 0).attr("y2", -innerTickSize);
textUpdate.attr("x", 0).attr("y", -tickLength);
text.style("text-anchor", "middle");
tspan.attr('x', 0).attr("dy", "0em");
pathUpdate.attr("d", "M" + range[0] + "," + -outerTickSize + "V0H" + range[1] + "V" + -outerTickSize);
break;
}
case "left":
{
tickTransform = axisY;
lineEnter.attr("x2", -innerTickSize);
textEnter.attr("x", -tickLength);
lineUpdate.attr("x2", -innerTickSize).attr("y1", tickY).attr("y2", tickY);
textUpdate.attr("x", -tickLength).attr("y", tickOffset);
text.style("text-anchor", "end");
tspan.attr('x', -tickLength).attr("dy", tspanDy);
pathUpdate.attr("d", "M" + -outerTickSize + "," + range[0] + "H0V" + range[1] + "H" + -outerTickSize);
break;
}
case "right":
{
tickTransform = axisY;
lineEnter.attr("x2", innerTickSize);
textEnter.attr("x", tickLength);
lineUpdate.attr("x2", innerTickSize).attr("y2", 0);
textUpdate.attr("x", tickLength).attr("y", 0);
text.style("text-anchor", "start");
tspan.attr('x', tickLength).attr("dy", tspanDy);
pathUpdate.attr("d", "M" + outerTickSize + "," + range[0] + "H0V" + range[1] + "H" + outerTickSize);
break;
}
}
if (scale1.rangeBand) {
var x = scale1, dx = x.rangeBand() / 2;
scale0 = scale1 = function (d) {
return x(d) + dx;
};
} else if (scale0.rangeBand) {
scale0 = scale1;
} else {
tickExit.call(tickTransform, scale1);
}
tickEnter.call(tickTransform, scale0);
tickUpdate.call(tickTransform, scale1);
});
}
axis.scale = function (x) {
if (!arguments.length) { return scale; }
scale = x;
return axis;
};
axis.orient = function (x) {
if (!arguments.length) { return orient; }
orient = x in {top: 1, right: 1, bottom: 1, left: 1} ? x + "" : "bottom";
return axis;
};
axis.tickFormat = function (format) {
if (!arguments.length) { return tickFormat; }
tickFormat = format;
return axis;
};
axis.tickCentered = function (isCentered) {
if (!arguments.length) { return tickCentered; }
tickCentered = isCentered;
return axis;
};
axis.tickOffset = function () {
return tickOffset;
};
axis.tickInterval = function () {
var interval, length;
if (params.isCategory) {
interval = tickOffset * 2;
}
else {
length = axis.g.select('path.domain').node().getTotalLength() - outerTickSize * 2;
interval = length / axis.g.selectAll('line').size();
}
return interval === Infinity ? 0 : interval;
};
axis.ticks = function () {
if (!arguments.length) { return tickArguments; }
tickArguments = arguments;
return axis;
};
axis.tickCulling = function (culling) {
if (!arguments.length) { return tickCulling; }
tickCulling = culling;
return axis;
};
axis.tickValues = function (x) {
if (typeof x === 'function') {
tickValues = function () {
return x(scale.domain());
};
}
else {
if (!arguments.length) { return tickValues; }
tickValues = x;
}
return axis;
};
return axis;
}

23
src/core.js

@ -6,25 +6,10 @@ export var c3 = { version: "0.4.17" };
export var c3_chart_fn;
export var c3_chart_internal_fn;
export var c3_chart_internal_axis_fn;
export function API(owner) {
export function Component(owner, componentKey, fn) {
this.owner = owner;
}
export function inherit(base, derived) {
if (Object.create) {
derived.prototype = Object.create(base.prototype);
} else {
var f = function f() {};
f.prototype = base.prototype;
derived.prototype = new f();
}
derived.prototype.constructor = derived;
return derived;
c3.chart.internal[componentKey] = fn;
}
function Chart(config) {
@ -64,14 +49,10 @@ c3.chart = {
fn: Chart.prototype,
internal: {
fn: ChartInternal.prototype,
axis: {
fn: Axis.prototype
}
}
};
c3_chart_fn = c3.chart.fn;
c3_chart_internal_fn = c3.chart.internal.fn;
c3_chart_internal_axis_fn = c3.chart.internal.axis.fn;
c3_chart_internal_fn.beforeInit = function () {
// can do something

1
src/index.js

@ -22,7 +22,6 @@ import './api.x';
import './api.zoom';
import './arc';
import './axis';
import './c3.axis';
import './cache';
import './category';
import './class';

Loading…
Cancel
Save