Adding specific ticks on D3 axis w/ brush - d3.js

I need help adding specific ticks to an axis that is also being focused with a brush. I found a similar question/answer for a static axis (without a brush) here:
Adding a specific tick to a D3.js axis
It's along the lines of what I'm trying to do. My chart is more similar to this example though :
http://blockbuilder.org/pergamonster/419aef09c21ffe8dd1dac971016468d6
Say you wanted to have a 'July 4' tick on both the full axis and the focus axis at all times. I've tried the axis.ticks().push(...arrayOfNumbers) from the above question/answer in my code, but nothing happens. If I do axis.tickValues(arrayOfNumbers) it puts ONLY those numbers as ticks, but I would like to have the auto scaling number PLUS my specific numbers on the axis for the focus area.
Any help would be appreciated.
P.S. Though my chart is similar the about blockBuilder example, I'm using a linearScale not a timeScale if that matters. Thanks

draw a second x-axis where you only specify the extra ticks.
svg.append('g')
.attr('class', 'x-axis-extra')
.attr("transform", `translate(0,${height})`)
.call(d3.axisBottom(xScale).tickValues([2.78,3.14]));
You need a bit of CSS to hide the extra path
.x-axis-extra path { opacity: 0; }

Related

D3.JS Change geometry from rect to path

Hi Stackoverflow folks,
I have a question on D3.js
Basically what I want to achieve is the change of the shape as below.
However,
Once I finished the coding of the first drawing, the coordinate information was as below.
d3.selectAll('body').select('svg').append('rect')
.attr('x', 50)
.attr('y', 50)
.attr('width',150)
.attr('height',150)
.attr('fill','none')
.attr('stroke-width',2)
.attr('stroke','black')
I tried to change the shape as I clicked the rectangle.
So I made a click event. But!
the Parallelogram I want to achieve should be made of 'path'.
How do I change the square shape as I click the square.
Since I want to do 'animate'for the change,
simply toggle off the square and and toggle on Parallelogram is not what I'm trying to make.
Can any please help me?
Or anyone has better solution for this??
Thank you in advance.

Amcharts - Gap between Y-axis title & axis

I've tried setting the autoMargins: true and autoMarginOffset: 50 expecting to see some space between the Y-Axis title & the Y-Axis. However, the whole chart moves towards the right as I increase the autoMarginOffset. I've also tried the marginLeft property.
The width of the plot div is 100%. I tried reducing this to about 80% to see if the above properties take effect. However, I'm not seeing what I expect to with these properties in place.
Could I get some guidance on which property to use please?
There's currently no way to set specific gap between axis title and axis itself, I'm afraid.
The only (and unfortunately ugly) workaround is to put a line break \n and a non-breaking space into the axis title.
"valueAxes": [{
// other axis properties
// ...
"title": "Axis title\n "
}]
On Windows you can type in a non-breaking space using Alt-0160. On Mac: Opt-Space.

NVD3.js multiChart x-axis labels is aligned to multiple lines, but not multiple bars

