Browse Source

Merge branch 'yordis-put-cero-under-the-line-when-all-data-is-negative'

pull/1207/head
Masayuki Tanaka 9 years ago
parent
commit
606bf62cb8
  1. 11
      c3.js
  2. 8
      c3.min.js
  3. 24
      spec/data-spec.js
  4. 9
      src/data.convert.js
  5. 2
      src/text.js

11
c3.js

@ -2110,7 +2110,8 @@
id: convertedId, id: convertedId,
id_org: id, id_org: id,
values: data.map(function (d, i) { 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 // use x as categories if custom x and categorized
if ($$.isCustomX() && $$.isCategorized() && index === 0 && rawX) { if ($$.isCustomX() && $$.isCategorized() && index === 0 && rawX) {
if (i === 0) { config.axis_x_categories = []; } if (i === 0) { config.axis_x_categories = []; }
@ -2120,7 +2121,7 @@
if (isUndefined(d[id]) || $$.data.xs[id].length <= i) { if (isUndefined(d[id]) || $$.data.xs[id].length <= i) {
x = undefined; x = undefined;
} }
return {x: x, value: d[id] !== null && !isNaN(d[id]) ? +d[id] : null, id: convertedId}; return {x: x, value: value, id: convertedId};
}).filter(function (v) { return isDefined(v.x); }) }).filter(function (v) { return isDefined(v.x); })
}; };
}); });
@ -2147,6 +2148,10 @@
}); });
}); });
// cache information about values
$$.hasNegativeValue = $$.hasNegativeValueInTargets(targets);
$$.hasPositiveValue = $$.hasPositiveValueInTargets(targets);
// set target types // set target types
if (config.data_type) { if (config.data_type) {
$$.setTargetType($$.mapToIds(targets).filter(function (id) { return ! (id in config.data_types); }), config.data_type); $$.setTargetType($$.mapToIds(targets).filter(function (id) { return ! (id in config.data_types); }), config.data_type);
@ -3387,7 +3392,7 @@
yPos = (points[0][0] + points[2][0] + box.height * 0.6) / 2; yPos = (points[0][0] + points[2][0] + box.height * 0.6) / 2;
} else { } else {
yPos = points[2][1]; yPos = points[2][1];
if (d.value < 0) { if (d.value < 0 || (d.value === 0 && !$$.hasPositiveValue)) {
yPos += box.height; yPos += box.height;
if ($$.isBarType(d) && $$.isSafari()) { if ($$.isBarType(d) && $$.isSafari()) {
yPos -= 3; yPos -= 3;

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

9
src/data.convert.js

@ -140,7 +140,8 @@ c3_chart_internal_fn.convertDataToTargets = function (data, appendXs) {
id: convertedId, id: convertedId,
id_org: id, id_org: id,
values: data.map(function (d, i) { 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 // use x as categories if custom x and categorized
if ($$.isCustomX() && $$.isCategorized() && index === 0 && rawX) { if ($$.isCustomX() && $$.isCategorized() && index === 0 && rawX) {
if (i === 0) { config.axis_x_categories = []; } if (i === 0) { config.axis_x_categories = []; }
@ -150,7 +151,7 @@ c3_chart_internal_fn.convertDataToTargets = function (data, appendXs) {
if (isUndefined(d[id]) || $$.data.xs[id].length <= i) { if (isUndefined(d[id]) || $$.data.xs[id].length <= i) {
x = undefined; x = undefined;
} }
return {x: x, value: d[id] !== null && !isNaN(d[id]) ? +d[id] : null, id: convertedId}; return {x: x, value: value, id: convertedId};
}).filter(function (v) { return isDefined(v.x); }) }).filter(function (v) { return isDefined(v.x); })
}; };
}); });
@ -177,6 +178,10 @@ c3_chart_internal_fn.convertDataToTargets = function (data, appendXs) {
}); });
}); });
// cache information about values
$$.hasNegativeValue = $$.hasNegativeValueInTargets(targets);
$$.hasPositiveValue = $$.hasPositiveValueInTargets(targets);
// set target types // set target types
if (config.data_type) { if (config.data_type) {
$$.setTargetType($$.mapToIds(targets).filter(function (id) { return ! (id in config.data_types); }), config.data_type); $$.setTargetType($$.mapToIds(targets).filter(function (id) { return ! (id in config.data_types); }), 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; yPos = (points[0][0] + points[2][0] + box.height * 0.6) / 2;
} else { } else {
yPos = points[2][1]; yPos = points[2][1];
if (d.value < 0) { if (d.value < 0 || (d.value === 0 && !$$.hasPositiveValue)) {
yPos += box.height; yPos += box.height;
if ($$.isBarType(d) && $$.isSafari()) { if ($$.isBarType(d) && $$.isSafari()) {
yPos -= 3; yPos -= 3;

Loading…
Cancel
Save