Skip to content

Commit

Permalink
Cache tick text size - #730
Browse files Browse the repository at this point in the history
  • Loading branch information
masayuki0812 committed Nov 21, 2014
1 parent 1c35022 commit 3334ef8
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 16 deletions.
6 changes: 6 additions & 0 deletions c3.js
Original file line number Diff line number Diff line change
Expand Up @@ -6455,6 +6455,8 @@
// Features:
// 1. category axis
// 2. ceil values of translate/x/y to int for half pixel antialiasing
// 3. multiline tick text
var tickTextCharSize;
function c3_axis(d3, params) {
var scale = d3.scale.linear(), orient = "bottom", innerTickSize = 6, outerTickSize, tickPadding = 3, tickValues = null, tickFormat, tickArguments;

Expand Down Expand Up @@ -6503,6 +6505,9 @@
return tickFormat ? tickFormat(v) : v;
}
function getSizeFor1Char(tick) {
if (tickTextCharSize) {
return tickTextCharSize;
}
var size = {
h: 11.5,
w: 5.5
Expand All @@ -6517,6 +6522,7 @@
size.w = w;
}
}).text('');
tickTextCharSize = size;
return size;
}
function axis(g) {
Expand Down
10 changes: 5 additions & 5 deletions c3.min.js

Large diffs are not rendered by default.

23 changes: 12 additions & 11 deletions spec/axis-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,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 () {
Expand Down Expand Up @@ -286,7 +286,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 () {
Expand Down Expand Up @@ -378,12 +378,13 @@ 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);
expect(tspans.size()).toBe(4);
tspans.each(function (d, i) {
var tspan = d3.select(this);
expect(tspan.text()).toBe(expectedTickTexts[i]);
Expand Down Expand Up @@ -426,9 +427,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);
Expand Down Expand Up @@ -479,8 +480,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);
Expand Down
6 changes: 6 additions & 0 deletions src/c3.axis.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Features:
// 1. category axis
// 2. ceil values of translate/x/y to int for half pixel antialiasing
// 3. multiline tick text
var tickTextCharSize;
function c3_axis(d3, params) {
var scale = d3.scale.linear(), orient = "bottom", innerTickSize = 6, outerTickSize, tickPadding = 3, tickValues = null, tickFormat, tickArguments;

Expand Down Expand Up @@ -49,6 +51,9 @@ function c3_axis(d3, params) {
return tickFormat ? tickFormat(v) : v;
}
function getSizeFor1Char(tick) {
if (tickTextCharSize) {
return tickTextCharSize;
}
var size = {
h: 11.5,
w: 5.5
Expand All @@ -63,6 +68,7 @@ function c3_axis(d3, params) {
size.w = w;
}
}).text('');
tickTextCharSize = size;
return size;
}
function axis(g) {
Expand Down

0 comments on commit 3334ef8

Please sign in to comment.