1

I'm building an application wherein the user can select a dataset which will then be shown in a Leaflet map. To distinguish between the markers, each marker will be given a color, based on a variable. The data has a matching legend.

For now, I just use a small dataset for testing and building, but I want my application to be able to work with larger datasets as well.

I set up a basic array with a couple of static colors, which is fine for a small dataset. However when I add a larger dataset, the colors "run out" of course, as you can see below. I increased the amount of colors in the right legend to show what I would like to see happen.

enter image description here

What I would like to have, is way to effectively "repeat" arrayKleur if arrayMetKetens is bigger then the array of colors.

arrayMetKetens is an dynamic array, filled with unique values and changes depending on what dataset is selected.

Array for colors:

arrayKleur = ["#b15928", "#6a3d9a", "#ff7f00", "#e31a1c", "#33a02c", "#1f78b4", "#a6cee3", "#b2df8a", "#fb9a99", "#fdbf6f"];

Generate legend code:

function legenda(){
    var HTMLlegenda = '<h4>Legenda</h4>';

    // if arrayMetKetens is empty => default legenda text 
    if (arrayMetKetens.length == 0 ){
        HTMLlegenda += '<p>Selecteer een tabel in de "Advanced selection" tab om de legenda weer te geven</p>'

        $("#tab1").html(HTMLlegenda);
    }
    // if arrayMetKetens is filled => generate legenda
    else{

        $("#tab1").html(arrayMetKetens);

        // stuk code om van bovenstaande data een HTML tabel te maken
        for(ii = 0; ii < arrayMetKetens.length; ii++){

            HTMLlegenda += '<i id="background" style="background:'+arrayKleur[ii]+'">&nbsp;&nbsp;&nbsp;&nbsp;</i>'+arrayMetKetens[ii]+'</br>'
        }
        $("#tab1").html(HTMLlegenda);
    }
}

getColor function:

function getArray(){
    var ketens = [];

    for(i=0;i < geojson_dataTable.features.length;i++){
       ketens = ketens.concat(geojson_dataTable.features[i].properties[featureVoorSorteer])
    }
    window.arrayMetKetens = jQuery.unique( ketens );
}

function getColor(keten) {
    var i = window.arrayMetKetens.indexOf(keten);
    if (i !== -1) {
        return arrayKleur[ i ];
        } 
    else {
        return '#999999';
    } 
}
2
  • 3
    just use arrayKleur[ii % arrayKleur.length] Commented Jun 29, 2016 at 18:03
  • 2
    If you want to just "repeat" when you run out then to find your color you can take the index of your item and modulo it with the length of color array then use that as the index into your color array. var color = colors[ itemindex % colors.length ] Commented Jun 29, 2016 at 18:04

1 Answer 1

5

In the general case, you want to use an array with length x, to define an array with length y. You can use modulo division to cycle over the indexes for the original array.

function repeatFor(arr, size) {
  var newArr = new Array(size);

  for(var i = 0; i < size; i++) {
    newArr[i] = arr[i % i.length];
  }

  return newArr;
}

Then use this to create a new array defined in terms of your existing array of colors.

var kleurs = ["#b15928", "#6a3d9a", "#ff7f00", "#e31a1c", "#33a02c", "#1f78b4", "#a6cee3", "#b2df8a", "#fb9a99", "#fdbf6f"];

var arrayKleur = repeatFor(kleurs, arrayMetKetens.length);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.