Forum Discussion
Need help getting D3 charts on Leaflet map
- 8 years ago
We replied with the fixed version of your code.
Could you please post the fixed code here if you are ok with sharing?
Ignat Vilesov,
Software Engineer
Microsoft Power BI Custom Visuals
- 8 years ago
Thanks, v-viig. You fix works. Here is the fixed visual.ts.
module powerbi.extensibility.visual { export interface Data { lat: number; lng: number; latlng: {}; status: string[]; itemCnt: number[]; } var L = typeof L !== 'undefined' ? L : window['L']; export class Visual implements IVisual { private target: HTMLElement; private divMap: HTMLElement; private divTable: HTMLElement; private map: L.Map; private basemap: L.TileLayer; private layer: L.TileLayer; constructor(options: VisualConstructorOptions) { console.log('Visual constructor', options); this.target = options.element; this.divMap = document.createElement("div"); this.divMap.id = "map"; this.divMap.style.height = "100%"; this.divMap.style.width = "100%"; options.element.appendChild(this.divMap); this.map = L.map('map'); this.map.setView([-41.28, 174.77], 11); var mapLink = '<a href="http://openstreetmap.org">OpenStreetMap</a>'; this.layer = L.tileLayer('http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', { attribution: '© ' + mapLink + ' Contributors', maxZoom: 18, }); this.map.addLayer(this.layer); L.svg().addTo(this.map); } public update(options: VisualUpdateOptions) { debugger; //update map size this.divMap.style.height = options.viewport.height.toString() + "px"; this.divMap.style.width = options.viewport.width.toString() + "px"; var svg = d3.select("#map").select("svg"), g = svg.append("g"); var data: Data[] = [ { lat: -41.28, lng: 174.77, latlng: {}, status: ['Under', 'Over', 'Normal'], itemCnt: [30, 40, 50] }, { lat: -41.29, lng: 174.76, latlng: {}, status: ['Under', 'Over', 'Normal'], itemCnt: [30, 30, 60] }, { lat: -41.23, lng: 174.79, latlng: {}, status: ['Under', 'Over', 'Normal'], itemCnt: [30, 70, 10] } ]; data.forEach(function (d) { d.latlng = new L.LatLng(<number>d.lat, <number>d.lng); }) var circle = g.selectAll("circle") .data(data) .enter().append("circle") .style("stroke", "black") .style("opacity", .6) .style("fill", "red") .attr("r", 20) const map = this.map; function update() { circle.attr("transform", function (d) { return "translate(" + map.latLngToLayerPoint(d.latlng as any).x + "," + map.latLngToLayerPoint(d.latlng as any).y + ")"; } ) } this.map.on("viewreset", update);
this.map.on("zoom", update); update(); } } }
We replied with the fixed version of your code.
Could you please post the fixed code here if you are ok with sharing?
Ignat Vilesov,
Software Engineer
Microsoft Power BI Custom Visuals
Thanks, v-viig. You fix works. Here is the fixed visual.ts.
module powerbi.extensibility.visual {
export interface Data {
lat: number;
lng: number;
latlng: {};
status: string[];
itemCnt: number[];
}
var L = typeof L !== 'undefined' ? L : window['L'];
export class Visual implements IVisual {
private target: HTMLElement;
private divMap: HTMLElement;
private divTable: HTMLElement;
private map: L.Map;
private basemap: L.TileLayer;
private layer: L.TileLayer;
constructor(options: VisualConstructorOptions) {
console.log('Visual constructor', options);
this.target = options.element;
this.divMap = document.createElement("div");
this.divMap.id = "map";
this.divMap.style.height = "100%";
this.divMap.style.width = "100%";
options.element.appendChild(this.divMap);
this.map = L.map('map');
this.map.setView([-41.28, 174.77], 11);
var mapLink = '<a href="http://openstreetmap.org">OpenStreetMap</a>';
this.layer = L.tileLayer('http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', { attribution: '© ' + mapLink + ' Contributors', maxZoom: 18, });
this.map.addLayer(this.layer);
L.svg().addTo(this.map);
}
public update(options: VisualUpdateOptions) {
debugger;
//update map size
this.divMap.style.height = options.viewport.height.toString() + "px";
this.divMap.style.width = options.viewport.width.toString() + "px";
var svg = d3.select("#map").select("svg"),
g = svg.append("g");
var data: Data[] = [
{ lat: -41.28, lng: 174.77, latlng: {}, status: ['Under', 'Over', 'Normal'], itemCnt: [30, 40, 50] },
{ lat: -41.29, lng: 174.76, latlng: {}, status: ['Under', 'Over', 'Normal'], itemCnt: [30, 30, 60] },
{ lat: -41.23, lng: 174.79, latlng: {}, status: ['Under', 'Over', 'Normal'], itemCnt: [30, 70, 10] }
];
data.forEach(function (d) {
d.latlng = new L.LatLng(<number>d.lat, <number>d.lng);
})
var circle = g.selectAll("circle")
.data(data)
.enter().append("circle")
.style("stroke", "black")
.style("opacity", .6)
.style("fill", "red")
.attr("r", 20)
const map = this.map;
function update() {
circle.attr("transform",
function (d) {
return "translate(" +
map.latLngToLayerPoint(d.latlng as any).x + "," +
map.latLngToLayerPoint(d.latlng as any).y + ")";
}
)
}
this.map.on("viewreset", update);
this.map.on("zoom", update);
update();
}
}
}- Anonymous7 years agoNot applicable
v-viig hi there, I came across this and followed the steps you guys took but I got an error Invalid API version 1.10.0 Anyway idea how to fix this please?
Thank you
Pedzilla
- v-viig7 years agoCommunity Champion
What version of pbiviz do you ($ pbiviz --version)?
You might try updating API version by running pbiviz update 1.13.0 in the directory where you visual is locatted.
Ignat Vilesov,
Software Engineer
Microsoft Power BI Custom Visuals
- Anonymous7 years agoNot applicable
Thank you v-viig Ive done that but got the error
TYPESCRIPT File 'C:/viz/d3leaflet/src/Clients/Typedefs/d3/d3.d.ts' not found
error TYPESCRIPT File 'C:/viz/d3leaflet/src/Clients/Typedefs/jquery-visible/
jquery-visible.d.ts' not found.
error TYPESCRIPT File 'C:/viz/d3leaflet/src/Clients/Typedefs/jquery/jquery.d
.ts' not found.
error TYPESCRIPT File 'C:/viz/d3leaflet/src/Clients/Typedefs/leaflet/leaflet
.d.ts' not found.
error TYPESCRIPT /src/visual.ts : (18,22) Cannot find namespace 'L'.
error TYPESCRIPT /src/visual.ts : (19,26) Cannot find namespace 'L'.
error TYPESCRIPT /src/visual.ts : (20,24) Cannot find namespace 'L'.what have i done wrong please?
Thanks so much