Snippets

Piotr Szrajber Smart M.App - workaround for custom colors sorting issue in choropleth widget

Created by Piotr Szrajber
/*
 * Workaround for the bug with custom colors sorting in choropleth on the production environment
 *
 * 2017-05-15 Marcin KarpiƄski <marcin.karpinski@hexagongeospatial.com>
 */
// Code snippet to make all choropleths with custom colors use alpabetical domain sort order
function castColors(dataStage, chartM) {
    var inlinedColors = chartM.colors,
        domain;
    if (!inlinedColors || inlinedColors.length <= 0) return;

    domain = this.getAlphabeticallySortedDomainValues(dataStage.facts(), chartM.values);
    this.colors(d3.scale.ordinal().range(inlinedColors).domain(domain));
}

function getDomainValues(values, key) {
    this.data();
    return values.dimension(function(d) {
            return d[key];
        })
        .group().all().map(function(d) {
            return d.key;
        })
        .sort();
}

function fixColors() {
    // Wait for the map to be ready
    if (!document.querySelector(".leaflet-zoom-animated")) {
        setTimeout(fixColors, 500);
    } else {
        // Find all choroplets
        $GP.bi.stage.findWidgets({
            descriptors: [{
                chartM: {
                    chart: "choropleth"
                }
            }]
        }, function(widgets) {
            var l = widgets.length;
            while (l--) {
                var widget = widgets[l];
                if (!Array.isArray(widget.chartM.colors)) return;
                // Alphabetical domain order for all choropleths with custom colors.
                widget.chart.castColors = castColors;
                widget.chart.getAlphabeticallySortedDomainValues = getDomainValues;
                widget.chart.castColors(widget.chart.dataStage(), widget.chartM);
                widget.chart.redraw();
            }
        });
    }
}

fixColors();

Comments (0)

HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.