This question relates to NVD3.js multiChart x-axis labels is aligned to lines, but not bars
I am using NVD3.js multiChart to show multiple lines and multiple bars in the chart. All is working fine, but the x-axis labels is aligned only to the line points, not bars. I want to correctly align labels directly below the bars as it should. But I get this:
As you can see - x-axis (example, 2014-Feb) is not aligned to Bars.
1) How to align x-axis labels to bars and lines at the same time?
2) I need this solution for NVD3.js or how to properly integrate.
I made jsFiddle: http://jsfiddle.net/n2hfN/28/
Thanks!
The problem here is that nv.models.multiChart uses a linear scale for its x-axis, and then when it draws the bars it calls nv.models.multiBar, which uses an ordinal scale with .rangeBands().
You can follow this mess through the source code:
First lets look at multiChart.js
HERE is where it sets the x-scale to be a linear scale.
HERE it calls the nv.models.multiBar model to create the bars.
If we jump over to have a look at multiBar.js
HERE it creates an ordinal scale, and HERE it sets the range of the scale using .rangeBands()
The result is that the ordinal scale used for placing the bars, and the linear scale used for the chart's axis do not align. Here's what the two scales look like on their own if plotted on an axis:
The solution would be to force the chart to render the line graphs and the x-axis in terms of the ordinal scale used by the bars. This would work in your case because the bars and the lines all use the same data for the x-axis. This is very simple to do if you are making your own chart and not relying on nvd3, as I showed in my answer to your previous question HERE. This is extraordinarily complicated to do if you're trying to work within nvd3, and many others have tried and failed to switch out the default scales used by nvd3 charts. Have a look at this issue on the nvd3 github page that has been open since January, 2013 for example.
I've tried a number of approaches myself to reuse the bars' ordinal scale, but with little success. If you want to poke around and try to brute-force it yourself, I can tell you that from my experiments I came closest when using chart.bars1.xScale().copy() to make a copy of the bars' scale, and set its domain and rangeBands. Unfortunately, since the chart's width is computed at render time, and I can't seem to create a hook into the chart.update function, it is impossible to set the rangeBands' extent to the correct values.
In short, if you can't live with the labels being offset, you're probably going to need to code up your own chart without nvd3, or else find a different type of layout for your visualization.
After playing around with the NVD3 v1.7.1 source code with the immensely helpful guidance offered by jshanley's answer, I think I've managed to come up with an answer (perhaps more of a kludge than a good solution).
What I did was to have the x-axis labels align with the bars, and have the line data points align with the bars.
1.1. To align the x-axis label, I shifted the x-axis to the right so that the first label appears underneath the middle of the first bar. I then shifted the last label to the left, so that it appears underneath the middle of the last bar. See code here. The amount to shift by is computed at drawing time using .rangeBand() and saved in a rbcOffset variable (I had to modify multiBar.js for this to work).
1.2. To align the line data points with the bars, a similar shift is also required. Luckily, this part is easy because scatter.js (which is used by line chart) comes with a padData boolean variable that does what we want already. So basically, I just set padData to true and the lines shift to align with the bars, see here.
In order to properly integrate with NVD3 and make everything look good, some additional changes are required. I've forked NVD3 on GitHub so you can see the complete solution there. Of course, contributions are welcome.
I use last solution and it runs. So, you can specify
lines1.padData(true)
in order to align lines too.
Same here, I used the last solution,it worked for me as well. Find the following line in multiChart.js
if(dataLines1.length){
lines1.scatter.padData(true); // add this code to make the line in sync with the bar
d3.transition(lines1Wrap).call(lines1);
}
I encountered the same problem and fixed it with below code:
at lines 7832 and 7878 replace
.attr('transform', function(d,i) { return 'translate(' + x(getX(d,i)) + ',0)'; })
with :
var w = (x.rangeBand() / (stacked && !data[j].nonStackable ? 1 : data.length));
var sectionWidth = availableWidth/(bars.enter()[0].length - 1);
if(bars.enter().length == 2)
return 'translate(' + ((i-1)*w + i*w + (i*(sectionWidth - 2*w))) + ',0)';
else
return 'translate(' + ((i-0.5)*w + i*(sectionWidth - w)) + ',0)';
The first case handles multiple bars case while the second one handles single bar case.
lawry's solution works. Also if using interactive guidelines, you need to shift the interactive line to match the new scale. Modify:
if(useInteractiveGuideline){
interactiveLayer
.width(availableWidth)
.height(availableHeight)
.margin({left:margin.left, top:margin.top})
.svgContainer(container)
.xScale(x);
wrap.select(".nv-interactive").call(interactiveLayer);
//ADD THIS LINE
wrap.select(".nv-interactiveGuideLine")
.attr('transform', 'translate(' + rbcOffset +', ' + 0 + ')' +
'scale(' + ((availableWidth - rbcOffset*2)/availableWidth) + ', 1)');
}
in multiChart.js.

d3 center bar chart's x-axis on arbitrary value

I have a bar chart that is wider than the svg element but, with panning, you're able to drag left and right. The x-axis is time based (I use d3.time.scale()).
After building the chart, I'd like to be able to pan to a specific point on the x-axis. For example, the user may have already panned to a certain point and shut down their session - I'd like to put them back where they were when they return.
I've been looking at doing something like:
d3.selectAll('rect')
.attr('transform', 'translate(' + savedXaxisLocation + ',0)';
Is that the right idea? I'm assuming I also need to do that to the x axis itself?
As you can tell I'm feeling my way around this - I'd be happy to include any other code or screenshots if y'all feel it relevant.
In general, you would have a top-level g element that contains everything else and would translate that. This saves you from having to translate all the elements individually. When you do that, you may want to apply a clipPath to hide the elements that have been panned out of view.
This example should help you to get a better idea of what you can do and how.

Vary font size in tick labels of time axis

I'm plotting time series data based on a regular time axis. For now, I use the default "multi-scale" tick format, so I see dates combined with AM/PM hours on the X axis.
Now: how can I show the dates in a bigger font size, to make them stand out from the hour ticks? All ticks seem to have the same "tick" CSS class, so I can't differentiate between them using CSS...
Any suggestion is appreciated.
D3 doesn't provide anything to distinguish between the different types of ticks generated, but you can check the condition used in generating the labels in setting the styles as well. The code would look something like this.
d3.selectAll(".tick > text")
.style("font-size", function(d) { return d.getHours() == 0 ? 32 : 24; });

Resources