Activate the scrolling feature along the axis of the D3 graph

I'm working on a basic stacked bar chart and I need to incorporate a scroll bar on the axis. Currently, the scroll only appears for the div container due to CSS. Here's the code

However, I'm looking to achieve something similar to this chart with scroll.

<!doctype html>
  <html>
    <head>
      <meta charset="utf-8">
       <link href='https://fonts.googleapis.com/css?family=Open+Sans:400,300,700' rel='stylesheet' type='text/css'>
       <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/font-awesome/4.3.0/css/font-awesome.min.css">
       <script src="https://ajax.goquery.min.js"></script>
       <style>
        .axis path,
        .axis line {
          fill: none;
          stroke: #BDBDBD;
        }
       .axis text {
         font-family: 'Open Sans regular', 'Open Sans';
         font-size: 13px;
       }
       .y.axis{
         direction: ltr;
       }
      .grid .tick {
        stroke: lightgrey;
        opacity: 0.7;
      }
     .grid path {
       stroke-width: 0;
     }
     .rect {
       stroke: lightgrey;
       fill-opacity: 0.6;
     }
     .wrapperDiv {
     Width: 984px;
     height:  35px;
     border: thin solid black;
     #margin-top: 36px;
     #margin-bottom: 48px;
     #margin-right: 20px;
     #margin-left: 0px;
     }
     .divChart {
     float:left;
     font-size:13px;
     color : #424242;
     font-family: 'Open Sans regular', 'Open Sans';
     #border: thin solid white;
     margin-top: -0px;
     #margin-bottom: 48px;
     #margin-right: 150px;
     margin-left: 50px;
     #background-color: lightgrey;
     width: 984px;
     height: 500px;
     #padding: 25px;
     border: thin solid navy;
     #margin: 25px;
     #max-height:500px;
     overflow-y:scroll;
     direction: rtl;
     }
  </style>
  </head>
  <body>
  <div class="divChart" id="wrapper-chart">
     <div id ="chartID" ></div>
  </div>
  <script src="http://d3js.org/d3.v3.min.js"></script><script>  
  <script>       
     var dataset = [{"key":"Completion","values":[{"name":"Module 1","value":0},{"name":"Module 2","value":0},{"name":"Module 3","value":0},{"name":"Module 4","value":0},{"name":"Module 5","value":0},{"name":"Module 6","value":0},{"name":"Module 7","value":0},{"name":"Module 8","value":0.56},{"name":"Module 9","value":13.24},{"name":"Module 10","value":12.66}]},{"key":"NonCompletion","values":[{"name":"Module 1","value":100},{"name":"Module 2","value":100},{"name":"Module 3","value":100},{"name":"Module 4","value":100},{"name":"Module 5","value":100},{"name":"Module 6","value":100},{"name":"Module 7","value":100},{"name":"Module 8","value":99.44},{"name":"Module 9","value":86.76},{"name":"Module 10","value":87.34}]}]; 

     function intChart(chartID, dataset) {

     // code for creating the stacked bar chart goes here

     }  

     $(document).ready(function(){

     intChart("chartID", dataset);

     });


  </script>

If anyone can provide assistance with implementing the scroll on the axis, it would be greatly appreciated. Thank you in advance.

Answer №1

Utilizing D3 alone may not achieve this task. If CSS is to be incorporated, the x-axis positioning must be addressed. Separating the X axis using a distinct DIV and SVG container (non-scrollable) from the rest of the chart is recommended.

I have made adjustments to your code to demonstrate this view here. It's essential to tidy up your code as some non-functional segments contribute to confusion.

The alterations include:

HTML

Incorporated a new DIV (xaxis)

<div  id="wrapper-chart">
  <div class="divChart" id="chartID"></div>
  <div id="xaxis"></div>  
</div>

CSS

Added styling for the new div (similar to divChart but minus scrolling)

#xaxis {
  float: left;
  font-size: 13px;
  color: #424242;
  font-family: 'Open Sans regular', 'Open Sans';
  width: 984px;
  direction: rtl;
}

JS

An additional SVG container for the x-axis. Pay attention to the height attribute.

var xaxis_svg = d3.select('#xaxis')
  .append('svg')
  .attr("width", width + margins.left + margins.right)
  .attr("height", margins.bottom)
  .append('g')
    .attr('transform', 'translate(60,0)');

Attach the x-axis to the container.

  xaxis_svg.append('g')
    .attr('class', 'x axis')
    .attr('transform', 'translate(0,' + 0 + ')')
    .call(xAxis)
    .append("text")
    .attr("y", 10)
    .attr("x", 140)
    .attr("dy", ".30em")
    .text("Percentage of Students");

Trust this guidance proves beneficial.

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

Fixing the issue of a div exceeding the height of its parent div in Tailwindcss

One issue I'm encountering is with my card element, which has a fixed height of h-80 in Tailwind. Typically, I use this card within a grid. However, I recently added a div inside the card that is larger than its parent div, and I want it to scroll ve ...

