Browse Source

Resolved #305; Now you may pass axis.y.tick.rotate

pull/1047/head
timopheym 10 years ago
parent
commit
92e4e02960
  1. 14
      c3.js
  2. 43
      spec/axis-spec.js
  3. 13
      src/axis.js
  4. 1
      src/config.js

14
c3.js

@ -1099,6 +1099,7 @@
axis_y_tick_format: undefined,
axis_y_tick_outer: true,
axis_y_tick_values: null,
axis_y_tick_rotate: 0,
axis_y_tick_count: undefined,
axis_y_tick_time_value: undefined,
axis_y_tick_time_interval: undefined,
@ -4340,8 +4341,13 @@
return 11.5 - 2.5 * (r / 15) * (r > 0 ? 1 : -1);
};
c3_chart_internal_fn.rotateTickText = function (axis, transition, rotate) {
axis.selectAll('.tick text')
.style("text-anchor", rotate > 0 ? "start" : "end");
if (axis.classed(c3_chart_internal_fn.CLASS.axisY) && !this.config.axis_rotated) {
axis.selectAll('.tick text')
.style("text-anchor", rotate > 0 ? "end" : "start");
} else {
axis.selectAll('.tick text')
.style("text-anchor", rotate > 0 ? "start" : "end");
}
transition.selectAll('.tick text')
.attr("y", this.yForRotatedTickText(rotate))
.attr("transform", "rotate(" + rotate + ")")
@ -4473,6 +4479,10 @@
$$.rotateTickText($$.axes.x, transitions.axisX, config.axis_x_tick_rotate);
$$.rotateTickText($$.axes.subx, transitions.axisSubX, config.axis_x_tick_rotate);
}
// we may want to rotate y axis when chart in horizontal
if (config.axis_y_tick_rotate) {
$$.rotateTickText($$.axes.y, transitions.axisY, config.axis_y_tick_rotate);
}
};
c3_chart_internal_fn.getClipPath = function (id) {

43
spec/axis-spec.js

@ -569,6 +569,49 @@ describe('c3 chart axis', function () {
});
});
describe('axis.y.tick.rotate', function () {
describe('not rotated', function () {
it('should update args successfully', function () {
args = {
data: {
columns: [
['data1', 30, 200, 100, 400, 150, 250, 100, 600],
['data2', 50, 20, 10, 40, 15, 25],
]
},
axis: {
y: {
tick: {
rotate: 45
}
}
}
};
expect(true).toBeTruthy();
});
it('should rotate tick texts', function () {
chart.internal.main.selectAll('.c3-axis-y g.tick').each(function () {
var tick = d3.select(this),
text = tick.select('text'),
tspan = text.select('tspan');
expect(text.attr('transform')).toBe('rotate(45)');
expect(text.attr('y')).toBe('4');
expect(tspan.attr('dx')).toBe('5.65685424949238');
});
});
it('should have automatically calculated y axis width', function () {
var box = chart.internal.main.select('.c3-axis-y').node().getBoundingClientRect();
expect(box.width).toBeLessThan(25);
});
});
});
describe('axis.x.tick.fit', function () {
describe('axis.x.tick.fit = true', function () {

13
src/axis.js

@ -261,8 +261,13 @@ c3_chart_internal_fn.yForRotatedTickText = function (r) {
return 11.5 - 2.5 * (r / 15) * (r > 0 ? 1 : -1);
};
c3_chart_internal_fn.rotateTickText = function (axis, transition, rotate) {
axis.selectAll('.tick text')
.style("text-anchor", rotate > 0 ? "start" : "end");
if (axis.classed(c3_chart_internal_fn.CLASS.axisY) && !this.config.axis_rotated) {
axis.selectAll('.tick text')
.style("text-anchor", rotate > 0 ? "end" : "start");
} else {
axis.selectAll('.tick text')
.style("text-anchor", rotate > 0 ? "start" : "end");
}
transition.selectAll('.tick text')
.attr("y", this.yForRotatedTickText(rotate))
.attr("transform", "rotate(" + rotate + ")")
@ -394,4 +399,8 @@ c3_chart_internal_fn.redrawAxis = function (transitions, isHidden) {
$$.rotateTickText($$.axes.x, transitions.axisX, config.axis_x_tick_rotate);
$$.rotateTickText($$.axes.subx, transitions.axisSubX, config.axis_x_tick_rotate);
}
// we may want to rotate y axis when chart in horizontal
if (config.axis_y_tick_rotate) {
$$.rotateTickText($$.axes.y, transitions.axisY, config.axis_y_tick_rotate);
}
};

1
src/config.js

@ -112,6 +112,7 @@ c3_chart_internal_fn.getDefaultConfig = function () {
axis_y_tick_format: undefined,
axis_y_tick_outer: true,
axis_y_tick_values: null,
axis_y_tick_rotate: 0,
axis_y_tick_count: undefined,
axis_y_tick_time_value: undefined,
axis_y_tick_time_interval: undefined,

Loading…
Cancel
Save