简体   繁体   中英

Using datamaps, I am trying to create a color for each department

I'm using datamaps ( http://datamaps.github.io/ ) and trying to make a color for each department that have an interval according to the if condition in a France map.

For example if interval == "1-50" make a red color for all the departments that are in this interval, and add this to

data: {
    "mydepartment1": {
      fillKey: "red",
    },
    "mydepartment2": {
      fillKey: "gt50",
    }
  }

This is my js: import departement_list from "../data/departements-list.json";

var jsonData = JSON.parse(JSON.stringify(departement_list));
const dataValues = jsonData.map((data) => data.Departement);
const uniqueValues = [...new Set(dataValues)];

let interval,
nb_votes = [];
for (const valeur of uniqueValues) {
  nb_votes = dataValues.filter((v) => v == valeur).length;
  if (nb_votes <= 0) {
    interval = "defaultFill";
  } else if (nb_votes > 1 && nb_votes < 50) {
    interval = "1-50";
  } else if (nb_votes > 51 && nb_votes < 100) {
    interval = "51-100";
  } else if (nb_votes > 101 && nb_votes < 150) {
    interval = "101-150";
  } else if (nb_votes > 151 && nb_votes < 200) {
    interval = "151-200";
  } else if (nb_votes > 201 && nb_votes < 300) {
    interval = "201-300";
  } else if (nb_votes > 301 && nb_votes < 400) {
    interval = "301-400";
  } else {
    interval = "401-";
  }
}

var map = new Datamap({
  scope: "fra",
  element: document.getElementById("map-france"),
  responsive: true,
  fills: {
    defaultFill: "#EDE8D6",
    "1-50": "#F4F1E6",
    "51-100": "#EDE8D6",
    "101-150": "#E2DABF",
    "151-200": "#CEC191",
    "201-300": "#BCAE7C",
    "301-400": "#9D893E",
    "401-": "#827131",
  },
  data: {
    "mydepartment1": {
      fillKey: "101-150",
    },
    "mydepartment2": {
      fillKey: "151-200",
    }
  }
});

Hi it's not clear what your code is meant to do, interval never seems to get used and just get's reset with each iteration of uniqueValues

I've looked up how to use datamaps it looks like the fill "key" corresponds to the fillKey in the data parameter

so for

data: {
    "mydepartment1": {
      fillKey: "red",
    },
    "mydepartment2": {
      fillKey: "gt50",
    }
  }

To work you would need

fills: {
    defaultFill: "#EDE8D6",
    "red": "#F4F1E6",
    "gt50": "#EDE8D6",
  },

I found this blog post that goes into it in more detail. Hopefully that puts you on the right path

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM