From 15f70e31f157740162b21dab006fc23303e2f689 Mon Sep 17 00:00:00 2001 From: Yoshiya Hinosawa Date: Sat, 24 Jun 2017 13:49:46 +0900 Subject: [PATCH 1/5] refactor(data.convert.utils): create data.convert.utils.js --- package-lock.json | 26 ++++++++++++++++++++- package.json | 4 +++- rollup.config.js | 15 ++++++++++++ src/api.flow.js | 5 ++-- src/core.js | 5 ++-- src/data.convert.js | 36 ++++------------------------ src/data.convert.utils.js | 49 +++++++++++++++++++++++++++++++++++++++ src/data.load.js | 5 ++-- 8 files changed, 105 insertions(+), 40 deletions(-) create mode 100644 rollup.config.js create mode 100644 src/data.convert.utils.js diff --git a/package-lock.json b/package-lock.json index 17a11baa9..2904e2204 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "c3", - "version": "0.4.13", + "version": "0.4.14", "lockfileVersion": 1, "dependencies": { "abbrev": { @@ -297,6 +297,12 @@ "integrity": "sha1-NRV7EBQm/S/9PaP3XH0ekYNbv4o=", "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": { "version": "4.1.4", "resolved": "https://registry.npmjs.org/babel-plugin-istanbul/-/babel-plugin-istanbul-4.1.4.tgz", @@ -1292,6 +1298,12 @@ "integrity": "sha1-/cpRzuYTOJXjyI1TXOSdv/YqRjM=", "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": { "version": "2.0.2", "resolved": "https://registry.npmjs.org/esutils/-/esutils-2.0.2.tgz", @@ -3680,6 +3692,18 @@ "integrity": "sha1-4NBUl4d6OYwQTYFtJzOnGKepTio=", "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": { "version": "5.0.1", "resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.0.1.tgz", diff --git a/package.json b/package.json index 44d4cb826..81e08d97b 100644 --- a/package.json +++ b/package.json @@ -8,7 +8,7 @@ "lint": "jshint --reporter=node_modules/jshint-stylish src/ spec/", "build": "npm run build:js && npm run build:css", "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:css": "npm run build:css:sass && npm run build:css:min", "build:css:sass": "node-sass src/scss/main.scss > c3.css", @@ -38,6 +38,7 @@ "d3": "~3.5.0" }, "devDependencies": { + "babel-plugin-external-helpers": "^6.22.0", "babel-plugin-istanbul": "^4.1.4", "babel-preset-es2015": "^6.24.1", "babelify": "^7.3.0", @@ -56,6 +57,7 @@ "node-sass": "^4.5.3", "node-static": "^0.7.9", "rollup": "^0.41.6", + "rollup-plugin-babel": "^2.7.1", "uglify-js": "^3.0.15", "watchify": "^3.9.0" }, diff --git a/rollup.config.js b/rollup.config.js new file mode 100644 index 000000000..7f14b76f7 --- /dev/null +++ b/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' + ] + })] +}; diff --git a/src/api.flow.js b/src/api.flow.js index a9431aaae..3c3a6607e 100644 --- a/src/api.flow.js +++ b/src/api.flow.js @@ -1,6 +1,7 @@ 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, @@ -11,10 +12,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; diff --git a/src/core.js b/src/core.js index fc2676b85..f5977f18a 100644 --- a/src/core.js +++ b/src/core.js @@ -1,6 +1,7 @@ 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" }; @@ -91,10 +92,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.'); diff --git a/src/data.convert.js b/src/data.convert.js index 34808a90b..a7da7d1be 100644 --- a/src/data.convert.js +++ b/src/data.convert.js @@ -1,5 +1,6 @@ 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'; @@ -66,12 +67,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; }; @@ -89,36 +90,7 @@ c3_chart_internal_fn.findValueInJson = function (object, path) { } 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) { var $$ = this, config = $$.config, ids = $$.d3.keys(data[0]).filter($$.isNotX, $$), diff --git a/src/data.convert.utils.js b/src/data.convert.utils.js new file mode 100644 index 000000000..55b146d76 --- /dev/null +++ b/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; +}; diff --git a/src/data.load.js b/src/data.load.js index 376d7e989..12da7b283 100644 --- a/src/data.load.js +++ b/src/data.load.js @@ -1,5 +1,6 @@ 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; @@ -50,10 +51,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); From d39f5a7bc79e53e93101b9e8e0d5013f092e9db3 Mon Sep 17 00:00:00 2001 From: Yoshiya Hinosawa Date: Sat, 24 Jun 2017 17:46:52 +0900 Subject: [PATCH 2/5] test: add test of data.convert.utils --- karma.conf.js | 4 +- spec/data.convert.utils.js | 95 ++++++++++++++++++++++++++++++++++++++ spec/shape.line-spec.js | 4 +- spec/svg-helper.js | 3 +- src/data.convert.utils.js | 32 ++++++------- 5 files changed, 115 insertions(+), 23 deletions(-) create mode 100644 spec/data.convert.utils.js diff --git a/karma.conf.js b/karma.conf.js index 269767d50..cc2feda57 100644 --- a/karma.conf.js +++ b/karma.conf.js @@ -6,10 +6,10 @@ module.exports = function(config) { files: [ 'c3.css', 'spec/*-helper.js', - 'spec/*-spec.js' + 'spec/*.js' ], preprocessors: { - 'spec/c3-helper.js': ['browserify'] + 'spec/*.js': ['browserify'] }, browserify: { debug: true, diff --git a/spec/data.convert.utils.js b/spec/data.convert.utils.js new file mode 100644 index 000000000..bf2b0f433 --- /dev/null +++ b/spec/data.convert.utils.js @@ -0,0 +1,95 @@ +import { convertColumnsToData, convertRowsToData } from '../src/data.convert.utils'; + +describe('convertColumnsToData', () => { + it('converts column data to normalized data', () => { + const data = convertColumnsToData([ + ["cat1", "a", "b", "c", "d"], + ["data1", 30, 200, 100, 400], + ["cat2", "b", "a", "c", "d", "e", "f"], + ["data2", 400, 60, 200, 800, 10, 10] + ]); + + expect(data).toEqual([{ + cat1: 'a', + data1: 30, + cat2: 'b', + data2: 400 + }, { + cat1: 'b', + data1: 200, + cat2: 'a', + data2: 60 + }, { + cat1: 'c', + data1: 100, + cat2: 'c', + data2: 200 + }, { + cat1: 'd', + data1: 400, + cat2: 'd', + data2: 800 + }, { + cat2: 'e', + data2: 10 + }, { + cat2: 'f', + data2: 10 + }]); + }); + + it('throws when the column data contains undefined', () => { + expect(() => convertColumnsToData([ + ["cat1", "a", "b", "c", "d"], + ["data1", undefined] + ])).toThrowError(Error, /Source data is missing a component/); + }); +}); + +describe('convertRowsToData', () => { + it('converts the row data to normalized data', () => { + const data = convertRowsToData([ + ['data1', 'data2', 'data3'], + [90, 120, 300], + [40, 160, 240], + [50, 200, 290], + [120, 160, 230], + [80, 130, 300], + [90, 220, 320] + ]); + + expect(data).toEqual([{ + data1: 90, + data2: 120, + data3: 300 + }, { + data1: 40, + data2: 160, + data3: 240 + }, { + data1: 50, + data2: 200, + data3: 290 + }, { + data1: 120, + data2: 160, + data3: 230 + }, { + data1: 80, + data2: 130, + data3: 300 + }, { + data1: 90, + data2: 220, + data3: 320 + }]); + }); + + it('throws when the row data contains undefined', () => { + expect(() => convertRowsToData([ + ['data1', 'data2', 'data3'], + [40, 160, 240], + [90, 120, undefined] + ])).toThrowError(Error, /Source data is missing a component/); + }); +}); diff --git a/spec/shape.line-spec.js b/spec/shape.line-spec.js index 6d6a65d96..9c8df3464 100644 --- a/spec/shape.line-spec.js +++ b/spec/shape.line-spec.js @@ -1,3 +1,5 @@ +import { parseSvgPath } from './svg-helper'; + describe('c3 chart shape line', function () { 'use strict'; @@ -7,8 +9,6 @@ describe('c3 chart shape line', function () { chart = window.initChart(chart, args, done); }); - var parseSvgPath = window.parseSvgPath; - describe('shape-rendering for line chart', function () { beforeAll(function () { diff --git a/spec/svg-helper.js b/spec/svg-helper.js index c1f2b4906..5a07768a1 100644 --- a/spec/svg-helper.js +++ b/spec/svg-helper.js @@ -3,8 +3,7 @@ * @param {String} d SvgPath d attribute.] * @return {Array} an array of drawing commands. */ - -function parseSvgPath(d) { //jshint ignore:line +export function parseSvgPath(d) { //jshint ignore:line 'use strict'; var commands = []; diff --git a/src/data.convert.utils.js b/src/data.convert.utils.js index 55b146d76..e922b1c93 100644 --- a/src/data.convert.utils.js +++ b/src/data.convert.utils.js @@ -6,21 +6,20 @@ import { isUndefined } from './util'; * @return {any[][]} */ export const convertRowsToData = (rows) => { - const new_rows = []; + const newRows = []; 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++) { + 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 + ")!"); } - new_row[keys[j]] = rows[i][j]; + newRow[keys[j]] = rows[i][j]; } - new_rows.push(new_row); + newRows.push(newRow); } - return new_rows; + return newRows; }; /** @@ -29,21 +28,20 @@ export const convertRowsToData = (rows) => { * @return {any[][]} */ export const convertColumnsToData = (columns) => { - const new_rows = []; - let i, j, key; + const newRows = []; - 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] = {}; + 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 + ")!"); } - new_rows[j - 1][key] = columns[i][j]; + newRows[j - 1][key] = columns[i][j]; } } - return new_rows; + return newRows; }; From baa195583c8f50494c15c82bfae9a5e3b2c5363a Mon Sep 17 00:00:00 2001 From: Yoshiya Hinosawa Date: Sat, 24 Jun 2017 17:53:09 +0900 Subject: [PATCH 3/5] docs: fix type annotation --- src/data.convert.utils.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/data.convert.utils.js b/src/data.convert.utils.js index e922b1c93..ff7f18cc4 100644 --- a/src/data.convert.utils.js +++ b/src/data.convert.utils.js @@ -1,9 +1,9 @@ import { isUndefined } from './util'; /** - * Converts the rows to data. + * Converts the rows to normalized data. * @param {any[][]} rows The row data - * @return {any[][]} + * @return {Object[]} */ export const convertRowsToData = (rows) => { const newRows = []; @@ -23,9 +23,9 @@ export const convertRowsToData = (rows) => { }; /** - * Converts the columns to data. + * Converts the columns to normalized data. * @param {any[][]} columns The column data - * @return {any[][]} + * @return {Object[]} */ export const convertColumnsToData = (columns) => { const newRows = []; From da6b20e60b4bdb4c67dad6e235430489aadebaaf Mon Sep 17 00:00:00 2001 From: Yoshiya Hinosawa Date: Sat, 24 Jun 2017 19:13:24 +0900 Subject: [PATCH 4/5] chore: add coverage ignore settings --- package.json | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/package.json b/package.json index 81e08d97b..aba51e03c 100644 --- a/package.json +++ b/package.json @@ -63,7 +63,8 @@ }, "nyc": { "exclude": [ - "src/polyfill.js" + "src/polyfill.js", + "spec/" ] } } From 0fc1ae7581c80f2452a9d9665220b2c929410db7 Mon Sep 17 00:00:00 2001 From: Yoshiya Hinosawa Date: Wed, 28 Jun 2017 21:01:32 +0900 Subject: [PATCH 5/5] refactor: move back convert(Rows|Columns)ToData to data.convert.js --- ...{data.convert.utils.js => data.convert.js} | 16 +++--- src/api.flow.js | 5 +- src/core.js | 5 +- src/data.convert.js | 51 +++++++++++++++++-- src/data.convert.utils.js | 47 ----------------- src/data.load.js | 5 +- 6 files changed, 63 insertions(+), 66 deletions(-) rename spec/{data.convert.utils.js => data.convert.js} (86%) delete mode 100644 src/data.convert.utils.js diff --git a/spec/data.convert.utils.js b/spec/data.convert.js similarity index 86% rename from spec/data.convert.utils.js rename to spec/data.convert.js index bf2b0f433..d5d38bb8e 100644 --- a/spec/data.convert.utils.js +++ b/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] diff --git a/src/api.flow.js b/src/api.flow.js index 3c3a6607e..a9431aaae 100644 --- a/src/api.flow.js +++ b/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; diff --git a/src/core.js b/src/core.js index f5977f18a..fc2676b85 100644 --- a/src/core.js +++ b/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.'); diff --git a/src/data.convert.js b/src/data.convert.js index a7da7d1be..0edac365a 100644 --- a/src/data.convert.js +++ b/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, $$), diff --git a/src/data.convert.utils.js b/src/data.convert.utils.js deleted file mode 100644 index ff7f18cc4..000000000 --- a/src/data.convert.utils.js +++ /dev/null @@ -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; -}; diff --git a/src/data.load.js b/src/data.load.js index 12da7b283..376d7e989 100644 --- a/src/data.load.js +++ b/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);