Skip to content

Commit

Permalink
Pass position to the value function.
Browse files Browse the repository at this point in the history
The used function can be overridden for isoline and contour features.
This defaults to the value function returning a non-null, finite number.

Pass the calculated position to the value function.  This allows the
value to be modified based on the position.

Add the isoline label feature to the list of dependent features so that
something like `myfeature.visible(false).draw()` does what is expected.

When auto-spacing isolines, make sure that labels don't have an
excessive number of digits due to bad rounding.

Don't wrap across longitude if the data is more than 360 units wide,
since it probably isn't actually longitude in that case.
  • Loading branch information
manthey committed Jun 8, 2018
1 parent 8ec0b33 commit 6f690fd
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 15 deletions.
7 changes: 4 additions & 3 deletions src/contourFeature.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,11 +117,12 @@ var contourFeature = function (arg) {
this._createContours = function () {
var contour = m_this.contour,
valueFunc = m_this.style.get('value'),
usedFunc = m_this.style('used') !== undefined ?
m_this.style.get('used') :
function (d, i) { return util.isNonNullFinite(valueFunc(d, i)); },
minmax, val, range, i, k;
var result = this.createMesh({
used: function (d, i) {
return util.isNonNullFinite(valueFunc(d, i));
},
used: usedFunc,
opacity: m_this.style.get('opacity'),
value: valueFunc
});
Expand Down
22 changes: 15 additions & 7 deletions src/isolineFeature.js
Original file line number Diff line number Diff line change
Expand Up @@ -215,13 +215,14 @@ var isolineFeature = function (arg) {
*/
this._createIsolines = function () {
var valueFunc = m_this.style.get('value'),
usedFunc = m_this.style('used') !== undefined ?
m_this.style.get('used') :
function (d, i) { return util.isNonNullFinite(valueFunc(d, i)); },
values,
hasLabels = false,
lines = [];
var mesh = this.createMesh({
used: function (d, i) {
return util.isNonNullFinite(valueFunc(d, i));
},
used: usedFunc,
value: valueFunc
});
values = this._getValueList(mesh);
Expand Down Expand Up @@ -258,7 +259,7 @@ var isolineFeature = function (arg) {
count = isoline.get('count')(mesh),
autofit = isoline.get('autofit')(mesh),
levels = isoline.get('levels')(mesh),
minmax, delta, step, steppow, i;
minmax, delta, step, steppow, steplog10, fixedDigits, i;
if (!mesh.numVertices || !mesh.numElements) {
return [];
}
Expand Down Expand Up @@ -304,7 +305,9 @@ var isolineFeature = function (arg) {
* 5/2 of that when adjusted to "nice values" (so between 2/3 and 5/3
* of the specified count). */
step = delta / (count * 2 / 3);
steppow = Math.pow(10, Math.floor(Math.log10(step)));
steplog10 = Math.floor(Math.log10(step));
fixedDigits = Math.max(0, -steplog10);
steppow = Math.pow(10, steplog10);
step /= steppow; // will now be in range [1, 10)
step = step >= 5 ? 5 : step >= 2 ? 2 : 1; // now 1, 2, or 5
spacing = step * steppow;
Expand All @@ -314,7 +317,7 @@ var isolineFeature = function (arg) {
* maximum level. */
values = [];
for (i = Math.ceil(mesh.minValue / spacing); i < Math.floor(mesh.maxValue / spacing); i += 1) {
values.push({value: i * spacing, position: i});
values.push({value: i * spacing, position: i, fixedDigits: fixedDigits});
}
}
/* Mark levels for each value. These are intended for styling. All values
Expand All @@ -330,7 +333,11 @@ var isolineFeature = function (arg) {
if (isoline.get('label')(val, val.position)) {
var label = isoline.get('labelText')(val, val.position);
if (label === undefined) {
label = '' + val.value;
if (val.fixedDigits !== undefined) {
label = '' + parseFloat(val.value.toFixed(val.fixedDigits));
} else {
label = '' + val.value;
}
}
if (label) {
val.label = label;
Expand Down Expand Up @@ -707,6 +714,7 @@ var isolineFeature = function (arg) {
m_labelFeature.style(styleName, style[styleName]);
}
});
m_this.dependentFeatures([m_lineFeature, m_labelFeature]);
}
} else if (m_lineFeature) {
m_lineFeature.data([]);
Expand Down
12 changes: 7 additions & 5 deletions src/meshFeature.js
Original file line number Diff line number Diff line change
Expand Up @@ -204,9 +204,10 @@ var meshFeature = function (arg) {
* @param {object} [vertexValueFuncs] A dictionary where the keys are the
* names of properties to include in the results and the values are
* functions that are evaluated at each vertex with the arguments
* (data[idx], idx). If a key is named `used`, then if its function
* returns a falsy value for a data point, the vertex associated with that
* data point is removed from the resultant mesh.
* `(data[idx], idx, position)`. If a key is named `used`, then its
* function is passed `(data[idx], idx)` and if it returns a falsy value
* for a data point, the vertex associated with that data point is removed
* from the resultant mesh.
* @returns {geo.meshFeature.meshInfo} An object with the mesh information.
*/
this.createMesh = function (vertexValueFuncs) {
Expand Down Expand Up @@ -242,7 +243,7 @@ var meshFeature = function (arg) {
wrapLongitude = !!(wrapLongitude === undefined || wrapLongitude);
if (!usePos && wrapLongitude && (x0 < -180 || x0 > 180 ||
x0 + dx * (gridW - 1) < -180 || x0 + dx * (gridW - 1) > 180) &&
dx > -180 && dx < 180) {
dx > -180 && dx < 180 && dx * (gridW - 1) < 360 + 1e-4) {
calcX = [];
for (i = 0; i < gridW; i += 1) {
x = x0 + i * dx;
Expand Down Expand Up @@ -401,10 +402,11 @@ var meshFeature = function (arg) {
}
result.pos[i3 + 1] = y0 + dy * Math.floor(idx / gridW);
result.pos[i3 + 2] = 0;
posVal = {x: result.pos[i3], y: result.pos[i3 + 1], z: result.pos[i3 + 2]};
}
for (key in vertexValueFuncs) {
if (key !== 'used' && vertexValueFuncs.hasOwnProperty(key)) {
result[key][i] = vertexValueFuncs[key](item, idx);
result[key][i] = vertexValueFuncs[key](item, idx, posVal);
}
}
}
Expand Down

0 comments on commit 6f690fd

Please sign in to comment.