Blog>
Snippets

Conditional Styling for Geographical Data Points

Showcase how to apply conditional styling to data points on a geographic chart based on certain thresholds or criteria, such as changing colors or shapes to signify different data metrics.
const data = [
  { id: 'US', value: 300 },
  { id: 'CA', value: 150 },
  // more data points
];
Defines the dataset. Each data point signifies a geographical area (e.g., countries identified by their 'id') and a numeric 'value' indicating some metric for that area.
const getColor = (value) => {
  if (value > 200) return 'red';
  else if (value > 100) return 'orange';
  else return 'green';
};
Defines a function to decide the color of a data point based on its value. Here, higher values might indicate greater concern, thus assigned warmer colors.
data.forEach(d => {
  const color = getColor(d.value);
  // Assume a function drawPoint exists to render the point
  drawPoint(d.id, color);
});
Iterates through each data point, determines its color using the getColor function, and draws it on the map. The drawPoint function would need to be defined elsewhere to actually render these points.