UNPKG

@icgcat/thematic-stats

Version:

Thematic-stats is a library to generate thematic styles for geospatial data visualization

61 lines (57 loc) 2.23 kB
"use strict"; /** * Generates a style configuration for thematic mapping based on GeoStats data. * * @function generateStyleJSTAT * @param {String} fieldName - The field name used for matching values. * @param {Array} arrayData - An array of data values. * @param {Array} arrayKeys - An array of keys corresponding to the data values. * @param {Object} geostatObj - The GeoStats object containing data and color mapping. * @param {Boolean} forceInt - Whether to parse keys as integers. * @returns {Array} A style configuration array for mapping tools. * * @example * const style = generateStyleJSTAT('field', [10, 20], [1, 2], geoStatsObj, true); * console.log(style); */ export function generateStyleJSTAT(fieldName, arrayData, arrayKeys, geostatObj, forceInt) { let styleFill = ["match", ["get", fieldName]]; arrayKeys.forEach(function (key, index) { const value = arrayData[index]; let color = geostatObj.colors[geostatObj.getRangeNum(value)]; if (!color) { color = "rgba(0,0,0,0)"; } const _key = forceInt ? parseInt(key) : key; styleFill.push(_key, color); }); styleFill.push("rgba(0,0,0,0)"); return styleFill; } /** * Generates a step-based style configuration for mapping numeric ranges. * * @function generateStyle * @param {String} fieldName - The field name used for styling. * @param {Array} arrayValues - An array of numeric values defining range boundaries. * @param {Array} arrayColors - An array of colors corresponding to each range. * @returns {Array} A style configuration array for mapping tools. * * @example * const style = generateStyle('field', [10, 20, 30], ['#FF0000', '#00FF00']); * console.log(style); */ export function generateStyle(fieldName, arrayValues, arrayColors) { let styleFill = ["step", ["get", fieldName]]; styleFill.push("rgba(0,0,0,0)"); arrayValues.forEach(function (value, index) { const color = arrayColors[index] ? arrayColors[index] : "rgba(0,0,0,0)"; if (index == 0) { value = parseFloat(value) - 0.1; } else { value = parseFloat(value) + 0.1; } styleFill.push(value, color); }); return styleFill; }