Issue with depth perception in a 3D bar graph created with D3.js

I am currently working on a 3D bar chart in D3.js, inspired by the code found here

I'm facing an issue where, when the bars are placed close together and the chart is rotated, the perspective/projection/view behaves strangely, causing some values to appear completely flat at certain angles. I understand that this is likely a perspective problem, but I'm keen on finding a solution.

Here is the code snippet I'm using:

<!DOCTYPE html>
<meta charset="utf-8">
<script src="https://d3js.org/d3.v4.min.js"></script>
<script src="https://unpkg.com/d3-3d/build/d3-3d.min.js"></script>
<body>
<svg width="960" height="500"></svg>
<style type="text/css">
button {
    position: absolute;
    right: 10px;
    top: 10px;
}
</style>
<button>update</button>
<script>
    // JavaScript code here
</script>
</body>

Furthermore, I have encountered an issue while working with two images:

The first image clearly displays the bar and its value https://i.sstatic.net/lL4MZ.png

However, a slight rotation to achieve the second image seems to make it "disappear" (though it might just decrease in height significantly) https://i.sstatic.net/SDp5t.png

Does anyone have insights on what might be causing this?

Answer №1

After receiving helpful comments from @rioV8, I realized that using SVG was not suitable for this scenario.

As a result, I turned to Three.js and created the function makeCube shown below.

function createBox(height, x, lengthX, z, lengthZ){

            height = normalizeHeight(height);
            height = -height; // for visualization purposes

            x = x;
            z = z;

            var geometry = new THREE.CubeGeometry(lengthX, height, lengthZ);
            var material = new THREE.MeshBasicMaterial( { color: 0x00ff00ff, wireframe: false } );
            var mesh = new THREE.Mesh(geometry, material);

            var edgeGeometry = new THREE.Geometry();
            var positions = [];
            positions.push([0, 0, 0]);
            // more position points...

            for(i = 0; i < positions.length; i++)
            {
                edgeGeometry.vertices.push(new THREE.Vector3(positions[i][0]-lengthX/2.0,
                                                          positions[i][1]-height/2.0,
                                                          positions[i][2]-lengthZ/2.0));
            }

            lengthX -= epsilon;
            lengthZ -= epsilon;

            var edgeMaterial = new THREE.LineBasicMaterial( { color: 0x0000000, linewidth: 1 } );
            var edges = new THREE.Line( edgeGeometry , edgeMaterial );
            mesh.add( edges );

            mesh.position.set(x+lengthX/2.0, height/2.0, z+lengthZ/2.0);
            parentObject.add(mesh);

            return mesh;
        } 

Answer №2

The sorting process in d3-3d is currently using incorrect coordinates for arranging cubes. By including the bottom face centroid in the sorting function, this issue can be resolved.

cubes3D.sort = function(a, b){
    var _a = a.faces[5].centroid.z, _b = b.faces[5].centroid.z;
    return _a < _b ? -1 : _a > _b ? 1 : _a >= _b ? 0 : NaN;
};

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

How to delete an element from an array with UnderscoreJS

Here's a scenario: var arr = [{id:1,name:'a'},{id:2,name:'b'},{id:3,name:'c'}]; I'm looking to delete the element with id value of 3 from this array. Is there a method to achieve this without using splice? Perhap ...

Embed information from a MySQL database into an HTML layout

I have a main webpage containing various links (e.g. fist_link, second_link, etc...) When a user clicks on any link, it should open blank_template.html. If the user clicks on fist_link, the template should display the first string from the database table. ...

Setting the default <a-sky> in Aframe: A step-by-step guide

