From da9db27200ac529f3dea7e22db9f94147679aedc Mon Sep 17 00:00:00 2001 From: Edmundo Alvarez Date: Mon, 13 Jul 2015 14:16:31 +0200 Subject: [PATCH] Add spline custom interpolation With these changes it is possible to set a custom spline interpolation by giving a valid d3 interpolation to spline.interpolation.type. Fixes #479 --- spec/shape.line-spec.js | 47 ++++++++++++++++++++++++++++++++++++++++- src/config.js | 2 ++ src/shape.js | 3 ++- src/type.js | 3 +++ 4 files changed, 53 insertions(+), 2 deletions(-) diff --git a/spec/shape.line-spec.js b/spec/shape.line-spec.js index b4f5dc8..0febecb 100644 --- a/spec/shape.line-spec.js +++ b/spec/shape.line-spec.js @@ -30,7 +30,7 @@ describe('c3 chart shape line', function () { }); }); - it('should chnage to step chart', function () { + it('should change to step chart', function () { args.data.type = 'step'; expect(true).toBeTruthy(); }); @@ -42,6 +42,15 @@ describe('c3 chart shape line', function () { }); }); + it('should change to spline chart', function () { + args.data.type = 'spline'; + expect(true).toBeTruthy(); + }); + + it('should use cardinal interpolation by default', function () { + expect(chart.internal.config.spline_interpolation_type).toBe('cardinal'); + }); + }); describe('point.show option', function () { @@ -96,4 +105,40 @@ describe('c3 chart shape line', function () { }); + describe('spline.interpolation option', function () { + + it('should update args', function () { + args = { + data: { + columns: [ + ['data1', 30, 200, 100, 400, -150, 250], + ['data2', 50, 20, 10, 40, 15, 25], + ['data3', -150, 120, 110, 140, 115, 125] + ], + type: 'spline' + }, + spline: { + interpolation: { + type: 'monotone' + } + } + }; + expect(true).toBeTruthy(); + }); + + it('should update interpolation function', function() { + expect(chart.internal.getInterpolate(chart.data()[0])).toBe('monotone'); + }); + + it('should not use a non-valid interpolation', function () { + args.spline.interpolation.type = 'foo'; + expect(true).toBeTruthy(); + }); + + it('should use cardinal interpolation when given option is not valid', function() { + expect(chart.internal.getInterpolate(chart.data()[0])).toBe('cardinal'); + }); + + }); + }); diff --git a/src/config.js b/src/config.js index f8bfe2e..0972b12 100644 --- a/src/config.js +++ b/src/config.js @@ -188,6 +188,8 @@ c3_chart_internal_fn.getDefaultConfig = function () { donut_title: "", donut_expand: {}, donut_expand_duration: 50, + // spline + spline_interpolation_type: 'cardinal', // region - region to change style regions: [], // tooltip - show when mouseover on each data diff --git a/src/shape.js b/src/shape.js index fd40598..17fa74b 100644 --- a/src/shape.js +++ b/src/shape.js @@ -67,5 +67,6 @@ c3_chart_internal_fn.isWithinShape = function (that, d) { c3_chart_internal_fn.getInterpolate = function (d) { var $$ = this; - return $$.isSplineType(d) ? "cardinal" : $$.isStepType(d) ? $$.config.line_step_type : "linear"; + var interpolation = $$.isInterpolationType() ? $$.config.spline_interpolation_type : 'cardinal'; + return $$.isSplineType(d) ? interpolation : $$.isStepType(d) ? $$.config.line_step_type : "linear"; }; diff --git a/src/type.js b/src/type.js index 51511a5..144ad24 100644 --- a/src/type.js +++ b/src/type.js @@ -89,3 +89,6 @@ c3_chart_internal_fn.lineOrScatterData = function (d) { c3_chart_internal_fn.barOrLineData = function (d) { return this.isBarType(d) || this.isLineType(d) ? d.values : []; }; +c3_chart_internal_fn.isInterpolationType = function () { + return ['linear', 'linear-closed', 'basis', 'basis-open', 'basis-closed', 'bundle', 'cardinal', 'cardinal-open', 'cardinal-closed', 'monotone'].indexOf(this.config.spline_interpolation_type) >= 0; +};