score:2
Here is a working example based on your code: http://jsfiddle.net/26sd8uc9/4/
1 - You are right about the .extent, the problem is that you haven't specify the domain for you x2 scale. By adding the following code it works:
x2 = d3.time.scale()
.domain([
parseDate("2014-08-11 05:30:00"),
parseDate("2014-08-12 19:25:00")
])
.nice(d3.time.minute)
.range([0, width]);
And to initialize the circles, you also have to call the brushed event after creating the brush by adding .call(brush.event)
:
// brush slider display
context.append("g")
.attr("class", "x brush")
.call(brush)
.call(brush.event)
.selectAll("rect")
.attr("y", -6)
.attr("height", height2 + 7);
2 - use a variable to keep track where is the current range under the brush, and hide the circles that are not in the range by setting the radius to zero(alternatively you can set the visibility)
var currentRange;
var inRange = function(d) {
if(!currentRange || d.time < currentRange[0] || d.time > currentRange[1] ) {
return 0;
} else {
return 5;
}
}
function brushed() {
currentRange = (brush.empty()? undefined : brush.extent());
x.domain(brush.empty() ? x2.domain() : brush.extent());
focus.select(".x.axis").call(xAxis);
mydots.selectAll(".circle")
.attr("cx", xMap)
.attr("cy", yMap)
.attr("r", inRange); // set radius to zero if it's not in range
console.log(brush.extent())
}
For the scale definition, it will be better to write something like this:
.domain([
d3.min(dataset, function(d){ return d.time; }),
d3.max(dataset, function(d){ return d.time; })
])
In your example, you have to make sure to parse and initialize the data before you do this.
score:3
For your first question, you need to call brush.event
to get the brush to pick up the new extent.
brush = d3.svg.brush()
.x(x)
.extent([config.start, d3.time.day.offset(config.start, config.range)])
.on("brush", brushmove)
.on("brushend", brushend);
gBrush = svg.select('.brush')
.call(brush);
gBrush.call(brush.event);
For your second question, I usually just filter out the data that is outside the brush extent such that I am only drawing the visible data. Here is an example that would be called on your brush move/zoom event:
// update the data so that only visible points are shown
var points= svg.selectAll('.point')
.data(function(d) { return onScreen(d, brush.extent); },
function(d) { return d.id; });
// draw the points that are now onscreen
var pointsEnter = points.enter().append('g').attr('class', 'point');
// remove any points that are now offscreen
points.exit().remove();
// up date the x/y position of your points based on the new axis.
// ... left up to you
It's helpful to have a unique id for the points so that they can just be translated to their new positions as the brush moves instead of having to destroy them and redraw them.
I have an example that uses these techniques at http://bl.ocks.org/bunkat/1962173.
Source: stackoverflow.com
Related Query
- javascript d3.js: initialize brush with brush.extent and stop data from spilling over margin
- Select data from a CSV before loading it with javascript (d3 library)
- How to pull data from mysql database and visualize with D3.JS?
- Date and time transition from data with line
- get data from external json with react and d3
- Javascript d3 pie chart doesn't pull data from JSON file with list of dictionaries
- return tabulate data from brush selection with d3.js
- How to convert D3 data array-of-objects from long-format to wide-format in JavaScript and D3?
- How to plot, in Javascript and on a D3.js line graph, real time time data coming from web socket messages via stomp.js?
- Change format and data of datetime X-axis in a line chart with d3 Javascript library
- JavaScript d3 line graph with ordinal axis and json data
- Get data from mysql with json and plot with D3
- (Javascript with D3) Data from CSV file only being used once and returning undefined or NaN second time
- Same data structure returned from javascript and python behaves differently in d3.js
- consolidating data from a json in a new array and drawing shapes with it using d3.js
- consolidating data from a json in a new array and drawing shapes with it using d3.js
- How to read data generated from REST API in a Javascript file and use it another?
- How do I remove all children elements from a node and then apply them again with different color and size?
- Combining Parent and Nested Data with d3.js
- Updating the data of a pack layout from JSON call and redrawing
- Resizeable SVG elements with JavaScript and D3
- d3.js - max and min value from json data which has array of values
- Visualize date specific data with a line chart in browser with javascript
- Fetching and processing data with semicolon delimiters and no headers
- Initialize element and start dragging with just one click
- Passing JSON data from server to client with Flask
- How to count and convert an array with specific condition javascript
- Create an interactive 3d scatter plot with D3.js and Three.js with json data
- Loading and visualizing data with cal-heatmap
- Groups and lines from nested data
More Query from same tag
- D3.js Updating onclick other element
- User input to create D3 pie chart
- Need to connect two nodes of different circle packed layout in d3
- Setting the circle radius in d3js with a dynamic variable
- Why do I need a 00 time zone offset to display values correctly in d3v4?
- D3 Adding hyperlinks to objects?
- d3 - axis ticks not working unlike tickValues
- how to draw a dated graph using D3.js
- d3.geoPath() vs d3.geo.path()
- Dynamically generate array of objects - Javascript
- D3js: scale an axis on every bar chart
- D3.js v4: Access current DOM element in ES6 arrow function event listener
- Changing loaded data with D3
- D3.js: Scale for circle radius is not displaying the circles
- D3: selecting a <g> in an SVG does not work in Safari
- Scaling the arrowheads according to edge thickness and have them touch the outer edged of the nodes whose size vary
- D3 v4 line chart with JSON dates
- D3 CSV Column Space Parsing
- Multiple Bar charts with fixed y-axis to highlight the different as shown in fig
- SVG clipping in D3 brush doesn't work in Firefox
- How to access i'th shape's attributes using d3
- D3 Force - creating markers for links with dynamic refX to reflect varying node size
- How do I make all my graphs show up on my html page?
- How to have multiple d3 window resize events
- d3.js, how to draw multiple circles at the same (lat,long)?
- How to draw a Bar Chart with time on xAxis and one or more numeric data for yAxis in d3js?
- D3 charts : How to open a radar ticket by clicking on the graph.?
- d3.js and geojson map throws error
- Propagating datum to children on selection data update
- d3.js - How to fix grid `z-index` issue?