From c17ec0ab23c62d54fc5a14cb8c3e900ece298735 Mon Sep 17 00:00:00 2001 From: Yoshiya Hinosawa Date: Sat, 27 May 2017 04:44:34 +0900 Subject: [PATCH] Switch to rollup (#2037) * chore: define run scripts, reduce grunt usage * refactor(rollup): switch to rollup * chore: fix node.js version on ci env --- .jshintrc | 7 ++- .travis.yml | 5 +-- Gruntfile.coffee | 80 +--------------------------------- package.json | 8 +++- src/api.axis.js | 3 ++ src/api.category.js | 2 + src/api.chart.js | 2 + src/api.color.js | 2 + src/api.data.js | 2 + src/api.flow.js | 4 ++ src/api.focus.js | 3 ++ src/api.grid.js | 2 + src/api.group.js | 3 ++ src/api.legend.js | 2 + src/api.load.js | 2 + src/api.region.js | 4 ++ src/api.selection.js | 4 ++ src/api.show.js | 2 + src/api.tooltip.js | 3 ++ src/api.transform.js | 2 + src/api.x.js | 2 + src/api.zoom.js | 3 ++ src/arc.js | 4 ++ src/axis.js | 7 ++- src/c3.axis.js | 2 +- src/cache.js | 2 + src/category.js | 2 + src/class-utils.js | 101 +++++++++++++++++++++++++++++++++++++++++++ src/class.js | 100 +----------------------------------------- src/clip.js | 2 + src/color.js | 3 ++ src/config.js | 5 ++- src/core.js | 38 +++++++++++++--- src/data.convert.js | 3 ++ src/data.js | 4 ++ src/data.load.js | 3 ++ src/domain.js | 3 ++ src/drag.js | 4 ++ src/format.js | 3 ++ src/grid.js | 4 ++ src/head.js | 4 -- src/index.js | 58 +++++++++++++++++++++++++ src/interaction.js | 3 ++ src/legend.js | 4 ++ src/region.js | 4 ++ src/scale.js | 2 + src/selection.js | 3 ++ src/shape.bar.js | 4 ++ src/shape.js | 4 ++ src/shape.line.js | 4 ++ src/size.js | 10 +++-- src/subchart.js | 4 ++ src/tail.js | 9 ---- src/text.js | 3 ++ src/title.js | 3 ++ src/tooltip.js | 4 ++ src/type.js | 3 ++ src/ua.js | 2 + src/util.js | 98 +++++++++++++++++++++-------------------- src/zoom.js | 4 ++ 60 files changed, 408 insertions(+), 259 deletions(-) create mode 100644 src/class-utils.js delete mode 100644 src/head.js create mode 100644 src/index.js delete mode 100644 src/tail.js diff --git a/.jshintrc b/.jshintrc index 164107c..f5504fa 100644 --- a/.jshintrc +++ b/.jshintrc @@ -1,7 +1,7 @@ { "eqeqeq": true, "curly": true, - "strict": true, + "strict": false, "trailing": true, "white": true, "maxlen": 210, @@ -24,7 +24,10 @@ "beforeAll": false, "it": false, "expect": false, - "d3": false + "d3": false, + "module": false, + "require": false, + "define": false } } diff --git a/.travis.yml b/.travis.yml index 989225e..9642828 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,12 +1,11 @@ language: node_js +node_js: +- 6 before_install: - npm install -g grunt-cli - gem install sass -script: -- grunt - after_success: - npm run codecov diff --git a/Gruntfile.coffee b/Gruntfile.coffee index f6dc622..745bf2a 100644 --- a/Gruntfile.coffee +++ b/Gruntfile.coffee @@ -1,84 +1,12 @@ module.exports = (grunt) -> - require('load-grunt-tasks') grunt, pattern: ['grunt-contrib-*', 'grunt-sass', 'grunt-karma'] + require('load-grunt-tasks') grunt, pattern: ['grunt-contrib-*', 'grunt-sass'] grunt.initConfig watch: - concat: - tasks: 'concat' - files: ['src/*.js'] sass: tasks: 'sass' files: ['src/scss/*.scss'] - concat: - dist: - options: - process: (src, filepath) -> - if filepath != 'src/head.js' && filepath != 'src/tail.js' - lines = [] - src.split('\n').forEach (line) -> - lines.push( (if line.length > 0 then ' ' else '') + line) - src = lines.join('\n') - return src - src: [ - 'src/head.js', - 'src/core.js', - 'src/config.js', - 'src/scale.js', - 'src/domain.js', - 'src/data.js', - 'src/data.convert.js', - 'src/data.load.js', - 'src/category.js', - 'src/interaction.js', - 'src/size.js', - 'src/shape.js', - 'src/shape.line.js', - 'src/shape.bar.js', - 'src/text.js', - 'src/type.js', - 'src/grid.js', - 'src/tooltip.js', - 'src/legend.js', - 'src/title.js', - 'src/axis.js', - 'src/clip.js', - 'src/arc.js', - 'src/region.js', - 'src/drag.js', - 'src/selection.js', - 'src/subchart.js', - 'src/zoom.js', - 'src/color.js', - 'src/format.js', - 'src/cache.js', - 'src/class.js', - 'src/util.js', - 'src/api.focus.js', - 'src/api.show.js', - 'src/api.zoom.js', - 'src/api.load.js', - 'src/api.flow.js', - 'src/api.selection.js', - 'src/api.transform.js', - 'src/api.group.js', - 'src/api.grid.js', - 'src/api.region.js', - 'src/api.data.js', - 'src/api.category.js', - 'src/api.color.js', - 'src/api.x.js', - 'src/api.axis.js', - 'src/api.legend.js', - 'src/api.chart.js', - 'src/api.tooltip.js', - 'src/c3.axis.js', - 'src/ua.js', - 'src/polyfill.js', - 'src/tail.js' - ] - dest: 'c3.js' - jshint: c3: 'c3.js' spec: 'spec/*.js' @@ -86,10 +14,6 @@ module.exports = (grunt) -> reporter: require('jshint-stylish') jshintrc: '.jshintrc' - karma: - unit: - configFile: 'karma.conf.js' - uglify: c3: files: @@ -108,7 +32,5 @@ module.exports = (grunt) -> 'c3.css': 'src/scss/main.scss' grunt.registerTask 'lint', ['jshint'] - grunt.registerTask 'test', ['karma'] grunt.registerTask 'build', ['concat', 'sass'] grunt.registerTask 'minify', ['cssmin', 'uglify'] - grunt.registerTask 'default', ['lint', 'build', 'test', 'minify'] diff --git a/package.json b/package.json index edaf70c..7c08c28 100644 --- a/package.json +++ b/package.json @@ -6,7 +6,10 @@ "scripts": { "prepublish": "grunt", "lint": "grunt lint", - "test": "karma start karma.conf.js", + "build": "rollup -f umd --name c3 src/index.js > c3.js", + "minify": "grunt minify", + "test": "npm run build && npm run lint && karma start karma.conf.js", + "dist": "npm run build && npm run minify", "codecov": "cat coverage/*/lcov.info | codecov" }, "repository": { @@ -43,6 +46,7 @@ "karma-phantomjs-launcher": "^0.2.1", "karma-spec-reporter": "0.0.20", "load-grunt-tasks": "~0.2.0", - "phantomjs": "^1.9.18" + "phantomjs": "^1.9.18", + "rollup": "^0.41.6" } } diff --git a/src/api.axis.js b/src/api.axis.js index 281a7bb..40c9bcc 100644 --- a/src/api.axis.js +++ b/src/api.axis.js @@ -1,3 +1,6 @@ +import { c3_chart_fn } from './core'; +import { isValue, isDefined } from './util'; + c3_chart_fn.axis = function () {}; c3_chart_fn.axis.labels = function (labels) { var $$ = this.internal; diff --git a/src/api.category.js b/src/api.category.js index 1616c4a..a1c73cc 100644 --- a/src/api.category.js +++ b/src/api.category.js @@ -1,3 +1,5 @@ +import { c3_chart_fn } from './core'; + c3_chart_fn.category = function (i, category) { var $$ = this.internal, config = $$.config; if (arguments.length > 1) { diff --git a/src/api.chart.js b/src/api.chart.js index 0e4eacb..a06381a 100644 --- a/src/api.chart.js +++ b/src/api.chart.js @@ -1,3 +1,5 @@ +import { c3_chart_fn } from './core'; + c3_chart_fn.resize = function (size) { var $$ = this.internal, config = $$.config; config.size_width = size ? size.width : null; diff --git a/src/api.color.js b/src/api.color.js index 15aaa3e..680c4bb 100644 --- a/src/api.color.js +++ b/src/api.color.js @@ -1,3 +1,5 @@ +import { c3_chart_fn } from './core'; + // TODO: fix c3_chart_fn.color = function (id) { var $$ = this.internal; diff --git a/src/api.data.js b/src/api.data.js index be5b8f7..8793e64 100644 --- a/src/api.data.js +++ b/src/api.data.js @@ -1,3 +1,5 @@ +import { c3_chart_fn } from './core'; + c3_chart_fn.data = function (targetIds) { var targets = this.internal.data.targets; return typeof targetIds === 'undefined' ? targets : targets.filter(function (t) { diff --git a/src/api.flow.js b/src/api.flow.js index 2abad3e..a9431aa 100644 --- a/src/api.flow.js +++ b/src/api.flow.js @@ -1,3 +1,7 @@ +import CLASS from './class'; +import { c3_chart_fn, c3_chart_internal_fn } from './core'; +import { isValue, isDefined, diffDomain } from './util'; + c3_chart_fn.flow = function (args) { var $$ = this.internal, targets, data, notfoundIds = [], orgDataCount = $$.getMaxDataCount(), diff --git a/src/api.focus.js b/src/api.focus.js index 1a65bcc..bf00922 100644 --- a/src/api.focus.js +++ b/src/api.focus.js @@ -1,3 +1,6 @@ +import CLASS from './class'; +import { c3_chart_fn } from './core'; + c3_chart_fn.focus = function (targetIds) { var $$ = this.internal, candidates; diff --git a/src/api.grid.js b/src/api.grid.js index 038aa6b..1754188 100644 --- a/src/api.grid.js +++ b/src/api.grid.js @@ -1,3 +1,5 @@ +import { c3_chart_fn } from './core'; + c3_chart_fn.xgrids = function (grids) { var $$ = this.internal, config = $$.config; if (! grids) { return config.grid_x_lines; } diff --git a/src/api.group.js b/src/api.group.js index 47fedb4..e076b11 100644 --- a/src/api.group.js +++ b/src/api.group.js @@ -1,3 +1,6 @@ +import { c3_chart_fn } from './core'; +import { isUndefined } from './util'; + c3_chart_fn.groups = function (groups) { var $$ = this.internal, config = $$.config; if (isUndefined(groups)) { return config.data_groups; } diff --git a/src/api.legend.js b/src/api.legend.js index 182caa1..cc9ed75 100644 --- a/src/api.legend.js +++ b/src/api.legend.js @@ -1,3 +1,5 @@ +import { c3_chart_fn } from './core'; + c3_chart_fn.legend = function () {}; c3_chart_fn.legend.show = function (targetIds) { var $$ = this.internal; diff --git a/src/api.load.js b/src/api.load.js index b2f0272..69be061 100644 --- a/src/api.load.js +++ b/src/api.load.js @@ -1,3 +1,5 @@ +import { c3_chart_fn } from './core'; + c3_chart_fn.load = function (args) { var $$ = this.internal, config = $$.config; // update xs if specified diff --git a/src/api.region.js b/src/api.region.js index b587b4f..14d5bce 100644 --- a/src/api.region.js +++ b/src/api.region.js @@ -1,3 +1,7 @@ +import CLASS from './class'; +import { c3_chart_fn } from './core'; +import { getOption } from './util'; + c3_chart_fn.regions = function (regions) { var $$ = this.internal, config = $$.config; if (!regions) { return config.regions; } diff --git a/src/api.selection.js b/src/api.selection.js index 02ad601..5b94b4b 100644 --- a/src/api.selection.js +++ b/src/api.selection.js @@ -1,3 +1,7 @@ +import CLASS from './class'; +import { c3_chart_fn } from './core'; +import { isDefined } from './util'; + c3_chart_fn.selected = function (targetId) { var $$ = this.internal, d3 = $$.d3; return d3.merge( diff --git a/src/api.show.js b/src/api.show.js index 08e99e1..5a34031 100644 --- a/src/api.show.js +++ b/src/api.show.js @@ -1,3 +1,5 @@ +import { c3_chart_fn } from './core'; + c3_chart_fn.show = function (targetIds, options) { var $$ = this.internal, targets; diff --git a/src/api.tooltip.js b/src/api.tooltip.js index 7c6dcb9..4f0b3cc 100644 --- a/src/api.tooltip.js +++ b/src/api.tooltip.js @@ -1,3 +1,6 @@ +import { c3_chart_fn } from './core'; +import { isValue } from './util'; + c3_chart_fn.tooltip = function () {}; c3_chart_fn.tooltip.show = function (args) { var $$ = this.internal, index, mouse; diff --git a/src/api.transform.js b/src/api.transform.js index f04b505..b89c733 100644 --- a/src/api.transform.js +++ b/src/api.transform.js @@ -1,3 +1,5 @@ +import { c3_chart_fn, c3_chart_internal_fn } from './core'; + c3_chart_fn.transform = function (type, targetIds) { var $$ = this.internal, options = ['pie', 'donut'].indexOf(type) >= 0 ? {withTransform: true} : null; diff --git a/src/api.x.js b/src/api.x.js index 93d25d4..fedd5f2 100644 --- a/src/api.x.js +++ b/src/api.x.js @@ -1,3 +1,5 @@ +import { c3_chart_fn } from './core'; + c3_chart_fn.x = function (x) { var $$ = this.internal; if (arguments.length) { diff --git a/src/api.zoom.js b/src/api.zoom.js index 023ce68..cbcf6bf 100644 --- a/src/api.zoom.js +++ b/src/api.zoom.js @@ -1,3 +1,6 @@ +import { c3_chart_fn } from './core'; +import { isDefined } from './util'; + c3_chart_fn.zoom = function (domain) { var $$ = this.internal; if (domain) { diff --git a/src/arc.js b/src/arc.js index b94baab..c8c896b 100644 --- a/src/arc.js +++ b/src/arc.js @@ -1,3 +1,7 @@ +import CLASS from './class'; +import { c3_chart_internal_fn } from './core'; +import { isFunction } from './util'; + c3_chart_internal_fn.initPie = function () { var $$ = this, d3 = $$.d3, config = $$.config; $$.pie = d3.layout.pie().value(function (d) { diff --git a/src/axis.js b/src/axis.js index e3310b4..982af2d 100644 --- a/src/axis.js +++ b/src/axis.js @@ -1,4 +1,9 @@ -function Axis(owner) { +import CLASS from './class'; +import { inherit, API } from './core' +import { isValue, isFunction, isString, isEmpty } from './util'; +import c3_axis from './c3.axis' + +export default function Axis(owner) { API.call(this, owner); } diff --git a/src/c3.axis.js b/src/c3.axis.js index 6d4f7a3..0a702e1 100644 --- a/src/c3.axis.js +++ b/src/c3.axis.js @@ -3,7 +3,7 @@ // 2. ceil values of translate/x/y to int for half pixel antialiasing // 3. multiline tick text var tickTextCharSize; -function c3_axis(d3, params) { +export default function c3_axis(d3, params) { var scale = d3.scale.linear(), orient = "bottom", innerTickSize = 6, outerTickSize, tickPadding = 3, tickValues = null, tickFormat, tickArguments; var tickOffset = 0, tickCulling = true, tickCentered; diff --git a/src/cache.js b/src/cache.js index 6566616..b2f05cc 100644 --- a/src/cache.js +++ b/src/cache.js @@ -1,3 +1,5 @@ +import { c3_chart_internal_fn } from './core'; + c3_chart_internal_fn.hasCaches = function (ids) { for (var i = 0; i < ids.length; i++) { if (! (ids[i] in this.cache)) { return false; } diff --git a/src/category.js b/src/category.js index 5143c6c..ea7fcba 100644 --- a/src/category.js +++ b/src/category.js @@ -1,3 +1,5 @@ +import { c3_chart_internal_fn } from './core'; + c3_chart_internal_fn.categoryName = function (i) { var config = this.config; return i < config.axis_x_categories.length ? config.axis_x_categories[i] : i; diff --git a/src/class-utils.js b/src/class-utils.js new file mode 100644 index 0000000..63d54e9 --- /dev/null +++ b/src/class-utils.js @@ -0,0 +1,101 @@ +import CLASS from './class' +import { c3_chart_internal_fn } from './core'; + +c3_chart_internal_fn.generateClass = function (prefix, targetId) { + return " " + prefix + " " + prefix + this.getTargetSelectorSuffix(targetId); +}; +c3_chart_internal_fn.classText = function (d) { + return this.generateClass(CLASS.text, d.index); +}; +c3_chart_internal_fn.classTexts = function (d) { + return this.generateClass(CLASS.texts, d.id); +}; +c3_chart_internal_fn.classShape = function (d) { + return this.generateClass(CLASS.shape, d.index); +}; +c3_chart_internal_fn.classShapes = function (d) { + return this.generateClass(CLASS.shapes, d.id); +}; +c3_chart_internal_fn.classLine = function (d) { + return this.classShape(d) + this.generateClass(CLASS.line, d.id); +}; +c3_chart_internal_fn.classLines = function (d) { + return this.classShapes(d) + this.generateClass(CLASS.lines, d.id); +}; +c3_chart_internal_fn.classCircle = function (d) { + return this.classShape(d) + this.generateClass(CLASS.circle, d.index); +}; +c3_chart_internal_fn.classCircles = function (d) { + return this.classShapes(d) + this.generateClass(CLASS.circles, d.id); +}; +c3_chart_internal_fn.classBar = function (d) { + return this.classShape(d) + this.generateClass(CLASS.bar, d.index); +}; +c3_chart_internal_fn.classBars = function (d) { + return this.classShapes(d) + this.generateClass(CLASS.bars, d.id); +}; +c3_chart_internal_fn.classArc = function (d) { + return this.classShape(d.data) + this.generateClass(CLASS.arc, d.data.id); +}; +c3_chart_internal_fn.classArcs = function (d) { + return this.classShapes(d.data) + this.generateClass(CLASS.arcs, d.data.id); +}; +c3_chart_internal_fn.classArea = function (d) { + return this.classShape(d) + this.generateClass(CLASS.area, d.id); +}; +c3_chart_internal_fn.classAreas = function (d) { + return this.classShapes(d) + this.generateClass(CLASS.areas, d.id); +}; +c3_chart_internal_fn.classRegion = function (d, i) { + return this.generateClass(CLASS.region, i) + ' ' + ('class' in d ? d['class'] : ''); +}; +c3_chart_internal_fn.classEvent = function (d) { + return this.generateClass(CLASS.eventRect, d.index); +}; +c3_chart_internal_fn.classTarget = function (id) { + var $$ = this; + var additionalClassSuffix = $$.config.data_classes[id], additionalClass = ''; + if (additionalClassSuffix) { + additionalClass = ' ' + CLASS.target + '-' + additionalClassSuffix; + } + return $$.generateClass(CLASS.target, id) + additionalClass; +}; +c3_chart_internal_fn.classFocus = function (d) { + return this.classFocused(d) + this.classDefocused(d); +}; +c3_chart_internal_fn.classFocused = function (d) { + return ' ' + (this.focusedTargetIds.indexOf(d.id) >= 0 ? CLASS.focused : ''); +}; +c3_chart_internal_fn.classDefocused = function (d) { + return ' ' + (this.defocusedTargetIds.indexOf(d.id) >= 0 ? CLASS.defocused : ''); +}; +c3_chart_internal_fn.classChartText = function (d) { + return CLASS.chartText + this.classTarget(d.id); +}; +c3_chart_internal_fn.classChartLine = function (d) { + return CLASS.chartLine + this.classTarget(d.id); +}; +c3_chart_internal_fn.classChartBar = function (d) { + return CLASS.chartBar + this.classTarget(d.id); +}; +c3_chart_internal_fn.classChartArc = function (d) { + return CLASS.chartArc + this.classTarget(d.data.id); +}; +c3_chart_internal_fn.getTargetSelectorSuffix = function (targetId) { + return targetId || targetId === 0 ? ('-' + targetId).replace(/[\s?!@#$%^&*()_=+,.<>'":;\[\]\/|~`{}\\]/g, '-') : ''; +}; +c3_chart_internal_fn.selectorTarget = function (id, prefix) { + return (prefix || '') + '.' + CLASS.target + this.getTargetSelectorSuffix(id); +}; +c3_chart_internal_fn.selectorTargets = function (ids, prefix) { + var $$ = this; + ids = ids || []; + return ids.length ? ids.map(function (id) { return $$.selectorTarget(id, prefix); }) : null; +}; +c3_chart_internal_fn.selectorLegend = function (id) { + return '.' + CLASS.legendItem + this.getTargetSelectorSuffix(id); +}; +c3_chart_internal_fn.selectorLegends = function (ids) { + var $$ = this; + return ids && ids.length ? ids.map(function (id) { return $$.selectorLegend(id); }) : null; +}; diff --git a/src/class.js b/src/class.js index 66171a7..fa18b13 100644 --- a/src/class.js +++ b/src/class.js @@ -1,4 +1,4 @@ -var CLASS = c3_chart_internal_fn.CLASS = { +export default { target: 'c3-target', chart: 'c3-chart', chartLine: 'c3-chart-line', @@ -75,101 +75,3 @@ var CLASS = c3_chart_internal_fn.CLASS = { SELECTED: '_selected_', INCLUDED: '_included_' }; -c3_chart_internal_fn.generateClass = function (prefix, targetId) { - return " " + prefix + " " + prefix + this.getTargetSelectorSuffix(targetId); -}; -c3_chart_internal_fn.classText = function (d) { - return this.generateClass(CLASS.text, d.index); -}; -c3_chart_internal_fn.classTexts = function (d) { - return this.generateClass(CLASS.texts, d.id); -}; -c3_chart_internal_fn.classShape = function (d) { - return this.generateClass(CLASS.shape, d.index); -}; -c3_chart_internal_fn.classShapes = function (d) { - return this.generateClass(CLASS.shapes, d.id); -}; -c3_chart_internal_fn.classLine = function (d) { - return this.classShape(d) + this.generateClass(CLASS.line, d.id); -}; -c3_chart_internal_fn.classLines = function (d) { - return this.classShapes(d) + this.generateClass(CLASS.lines, d.id); -}; -c3_chart_internal_fn.classCircle = function (d) { - return this.classShape(d) + this.generateClass(CLASS.circle, d.index); -}; -c3_chart_internal_fn.classCircles = function (d) { - return this.classShapes(d) + this.generateClass(CLASS.circles, d.id); -}; -c3_chart_internal_fn.classBar = function (d) { - return this.classShape(d) + this.generateClass(CLASS.bar, d.index); -}; -c3_chart_internal_fn.classBars = function (d) { - return this.classShapes(d) + this.generateClass(CLASS.bars, d.id); -}; -c3_chart_internal_fn.classArc = function (d) { - return this.classShape(d.data) + this.generateClass(CLASS.arc, d.data.id); -}; -c3_chart_internal_fn.classArcs = function (d) { - return this.classShapes(d.data) + this.generateClass(CLASS.arcs, d.data.id); -}; -c3_chart_internal_fn.classArea = function (d) { - return this.classShape(d) + this.generateClass(CLASS.area, d.id); -}; -c3_chart_internal_fn.classAreas = function (d) { - return this.classShapes(d) + this.generateClass(CLASS.areas, d.id); -}; -c3_chart_internal_fn.classRegion = function (d, i) { - return this.generateClass(CLASS.region, i) + ' ' + ('class' in d ? d['class'] : ''); -}; -c3_chart_internal_fn.classEvent = function (d) { - return this.generateClass(CLASS.eventRect, d.index); -}; -c3_chart_internal_fn.classTarget = function (id) { - var $$ = this; - var additionalClassSuffix = $$.config.data_classes[id], additionalClass = ''; - if (additionalClassSuffix) { - additionalClass = ' ' + CLASS.target + '-' + additionalClassSuffix; - } - return $$.generateClass(CLASS.target, id) + additionalClass; -}; -c3_chart_internal_fn.classFocus = function (d) { - return this.classFocused(d) + this.classDefocused(d); -}; -c3_chart_internal_fn.classFocused = function (d) { - return ' ' + (this.focusedTargetIds.indexOf(d.id) >= 0 ? CLASS.focused : ''); -}; -c3_chart_internal_fn.classDefocused = function (d) { - return ' ' + (this.defocusedTargetIds.indexOf(d.id) >= 0 ? CLASS.defocused : ''); -}; -c3_chart_internal_fn.classChartText = function (d) { - return CLASS.chartText + this.classTarget(d.id); -}; -c3_chart_internal_fn.classChartLine = function (d) { - return CLASS.chartLine + this.classTarget(d.id); -}; -c3_chart_internal_fn.classChartBar = function (d) { - return CLASS.chartBar + this.classTarget(d.id); -}; -c3_chart_internal_fn.classChartArc = function (d) { - return CLASS.chartArc + this.classTarget(d.data.id); -}; -c3_chart_internal_fn.getTargetSelectorSuffix = function (targetId) { - return targetId || targetId === 0 ? ('-' + targetId).replace(/[\s?!@#$%^&*()_=+,.<>'":;\[\]\/|~`{}\\]/g, '-') : ''; -}; -c3_chart_internal_fn.selectorTarget = function (id, prefix) { - return (prefix || '') + '.' + CLASS.target + this.getTargetSelectorSuffix(id); -}; -c3_chart_internal_fn.selectorTargets = function (ids, prefix) { - var $$ = this; - ids = ids || []; - return ids.length ? ids.map(function (id) { return $$.selectorTarget(id, prefix); }) : null; -}; -c3_chart_internal_fn.selectorLegend = function (id) { - return '.' + CLASS.legendItem + this.getTargetSelectorSuffix(id); -}; -c3_chart_internal_fn.selectorLegends = function (ids) { - var $$ = this; - return ids && ids.length ? ids.map(function (id) { return $$.selectorLegend(id); }) : null; -}; diff --git a/src/clip.js b/src/clip.js index 322dbb5..bb6029d 100644 --- a/src/clip.js +++ b/src/clip.js @@ -1,3 +1,5 @@ +import { c3_chart_internal_fn } from './core'; + c3_chart_internal_fn.getClipPath = function (id) { var isIE9 = window.navigator.appVersion.toLowerCase().indexOf("msie 9.") >= 0; return "url(" + (isIE9 ? "" : document.URL.split('#')[0]) + "#" + id + ")"; diff --git a/src/color.js b/src/color.js index da1888e..4585d85 100644 --- a/src/color.js +++ b/src/color.js @@ -1,3 +1,6 @@ +import { c3_chart_internal_fn } from './core'; +import { notEmpty } from './util'; + c3_chart_internal_fn.generateColor = function () { var $$ = this, config = $$.config, d3 = $$.d3, colors = config.data_colors, diff --git a/src/config.js b/src/config.js index c127be5..54d5b18 100644 --- a/src/config.js +++ b/src/config.js @@ -1,3 +1,6 @@ +import { c3_chart_internal_fn } from './core'; +import { isDefined } from './util'; + c3_chart_internal_fn.getDefaultConfig = function () { var config = { bindto: '#chart', @@ -121,7 +124,7 @@ c3_chart_internal_fn.getDefaultConfig = function () { axis_y_label: {}, axis_y_tick_format: undefined, axis_y_tick_outer: true, - axis_y_tick_values: null, + axis_y_tick_values: null, axis_y_tick_rotate: 0, axis_y_tick_count: undefined, axis_y_tick_time_value: undefined, diff --git a/src/core.js b/src/core.js index 7a933d5..94ac711 100644 --- a/src/core.js +++ b/src/core.js @@ -1,14 +1,20 @@ -var c3 = { version: "0.4.11" }; +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'; -var c3_chart_fn, - c3_chart_internal_fn, - c3_chart_internal_axis_fn; +export var c3 = { version: "0.4.11" }; -function API(owner) { +export var c3_chart_fn; +export var c3_chart_internal_fn; +export var c3_chart_internal_axis_fn; + +var d3 = window.d3 ? window.d3 : typeof require !== 'undefined' ? require("d3") : undefined; + +export function API(owner) { this.owner = owner; } -function inherit(base, derived) { +export function inherit(base, derived) { if (Object.create) { derived.prototype = Object.create(base.prototype); @@ -44,7 +50,7 @@ function Chart(config) { function ChartInternal(api) { var $$ = this; - $$.d3 = window.d3 ? window.d3 : typeof require !== 'undefined' ? require("d3") : undefined; + $$.d3 = d3 $$.api = api; $$.config = $$.getDefaultConfig(); $$.data = {}; @@ -1059,3 +1065,21 @@ c3_chart_internal_fn.isTabVisible = function () { return document[hidden] ? false : true; }; + +c3_chart_internal_fn.isValue = isValue; +c3_chart_internal_fn.isFunction = isFunction; +c3_chart_internal_fn.isString = isString; +c3_chart_internal_fn.isUndefined = isUndefined; +c3_chart_internal_fn.isDefined = isDefined; +c3_chart_internal_fn.ceil10 = ceil10; +c3_chart_internal_fn.asHalfPixel = asHalfPixel; +c3_chart_internal_fn.diffDomain = diffDomain; +c3_chart_internal_fn.isEmpty = isEmpty; +c3_chart_internal_fn.notEmpty = notEmpty; +c3_chart_internal_fn.notEmpty = notEmpty; +c3_chart_internal_fn.getOption = getOption; +c3_chart_internal_fn.hasValue = hasValue; +c3_chart_internal_fn.sanitise = sanitise; +c3_chart_internal_fn.getPathBox = getPathBox; +c3_chart_internal_fn.CLASS = CLASS; + diff --git a/src/data.convert.js b/src/data.convert.js index 8c523f5..00853f6 100644 --- a/src/data.convert.js +++ b/src/data.convert.js @@ -1,3 +1,6 @@ +import { c3_chart_internal_fn } from './core'; +import { isValue, isUndefined, isDefined, notEmpty } from './util'; + c3_chart_internal_fn.convertUrlToData = function (url, mimeType, headers, keys, done) { var $$ = this, type = mimeType ? mimeType : 'csv'; var req = $$.d3.xhr(url); diff --git a/src/data.js b/src/data.js index 531ab69..90cb36b 100644 --- a/src/data.js +++ b/src/data.js @@ -1,3 +1,7 @@ +import CLASS from './class'; +import { c3_chart_internal_fn } from './core'; +import { isValue, isFunction, notEmpty, hasValue } from './util'; + c3_chart_internal_fn.isX = function (key) { var $$ = this, config = $$.config; return (config.data_x && key === config.data_x) || (notEmpty(config.data_xs) && hasValue(config.data_xs, key)); diff --git a/src/data.load.js b/src/data.load.js index 3b5bf72..376d7e9 100644 --- a/src/data.load.js +++ b/src/data.load.js @@ -1,3 +1,6 @@ +import CLASS from './class'; +import { c3_chart_internal_fn } from './core'; + c3_chart_internal_fn.load = function (targets, args) { var $$ = this; if (targets) { diff --git a/src/domain.js b/src/domain.js index 770c63f..5439b57 100644 --- a/src/domain.js +++ b/src/domain.js @@ -1,3 +1,6 @@ +import { c3_chart_internal_fn } from './core'; +import { isValue, isDefined, diffDomain, notEmpty } from './util'; + c3_chart_internal_fn.getYDomainMin = function (targets) { var $$ = this, config = $$.config, ids = $$.mapToIds(targets), ys = $$.getValuesAsIdKeyed(targets), diff --git a/src/drag.js b/src/drag.js index 56ab070..dce7bcf 100644 --- a/src/drag.js +++ b/src/drag.js @@ -1,3 +1,7 @@ +import CLASS from './class'; +import { c3_chart_internal_fn } from './core'; +import { getPathBox } from './util'; + c3_chart_internal_fn.drag = function (mouse) { var $$ = this, config = $$.config, main = $$.main, d3 = $$.d3; var sx, sy, mx, my, minX, maxX, minY, maxY; diff --git a/src/format.js b/src/format.js index aa72780..6fe60e5 100644 --- a/src/format.js +++ b/src/format.js @@ -1,3 +1,6 @@ +import { c3_chart_internal_fn } from './core'; +import { isValue } from './util'; + c3_chart_internal_fn.getYFormat = function (forArc) { var $$ = this, formatForY = forArc && !$$.hasType('gauge') ? $$.defaultArcValueFormat : $$.yFormat, diff --git a/src/grid.js b/src/grid.js index 9a06432..ddf417e 100644 --- a/src/grid.js +++ b/src/grid.js @@ -1,3 +1,7 @@ +import CLASS from './class'; +import { c3_chart_internal_fn } from './core'; +import { isValue } from './util'; + c3_chart_internal_fn.initGrid = function () { var $$ = this, config = $$.config, d3 = $$.d3; $$.grid = $$.main.append('g') diff --git a/src/head.js b/src/head.js deleted file mode 100644 index d77cbd0..0000000 --- a/src/head.js +++ /dev/null @@ -1,4 +0,0 @@ -(function (window) { - 'use strict'; - - /*global define, module, exports, require */ diff --git a/src/index.js b/src/index.js new file mode 100644 index 0000000..dd9cbeb --- /dev/null +++ b/src/index.js @@ -0,0 +1,58 @@ +import { c3 } from './core' + +import './polyfill'; + +import './api.axis'; +import './api.category'; +import './api.chart'; +import './api.color'; +import './api.data'; +import './api.flow'; +import './api.focus'; +import './api.grid'; +import './api.group'; +import './api.legend'; +import './api.load'; +import './api.region'; +import './api.selection'; +import './api.show'; +import './api.tooltip'; +import './api.transform'; +import './api.x'; +import './api.zoom'; +import './arc'; +import './axis'; +import './c3.axis'; +import './cache'; +import './category'; +import './class'; +import './class-utils'; +import './clip'; +import './color'; +import './config'; +import './data.convert'; +import './data'; +import './data.load'; +import './domain'; +import './drag'; +import './format'; +import './grid'; +import './interaction'; +import './legend'; +import './region'; +import './scale'; +import './selection'; +import './shape.bar'; +import './shape'; +import './shape.line'; +import './size'; +import './subchart'; +import './text'; +import './title'; +import './tooltip'; +import './type'; +import './ua'; +import './util'; +import './zoom'; + +export default c3 diff --git a/src/interaction.js b/src/interaction.js index c6203d9..a51f6de 100644 --- a/src/interaction.js +++ b/src/interaction.js @@ -1,3 +1,6 @@ +import CLASS from './class'; +import { c3_chart_internal_fn } from './core'; + c3_chart_internal_fn.initEventRect = function () { var $$ = this; $$.main.select('.' + CLASS.chart).append("g") diff --git a/src/legend.js b/src/legend.js index 158b503..355a8e0 100644 --- a/src/legend.js +++ b/src/legend.js @@ -1,3 +1,7 @@ +import CLASS from './class'; +import { c3_chart_internal_fn } from './core'; +import { isDefined, isEmpty, getOption } from './util'; + c3_chart_internal_fn.initLegend = function () { var $$ = this; $$.legendItemTextBox = {}; diff --git a/src/region.js b/src/region.js index 0439a80..83ca16e 100644 --- a/src/region.js +++ b/src/region.js @@ -1,3 +1,7 @@ +import CLASS from './class'; +import { c3_chart_internal_fn } from './core'; +import { isValue } from './util'; + c3_chart_internal_fn.initRegion = function () { var $$ = this; $$.region = $$.main.append('g') diff --git a/src/scale.js b/src/scale.js index 6c4c045..85fa792 100644 --- a/src/scale.js +++ b/src/scale.js @@ -1,3 +1,5 @@ +import { c3_chart_internal_fn } from './core'; + c3_chart_internal_fn.getScale = function (min, max, forTimeseries) { return (forTimeseries ? this.d3.time.scale() : this.d3.scale.linear()).range([min, max]); }; diff --git a/src/selection.js b/src/selection.js index b71d286..c1d0551 100644 --- a/src/selection.js +++ b/src/selection.js @@ -1,3 +1,6 @@ +import CLASS from './class'; +import { c3_chart_internal_fn } from './core'; + c3_chart_internal_fn.selectPoint = function (target, d, i) { var $$ = this, config = $$.config, cx = (config.axis_rotated ? $$.circleY : $$.circleX).bind($$), diff --git a/src/shape.bar.js b/src/shape.bar.js index 514b7f0..de11922 100644 --- a/src/shape.bar.js +++ b/src/shape.bar.js @@ -1,3 +1,7 @@ +import CLASS from './class'; +import { c3_chart_internal_fn } from './core'; +import { isValue } from './util'; + c3_chart_internal_fn.initBar = function () { var $$ = this; $$.main.select('.' + CLASS.chart).append("g") diff --git a/src/shape.js b/src/shape.js index 38afae1..930b422 100644 --- a/src/shape.js +++ b/src/shape.js @@ -1,3 +1,7 @@ +import CLASS from './class'; +import { c3_chart_internal_fn } from './core'; +import { isUndefined } from './util'; + c3_chart_internal_fn.getShapeIndices = function (typeFilter) { var $$ = this, config = $$.config, indices = {}, i = 0, j, k; diff --git a/src/shape.line.js b/src/shape.line.js index e9dcb8c..421d7f9 100644 --- a/src/shape.line.js +++ b/src/shape.line.js @@ -1,3 +1,7 @@ +import CLASS from './class'; +import { c3_chart_internal_fn } from './core'; +import { isValue, isFunction, isUndefined, isDefined } from './util'; + c3_chart_internal_fn.initLine = function () { var $$ = this; $$.main.select('.' + CLASS.chart).append("g") diff --git a/src/size.js b/src/size.js index 445d11d..c4892ca 100644 --- a/src/size.js +++ b/src/size.js @@ -1,3 +1,7 @@ +import CLASS from './class'; +import { c3_chart_internal_fn } from './core'; +import { isValue, ceil10 } from './util'; + c3_chart_internal_fn.getCurrentWidth = function () { var $$ = this, config = $$.config; return config.size_width ? config.size_width : $$.getParentWidth(); @@ -5,7 +9,7 @@ c3_chart_internal_fn.getCurrentWidth = function () { c3_chart_internal_fn.getCurrentHeight = function () { var $$ = this, config = $$.config, h = config.size_height ? config.size_height : $$.getParentHeight(); - return h > 0 ? h : 320 / ($$.hasType('gauge') && !config.gauge_fullCircle ? 2 : 1); + return h > 0 ? h : 320 / ($$.hasType('gauge') && !config.gauge_fullCircle ? 2 : 1); }; c3_chart_internal_fn.getCurrentPaddingTop = function () { var $$ = this, @@ -95,8 +99,8 @@ c3_chart_internal_fn.getHorizontalAxisHeight = function (axisId) { var $$ = this, config = $$.config, h = 30; if (axisId === 'x' && !config.axis_x_show) { return 8; } if (axisId === 'x' && config.axis_x_height) { return config.axis_x_height; } - if (axisId === 'y' && !config.axis_y_show) { - return config.legend_show && !$$.isLegendRight && !$$.isLegendInset ? 10 : 1; + if (axisId === 'y' && !config.axis_y_show) { + return config.legend_show && !$$.isLegendRight && !$$.isLegendInset ? 10 : 1; } if (axisId === 'y2' && !config.axis_y2_show) { return $$.rotated_padding_top; } // Calculate x axis height when tick rotated diff --git a/src/subchart.js b/src/subchart.js index 47017a5..a4dacf7 100644 --- a/src/subchart.js +++ b/src/subchart.js @@ -1,3 +1,7 @@ +import CLASS from './class'; +import { c3_chart_internal_fn } from './core'; +import { isFunction } from './util'; + c3_chart_internal_fn.initBrush = function () { var $$ = this, d3 = $$.d3; $$.brush = d3.svg.brush().on("brush", function () { $$.redrawForBrush(); }); diff --git a/src/tail.js b/src/tail.js deleted file mode 100644 index 541ccf3..0000000 --- a/src/tail.js +++ /dev/null @@ -1,9 +0,0 @@ - if (typeof define === 'function' && define.amd) { - define("c3", ["d3"], function () { return c3; }); - } else if ('undefined' !== typeof exports && 'undefined' !== typeof module) { - module.exports = c3; - } else { - window.c3 = c3; - } - -})(window); diff --git a/src/text.js b/src/text.js index 9327037..25f55fa 100644 --- a/src/text.js +++ b/src/text.js @@ -1,3 +1,6 @@ +import CLASS from './class'; +import { c3_chart_internal_fn } from './core'; + c3_chart_internal_fn.initText = function () { var $$ = this; $$.main.select('.' + CLASS.chart).append("g") diff --git a/src/title.js b/src/title.js index a58b68e..6c198c5 100644 --- a/src/title.js +++ b/src/title.js @@ -1,3 +1,6 @@ +import CLASS from './class'; +import { c3_chart_internal_fn } from './core'; + c3_chart_internal_fn.initTitle = function () { var $$ = this; $$.title = $$.svg.append("text") diff --git a/src/tooltip.js b/src/tooltip.js index 1ceafe4..148e80b 100644 --- a/src/tooltip.js +++ b/src/tooltip.js @@ -1,3 +1,7 @@ +import CLASS from './class'; +import { c3_chart_internal_fn } from './core'; +import { isValue, isString, sanitise } from './util'; + c3_chart_internal_fn.initTooltip = function () { var $$ = this, config = $$.config, i; $$.tooltip = $$.selectChart diff --git a/src/type.js b/src/type.js index 6497280..620f7ef 100644 --- a/src/type.js +++ b/src/type.js @@ -1,3 +1,6 @@ +import { c3_chart_internal_fn } from './core'; +import { isString } from './util'; + c3_chart_internal_fn.setTargetType = function (targetIds, type) { var $$ = this, config = $$.config; $$.mapToTargetIds(targetIds).forEach(function (id) { diff --git a/src/ua.js b/src/ua.js index 17fcde5..71dacaa 100644 --- a/src/ua.js +++ b/src/ua.js @@ -1,3 +1,5 @@ +import { c3_chart_internal_fn } from './core'; + c3_chart_internal_fn.isSafari = function () { var ua = window.navigator.userAgent; return ua.indexOf('Safari') >= 0 && ua.indexOf('Chrome') < 0; diff --git a/src/util.js b/src/util.js index 1365ab3..63304bf 100644 --- a/src/util.js +++ b/src/util.js @@ -1,49 +1,51 @@ -var isValue = c3_chart_internal_fn.isValue = function (v) { +import { c3_chart_internal_fn } from './core'; + +export var isValue = function (v) { return v || v === 0; -}, - isFunction = c3_chart_internal_fn.isFunction = function (o) { - return typeof o === 'function'; - }, - isString = c3_chart_internal_fn.isString = function (o) { - return typeof o === 'string'; - }, - isUndefined = c3_chart_internal_fn.isUndefined = function (v) { - return typeof v === 'undefined'; - }, - isDefined = c3_chart_internal_fn.isDefined = function (v) { - return typeof v !== 'undefined'; - }, - ceil10 = c3_chart_internal_fn.ceil10 = function (v) { - return Math.ceil(v / 10) * 10; - }, - asHalfPixel = c3_chart_internal_fn.asHalfPixel = function (n) { - return Math.ceil(n) + 0.5; - }, - diffDomain = c3_chart_internal_fn.diffDomain = function (d) { - return d[1] - d[0]; - }, - isEmpty = c3_chart_internal_fn.isEmpty = function (o) { - return typeof o === 'undefined' || o === null || (isString(o) && o.length === 0) || (typeof o === 'object' && Object.keys(o).length === 0); - }, - notEmpty = c3_chart_internal_fn.notEmpty = function (o) { - return !c3_chart_internal_fn.isEmpty(o); - }, - getOption = c3_chart_internal_fn.getOption = function (options, key, defaultValue) { - return isDefined(options[key]) ? options[key] : defaultValue; - }, - hasValue = c3_chart_internal_fn.hasValue = function (dict, value) { - var found = false; - Object.keys(dict).forEach(function (key) { - if (dict[key] === value) { found = true; } - }); - return found; - }, - sanitise = c3_chart_internal_fn.sanitise = function (str) { - return typeof str === 'string' ? str.replace(//g, '>') : str; - }, - getPathBox = c3_chart_internal_fn.getPathBox = function (path) { - var box = path.getBoundingClientRect(), - items = [path.pathSegList.getItem(0), path.pathSegList.getItem(1)], - minX = items[0].x, minY = Math.min(items[0].y, items[1].y); - return {x: minX, y: minY, width: box.width, height: box.height}; - }; +}; +export var isFunction = function (o) { + return typeof o === 'function'; +}; +export var isString = function (o) { + return typeof o === 'string'; +}; +export var isUndefined = function (v) { +return typeof v === 'undefined'; +}; +export var isDefined = function (v) { + return typeof v !== 'undefined'; +}; +export var ceil10 = function (v) { + return Math.ceil(v / 10) * 10; +}; +export var asHalfPixel = function (n) { + return Math.ceil(n) + 0.5; +}; +export var diffDomain = function (d) { + return d[1] - d[0]; +}; +export var isEmpty = function (o) { + return typeof o === 'undefined' || o === null || (isString(o) && o.length === 0) || (typeof o === 'object' && Object.keys(o).length === 0); +}; +export var notEmpty = function (o) { + return !c3_chart_internal_fn.isEmpty(o); +}; +export var getOption = function (options, key, defaultValue) { + return isDefined(options[key]) ? options[key] : defaultValue; +}; +export var hasValue = function (dict, value) { + var found = false; + Object.keys(dict).forEach(function (key) { + if (dict[key] === value) { found = true; } + }); + return found; +}; +export var sanitise = function (str) { + return typeof str === 'string' ? str.replace(//g, '>') : str; +}; +export var getPathBox = function (path) { + var box = path.getBoundingClientRect(), + items = [path.pathSegList.getItem(0), path.pathSegList.getItem(1)], + minX = items[0].x, minY = Math.min(items[0].y, items[1].y); + return {x: minX, y: minY, width: box.width, height: box.height}; +}; diff --git a/src/zoom.js b/src/zoom.js index c11dd8b..fea6bda 100644 --- a/src/zoom.js +++ b/src/zoom.js @@ -1,3 +1,7 @@ +import CLASS from './class'; +import { c3_chart_internal_fn } from './core'; +import { diffDomain } from './util'; + c3_chart_internal_fn.initZoom = function () { var $$ = this, d3 = $$.d3, config = $$.config, startEvent;