score:1
Here's a couple examples I just put together. The first is the straight d3
way of doing what you are asking. The second is a hacked up plottable.js
. With plottable.js
I can't find a way to position the axis outside of their table system, I had to resort to manually moving them. The table system they use is designed to relieve the developer of having to manually position things. This is great and easy, of course, until you want to control where to position things.
Here's the hack, after you render your plottable:
// move the axis...
d3.select(".y-axis")
.attr('transform',"translate(" + width / 2 + "," + 0 + ")");
d3.select(".x-axis")
.attr("transform", "translate(" + 48 + "," + height / 2 + ")");
Note, I didn't remove the left side margin (the 48 above) that plottable puts in. This could be hacked in as well, but at that point, what is plottable providing for you anyway...
It should be noted that the different appearance of each plot is entirely controlled through the CSS.
Complete d3 scatter plot:
// D3 EXAMPLE
var margin = {
top: 20,
right: 20,
bottom: 20,
left: 20
},
width = 500 - margin.left - margin.right,
height = 500 - margin.top - margin.bottom;
var x = d3.scale.linear()
.range([0, width]);
var y = d3.scale.linear()
.range([height, 0]);
var xAxis = d3.svg.axis()
.scale(x)
.orient("bottom");
var yAxis = d3.svg.axis()
.scale(y)
.orient("left");
var svg = d3.select("#d3").append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
x.domain([-100, 100]);
y.domain([-100, 100]);
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(" + 0 + "," + height / 2 + ")")
.call(xAxis);
svg.append("g")
.attr("class", "y axis")
.attr("transform", "translate(" + width / 2 + "," + 0 + ")")
.call(yAxis)
.append("text");
svg.selectAll(".dot")
.data(data)
.enter().append("circle")
.attr("class", "dot")
.attr("r", function(d) {
return d.r;
})
.attr("cx", function(d) {
return x(d.x);
})
.attr("cy", function(d) {
return y(d.y);
})
.style("fill", function(d) {
return d.c;
});
Plottable.js:
// PLOTTABLE.JS
var xScale = new Plottable.Scale.Linear();
var yScale = new Plottable.Scale.Linear();
var xAxis = new Plottable.Axis.Numeric(xScale, "bottom");
var yAxis = new Plottable.Axis.Numeric(yScale, "left");
var plot = new Plottable.Plot.Scatter(xScale, yScale);
plot.addDataset(data);
function getXDataValue(d) {
return d.x;
}
plot.project("x", getXDataValue, xScale);
function getYDataValue(d) {
return d.y;
}
plot.project("y", getYDataValue, yScale);
function getRDataValue(d){
return d.r;
}
plot.project("r", getRDataValue);
function getFillValue(d){
return d.c;
}
plot.project("fill", getFillValue);
var chart = new Plottable.Component.Table([
[yAxis, plot],
[null, xAxis]
]);
chart.renderTo("#plottable");
d3.select(".y-axis")
.attr('transform',"translate(" + width / 2 + "," + 0 + ")");
d3.select(".x-axis")
.attr("transform", "translate(" + 48 + "," + height / 2 + ")");
score:2
Mark has the right idea - the table system doesn't natively support this layout, so you need to take some manual control over how they are laid out. However, using somewhat obscure parts of the Plottable API, there is a cleaner and better-supported way to lay out the chart you want, which doesn't have the problem of the axes being slightly offset.
The first change is we are going to stop using the table layout engine entirely, since it isn't able to do what we want. Instead, we will plop all the components together in a Component.Group
. A Group just overlays components in the same space without trying to position them at all.
var chart = new Plottable.Component.Group([yAxis, xAxis, plot]);
Then we are going to use the alignment and offset methods that are defined on the base (abstract) component class. We set the x-alignment of the y axis to "center" and the y-alignment of the x axis to "center" This will put the axes in the center of the chart.
var xAxis = new Plottable.Axis.Numeric(xScale, "bottom").yAlign("center");
var yAxis = new Plottable.Axis.Numeric(yScale, "left").xAlign("center");
We're not quite done at this point, since to really center the axes we need to shift them back by one half of their own width. The width is only calculated when the chart is rendered (strictly speaking, in the computeLayout
call, but that is an internal detail), so we need to set an offset after the chart is rendered:
chart.renderTo("#plottable");
xAxis.yOffset(xAxis.height()/2);
yAxis.xOffset(-yAxis.width()/2);
You can see the final result here (it's a fork of Mark's plnkr). Note that now the axes are aligned on the center of the chart, as the center dot is perfectly on 0,0.
Source: stackoverflow.com
Related Query
- Basic math 101 scatter plot with Plottable.js
- Using D3 transition method with data for scatter plot
- D3js Grouped Scatter plot with no collision
- nvd3 scatter plot with ordinal scale
- how do you draw linear line in scatter plot with d3.js
- How can I plot positive and negative values for both axes in a scatter plot with d3?
- Making an interactive scatter plot with d3?
- Create an interactive 3d scatter plot with D3.js and Three.js with json data
- Moving scatter plot circles with context zooming and brush in D3
- D3 Ordinal Scales with Scatter Plot
- Create scatter plot with ordinal scale in d3 version 4
- How to handle the scales with d3.drag in a scatter plot
- Creating scatter plot with D3.js
- How to draw a scatter plot with multiple y values for each x value?
- dc.js Scatter Plot with multiple values for a single key
- Create scatter plot with unequal intervals on x-axis using d3.js
- How to render dates on x axis prior to 1900 with d3 .js scatter plot
- D3: Rescaling scatter plot elements with Update, without removing elements
- c3 (v4) scatter plot with two sets of data
- How to color the area under a scatter plot with different colors
- Y Axis not displaying for scatter plot with d3 v4
- Grouped scatter plot with multiple datasets D3
- Create simple scatter plot with Dimple
- Triangle scatter plot with D3.js
- Library for creating scatter plot charts with quadrants
- D3 Scatter Plot With Nested Array
- scatter plot d3.js freecodecamp pronect having trouble with yscale displaying minutes
- How to make a Scatter plot with D3 using a JSON file as input
- Scatter plot with percentage Axis in dimplejs
- NVD3 scatter plot tick spacing with only a few values
More Query from same tag
- d3plus.Geomap is not a constructor
- DC.js barchart bars disappear on brush and total amount for date range problems
- How can the elements in SVG can't be dragged out of the scope of SVG?
- D3 listening to "end" event of a transition
- Changing number values for text on Y Axis label for dc.js ScatterPlot
- how to show two d3.js diagrams on the same page
- Creating a sunburst diagram with dynamically chosen data source using D3
- Using JavaScript + D3 3.x code in SAS to create HTML output
- How to add hypertext link to Cluster Force Layout circle
- Javascript - JSON - Nesting and Grouping
- vertical bars not aligned with the x-axis label and last bar rendered outside the x-axis range - D3 js - simple bar with fixed width
- D3 force layout in Canvas: adding & updating nodes (& links)
- D3.js: Code not working
- D3 bubble chart / force layout forceAttraction
- How to improve my simple nested data binding in d3?
- How to add a line on x-axis on a horizontal bar chart in d3
- D3 parse number from string
- Points on an Arc using D3
- D3.js filter existing html table
- d3 nested selection tutorial not rendering table
- Multiple Y-Axis with JSON data in C3
- Dynamic JSON not loading - D3.js Line chart
- d3js with node, how do I add a lines to pie chart?
- d3 control space between ticks
- How do I create a custom force that constrains nodes to a specific area within an SVG?
- Charts showing on the same line in non-IE browsers, but on the next line on IE browsers
- Adding tooltip and focus guides for x and y2 axis on multi-axis linechart in d3
- D3 pie chart element popout
- Clockwise direction for RadarChart
- D3 generate multiple graph for single json