I have drawn a Line chart in D3. I am facing issue in x axis alignment.Fiddle Link
var x = d3.scale.ordinal().rangeRoundBands([0, width], .2);
x.domain(dataset.map(function(d) {
return d.qName; }));
The issue is -"points and the x-axis aren't on the same level". (there's some shift visible)).
How can i shift Q1 to intersection point of x-axis and Y axis.
Any help in this regard would be highly appreciated.
You can adjust the height by adjusting the translation of the group that contains the axis, e.g. change the value height in
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")");
You may want to try (height-20) for example.
Related
My d3 scatter plot uses historic date data in a range from 1600 to present. I can plot my dots successfully but can't display the dates prior to 1900 I the x axis.
I am using this example to make a scatterplot in d3 but my data has historic dates prior to 1900. I have tried to implement this solution but this returns a single date repeated for each tick mark
If I try to implement d3.axisBottom(x) this returns the dates from my data, but dates prior to 1900 are not formatted correctly.
I have made a plunker with full code
Here is my relevant scale and axis code (from the plunkr):
var x = d3.scaleTime().range([0, width]);
var y = d3.scaleLinear().range([height, 0]);
var xAxis = d3.axisBottom(x).ticks(10).tickFormat(function(d){return timeFormat(d);});
var yAxis = d3.axisLeft(y).ticks(10);
var x = d3.scaleTime()
.domain(d3.extent(data, function(d) {return (d.dates);}))
.range([ 0, width ]);
svg.append("g")
.attr("transform", "translate(0," + height + ")")
//.call(xAxis, function (d){return (d);});
//.call(xAxis, function (d){return (d.dates);}); // returns just a single date for all tick marks
.call(d3.axisBottom(x)); // partially correct dates but not formatting dates prior to 1900
My scatter plot is fine and the dots are as expected. What I want to see on the x axis is the dates prior to 1900, eg 1750.
Very grateful for help.
The referenced answer is correct, you have other issues in your code. I've also updated that answer a bit to clean the code and include a generic example with an axis from 1600-2000.
The first problem is that you define your x scale:
var x = d3.scaleTime().range([0, width]);
Then pretty much immediately define your axis:
var xAxis = d3.axisBottom(x)
.tickFormat(timeFormat);
Then you define the x domain, while redefining x as well with:
var x = d3.scaleTime()
.domain(d3.extent(data, function(d) {return (d.dates);}))
.range([ 0, width ]);
If we use svg.call(xAxis), this means that the axis is using the first x scales domain, which defaults to [January 1 2000, January 2 2000], which is why every tick will have the same year if you apply the axis with only a default domain.
Your code has .call(d3.axisBottom(x) rather than .call(xAxis), which creates a new axis again, but without the formatting needed to render pre 1900 dates
Instead, determine the scale's domain first, then create the axis:
var x = d3.scaleTime()
.range([0, width])
.domain(d3.extent(data, function(d) {return (d.dates);}))
var xAxis = d3.axisBottom(x)
.tickFormat(timeFormat);
And now you can just apply the axis:
selection
.attr("transform",...)
.call(xAxis);
Here's an updated plunkr
I have a function say Echo of size 100*100. I use:
x=linspace(-5000, 5000, 100); y=linspace(-200, 200, 100);imagesc(x, y, Echo);
I see the image is not properly oriented, so if I use:
Echo=rot90(Echo); imagesc(x, y, Echo);
I get the desired image but the yscale is starting from 200 (lower right) to -200 (upper right). I want -200 (lower right) and 200 (upper right). How do I do that?
You can try:
flipud on the matrix
or modify the figure/axes properties: axis ij (compare with axis xy)
In general if you want to reverse the direction of one of the axes relative to the normal orientation used in figures (example here: Y axis), use
set(gca,'YDir','reverse')
Edit
Since imagesc shows y axis in reverse orientation by default, try the following:
set(gca,'YDir','normal')
Note that in fact this is equivalent to #Bonlenfum's alternative suggestion of axis xy
I need to visualize a playing field for a robot game. Unfortunately, the game uses a right handed coordinate system, with the y axis pointing up.
Is there a way to adjust the cairo context of a drawing area so that it matches this coordinate system?
I can scale, translate and rotate, but I cant find a way of switching the y axis orientation, which would be more convenient compared to converting all coordinates individually.
Thank you for any input!
You are allowed to define every field in a cairo_matrix_t:
cairo_matrix_t flip_y;
cairo_matrix_init(&flip_y, 1, 0, 0, -1, 0, 0);
cairo_set_matrix(cr, &flip_y);
Just remember how the trasformation is applied:
x_new = xx * x + xy * y + x0;
y_new = yx * x + yy * y + y0;
Starting from the basic Crossfilter example, i'd like to create an alternate visualization that displays the barcharts vertically instead of horizontally. That is, essentially flip the axies of the barchart.
It's easy enough to modify the original barChart function to swap the bars in the chart. I've been able to do that with these following changes:
var width = x.range()[1],
height = y.range()[0];
Becomes
var width = x.range()[1],
height = y.range()[0];
and in the nested barPath method
path.push("M", x(d.key), ",", height, "V", y(d.value), "h9V", height);
becomes
path.push("M", 0, ",", x(d.key), "h", y(d.value), "v9H", 0);
These minor changes get the bars printed nicely, but it doesn't handle the x axis with it's ticks, nor does it handle the selection brushes. Is it possible to flip the brushes on their sides? If so, how would I go about doing that?
Thanks in advance!
An easiest way would be to rotate the "g" container of each chart by 90 in chart().
Something like this:
g = div.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 + ")"+ "**rotate(90)**");
I am implementing a rightclick context menu on my google v3 map and I need to get the pixel x and y to correctly position the menu. I get the lat and the lng, anyone have a nice solution to get the pixel x and y?
Best Regards
Henkemota
index=x+(y*height)
x = index % width
y = index / height
Correction to the above answer:
index=x+(y*width)
//(not y*height ... because you're taking one full horizontal line of pixels (e.g. 1280px) and multiplying that by the number of lines (y) down the screen at which x is, then adding x to account for x pixels over in the next full line.)
x = index % width
y = index / height