Terser is causing ng build --prod to fail

When I run ng build --prod on my Angular 7 application (which includes a C# app on the BE), I encounter the following error: ERROR in scripts.db02b1660e4ae815041b.js from Terser Unexpected token: keyword (var) [scripts.db02b1660e4ae815041b.js:5,8] It see ...

increase the font weight of the tooltip to make it stand out

Here is the code I have: $ToolTip = "test"; <td class="<?php echo BP_TBL_NUMBER_DATA_CLASS; ?>" Title="<?php echo $ToolTip;?>" ><?php echo $TotalStudentsCount; ?></td> ...

Tips on redirecting the treeview menu using Material-UI 4

Currently, I am utilizing Material UI 4's Treeview component. However, I am struggling to figure out how to include a parameter that allows me to redirect to other pages when the user clicks on an item. In the past, I relied on a different method. Ch ...

What is causing the content to appear cluttered inside the jQuery Dialog Box?

$(document).ready(function() { $('.something').each(function() { var $link = $(this); $link.click(function() { var $dialog = $('<div></div>') .load($link.att ...

Utilizing a jQuery AJAX request to invoke a web method within a

My goal is to integrate jQuery mobile into an existing ASP.NET webform application. I am currently considering using pure HTML controls to create the jQuery Mobile page. I am aware that when making an AJAX call, I can access code-behind static web methods, ...

How to select specific folders for packaging with asar within an Electron app

I'm currently working on an Electron application and experimenting with using asar to package the node_modules and sources directories, while excluding other directories. However, I've run into an issue where when building the application with a ...

Feeling trapped by the NullPointer Exception bug

I am currently automating the add to cart process on the website "http://www.oyehappy.com" using TestNG POM Structure. I have encountered a NullPointer Exception while handling autosuggestion. The code for my POM Class is as follows: public class productPa ...

What is the process for generating a null result cursor upon publication?

Is it possible to return an empty cursor in Meteor? Meteor.publish('example', function(id) { check(id, Match.Maybe(String)) if (!this.userId) return [] }) Although I want the publication to return an empty result when the user is not lo ...

I'm having trouble getting my HTML page to display appended text. What could be causing the issue?

let mainDiv = document.getElementById("main"); let myParagraph = document.createElement("p"); let myTextNode = document.createTextNode("hello"); myParagraph.append(myTextNode); mainDiv.append(myParagraph); let max = 25; let on ...

JavaScript has thrown an error stating that the function in my jQuery script is undefined

I encountered an issue Uncaught TypeError: undefined is not a function in my jQuery script. Here is the code snippet: <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <link re ...

Change the class name on a RichFaces 4 component to utilize themeroller functionality

Before I dive in, let me mention that I have thoroughly gone through the following content: Stackoverflow:how can i remove the css classes from a richfaces component However, my concern lies with version 4 as opposed to v3.x which was discussed in the re ...

Using jQuery AJAX to Populate a Textbox and Dropdown Menu

I am new to using JQuery AJAX, so I am still learning the syntax. Currently, I am retrieving values from a database and populating a dropdown box. What I would like AJAX to do is fill in three other fields with hardcoded information when a selection is mad ...

Using JavaScript to Transfer Data Between Web Pages

For our landing page, we're planning to incorporate two levels of questions for the respondents. Initially, the respondents will be asked to choose up to four interest tracks using checkboxes. Once they have made their selections, they can proceed to ...

Update the var value based on the specific image being switched using jQuery

I have implemented a jQuery function that successfully swaps images when clicked. However, I am now looking to enhance this functionality by passing a parameter using $.get depending on the image being displayed. Here is the scenario: I have multiple comm ...

What could be causing my Font Awesome icon background to fail to load?

As a newcomer to website design, I am currently learning CSS. I seem to be facing an issue where the background for the first two icons has loaded successfully, but the second one isn't displaying. .bg-instagram { padding: 7px 10px; border-radi ...

gulp-watch does not monitor files that are newly created or deleted

Currently working on a task: gulp.task('assetsInject', function () { gulp.src(paths.index) .pipe(plugins.inject( gulp.src(paths.scripts, {read: false}), { addRootSlash: false } ...

Do the elements only find their rightful position when the window is interacted with?

I've encountered a strange issue with a button in my code that inserts HTML textareas onto the page. Initially, when the button is clicked, everything appears as expected. However, upon subsequent clicks, the textareas start to appear in random places ...

Transmitting Data from a Form using an Ajax Call

After reviewing the instructions and solutions provided in the following discussions: jQuery AJAX submit form submitting a form via AJAX I decided to create a test form to experiment with ajax requests on form submission. However, the expected outcome d ...

The Aurelia application encounters a "Maximum call stack size exceeded" error while trying to bind FullCalendar

I am currently working on setting up a JQuery plugin (FullCalendar) within my Aurelia application, which is built using TypeScript. I am relatively new to web development and just trying to get a basic example up and running. To start off, I utilized this ...