Enable resizing of a d3 div element through drag interaction

Query:

I have successfully incorporated a version of a d3.js tree obtained from this source.

In my specific scenario, the tree does not occupy the entire screen but is nicely contained within a div, as it forms only a segment of the dashboard I designed.

Below is the CSS styling for the div housing the tree:

#tree-container {
    border-style: solid;
    border-width: 3px;
    border-color: #b0c4de;
    float:left;
    position:relative;
    top:2px;
    margin-top: 5px;
    margin-left: 10px;
}

My objective is to allow users to resize the div by clicking and dragging the mouse pointer along its border.


My Attempt So Far?

Following advice given in the accepted response posted here:

$('#tree-container').resizable({
    handles: 'n,w,s,e',
    minWidth: 200,
    maxWidth: 400
});

This approach has been unsuccessful, resulting in an error message:

Uncaught TypeError: undefined is not a function 

Even trying this variation:

$('#tree-container').resize({
        handles: 'n,w,s,e',
        minWidth: 200,
        maxWidth: 400
    });

does not generate an error, yet remains ineffective.

The following method also failed to yield results:

$('#tree-container').resize({
        handles: 'n,w,s,e'
    });

Any suggestions on how to address this issue?

Answer №1

Utilizing d3 on its own can help you accomplish this task without the need for jQuery UI.

To achieve this, create a div with absolute positioning inside the resizable div and apply a drag behavior to it.

Check out this jsFiddle example. Move the orange rectangle to resize it.

Below is the code snippet you need:

var resizable = d3.select('#resizable');
var resizer = resizable.select('.resizer');

var dragResize = d3.behavior.drag()
    .on('drag', function() {
        // Calculate resizer position relative to the parent (resizable element)
        x = d3.mouse(this.parentNode)[0];

        // Ensure the width is not negative or too small
        x = Math.max(50, x);

        resizable.style('width', x + 'px');
    })

resizer.call(dragResize);

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

inept java script circle

Each image in the array is superimposed on one another to form the final picture. However, a problem arises when all the images are loaded - the loop continues to run making it extremely difficult to scroll or execute any other animations. function map( ...

How to extract a JavaScript object from an array using a specific field

When dealing with an array of objects, my goal is to choose the object that has the highest value in one of its fields. I understand how to select the value itself: Math.max.apply(Math, list.map(function (o) { return o.DisplayAQI; })) ... but I am unsur ...

Ways to track the number of distinct users visiting a webpage

I am trying to find a reliable method to track unique visitors on my webpage using JavaScript (ReactJS + NodeJS(Express). I want the system to identify each user when they load the page and add their data to the database if they are unique. So far, I have ...

Displaying the scope in HTML from a custom Angular directive: A step-by-step guide

Just getting started with angular js and I have a question. Is there a way to show the values of e.width and e.height in an HTML document? Can I utilize the scope within this context? .directive( "cropping", [function ($scope) { return { rest ...

``Is there a way to display fetched data in a horizontal layout instead of vertical using angularJS?

I'm in need of generating reports using AngularJS and PHP. Desired Output: Spec_ID Bot_Name Color Colorno Screen AN/SN/18 750ML POCO PESCA ROTATION 1 Light Buff Dark Buff Red P ...

Ways to remove a task in ReactJs agendas?

I am currently working on a code to delete an item from a list. However, I have encountered a problem where it is deleting items in a FIFO order instead of according to the index. export default class Todo extends Component{ constructor(){ supe ...

The Relationship Between jQuery's .ajax Method and the Same Origin Policy

I have developed a functional HTML5/JS application intended for use in a vehicle, utilizing the network API provided by the vehicle manufacturer for get/post requests. Now, as I attempt to transition this app to run in a browser for a demonstration, I am ...

Updating the appearance of tooltips in Bootstrap 4 with custom CSS styles

I am trying to customize the tooltip style by changing the background color to white. I have used the following CSS code: HTML code: <th rowspan="2" style="text-align: center">Test <sup><img src="assets/icons/help.p ...

Changing a regular calendar date into the ISO-8601 standard format

Similar Question: How to Convert Date to ISO-8601 Format using Javascript? I am facing an issue with a date in the format of Thu Jul 12 2012 01:20:46 GMT+0530 Is there a way to change it to ISO-8601 format like below? 2012-07-12T01:20:46Z ...

Using Google Analytics to monitor open rates and click-through rates of emails

After carefully reviewing the document, I noticed a unique track code that should be able to assist me. However, I am unsure about how to effectively utilize it in order to obtain the open rates and URL click rates. Regarding open rates: I am currently us ...

Fade transition for background image position in CSS3

I'm working on a unique sprite image to control my slideshow. My goal is to make the controls appear active by changing the background position when a user hovers over them. To achieve this, I've implemented the following CSS transitions: -moz ...

Tips for sending a prompt within a .js document

Within my script.js file, I have a prompt that collects user input (specifically a username), stores it in an array, and then should send that array to the server. I'm struggling with getting the "prompt.submit" section to function properly. Whenever ...

What are the steps to adjusting page margins for Kindle devices?

While converting an HTML page for Kindle, I encountered a problem with multiple CSS files. The Kindle Guide recommends setting the left and right margins to 0 for normal body text in order to prevent content from falling off the edge of the screen or overl ...

Error encountered while using getElementsByTagName in Greasemonkey on a webpage built with extJS, showcasing a basic scenario

Currently, I am experimenting with Greasemonkey on a web-app that constructs the DOM step by step utilizing a substantial amount of Javascript and making use of the extJS library. When there is no Greasemonkey code in operation, the site loads as expected ...

How can Ajax assist in utilizing a JavaScript variable within a Rails call that is made within a JavaScript function?

I'm facing a challenge where I need to update a Ruby on Rails partial within a view using a JavaScript object-defined variable, but I'm struggling to make it work. Despite my research pointing towards using an Ajax call as the only viable solutio ...

Guide for dynamically populating Jqgrid Dropdown depending on another dropdown's data选择如何根

On my screen, I have two dropdowns. One is a standard Razor dropdown and the other is a Jqgrid dropdown. The code for the Razor dropdown looks like this: <div class="col-md-4"> <label for="" class="control-label">Loan Currency</ ...

Dealing with Bootstrap 3 compatibility issues in IE8: Finding solutions for mobile-like behavior

Currently, I am in the process of completing this website at . Most aspects of the site display properly on all standard browsers, however, there seems to be an issue with IE8. It appears as if the layout is behaving like a mobile view, with all containe ...

Display the div element only if the router is active and the navbar is visible

I am attempting to display a div underneath each link in a navbar when the page is active. I am looping through each link using *ngFor. What I am trying to achieve looks like this: https://i.sstatic.net/PpY66.png However, what I am currently getting is ...

Issue with IE/Edge: Scrollbar covering content

I am trying to achieve a layout with a horizontal scrollbar on the root container and vertical scrollbars on sub-containers while preventing any overlapping of content. This setup needs to be compatible with IE, Edge, Chrome, and FF (latest versions). < ...