Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 31 additions & 15 deletions src/charts/tau.chart.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,9 @@ const status = {
/* jshint ignore:start */
var strategyNormalizeAxis = {
[status.SUCCESS]: (axis) => axis,
[status.FAIL]: () => {
throw new Error('This configuration is not supported, See http://api.taucharts.com/basic/facet.html#easy-approach-for-creating-facet-chart');
[status.FAIL]: (axis, data) => {
throw new Error((data.messages || []).join('\n') ||
'This configuration is not supported, See http://api.taucharts.com/basic/facet.html#easy-approach-for-creating-facet-chart');
},
[status.WARNING]: (axis, config) => {
var measure = axis[config.indexMeasureAxis[0]];
Expand All @@ -39,26 +40,33 @@ var strategyNormalizeAxis = {
}
};
/* jshint ignore:end */
function validateAxis(dimensions, axis) {
function validateAxis(dimensions, axis, axisName) {
return axis.reduce(function (result, item, index) {
if (dimensions[item].type === 'measure') {
result.countMeasureAxis++;
result.indexMeasureAxis.push(index);
}
if (dimensions[item].type !== 'measure' && result.countMeasureAxis === 1) {
result.status = status.WARNING;
} else if (result.countMeasureAxis > 1) {
var dimension = dimensions[item];
if (!dimension){
result.status = status.FAIL;
result.messages.push('Undefined dimension "'+item+'" for axis "'+axisName+'"');
} else if (result.status != status.FAIL) {
if (dimension.type === 'measure') {
result.countMeasureAxis++;
result.indexMeasureAxis.push(index);
}
if (dimension.type !== 'measure' && result.countMeasureAxis === 1) {
result.status = status.WARNING;
} else if (result.countMeasureAxis > 1) {
result.status = status.FAIL;
result.messages.push('There are more then one measure dimensions for axis "'+axisName+'"');
}
}
return result;
}, {status: status.SUCCESS, countMeasureAxis: 0, indexMeasureAxis: []});
}, {status: status.SUCCESS, countMeasureAxis: 0, indexMeasureAxis: [], messages: []});
}
function transformConfig(type, config) {
var x = normalizeSettings(config.x);
var y = normalizeSettings(config.y);

var validatedX = validateAxis(config.dimensions, x);
var validatedY = validateAxis(config.dimensions, y);
var validatedX = validateAxis(config.dimensions, x, 'x');
var validatedY = validateAxis(config.dimensions, y, 'y');
x = strategyNormalizeAxis[validatedX.status](x, validatedX);
y = strategyNormalizeAxis[validatedY.status](y, validatedY);
var guide = normalizeSettings(config.guide);
Expand Down Expand Up @@ -225,7 +233,15 @@ class Chart extends Plot {
}
config.settings = this.setupSettings(config.settings);
config.dimensions = this.setupMetaInfo(config.dimensions, config.data);
super(typesChart[config.type](config));
var chartFactory = typesChart[config.type];
if (_.isFunction(chartFactory)) {
super(chartFactory(config));
}
else {
throw new Error('Chart type ' + config.type + ' is not supported. Use one of ' +
_.keys(typesChart).join(', ') + '.'
);
}
}
destroy() {
var index = Chart.winAware.indexOf(this);
Expand Down Expand Up @@ -262,4 +278,4 @@ Chart.resizeOnWindowEvent = (function () {
}());
Chart.winAware = [];
window.addEventListener('resize', Chart.resizeOnWindowEvent);
export {Chart};
export {Chart};
74 changes: 74 additions & 0 deletions test/chart-config.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
define(function(require){
var assert = require('chai').assert;
var expect = require('chai').expect;
var tauChart = require('tau_modules/tau.newCharts');
describe('Invalid chart definition', function(){
var testData = [
{x: 1, y: 1, color: 'red', size: 6},
{x: 0.5, y: 0.5, color: 'green', size: 6},
{x: 2, y: 2, color: 'green', size: 8}
];
it('should throw correct error on invalid chart type', function(){

expect(function(){
new tauChart.Chart({
type: 'invalidType',
data: testData,
x: 'x',
y: 'y',
color: 'color',
size: 'size'
});
}).to.throw('Chart type invalidType is not supported. Use one of scatterplot, line, bar, horizontalBar.');

});

it('should throw on unknown dimensions in axis definition', function(){

expect(function(){
new tauChart.Chart({
type: 'bar',
data: testData,
x: 'x',
y: 'y',
color: 'color',
size: 'size',
dimensions: {
'project': {
'type': 'category',
'scale': 'ordinal'
}
}
});
}).to.throw('Undefined dimension "x" for axis "x"');
});

it('should throw on unknown dimensions without dimensions', function(){

expect(function(){
new tauChart.Chart({
type: 'bar',
data: testData,
x: 'x',
y: 'project',
color: 'color',
size: 'size'
});
}).to.throw('Undefined dimension "project" for axis "y"');
});

it('should throw on several measures in facets', function(){

expect(function(){
new tauChart.Chart({
type: 'bar',
data: testData,
y: ['x','y'],
x: 'y',
color: 'color',
size: 'size'
});
}).to.throw('There are more then one measure dimensions for axis "y"');
});
});
});