Skip to content

Commit

Permalink
Changed so that all configured options in seriesMapping is imported a…
Browse files Browse the repository at this point in the history
…s properties on point objects
  • Loading branch information
Erik Olsson committed Feb 27, 2014
1 parent 0a9469f commit 2f23f8c
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 18 deletions.
53 changes: 42 additions & 11 deletions js/modules/data.src.js
Original file line number Diff line number Diff line change
Expand Up @@ -173,14 +173,18 @@
// if the property is not set. It will then be auto populated later.
builder.addXReader(mapping.x);

// Fill with readers from the series pointArrayMap
for (i = 0; i < numberOfValueColumnsNeeded; i++) {
builder.addNextColumnReader(pointArrayMap[i]);
// Add all column mappings
for (var name in mapping) {
if (mapping.hasOwnProperty(name) && name !== 'x') {
builder.addColumnReader(mapping[name], name);
}
}

// If there is a mapping for a label column, add it as well.
if (mapping.label !== undefined) {
builder.addColumnReader(mapping.label, 'label');
// Add missing columns
for (i = 0; i < numberOfValueColumnsNeeded; i++) {
if (!builder.hasReader(pointArrayMap[i])) {
builder.addNextColumnReader(pointArrayMap[i]);
}
}

seriesBuilders.push(builder);
Expand Down Expand Up @@ -727,9 +731,19 @@
}
});

// The name comes from the second reader (the one after the x column)
if (builder.readers.length >= 2) {
this.name = columns[builder.readers[1].columnIndex].name;
// The name comes from the first column (excluding the x column)
if (this.name === undefined && builder.readers.length >= 2) {
var columnIndexes = builder.getReferencedColumnIndexes();
if (columnIndexes.length >= 2) {
// remove the first one (x col)
columnIndexes.shift();

// Sort the remaining
columnIndexes.sort();

// Now use the lowest index as name column
this.name = columns[columnIndexes.shift()].name;
}
}

return point;
Expand Down Expand Up @@ -786,6 +800,23 @@
return referencedColumnIndexes;
};

/**
* Returns true if the builder has a reader for the given configName.
* @param configName
* @returns {boolean}
*/
SeriesBuilder.prototype.hasReader = function (configName) {
var i, columnReader;
for (i = 0; i < this.readers.length; i = i + 1) {
columnReader = this.readers[i];
if (columnReader.getConfigName() === configName) {
return true;
}
}

return false;
};

/**
* Returns the name that the created series should have.
* @returns {String}
Expand Down Expand Up @@ -906,9 +937,9 @@
return this.columnIndex;
};

/* // Exposed for testing
//* // Exposed for testing
window.ColumnReader = ColumnReader;
window.ColumnCursor = ColumnCursor;
window.SeriesBuilder = SeriesBuilder;*/
window.SeriesBuilder = SeriesBuilder;//*/

}(Highcharts));
29 changes: 22 additions & 7 deletions test/unit/DataTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ DataTest.prototype.testSeriesMappingSameX = function () {
data = result;
},
seriesMapping: [{
noXDefined: undefined
}]
});

Expand Down Expand Up @@ -133,7 +132,7 @@ DataTest.prototype.testSeriesMappingWithSpecifiedLabelCol = function () {
},
seriesMapping: [{
x: 2,
label: 3
name: 3
}, {
x: 2
}]
Expand All @@ -145,7 +144,7 @@ DataTest.prototype.testSeriesMappingWithSpecifiedLabelCol = function () {
type: 'datetime'
},
series: [{
data: [{x: Date.UTC(2013,0,1), y: 2, label: 'one'}, {x: Date.UTC(2013,0,2), y: 3, label: 'two'}, {x: Date.UTC(2013,0,3), y: 4, label: 'three'}],
data: [{x: Date.UTC(2013,0,1), y: 2, name: 'one'}, {x: Date.UTC(2013,0,2), y: 3, name: 'two'}, {x: Date.UTC(2013,0,3), y: 4, name: 'three'}],
name: 'Val1'
}, {
data: [[Date.UTC(2013,0,1), 3], [Date.UTC(2013,0,2), 4], [Date.UTC(2013,0,3), 4]],
Expand Down Expand Up @@ -205,16 +204,16 @@ DataTest.prototype.testSeriesBuilderWithSpecificConfig = function () {
var testData = [[0,10,20], [1,11,21], [2,12,22]];

var builder = new SeriesBuilder();
builder.addColumnReader(1, 'label');
builder.addColumnReader(1, 'name');
builder.addColumnReader(1, 'low');
builder.addColumnReader(2, 'high');
builder.addColumnReader(0, 'x');
var cursor = new ColumnCursor(testData.length, [builder]);
builder.populateColumns(cursor);

assertEquals({label:1, low: 1, high: 2, x: 0}, builder.read(testData, 0, cursor));
assertEquals({label:11, low: 11, high: 12, x: 10}, builder.read(testData, 1, cursor));
assertEquals({label:21, low: 21, high: 22, x: 20}, builder.read(testData, 2, cursor));
assertEquals({name:1, low: 1, high: 2, x: 0}, builder.read(testData, 0, cursor));
assertEquals({name:11, low: 11, high: 12, x: 10}, builder.read(testData, 1, cursor));
assertEquals({name:21, low: 21, high: 22, x: 20}, builder.read(testData, 2, cursor));
};

DataTest.prototype.testSeriesBuilderNextColWithConfig = function () {
Expand Down Expand Up @@ -283,3 +282,19 @@ DataTest.prototype.testSeriesBuilderNextColWithSparseConfig = function () {
// assertEquals({x:20, low: 21, high: 22}, builder.read(testData, 2, cursor));
};

DataTest.prototype.testSeriesBuilderHasReader = function () {
var testData = [[0,10,20], [1,11,21], [2,12,22]];

var builder = new SeriesBuilder();
builder.addColumnReader(1, 'name');
builder.addColumnReader(1, 'low');
builder.addColumnReader(2, 'high');
builder.addColumnReader(0, 'x');

assertTrue(builder.hasReader('name'));
assertTrue(builder.hasReader('low'));
assertTrue(builder.hasReader('high'));
assertTrue(builder.hasReader('x'));
assertFalse(builder.hasReader('meep'));
};

0 comments on commit 2f23f8c

Please sign in to comment.