// JS
var chart;
var palette = [
  '#ff3d00',
  '#ff7213',
  '#ffa726',
  '#ffc041',
  '#ffd85b',
  '#fff176',
  '#eaee87',
  '#d6eb99',
  '#c1e8aa',
  '#6ec1bb',
  '#1b9acb'
].reverse();
JSC.fetch(
  './resources/access-to-drinking-water-by-country.csv'
)
  .then(function(response) {
    return response.text();
  })
  .then(function(text) {
    var mapData = JSC.csv2Json(text);
    chart = renderMap(mapData);
  });
function renderMap(data) {
  var points = data.map(function(item) {
    return {
      map: item.country_code,
      z: 100 - item.value
    };
  });

  return JSC.chart('chartDiv', {
    type: 'map solid',
    title: {
      label: {
        text:
          'Percentage of population lacking access to basic drinking water',
        style_fontSize: 14
      },
      position: 'center'
    },
    mapping_projection: {
      type: 'lambertConformalConic',
      parallels: [10, 9]
    },
    defaultPoint: {
      tooltip: '%name <b>{%zValue:n1}%</b>',
      outline: { color: 'white', width: 0.5 },
      states_hover: {
        outline: { color: '#7a7a7a' }
      },
      focusGlow_width: 2
    },
    legend_position: 'bottom',

    palette: {
      pointValue: function(p) {
        return p.options('z');
      },

      colors: palette,
      colorBar: {
        width: 8,
        axis: {
          crosshair: {
            label_text: '{%value:n1}%',
            outline_width: 0
          },
          defaultTick_label: { text: '%value%' }
        }
      }
    },
    series: [{ points: points }],
    toolbar_items: {
      zoom_visible: false,
      'Donate and Save Lives': {
        events_click: function() {
          window.location.href =
            'https://jscharting.com/store/pay-what-you-want/';
        },
        position: 'inside bottom',
        margin_left: 100
      }
    }
  });
}
/*CSS*/