Browse Source

refactor: move back convert(Rows|Columns)ToData to data.convert.js

pull/2088/head
Yoshiya Hinosawa 8 years ago
parent
commit
0fc1ae7581
  1. 16
      spec/data.convert.js
  2. 5
      src/api.flow.js
  3. 5
      src/core.js
  4. 51
      src/data.convert.js
  5. 47
      src/data.convert.utils.js
  6. 5
      src/data.load.js

16
spec/data.convert.utils.js → spec/data.convert.js

@ -1,8 +1,10 @@
import { convertColumnsToData, convertRowsToData } from '../src/data.convert.utils';
import c3 from '../src';
describe('convertColumnsToData', () => {
const $$ = c3.chart.internal.fn;
describe('$$.convertColumnsToData', () => {
it('converts column data to normalized data', () => {
const data = convertColumnsToData([
const data = $$.convertColumnsToData([
["cat1", "a", "b", "c", "d"],
["data1", 30, 200, 100, 400],
["cat2", "b", "a", "c", "d", "e", "f"],
@ -39,16 +41,16 @@ describe('convertColumnsToData', () => {
});
it('throws when the column data contains undefined', () => {
expect(() => convertColumnsToData([
expect(() => $$.convertColumnsToData([
["cat1", "a", "b", "c", "d"],
["data1", undefined]
])).toThrowError(Error, /Source data is missing a component/);
});
});
describe('convertRowsToData', () => {
describe('$$.convertRowsToData', () => {
it('converts the row data to normalized data', () => {
const data = convertRowsToData([
const data = $$.convertRowsToData([
['data1', 'data2', 'data3'],
[90, 120, 300],
[40, 160, 240],
@ -86,7 +88,7 @@ describe('convertRowsToData', () => {
});
it('throws when the row data contains undefined', () => {
expect(() => convertRowsToData([
expect(() => $$.convertRowsToData([
['data1', 'data2', 'data3'],
[40, 160, 240],
[90, 120, undefined]

5
src/api.flow.js

@ -1,7 +1,6 @@
import CLASS from './class';
import { c3_chart_fn, c3_chart_internal_fn } from './core';
import { isValue, isDefined, diffDomain } from './util';
import { convertRowsToData, convertColumnsToData } from './data.convert.utils';
c3_chart_fn.flow = function (args) {
var $$ = this.internal,
@ -12,10 +11,10 @@ c3_chart_fn.flow = function (args) {
data = $$.convertJsonToData(args.json, args.keys);
}
else if (args.rows) {
data = convertRowsToData(args.rows);
data = $$.convertRowsToData(args.rows);
}
else if (args.columns) {
data = convertColumnsToData(args.columns);
data = $$.convertColumnsToData(args.columns);
}
else {
return;

5
src/core.js

@ -1,7 +1,6 @@
import Axis from './axis';
import CLASS from './class';
import { isValue, isFunction, isString, isUndefined, isDefined, ceil10, asHalfPixel, diffDomain, isEmpty, notEmpty, getOption, hasValue, sanitise, getPathBox } from './util';
import { convertRowsToData, convertColumnsToData } from './data.convert.utils';
export var c3 = { version: "0.4.14" };
@ -92,10 +91,10 @@ c3_chart_internal_fn.init = function () {
$$.initWithData($$.convertJsonToData(config.data_json, config.data_keys));
}
else if (config.data_rows) {
$$.initWithData(convertRowsToData(config.data_rows));
$$.initWithData($$.convertRowsToData(config.data_rows));
}
else if (config.data_columns) {
$$.initWithData(convertColumnsToData(config.data_columns));
$$.initWithData($$.convertColumnsToData(config.data_columns));
}
else {
throw Error('url or json or rows or columns is required.');

51
src/data.convert.js

@ -1,6 +1,5 @@
import { c3_chart_internal_fn } from './core';
import { isValue, isUndefined, isDefined, notEmpty } from './util';
import { convertRowsToData, convertColumnsToData } from './data.convert.utils';
c3_chart_internal_fn.convertUrlToData = function (url, mimeType, headers, keys, done) {
var $$ = this, type = mimeType ? mimeType : 'csv';
@ -67,12 +66,12 @@ c3_chart_internal_fn.convertJsonToData = function (json, keys) {
});
new_rows.push(new_row);
});
data = convertRowsToData(new_rows);
data = $$.convertRowsToData(new_rows);
} else {
Object.keys(json).forEach(function (key) {
new_rows.push([key].concat(json[key]));
});
data = convertColumnsToData(new_rows);
data = $$.convertColumnsToData(new_rows);
}
return data;
};
@ -91,6 +90,52 @@ c3_chart_internal_fn.findValueInJson = function (object, path) {
return object;
};
/**
* Converts the rows to normalized data.
* @param {any[][]} rows The row data
* @return {Object[]}
*/
c3_chart_internal_fn.convertRowsToData = (rows) => {
const newRows = [];
const keys = rows[0];
for (let i = 1; i < rows.length; i++) {
const newRow = {};
for (let j = 0; j < rows[i].length; j++) {
if (isUndefined(rows[i][j])) {
throw new Error("Source data is missing a component at (" + i + "," + j + ")!");
}
newRow[keys[j]] = rows[i][j];
}
newRows.push(newRow);
}
return newRows;
};
/**
* Converts the columns to normalized data.
* @param {any[][]} columns The column data
* @return {Object[]}
*/
c3_chart_internal_fn.convertColumnsToData = (columns) => {
const newRows = [];
for (let i = 0; i < columns.length; i++) {
const key = columns[i][0];
for (let j = 1; j < columns[i].length; j++) {
if (isUndefined(newRows[j - 1])) {
newRows[j - 1] = {};
}
if (isUndefined(columns[i][j])) {
throw new Error("Source data is missing a component at (" + i + "," + j + ")!");
}
newRows[j - 1][key] = columns[i][j];
}
}
return newRows;
};
c3_chart_internal_fn.convertDataToTargets = function (data, appendXs) {
var $$ = this, config = $$.config,
ids = $$.d3.keys(data[0]).filter($$.isNotX, $$),

47
src/data.convert.utils.js

@ -1,47 +0,0 @@
import { isUndefined } from './util';
/**
* Converts the rows to normalized data.
* @param {any[][]} rows The row data
* @return {Object[]}
*/
export const convertRowsToData = (rows) => {
const newRows = [];
const keys = rows[0];
for (let i = 1; i < rows.length; i++) {
const newRow = {};
for (let j = 0; j < rows[i].length; j++) {
if (isUndefined(rows[i][j])) {
throw new Error("Source data is missing a component at (" + i + "," + j + ")!");
}
newRow[keys[j]] = rows[i][j];
}
newRows.push(newRow);
}
return newRows;
};
/**
* Converts the columns to normalized data.
* @param {any[][]} columns The column data
* @return {Object[]}
*/
export const convertColumnsToData = (columns) => {
const newRows = [];
for (let i = 0; i < columns.length; i++) {
const key = columns[i][0];
for (let j = 1; j < columns[i].length; j++) {
if (isUndefined(newRows[j - 1])) {
newRows[j - 1] = {};
}
if (isUndefined(columns[i][j])) {
throw new Error("Source data is missing a component at (" + i + "," + j + ")!");
}
newRows[j - 1][key] = columns[i][j];
}
}
return newRows;
};

5
src/data.load.js

@ -1,6 +1,5 @@
import CLASS from './class';
import { c3_chart_internal_fn } from './core';
import { convertRowsToData, convertColumnsToData } from './data.convert.utils';
c3_chart_internal_fn.load = function (targets, args) {
var $$ = this;
@ -51,10 +50,10 @@ c3_chart_internal_fn.loadFromArgs = function (args) {
$$.load($$.convertDataToTargets($$.convertJsonToData(args.json, args.keys)), args);
}
else if (args.rows) {
$$.load($$.convertDataToTargets(convertRowsToData(args.rows)), args);
$$.load($$.convertDataToTargets($$.convertRowsToData(args.rows)), args);
}
else if (args.columns) {
$$.load($$.convertDataToTargets(convertColumnsToData(args.columns)), args);
$$.load($$.convertDataToTargets($$.convertColumnsToData(args.columns)), args);
}
else {
$$.load(null, args);

Loading…
Cancel
Save