Browse Source

refactor(data.convert.utils): create data.convert.utils.js

pull/2088/head
Yoshiya Hinosawa 8 years ago
parent
commit
15f70e31f1
  1. 26
      package-lock.json
  2. 4
      package.json
  3. 15
      rollup.config.js
  4. 5
      src/api.flow.js
  5. 5
      src/core.js
  6. 36
      src/data.convert.js
  7. 49
      src/data.convert.utils.js
  8. 5
      src/data.load.js

26
package-lock.json generated

@ -1,6 +1,6 @@
{ {
"name": "c3", "name": "c3",
"version": "0.4.13", "version": "0.4.14",
"lockfileVersion": 1, "lockfileVersion": 1,
"dependencies": { "dependencies": {
"abbrev": { "abbrev": {
@ -297,6 +297,12 @@
"integrity": "sha1-NRV7EBQm/S/9PaP3XH0ekYNbv4o=", "integrity": "sha1-NRV7EBQm/S/9PaP3XH0ekYNbv4o=",
"dev": true "dev": true
}, },
"babel-plugin-external-helpers": {
"version": "6.22.0",
"resolved": "https://registry.npmjs.org/babel-plugin-external-helpers/-/babel-plugin-external-helpers-6.22.0.tgz",
"integrity": "sha1-IoX0iwK9Xe3oUXXK+MYuhq3M76E=",
"dev": true
},
"babel-plugin-istanbul": { "babel-plugin-istanbul": {
"version": "4.1.4", "version": "4.1.4",
"resolved": "https://registry.npmjs.org/babel-plugin-istanbul/-/babel-plugin-istanbul-4.1.4.tgz", "resolved": "https://registry.npmjs.org/babel-plugin-istanbul/-/babel-plugin-istanbul-4.1.4.tgz",
@ -1292,6 +1298,12 @@
"integrity": "sha1-/cpRzuYTOJXjyI1TXOSdv/YqRjM=", "integrity": "sha1-/cpRzuYTOJXjyI1TXOSdv/YqRjM=",
"dev": true "dev": true
}, },
"estree-walker": {
"version": "0.2.1",
"resolved": "https://registry.npmjs.org/estree-walker/-/estree-walker-0.2.1.tgz",
"integrity": "sha1-va/oCVOD2EFNXcLs9MkXO225QS4=",
"dev": true
},
"esutils": { "esutils": {
"version": "2.0.2", "version": "2.0.2",
"resolved": "https://registry.npmjs.org/esutils/-/esutils-2.0.2.tgz", "resolved": "https://registry.npmjs.org/esutils/-/esutils-2.0.2.tgz",
@ -3680,6 +3692,18 @@
"integrity": "sha1-4NBUl4d6OYwQTYFtJzOnGKepTio=", "integrity": "sha1-4NBUl4d6OYwQTYFtJzOnGKepTio=",
"dev": true "dev": true
}, },
"rollup-plugin-babel": {
"version": "2.7.1",
"resolved": "https://registry.npmjs.org/rollup-plugin-babel/-/rollup-plugin-babel-2.7.1.tgz",
"integrity": "sha1-FlKBl7D5OKFTb0RoPHqT1XMYL1c=",
"dev": true
},
"rollup-pluginutils": {
"version": "1.5.2",
"resolved": "https://registry.npmjs.org/rollup-pluginutils/-/rollup-pluginutils-1.5.2.tgz",
"integrity": "sha1-HhVud4+UtyVb+hs9AXi+j1xVJAg=",
"dev": true
},
"safe-buffer": { "safe-buffer": {
"version": "5.0.1", "version": "5.0.1",
"resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.0.1.tgz", "resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.0.1.tgz",

4
package.json

@ -8,7 +8,7 @@
"lint": "jshint --reporter=node_modules/jshint-stylish src/ spec/", "lint": "jshint --reporter=node_modules/jshint-stylish src/ spec/",
"build": "npm run build:js && npm run build:css", "build": "npm run build:js && npm run build:css",
"build:js": "npm run build:js:rollup && npm run build:js:uglify", "build:js": "npm run build:js:rollup && npm run build:js:uglify",
"build:js:rollup": "rollup -f umd --name c3 --globals d3:d3 src/index.js > c3.js", "build:js:rollup": "rollup -c > c3.js",
"build:js:uglify": "uglifyjs c3.js --compress --mangle -o c3.min.js", "build:js:uglify": "uglifyjs c3.js --compress --mangle -o c3.min.js",
"build:css": "npm run build:css:sass && npm run build:css:min", "build:css": "npm run build:css:sass && npm run build:css:min",
"build:css:sass": "node-sass src/scss/main.scss > c3.css", "build:css:sass": "node-sass src/scss/main.scss > c3.css",
@ -38,6 +38,7 @@
"d3": "~3.5.0" "d3": "~3.5.0"
}, },
"devDependencies": { "devDependencies": {
"babel-plugin-external-helpers": "^6.22.0",
"babel-plugin-istanbul": "^4.1.4", "babel-plugin-istanbul": "^4.1.4",
"babel-preset-es2015": "^6.24.1", "babel-preset-es2015": "^6.24.1",
"babelify": "^7.3.0", "babelify": "^7.3.0",
@ -56,6 +57,7 @@
"node-sass": "^4.5.3", "node-sass": "^4.5.3",
"node-static": "^0.7.9", "node-static": "^0.7.9",
"rollup": "^0.41.6", "rollup": "^0.41.6",
"rollup-plugin-babel": "^2.7.1",
"uglify-js": "^3.0.15", "uglify-js": "^3.0.15",
"watchify": "^3.9.0" "watchify": "^3.9.0"
}, },

15
rollup.config.js

@ -0,0 +1,15 @@
import babel from 'rollup-plugin-babel';
export default {
entry: 'src/index.js',
format: 'umd',
moduleName: 'c3',
plugins: [babel({
presets: [['es2015', {
modules: false
}]],
plugins: [
'external-helpers'
]
})]
};

5
src/api.flow.js

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

5
src/core.js

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

36
src/data.convert.js

@ -1,5 +1,6 @@
import { c3_chart_internal_fn } from './core'; import { c3_chart_internal_fn } from './core';
import { isValue, isUndefined, isDefined, notEmpty } from './util'; 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) { c3_chart_internal_fn.convertUrlToData = function (url, mimeType, headers, keys, done) {
var $$ = this, type = mimeType ? mimeType : 'csv'; var $$ = this, type = mimeType ? mimeType : 'csv';
@ -66,12 +67,12 @@ c3_chart_internal_fn.convertJsonToData = function (json, keys) {
}); });
new_rows.push(new_row); new_rows.push(new_row);
}); });
data = $$.convertRowsToData(new_rows); data = convertRowsToData(new_rows);
} else { } else {
Object.keys(json).forEach(function (key) { Object.keys(json).forEach(function (key) {
new_rows.push([key].concat(json[key])); new_rows.push([key].concat(json[key]));
}); });
data = $$.convertColumnsToData(new_rows); data = convertColumnsToData(new_rows);
} }
return data; return data;
}; };
@ -89,36 +90,7 @@ c3_chart_internal_fn.findValueInJson = function (object, path) {
} }
return object; return object;
}; };
c3_chart_internal_fn.convertRowsToData = function (rows) {
var keys = rows[0], new_row = {}, new_rows = [], i, j;
for (i = 1; i < rows.length; i++) {
new_row = {};
for (j = 0; j < rows[i].length; j++) {
if (isUndefined(rows[i][j])) {
throw new Error("Source data is missing a component at (" + i + "," + j + ")!");
}
new_row[keys[j]] = rows[i][j];
}
new_rows.push(new_row);
}
return new_rows;
};
c3_chart_internal_fn.convertColumnsToData = function (columns) {
var new_rows = [], i, j, key;
for (i = 0; i < columns.length; i++) {
key = columns[i][0];
for (j = 1; j < columns[i].length; j++) {
if (isUndefined(new_rows[j - 1])) {
new_rows[j - 1] = {};
}
if (isUndefined(columns[i][j])) {
throw new Error("Source data is missing a component at (" + i + "," + j + ")!");
}
new_rows[j - 1][key] = columns[i][j];
}
}
return new_rows;
};
c3_chart_internal_fn.convertDataToTargets = function (data, appendXs) { c3_chart_internal_fn.convertDataToTargets = function (data, appendXs) {
var $$ = this, config = $$.config, var $$ = this, config = $$.config,
ids = $$.d3.keys(data[0]).filter($$.isNotX, $$), ids = $$.d3.keys(data[0]).filter($$.isNotX, $$),

49
src/data.convert.utils.js

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

5
src/data.load.js

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

Loading…
Cancel
Save