score:31
the general rule when using a data-join is that you want a one-to-one mapping from data to elements. so, if you have two series in your scatterplot, you’ll want two container elements (such as g elements) to represent the series. since you currently have only one data
array, you’ll also want to use array.map to convert the data representation into two parallel arrays with the same representation. this way, you don’t have to duplicate code for each series.
say your data was represented in a csv file with one column for the x-values, and multiple other columns for the y-values of each series:
x,y1,y2
5,90,22
25,30,25
45,50,80
65,55,9
85,25,95
if you want the code to be completely generic, you first need to compute the series’ names, such as ["y1", "y2"]
. (if you added a third column to the csv file, it might be ["y1", "y2", "y3"]
.) you can compute the names using d3.keys, which extracts the named properties from an object. for example, d3.keys({foo: 1, bar: 2})
returns ["foo", "bar"]
.
// compute the series names ("y1", "y2", etc.) from the loaded csv.
var seriesnames = d3.keys(data[0])
.filter(function(d) { return d !== "x"; })
.sort();
now that you have the series names, you can create an array of arrays of points. the outer array represents the series (of which there are two) and the inner arrays store the data points. you can simultaneously convert the points to a consistent representation (objects with x
and y
properties), allowing you to reuse code across series.
// map the data to an array of arrays of {x, y} tuples.
var series = seriesnames.map(function(series) {
return data.map(function(d) {
return {x: +d.x, y: +d[series]};
});
});
note this code uses the +
operator to coerce the csv values to numbers. (csv files are untyped, so they are initially strings.)
now that you’ve mapped your data to a regular format, you can create g elements for each series, and then circle elements within for each point. the resulting svg structure will look like this:
<g class="series">
<circle class="point" r="4.5" cx="1" cy="2"/>
<circle class="point" r="4.5" cx="3" cy="2"/>
…
</g>
<g class="series">
<circle class="point" r="4.5" cx="5" cy="4"/>
<circle class="point" r="4.5" cx="7" cy="6"/>
…
</g>
and the corresponding d3 code:
// add the points!
svg.selectall(".series")
.data(series)
.enter().append("g")
.attr("class", "series")
.style("fill", function(d, i) { return z(i); })
.selectall(".point")
.data(function(d) { return d; })
.enter().append("circle")
.attr("class", "point")
.attr("r", 4.5)
.attr("cx", function(d) { return x(d.x); })
.attr("cy", function(d) { return y(d.y); });
i’ve also added a bit of code to assign each series a unique color by adding a fill style to the containing g element. there are lots of different ways to do this, of course. (you might want to be more specific about the color for each series, for example.) i’ve also left out the code that computes the domains of your x and y scales (as well as rendering the axes), but if you want to see the entire working example:
score:5
place the two circles for each data point into a single svg:g
element. this produces a one-to-one mapping for the data to elements but still allows you to show two different points.
var nodeenter = vis1.selectall("circle")
.data(dataset)
.enter()
.insert("svg:g");
nodeenter.insert("svg:circle")
.attr("cx", function (d) { return 100 - d.xvar})
.attr("cy", function (d) { return 100 - d.yvar1})
.attr("r", 2)
.style("fill", "green");
nodeenter.insert("svg:circle")
.attr("cx", function (d) { return 100 - d.xvar})
.attr("cy", function (d) { return 100 - d.yvar2})
.attr("r", 2)
.style("fill", "blue");
working jsfiddle.
Source: stackoverflow.com
Related Query
- Can d3.js draw two scatterplots on the same graph using data from the same source?
- Two bar charts from same data object using D3.js
- How to fetch data from json file to draw a dynamic graph by using d3.js
- In a d3 scatterplot using data from a csv file, how do i draw lines connecting related points when the mouse is over a point?
- How can I get the source code from a page using javascript
- How can i trigger two different tooltips at the same time using D3/javascript?
- D3 - How can I show/hide a text element when hovering a circle element created from different attributes of the same data entry?
- How can i use the data from Hbase to visualize using d3.js
- Can I use d3.js to draw the graph using json string or json object?
- How to update elements of an HTML that the elements are created using data from a CSV file?
- How to create d3 graph using data from Rails database
- How can I offset the source and target points of a bezier curve using D3's link generator?
- D3.js doesn´t draw graph after changing source from tsv to JSON
- How to draw the interactions between source and destination countries using ip address?
- Two D3.js graph in the same page with AngularJS
- graph the data from a csv file
- In d3.js, while importing csv files using a row conversion, how can I "slice" the data to only include a range of rows?
- How can I specify a d3.js selector using values from a data object?
- How can I draw a continuous line using d3 without needing to click&drag the mouse, but rather just use the mouseover event?
- D3 - Multiple wordclouds from the same source file
- Can we use Google Fusion tables as data source for making a visualization using d3.js?
- Drawing a line using d3 is not visible when all data items are the same
- Using the Force-Directed Graph Example from bl.ocks.org
- D3.JS, How can I render multiple objects in the same data binding?
- D3.js: draw simple line graph starting from data arrays
- How to select a <tr> from the same data index as a scatterplot <circle>
- d3 graph duplicates on the DOM when data source changes
- Using data from two separate geoJSON files in D3.js map visualization
- How can I get the equivalent to a "this" keyword from d3 using d3.select()?
- Using different data in the same donut chart
More Query from same tag
- Drawing path trails between two points
- d3.js bar matrix - reset height if different column
- D3.JS Browser Support
- How to apply specific colors to D3.js map based on data values?
- Cal-Heatmap Stange (incorrect) color behavior
- Understanding D3 TopoJSON transformations
- D3.js: How do you tilt/rotate individual text elements within a collection without rotating them all?
- Cannot properly draw a scatterplot over a grouped bar chart in D3
- How do I reduce two arrays to a single value that is present at the same index?
- Not able to get the Zoom/Pan and States name appear in India Map - D3?
- turn Y axis upside down
- D3.js: Discontinuous Time Scales (Financial Data)
- Drawing Connecting Lines ("Great Arcs") on a D3 Symbol Map
- d3.js x axis specific number of string ticks
- Create Bars as Sums of Values
- D3.js linking node items is not working as expected
- Display non-ASCII characters from CSV file retrieved with d3.csv
- How to draw multiple self-edges in a node-link diagram in D3
- D3 Projection_Transform attr
- calculate the date diff in javascript from JSON array
- D3 V4 Properly placing a bubble in the US Map
- Making a grouped bar chart using d3.js
- Rebinding exports in d3.js v4
- How to emulate d3.js homepage?
- How to hide folders using partition layout based on available space in D3?
- Prerendering d3 and key functions
- Given a range of date intervals, create an array of all 1440 minutes in a day with the frequency each minute appears in the source intervals
- d3.js import csv using accessor function and callback
- D3.js y- axis ticks and grid lines do not align
- d3.js zooming mouse coordinates