score:1
Figured it out there was an issue with the positioning of my margins, width and height. There is the java code for anyone who needs the help.
(function() {
var margin = {top: 20, right: 30, bottom: 30, left: 40},
width = 700 - margin.left - margin.right,
height = 600 - margin.top - margin.bottom;
var x = d3.scale.ordinal().rangePoints([0, width], 1),
y = {};
var axis = d3.svg.axis().orient("left");
var line = d3.svg.line() //define a function to convert points into a polyline
.x(function(d) { return d.x; })
.y(function(d) { return d.y; })
.interpolate("linear");//line style. you can try "cardinal".
var chart = d3.select(".chart")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
var cars=[];
d3.csv("cars.csv", type, function(error, data) {
cars = data;
drawPC();
});
function drawPC() {
// Extract the list of dimensions and create a scale for each.
for (var dim in cars[0]) {
if (dim != "name") {
y[dim] = d3.scale.linear()
.domain([d3.min(cars, function(d) { return +d[dim]; }), d3.max(cars, function(d) { return +d[dim]; })])
.range([height,0]);
}
}
x.domain(dimensions = d3.keys(cars[0]).filter(function(d) { return d != "name";}));
//draw polylines
for (var i=1; i< cars.length; i++) { //for each car
//prepare the coordinates for a polyline
var lineData = []; //initialize an array for coordinates of a polyline
for (var prop in cars[0]) { //get each dimension
if (prop != "name" ) { //skip the name dimension
var point = {}; //initialize a coordinate object
var val = cars[i][prop]; //obtain the value of a car in a dimension
point['x'] = x(prop); //x value: mapping the dimension
point['y'] = y[prop](val);//y value: mapping the value in that dimension
lineData.push(point); //add the object into the array
}
}
//draw a polyline based on the coordindates
chart.append("g")
.attr("class", "polyline")
.append("path") // a path shape
.attr("d", line(lineData)); //line() is a function to turn coordinates into SVG commands
}
//next: draw individual dimension lines
//position dimension lines appropriately
var g = chart.selectAll(".dimension")
.data(dimensions)
.enter().append("g")
.attr("class", "dimension")
.attr("transform", function(d) { return "translate(" + x(d) + ")"; }); //translate each axis
// Add an axis and title.
g.append("g")
.attr("class", "axis")
.each(function(d) { d3.select(this).call(axis.scale(y[d])); })
.append("text")
.style("text-anchor", "middle")
.attr("y", -9)
.text(function(d) { return d; });
};
//this function coerces numerical data to numbers
function type(d) {
d.economy = +d.economy; // coerce to number
d.displacement = +d.displacement; // coerce to number
d.power = +d.power; // coerce to number
d.weight = +d.weight; // coerce to number
d.year = +d.year;
return d;
}
})();
(function() {
var margin = {top: 20, right: 30, bottom: 30, left: 690},
width = 1300 - margin.left - margin.right,
height = 600 - margin.top - margin.bottom;
var x = d3.scale.linear().range([50, width]),
y = d3.scale.linear().range([height-20,0]);
var chart = d3.select(".chart")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
var xAxis = d3.svg.axis().scale(x).orient("bottom");
var yAxis = d3.svg.axis().scale(y).orient("left");
var cars=[];
d3.csv("cars.csv", type, function(error, data) {
cars = data;
drawXY();
});
function drawXY(){
x.domain([d3.min(cars, function(d) { return d.year; }), d3.max(cars, function(d) { return d.year; })]);
y.domain([d3.min(cars, function(d) { return d.power; }), d3.max(cars, function(d) { return d.power; })]);
var yPos = height -20;
chart.append("g")
.attr("class", "xaxis")
.attr("transform", "translate(0," + yPos + ")")
.call(xAxis);
chart.append("g")
.attr("class", "yaxis")
.attr("transform", "translate(50,0)")
.call(yAxis);
chart.selectAll(".dot")
.data(cars)
.enter().append("circle")
.attr("class", "dot")
.attr("cx", function(d) { return x(d.year); })
.attr("cy", function(d) { return y(d.power); })
.attr("r", 3);
}
d3.selectAll("circle").data(cars).enter()
.append("circle")
.classed("circle", true)
.on("mouseover", function() { d3.select(d3.event.target).classed("highlight", true); })
.on("mouseout", function() { d3.select(d3.event.target).classed("highlight", false); });
function type(d) {
d.economy = +d.economy; // coerce to number
d.displacement = +d.displacement; // coerce to number
d.power = +d.power; // coerce to number
d.weight = +d.weight; // coerce to number
d.year = +d.year;
return d;
}
})();
Source: stackoverflow.com
Related Query
- Adding Multiple graphs to one page with D3
- Generating PDF/SVG/PNG with multiple D3.js graphs on one page
- Multiple graphs in one single page using angularjs and d3.js
- I'm having trouble getting multiple isotype graphs on one page. I think the problem may be with the svg <use> element .selectAll("use")
- Multiple simple graphs on one page d3.js
- Issue with multiple d3 graphs on same page
- Performance issue with dc.js with graphs in multiple tabs on a page
- Can't get multiple graphs to display on one page using D3 and Angularjs-nvd3-directives
- Why can't I display multiple graphs in one HTML page using D3?
- How can I properly aggregate / group multiple line graphs into one overall graph with d3.js when the x-values aren't matching exactly?
- D3 click event with multiple svg on one page
- How to set multiple attributes with one value function?
- Multiple force-layout graphs with d3 in seperate svg/div's
- d3 set multiple attributes with one function
- D3 with AngularJs, Multiple charts on same page with different data based on common key
- Enter() statement with a function from one data array to multiple arrays? (Adding multiple Gradient Paths)
- Multiple multi-series d3 line charts on one page
- multiple d3 graphs in a single page
- How to update multiple pie charts in one page in D3?
- import multiple svgs in one xml with d3.js
- injecting the d3.js graphs correctly when using Angular directive multiple times with bootstrap
- Multiple force-directed graphs in 1 svg (problems with text display)
- Multiple graphs on one page, scale error in D3.js
- How to create one group with multiple dimension on dc.js
- Multiple d3 charts on a page with different (linear and log) axes
- Multiple D3 TreeMap on the same page with different div id's
- c3.js. Creating graphs within a loop to multiple divs with the same class/ID?
- Multiple D3 draggable/zoomable trees on one page using Angular
- D3 transitions with multiple graphs
- D3 zoom behavior with multiple containers for a chart in one svg
More Query from same tag
- How to label one specific scatter point (not all points) using d3 or regular javascript?
- Limited horizontal ticks in D3js v4 bar chart
- parameterized D3js force directed graph node positioning
- How do I draw a zoomable d3 line chart over time?
- How stop d3 tooltip from going off page?
- d3.js Linking multi-line chart with bar chart on mouseover
- How to use d3.js with JSRender?
- d3: will selection.style('font-size') always return a value in px?
- Preemptive getBBox calculation
- How to plot histograms using d3.js from text file instead of random generation using map
- Deleting node from graph is wrong
- Loading d3 bullet chart into a span
- Is this approach right of making a heatmap matrix
- Time axis in d3.js with specific time zone
- Simple d3/dimple drill down example with bar chart
- Chrome not running D3.js
- How to get the minimum and maximum value from a Json
- Selecting a specific value from an array of objects in javascript
- crossfilter | animate through date range
- usable D3 replacement in IE8 (graphs with D3 and svg)
- How can I specify a custom XML/SVG namespace with D3.js?
- Angular- detecting a change in scope
- A more elegant way to bind new elements using data bound to multiple group elements
- d3, html in svg, animating opacity takes div out of position, why?
- How to get day of week on left side of D3 calendar heat map
- D3 Scatterplot: Tooltip appears on initial load, but disappears after filter updated using dropdown
- Acess object's attribute inside another object using Javascript / D3.js
- d3 stacked bar chart - start at y0, not yMax
- Is it possible to use hoverIntent with D3.js?
- Load Data and Update Existing View D3