Skip to content
  1. Examples

PNG export with geo map and vector-layers

This example shows how to export the visualisation as a PNG file. When using geo mode, the tiles and layers are exported as well.

ts
import Ogma from '@linkurious/ogma';
import L from 'leaflet';

const graph = {
  nodes: [
    {
      id: 'Paris',
      data: { latitude: 48.858838, longitude: 2.343436 },
      attributes: { x: 0, y: 0, text: 'Paris', radius: 10 }
    },
    {
      id: 'London',
      data: { latitude: 51.509615, longitude: -0.134514 },
      attributes: { x: 100, y: 0, text: 'London', radius: 10 }
    }
  ],
  edges: [
    {
      id: 'Eurostar',
      source: 'Paris',
      target: 'London',
      attributes: { width: 5, text: 'Eurostar' }
    }
  ]
};

const ogma = new Ogma({
  graph,
  container: 'graph-container'
});

await ogma.geo.enable({
  attribution:
    'Map data &copy; <a target="_blank" href="http://osm.org/copyright">OpenStreetMap contributors</a>'
});
const jsonString = await fetch('files/eu-shapes.geojson').then(response =>
  response.text()
);
const geojson = JSON.parse(jsonString);
geojson.features = geojson.features.filter(
  (_, i: number) => i === 7 || i === 13
);
const map = ogma.geo.getMap()!;
map.addLayer(L.geoJSON(geojson));
map.addLayer(new L.Marker([49.93890274394121, -1.666402816772461]));
await ogma.view.afterNextFrame();
const dataURL = await ogma.export.png({
  download: false,
  clip: true,
  textWatermark: {
    content: 'image',
    repeat: true,
    angle: 45
  }
});
// Preview image
if (dataURL) {
  const img = document.createElement('img');
  img.src = dataURL;
  document.body.appendChild(img);
}
html
<!doctype html>
<html>
  <head>
    <meta charset="utf-8" />
    <link
      rel="stylesheet"
      href="https://cdn.jsdelivr.net/npm/leaflet@1.9.0/dist/leaflet.min.css"
    />
    <link type="text/css" rel="stylesheet" href="styles.css" />
  </head>

  <body>
    <div id="graph-container"></div>
    <hr />
    <span class="arrow">&darr;</span>
    <br />
    <script type="module" src="index.ts"></script>
  </body>
</html>
css
body {
  margin: 0;
  padding: 0;
  font-family: Helvetica, Arial, sans-serif;
  text-align: center;
}

#graph-container {
  height: 300px;
  position: relative;
}

img {
  height: 300px;
}

.arrow {
  margin-top: -1.5em;
  display: inline-block;
  position: absolute;
  padding: 0.5em;
  background: white;
}