score:1
Solution: d3.xml
plus d3.queue
I needed to set up a d3.queue
, where one external call was bringing in the data for the visualization, and the other was bringing in the SVG image mentioned above.
The entire visualization became fairly large and complex; the code below captures the relevant parts as I'd used them.
// First create helper function with callback return, to provide structure `d3.queue` needs.
readSvg = function(svg_path, callback) {
d3.xml(svg_path).mimeType("image/svg+xml")
.get(function(err, xml) {
if (err) {
throw err;
}
callback(null, xml.documentElement.outerHTML);
});
};
// Next make the `d3.queue` call.
// (In reality, the `makeViz` function would need to be defined first, but
// logically, I felt it easier to understand if I show this first.)
d3.queue()
.defer(readSvg, my_svg_image_path)
.defer(d3.json, my_data_path)
.await(makeViz);
// "Finally" display the visualizations using `makeViz`.
var makeViz = function(error, svg_image, data) {
if (error) {
console.log(error);
} else {
var points = svg.selectAll('.point')
.data(data).enter()
.append('g')
.classed('point', true)
.style('fill', my_fill)
.style('stroke-opacity', my_opacity)
.html(svg_image); // Note the `html` call!!!
// These circles are added to make the icons more easily "clickable".
// NOTE: THE VARIABLE "points" IS STILL ASSOCIATED WITH THE IMAGE, NOT
// THE CIRCLES BELOW!!! THIS IS ON PURPOSE, FOR EASIER
// ASSOCIATION AND MAPPING LATER!!
points.append('circle').attr('r', icon_size);
// Now call a function that associates all of the x & y coordinates according
// to the bound data and what the users click on. (`clicked` is a variable
// defined out of scope, and is part of the interactivity.)
updatePoints(data, points, clicked);
A few things to note:
The image had a lot of "empty space". Imagine if the image were a smiley face; most of the smiley face is nothing. Therefore, most of it is not clickable. I therefore created a parent
g
DOM element and attached the circle to it as well as the image. Therefore, the entire "smiley face" was clickable, including the white space.By using this
d3.xml.mimeType.get
call, I was able to get the entire SVG structure of the file. Then sending on its.documentElement.outerHTML
gave me just the parts I was looking for: all of theg
s,svg
s,path
s, and all other elements of the image, and none of the other DOM stuff that comes along with anxml
import.Because I needed to import 2 things, I needed to use
d3.queue
. But becaused3.queue
works via an expected callback, I needed to structure that skeleton call withreadSvg
as I did. That function does not do much, except call the function that is passed to it, which is a function to return the data.- There might be a better or cleaner way to handle this part, but simply making a
d3.xml
call directly withind3.queue
I could not get to work properly.
- There might be a better or cleaner way to handle this part, but simply making a
If anyone has better suggestions, that's great, but I can say this works.
Also, I'm always looking for tips, pointers, and suggestions for how to indent all these chained calls. Is the way I've shown here the best? I find it challenging to read d3
code sometimes because of the combination of chained calls and callback functions. (I'm in the process of adopting an async/await
structure for some of my d3
work.)
Source: stackoverflow.com
Related Query
- In D3, how to use SVG files as DOM objects with bound data and paths still exposed?
- How can I scale my map to fit my svg size with d3 and geojson path data
- How can I apply a clipPath in SVG with multiple paths and NOT clip out the area between the paths?
- How do I use MongoDB data with D3 and Node?
- D3.JS how to load 3 csv files and change data on line chart with button click event?
- How do I use JSON and return data with D3
- How to load multiple csv files and use them mixed with each other
- how to use getElementById with getBBox to determine the svg width and height
- How do I save/export an SVG file after creating an SVG with D3.js (IE, safari and chrome)?
- How to pull data from mysql database and visualize with D3.JS?
- How to use x and width in a bar chart with scaleTime?
- how to zoom d3.js chart and reload data with greater granularity (in AJAX)
- Get attributes of existing SVG elements and bind as data with d3.js
- How to use D3 force layout with existing SVG elements as nodes
- how to use .scale and .translate with d3.js?
- Update with DOM children and data propagation
- How do you translate HTML elements along with SVG elements when zooming and panning with D3
- How can I use SVG translate to center a d3.js projection to given latitude and longitude values?
- How to find top 5 numbers in a data set and data associated with it?
- How to properly use the Use tag in SVG with d3 js?
- I am trying to use multiple 2 arrays for binding data for svg circles but cannot figure out how
- How can I append string to y-axis data with tick and d3.format?
- How to reset svg scaling and fit to screen for random but large maps/datasets with different orientations
- How do I append DOM to an Angular 2 component and still get encapsulated styles
- How to change JSON data to Javascript array of objects with D3
- How to make a mouseover interactive line graph with multiple data series and 2 y axes?
- How can i bind an json data with a key and Name for display
- How to use the quantize function with my json data file
- Ignore missing images in SVG with D3 data and enter methods
- How to load multiple files with Queue.js and D3.js?
More Query from same tag
- Make an SVG linearGradient follow the path
- D3 nest an image and a circle for a node?
- Can I use images as the background rectangles for d3 treemaps?
- area chart with d3 is not rendering on jsp page but working fine with html
- Bivariate Line Graph using Nested element data
- d3 nested data - individual graphs - setting y.domain?
- How to add multiple d3 items to one page
- sorting d3js bar chart
- How to use scaleBand.domain() for discrete data in D3 v6 without hardcoding the data?
- d3 chart won't append to any selector other than body
- Get X and Y axes values from the mouse position in d3.js
- d3.js Double-click is not Releasing Nodes
- D3 not rendering map data in JSON file
- Impulse plot in d3
- Parallel coordinates multidimensional data not visualised in D3
- In D3.js I am confused by behavior of exit()
- D3 map - combine two disjoint svg polygon paths into one path
- Format number to 2 decimal places only if it has decimal places
- D3js pie chart not showing up on localhost
- how to make simple directed graph using d3
- Remove Specific svg in d3
- Porting d3.js SVG to HTML5 Canvas?
- how to set button and rect for particular columns in table using d3?
- programmatically accessing compiled CSS class names in angular 2
- To show B(Billion) instead of G(Giga) in altair charts python
- D3 bar chart bar text labels not visible properly
- D3.js: Trying to build flat calendar
- How do I externalise my Data
- Interrupt exit transition in D3
- autoscale and format x axis in dimple.js