score:5
The short answer is that you should simply return the url
property from your data when you are assigning the xlink:href
attribute:
.attr("xlink:href", function(d) { return d.url; })
However, there are a couple other issues with the code you posted.
Issue 1. circles.selectAll('circles')
This starts with a the selection of your g
element, and within it, selects all elements with the tag-name circles
. The problem is that circles
is not a valid svg tag. This just creates an empty selection, which is okay in this case because the selection is only being used to create new elements. But, it's a bad habit to make dummy selections like this, and it can be confusing to others trying to understand your code. Instead, you should decide on a class name to give to each of the new link elements, and use that class name to make your selection. For example, if you decide to give them a class of link
you would want to do the following:
First create a selection for all of the elements with class="link"
:
circles.selectAll('.link')
This selection will initially be empty, but when you use .data()
to bind your data to it, it will be given an enter selection which you can use to create the new elements. Then you can add the class of link
to the newly created elements:
.data(schools).enter().append('svg:a')
.attr('class', 'link')
Issue 2. .attr("i", function(d,i) {return i})
This one's pretty straightforward, there is no such attribute as i
on svg elements. If you want to store arbitrary data on an element to be able to access it later, you can use a data attribute. In this case you might want to use something nice and semantic like data-index
.
Issue 3. positions.push(location)
This is a big one. I would not recommend that you make a separate array to store the altered values from your dataset. You can use an accessor function in your d3.csv()
function call, and clean up the incoming data that way. It will save you from having to maintain consistent data across two separate arrays. The accessor function will iterate over the dataset, taking as input the current datum, and should return an object representing the adjusted datum that will be used. This is a good spot to use your unary operator to coerce your latitude and longitude:
function accessor(d) {
return {
name: d.name,
longitude: +d.longitude,
latitude: +d.latitude,
city: d.city,
state: d.state,
url: d.url
};
}
There are two different ways to hook the accessor function into your d3.csv()
call:
Method 1: Add a middle parameter to d3.csv()
so that the parameters are (<url>, <accessor>, <callback>)
:
d3.csv('path/to/schools.csv', accessor, function(schools) {
// ...etc...
});
Method 2: Use the .row()
method of d3.csv()
d3.csv('path/to/schools.csv')
.row(accessor)
.get(function(schools) {
// ...etc...
});
Now when you want to access the latitude and longitude in your preferred format, you can get them right from the bound data, instead of from an outside source. This keeps everything clean and consistent.
Putting all of this together, you get the following:
d3.csv('http://sitename.com/wp-content/themes/vibe/homepage/schools.csv')
// provide the accessor function
.row(function accessor(d) {
return {
name: d.name,
longitude: +d.longitude,
latitude: +d.latitude,
city: d.city,
state: d.state,
url: d.url
};
})
// provide a callback function
.get(function callback(schools) {
circles.selectAll('.link')
.data(schools)
.enter().append('svg:a')
.attr('class', 'link')
// point the link to the url from the data
.attr('xlink:href', function(d) { return d.url; })
.append('svg:circle')
.attr('class', 'symbol')
// now we can just use longitude and latitude
// since we cleaned them up in the accessor fn
.attr('cx', function(d) { return d.longitude; })
.attr('cy', function(d) { return d.latitude; })
// constants can be assigned directly
.attr('r', 6)
.attr('data-index', function(d,i) { return i; });
});
Source: stackoverflow.com
Related Query
- How to add links from CSV file to SVG elements generated with D3?
- How to Properly Parse a CSV File to Work With SVG Elements
- How to modify tags of an already existing svg object with data from json file
- Add new row dynamically to an array with headers created from a csv file
- How to add two elements (circle and text) to SVG g element with D3js?
- How do I remove all children elements from a node and then apply them again with different color and size?
- How do I save/export an SVG file after creating an SVG with D3.js (IE, safari and chrome)?
- How to add border/outline/stroke to SVG elements in CSS?
- How to load data from a CSV file in D3 v5
- How to update the fill color on existing svg elements with d3.js?
- How do I associate SVG elements generated by graphviz to elements in the DOT source code
- Trying to add ID to svg path from json file
- How to update elements of an HTML that the elements are created using data from a CSV file?
- How to move elements along with svg group
- How to add external svg file (by D3.js) to Leaflet map
- Drawing voronoi diagram from a csv file with d3.js
- D3.js how to transition in opposite direction with basic SVG elements
- How to get projected path definition strings (not SVG elements) from a GeoJSON dataset with D3?
- How to use D3 force layout with existing SVG elements as nodes
- How do you translate HTML elements along with SVG elements when zooming and panning with D3
- how to get data with tsv or csv to array in d3.js from a txt file?
- How to use python pandas to get intersection of sets from a csv file
- Unable to reference d3.js data imported from a csv file with spaces in the header
- How can I add an external group to an existing svg with d3.js?
- How to fix unwanted circle on / break down of SVG path element for Sankey links with d3?
- d3js v5: Create a SVG with groups from a JSON file
- How to load a csv file from static in django into javascript function
- How do I convert an svg file from inkscape to geoJson format
- How to parse a csv file with d3 when parser is not the comma
- With D3, how can I avoid SVG graph links being rendered over nodes?
More Query from same tag
- C3 bar chart - Custom X-axis label
- using qtip2 tooltip with d3.js
- d3.js - how to automatically calculate arc lengths in radial dendrogram
- Update d3 chart
- unable to construct d3 tree structure unbalanced tree using d3 hierarchy
- Load json in d3.js
- How do I make sure that data is passed correctly in d3 on drag?
- How to avoid the labels getting displayed over the stacked bar chart?
- cannot clear d3.js div on dropdown change
- Scientific Notation in Handsontable
- TypeError: svg.append(...).attrs is not a function when creating charts using D3.js
- D3 adding text lables to horizontal stacked barchart
- multi line chart nvd3 not shown properly
- How to fix the configuration of the nodes that are returned? (D3, Javascript, Neo4j)
- d3 v4 how to add data labels to bar graph
- Responsive D3 Charts in React: ResizeObserver or the viewBox attribute?
- checkbox check/uncheck using d3
- D3 Responsive chart
- d3js v4: Scale circles with zoom
- D3 Line Chart not having proper X & Y axis plotting with Labels
- How do I connect a crossfilter(ed) dataset to a d3 chart?
- Callback for d3 click event
- How to change the alignment of circles in packed circle d3
- D3.js Focus+Context via Brushing Ordinal Scale
- d3 path - split into 2 new paths
- D3js - can't change style for svg text elemets
- Merging data in d3.js
- Time format not working in dc.js LineChart
- D3.js - Making legend use descriptive strings for legend instead of data values
- D3 select sibling, give it a class