Browse Source

Refactor #1086 and add spec

pull/1207/head
Masayuki Tanaka 10 years ago
parent
commit
74880d6458
  1. 26
      c3.js
  2. 4
      c3.min.js
  3. 24
      spec/data-spec.js
  4. 24
      src/data.convert.js
  5. 2
      src/text.js

26
c3.js

@ -2066,12 +2066,8 @@
var $$ = this, config = $$.config,
ids = $$.d3.keys(data[0]).filter($$.isNotX, $$),
xs = $$.d3.keys(data[0]).filter($$.isX, $$),
zeroUnderYAxis = true,
allZero = true,
targets;
$$.allDataIsNegative = true;
// save x for update data by load when custom x and c3.x API
ids.forEach(function (id) {
var xKey = $$.getXKey(id);
@ -2114,8 +2110,7 @@
id: convertedId,
id_org: id,
values: data.map(function (d, i) {
var xKey = $$.getXKey(id), rawX = d[xKey],
x = $$.generateTargetX(rawX, id, i),
var xKey = $$.getXKey(id), rawX = d[xKey], x = $$.generateTargetX(rawX, id, i),
value = d[id] !== null && !isNaN(d[id]) ? +d[id] : null;
// use x as categories if custom x and categorized
if ($$.isCustomX() && $$.isCategorized() && index === 0 && rawX) {
@ -2126,15 +2121,6 @@
if (isUndefined(d[id]) || $$.data.xs[id].length <= i) {
x = undefined;
}
if(value !== null && value !== 0) {
allZero = false;
}
if(value > 0) {
zeroUnderYAxis = false;
}
return {x: x, value: value, id: convertedId};
}).filter(function (v) { return isDefined(v.x); })
};
@ -2162,11 +2148,9 @@
});
});
if(allZero) {
zeroUnderYAxis = false;
}
$$.zeroUnderYAxis = zeroUnderYAxis;
// cache information about values
$$.hasNegativeValue = $$.hasNegativeValueInTargets(targets);
$$.hasPositiveValue = $$.hasPositiveValueInTargets(targets);
// set target types
if (config.data_type) {
@ -3408,7 +3392,7 @@
yPos = (points[0][0] + points[2][0] + box.height * 0.6) / 2;
} else {
yPos = points[2][1];
if (d.value < 0 || (d.value === 0 && $$.zeroUnderYAxis)) {
if (d.value < 0 || (d.value === 0 && !$$.hasPositiveValue)) {
yPos += box.height;
if ($$.isBarType(d) && $$.isSafari()) {
yPos -= 3;

4
c3.min.js vendored

File diff suppressed because one or more lines are too long

24
spec/data-spec.js

@ -755,7 +755,7 @@ describe('c3 chart data', function () {
});
describe('with positive values and null', function () {
describe('with negative values and null', function () {
describe('on not rotated axis', function () {
@ -763,7 +763,7 @@ describe('c3 chart data', function () {
args = {
data: {
columns: [
['data1', -190, -200, -190, null],
['data1', -190, 0, -190, null],
],
type: 'bar',
labels: {
@ -781,13 +781,13 @@ describe('c3 chart data', function () {
it('should have y domain with proper padding', function () {
var domain = chart.internal.y.domain();
expect(domain[0]).toBeCloseTo(-227, -1);
expect(domain[0]).toBeCloseTo(-215, -1);
expect(domain[1]).toBeCloseTo(0, -1);
});
it('should locate labels above each data point', function () {
var texts = chart.internal.main.selectAll('.c3-texts-data1 text'),
expectedYs = [368, 387, 368, 12],
expectedYs = [368, 12, 368, 12],
expectedXs = [74, 221, 368, 515];
texts.each(function (d, i) {
var text = d3.select(this);
@ -803,13 +803,13 @@ describe('c3 chart data', function () {
it('should have y domain with proper padding', function () {
var domain = chart.internal.y.domain();
expect(domain[0]).toBeCloseTo(-201, -1);
expect(domain[1]).toBeCloseTo(-189, -1);
expect(domain[0]).toBeCloseTo(-215, -1);
expect(domain[1]).toBeCloseTo(25, -1);
});
it('should locate labels above each data point', function () {
var texts = chart.internal.main.selectAll('.c3-texts-data1 text'),
expectedYs = [58, 392, 58, 12],
expectedYs = [395, 60, 395, 12],
expectedXs = [6, 198, 391, 583];
texts.each(function (d, i) {
var text = d3.select(this);
@ -832,14 +832,14 @@ describe('c3 chart data', function () {
it('should have y domain with proper padding', function () {
var domain = chart.internal.y.domain();
expect(domain[0]).toBeCloseTo(-232, -1);
expect(domain[0]).toBeCloseTo(-220, -1);
expect(domain[1]).toBeCloseTo(0, -1);
});
it('should locate labels above each data point', function () {
var texts = chart.internal.main.selectAll('.c3-texts-data1 text'),
expectedYs = [57, 163, 269, 375],
expectedXs = [103, 78, 103, 526];
expectedXs = [103, 594, 103, 526];
texts.each(function (d, i) {
var text = d3.select(this);
expect(+text.attr('y')).toBeCloseTo(expectedYs[i], -2);
@ -854,14 +854,14 @@ describe('c3 chart data', function () {
it('should have y domain with proper padding', function () {
var domain = chart.internal.y.domain();
expect(domain[0]).toBeCloseTo(-202, -1);
expect(domain[1]).toBeCloseTo(-188, -1);
expect(domain[0]).toBeCloseTo(-220, -1);
expect(domain[1]).toBeCloseTo(24, -1);
});
it('should locate labels above each data point', function () {
var texts = chart.internal.main.selectAll('.c3-texts-data1 text'),
expectedYs = [9, 147, 286, 424],
expectedXs = [511, 67, 511, 526];
expectedXs = [67, 537, 67, 526];
texts.each(function (d, i) {
var text = d3.select(this);
expect(+text.attr('y')).toBeCloseTo(expectedYs[i], -2);

24
src/data.convert.js

@ -96,12 +96,8 @@ c3_chart_internal_fn.convertDataToTargets = function (data, appendXs) {
var $$ = this, config = $$.config,
ids = $$.d3.keys(data[0]).filter($$.isNotX, $$),
xs = $$.d3.keys(data[0]).filter($$.isX, $$),
zeroUnderYAxis = true,
allZero = true,
targets;
$$.allDataIsNegative = true;
// save x for update data by load when custom x and c3.x API
ids.forEach(function (id) {
var xKey = $$.getXKey(id);
@ -144,8 +140,7 @@ c3_chart_internal_fn.convertDataToTargets = function (data, appendXs) {
id: convertedId,
id_org: id,
values: data.map(function (d, i) {
var xKey = $$.getXKey(id), rawX = d[xKey],
x = $$.generateTargetX(rawX, id, i),
var xKey = $$.getXKey(id), rawX = d[xKey], x = $$.generateTargetX(rawX, id, i),
value = d[id] !== null && !isNaN(d[id]) ? +d[id] : null;
// use x as categories if custom x and categorized
if ($$.isCustomX() && $$.isCategorized() && index === 0 && rawX) {
@ -156,15 +151,6 @@ c3_chart_internal_fn.convertDataToTargets = function (data, appendXs) {
if (isUndefined(d[id]) || $$.data.xs[id].length <= i) {
x = undefined;
}
if(value !== null && value !== 0) {
allZero = false;
}
if(value > 0) {
zeroUnderYAxis = false;
}
return {x: x, value: value, id: convertedId};
}).filter(function (v) { return isDefined(v.x); })
};
@ -192,11 +178,9 @@ c3_chart_internal_fn.convertDataToTargets = function (data, appendXs) {
});
});
if(allZero) {
zeroUnderYAxis = false;
}
$$.zeroUnderYAxis = zeroUnderYAxis;
// cache information about values
$$.hasNegativeValue = $$.hasNegativeValueInTargets(targets);
$$.hasPositiveValue = $$.hasPositiveValueInTargets(targets);
// set target types
if (config.data_type) {

2
src/text.js

@ -98,7 +98,7 @@ c3_chart_internal_fn.getYForText = function (points, d, textElement) {
yPos = (points[0][0] + points[2][0] + box.height * 0.6) / 2;
} else {
yPos = points[2][1];
if (d.value < 0 || (d.value === 0 && $$.zeroUnderYAxis)) {
if (d.value < 0 || (d.value === 0 && !$$.hasPositiveValue)) {
yPos += box.height;
if ($$.isBarType(d) && $$.isSafari()) {
yPos -= 3;

Loading…
Cancel
Save