npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

vue-d3-map

v1.4.0

Published

D3 map for vue

Downloads

25

Readme

Vue D3 Map

A Vue component that renders an interactive D3 map using GeoJSON data. It allows for region selection, zooming, and panning, and provides slots for displaying loading, error, and selection data.

Vue D3 Map

Features

  • Interactive Map: Zoom, pan, and click to select regions on the map.
  • Customizable: Supports custom region styles, selection color, and data attributes.
  • Slots for Custom Content: Add custom loading, error messages, and selected region data using named slots.

Installation

npm install vue-d3-map

Usage

In component


<template>
  <D3Map/>
</template>

<script setup>
import {D3Map} from "vue-d3-map";
import "vue-d3-map/style.css";
</script>

Install globally

import VueD3Map from "vue-d3-map";
import "vue-d3-map/style.css";

createApp(App)
    .use(router)
    .use(VueD3Map)
    .mount("#app");

Nuxt 3

// plugins/vueD3Map.ts
import VueD3Map from "vue-d3-map";
import "vue-d3-map/style.css";

export default defineNuxtPlugin(({vueApp}) => {
    vueApp.use(VueD3Map);
});

Props

| prop | type | description | required | |----------------|------------------|------------------------------------------------------------------------------------------------------------------------------------------------------------|----------| | width | Number | Defines the width of the map | Yes | | height | Number | Defines the height of the map. | Yes | | titleAttribute | String | Specifies the GeoJSON property that will be displayed as a title on each region, visible . | Yes | | dataUrl | String | URL from which the GeoJSON data will be fetched. | Yes | | selectionColor | String | Defines the color used to highlight the selected region. Defaults to #474747. | No | | regionClass | Function, String | Defines a CSS class or a function that returns a CSS class for each region. If a function is provided, it receives the region's properties as an argument. | No |

Emits

| Name | parameters | description | |--------|------------|---------------------------------------------| | loaded | None | Will be called when the map is fully loaded |

Methods

| method | parameters | description | |--------------------|-------------------------------------------------------|-------------------------------------------------------------------------------------------------------------------------| | resetSelection | None | Resets the selected region and clears the selection highlight. | | resetRegionClasses | None | Recalculates all the classes, can be used if values changed | | selectRegion | regionIdentifier: string, identifyingProperty: string | Selects the region with region based on where the identifyingProperty of the region is equal to the identifyingProperty |

Slots

selection-data

This slot is displayed when a region is selected. It provides the following scoped properties:

  • selectedRegion: The properties of the selected region.
  • close: A function to reset the selection.

<template #selection-data="{ selectedRegion, close }">
  <!-- Custom content for the selected region -->
  <div>
    <h3>{{ selectedRegion.name }}</h3>
    <button @click="close">Close</button>
  </div>
</template>

selection-data

This slot is displayed on the top of the map

<template #actions>
  <input type="text" placeholder="ISO code" v-model="selectIsoCode">
  <button style="margin-left: 10px" @click="selectRegion">Select region</button>
  <button style="margin-left: auto" @click="resetSelection">Reset selection</button>
</template>

on-loading

This slot is displayed when the map is loading. You can provide custom loading content.


<template #on-loading>
  <!-- Custom loading message -->
  <div>Loading map...</div>
</template>

on-error

This slot is displayed if the map fails to load. You can provide custom error content.


<template #on-error>
  <!-- Custom error message -->
  <div>Error loading map.</div>
</template>

Example


<template>
  <D3Map
      :width="800"
      :height="600"
      title-attribute="name"
      data-url="/path/to/geojson"
      selection-color="blue"
      :region-class="getRegionClass"
  >
    <template #selection-data="{ selectedRegion, close }">
      <div class="region-info">
        <h2>{{ selectedRegion.name }}</h2>
        <p>{{ selectedRegion.description }}</p>
        <button @click="close">Close</button>
      </div>
    </template>

    <template #on-loading>
      <div>Loading map...</div>
    </template>

    <template #on-error>
      <div>Failed to load the map.</div>
    </template>
  </D3Map>
</template>

<script>
export default {
  methods: {
    getRegionClass(region) {
      return region.population > 1000000 ? 'large-region' : 'small-region';
    },
  },
};
</script>

<style>
.large-region {
  fill: red;
}

.small-region {
  fill: green;
}
</style>

License

This project is licensed under the MIT License.