There was a fascinating projection I witnessed where two images were displayed in the sky. [https://codepen.io/captDaylight/full/PNaVmR/][code] Upon opening it, you are greeted with two spheres and a default white background. As you move your cursor over ...

Designing a fixed bottom footer enclosed within a wrapper that expands from the top header to the bottom footer

I have created the basic structure of my webpage using HTML / CSS. However, I now realize that I need a sticky footer that remains at the bottom of the screen with a fixed position. Additionally, I want the main content area, known as the "wrapper," to str ...

Choose the appropriate data type for the class variable (for example, fArr = Uint32Array)

const functionArray: Function = Uint32Array; new fArr(5); The code snippet above is functioning properly. However, TypeScript is throwing a TS2351 error: "This expression is not constructable. Type 'Function' has no construct signatures". I wo ...

Implementing visibility toggles for objects in three.js using a graphical user interface

I am interested in controlling the visibility of objects in my scene through a button on a GUI. The following function currently hides/shows objects individually: g3white.traverse(function(child){child.visible = true;}); g3black.traverse(function(child){ ...

Assigning a value to a cell depending on a specific condition

Currently, I have a column connected to a field known as state. The possible values for this field are either S or L. My objective is to map these values as follows: S => Short, L => Long The binding is structured in the following way: $scope.grid ...

Merging Data with mongoDB

Just starting out with MongoDB, I have developed an app that functions similar to Twitter with followers and following. Here is the schema I am working with: UserSchema : username:'Alex', pass:'salted', likes:'200&ap ...

The component encounters a transformation in which prop values shift to undefined

My component seems to be encountering some instability. The value assigned to "this.state.list" from this.props is being immediately set to "undefined". I'm struggling to comprehend why this is happening. Can anyone spot the issue in the code? this.s ...

Adjusting the value of 'this' within a service using a function

I am a newcomer to Angular and currently delving deeper into its intricacies. Despite my efforts in researching, I have not come across a solution for the issue at hand. My service sets the initial value of this.totalCount = 0; Within my controller, upo ...

Incorporate a background image property using Jquery

Can anyone help me with adding the css background-image:url("xxxxx") property to my code? jQuery('#'+$trackID+' .sc_thumb').attr('src',$thumb_url); jQuery('#'+$trackID+' .sc_container').css('display& ...

What is the significance of the "@" in JXON conversion in JavaScript?

Looking at an XML structure like the one below: <calendar> <month year="2013" num="5"> <day num="1"> </month> </calendar> I decided to convert it to JSON using MDN's JXON Snippet 3. https://developer.moz ...

How can you modify a specific field using a modal form in Django?

Managing a form to track the entry and exit times of individuals in different areas can sometimes lead to discrepancies. For instance, if someone forgets to "leave" an area before entering a new one, a prompt is shown asking for an "estimate" of the time t ...

Revolutionize iPad user experience with seamless HTML5 video integration

What is the most effective method for dynamically embedding HTML5 video to ensure compatibility with the iPad? (using pure Javascript) I'm having difficulty getting this approach to work: <div id="placeholder"><script type="text/javascript" ...

Tips for accessing a value in a multidimensional json array without the key in JavaScript/jQuery?

I'm working with a JSON file that contains a multidimensional array. The array consists of city data at the first level and temperature data at the second level. I'm facing difficulties in dynamically extracting values from the second level. I a ...

Error in Babel-Loader when executing npm run build

Not being a webpack expert, I decided to upgrade from V3 to V4. After fixing the depreciation errors in my webpack config, I hit a roadblock with a syntax error from babel-loader: Module build failed (from ./node_modules/babel-loader/lib/index.js): ...

Using a Dynamic Function to Replace the jQuery Global Object Variable

In a global object, there are settings that need to be updated based on user input. For instance, if "no" is selected for div 1, not only will div-one be hidden, but the variable's value will also change to 0. The problem lies in the dynamic function ...

What is the method for implementing type notation with `React.useState`?

Currently working with React 16.8.3 and hooks, I am trying to implement React.useState type Mode = 'confirm' | 'deny' type Option = Number | null const [mode, setMode] = React.useState('confirm') const [option, setOption] ...

Using an arbitrary object as an argument in a CoffeeScript anonymous function

When I execute the below Coffeescript code: @total = (a, b) -> a + b The resulting compiled Javascript is: (function() { this.total = function(a, b) { return a + b; }; }).call(this); Is there a method in Coffeescript to substitute ...

Shadows on menu buttons transform when clicked using React and CSS

I'm currently working on customizing the styling of a menu using CSS in a project that involves the use of "react-horizontal-scrolling-menu". While I've been successful in styling the menu items with .menu-item:hover & .menu-item:active, I am ...