Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

JavaScript D3 Zoom on a chart with axes and circle line graph

Dvdscot

New Coder
Hello,

I am trying to implement Zoom on this example:

I found several examples of zoom, with axes and without. Generally you need to select an area, call zoom there and this zoom is a d3.zoom which then calls another self programmed function where the graph is redrawn.

So I added this:
JavaScript:
            let zoom = d3.zoom()
                .scaleExtent([0.5, 10])
                .extent([[0, 0], [width, height]])
                .on('zoom', NeuerChart);

and this at the bottom
JavaScript:
            svg.append("rect")
                .attr("width", width)
                .attr("height", height)
                .style("fill", "none")
                .style("pointer-events", "all")
                .attr("transform", "translate(" + 100 + "," + 100 + ")")
                .call(zoom);

            function NeuerChart () {
                // recover the new scale
                var newX = d3.event.transform.rescaleX(xScale);
                var newY = d3.event.transform.rescaleY(yScale);
   
                // update axes with these new boundaries
                xAxis.call(d3.axisBottom(newX))
                yAxis.call(d3.axisLeft(newY))
            }

Of course the actual redrawing is missing. I also find examples of defining a viewport or d3.selectall. How does it work?

https://www.d3indepth.com/zoom-and-pan/
 
Maybe that example is too complicated, I found simpler code in this video:


Basically the actual zooming has to be programmed and it is like repeating its own code. First draw the graph, in the zoom function redraw it simpler.
 
Last edited by a moderator:

New Threads

Buy us a coffee!

Back
Top Bottom