Is there a way to have two SVG files displayed on a single HTML page at the same time

Currently, I am facing an issue with my graphs. I have a line graph and a boxplot, but the boxplot is appearing below the line graph when I want it to be next to it. Any suggestions on how I can achieve this layout? Thank you!

I attempted to use 2 different SVGs, but that approach did not work for me. Here is the code snippet I tried:

<!DOCTYPE html>
<meta charset="utf-8">
<style>

.line {
    fill: none;                 
  stroke: url(#grad);    
  stroke-width: 2px; 
}
.zoom {
  cursor: move;
  fill: none;
  pointer-events: all;
}

</style>
<svg width="900" height="700">  
</svg>
<script src="https://d3js.org/d3.v4.min.js"></script>
<script>

var svg = d3.select("svg"),
    margin = {top: 90, right: 50, bottom: 100, left: 40},
    margin2 = {top: 630, right: 50, bottom: 30, left: 40},
    width = +svg.attr("width") - margin.left - margin.right,
    height = +svg.attr("height") - margin.top - margin.bottom,
    height2 = +svg.attr("height") - margin2.top - margin2.bottom;

...more code 
</script>

//end of line graph 

//beginning of boxplot
<!-- Load d3.js -->
<script src="https://d3js.org/d3.v4.js"></script>

<!-- Create a div where the graph will take place -->
<div id="svg2"></div>

<!-- To use the monochrome scale -->
<script src="https://d3js.org/d3-scale-chromatic.v1.min.js"></script>

<!-- Tooltip style -->
<style>
.tooltip {
  background-color: black;
  border: 1px black solid;
  display: inline-block;
  border-radius: 5px;
  padding: 15px;
  min-width: 1000px;
  text-align: left;
  color: white;  
}
</style>

<script>
// set the dimensions and margins of the graph
var margin = {top: 70, right: 30, bottom: 50, left: 80},
    width = 460 - margin.left - margin.right,
    height = 400 - margin.top - margin.bottom;

...more code, end of boxplot
</script>

You can see the current layout here

Answer №1

HTML consists of two main types of elements - block and inline elements. Block elements, like divs, will push subsequent elements to the next line. On the other hand, inline elements, such as spans, share the same line with the content that follows them unless there is limited space.

If you need a block element to behave like an inline element in terms of line breaks, you can use the CSS property float: left.

Edit: To achieve this, you can assign a class named float-left to the first SVG element and then update one of your <style> tags by adding:

.float-left {
    float: left;
}

This approach allows you to apply the float-left class to any other elements where you wish to replicate this behavior. I hope this explanation helps.

Answer №2

When I tested your code on plnkr, I found that adjusting the width of the elements can make them align next to each other easily. It seems like there is a discrepancy in how you're setting the width for each element (the line graph relies on the HTML element's width while the box plot has its own fixed width at 460). However, if you set both widths to a smaller value, such as 200 initially, you'll notice that they display side by side without any issues.

<svg width="200" height="700">  
.
.
.
<!-- BOXPLOT -->
.
.
.
var margin = {top: 70, right: 30, bottom: 50, left: 100},
    width = 200 - margin.left - margin.right,

I also noticed that you're using different HTML structures for rendering the two svg elements (one within an svg element and the other in a span with id="svg2"), which might be causing some inconsistencies. Ensuring consistency in your HTML structure will give you more control over how the elements are rendered, as they will be treated the same way by default.

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

Learn how to utilize ng-class to assign an ID selector to HTML elements

In my code, I have an icon that is displayed conditionally using ng-class. I am looking to include ID's for both the HTML elements. <i ng-show="ctrl.data.length > 1" ng-class="{'glyphicon glyphicon-chevron-down': !ctrl.isOpen, ...

React Next.js failing to trigger useEffect hook on live application

Recently, in my Next.js/React application, some previously functional pages began to exhibit strange behavior. Specifically, certain Axios requests within a useEffect hook are no longer being sent at all. Additional Information: The problematic file is a ...

I'm experiencing an issue where using .innerHTML works when viewing the file locally, but not when served from a web server. What could be causing this discrepancy?

Utilizing mootool's Request.JSON to fetch tweets from Twitter results in a strange issue for me. When I run the code locally as a file (file:// is in the URL), my formatted tweets appear on the webpage just fine. However, when I serve this from my loc ...

Retrieve jQuery CSS styles from a JSON database

I've been attempting to pass CSS attributes into a jQuery method using data stored in a JSON database. However, it doesn't seem to be functioning as expected. I suspect that directly inputting the path to the JSON variable may not be the correct ...

Why won't my button's text resize?

I am facing an issue with my button's CSS code. In Chrome, when I resize the window to view the mobile layout, the text overflows outside of the div. a.syntra-green-button { background-color: #6faf3c; border: 1px solid #9dcc77; color: w ...

Issues encountered with PHP integration for Bootstrap carousel

I'm facing a challenge with modifying an existing carousel to load data from an SQL database instead of hardcoding it. After successfully fetching the data using PHP, I noticed that the controls of the carousel are not functioning as expected. Upon i ...

Difficulty encountered with React's map function on the example

I am attempting to display data using the example below. Here is my list.js file: import React, { Component } from 'react'; import { Person} from './Person'; export class List extends Component { render() { const persons ...

Incorporating a hyperlink within a list item for an image

Currently, I am attempting to create a clickable image within an unordered list. Although the paragraph is clickable, unfortunately, the image itself is not responding as expected. <ul> <a href="/movies/test/"><li> ...

Change the price directly without having to click on the text field

Our Magento marketplace site is currently utilizing the code below for updating prices. However, we are looking to make the price field editable without requiring a click on the textfield. This means that users should be able to edit the price directly wi ...

Using dialogue boxes instead of alert and prompt functions in jQuery can enhance user interaction and

I need assistance with implementing a dialog feature in fullcalendar. Currently, I am only able to trigger an alert or prompt when clicking on an event. What is the appropriate JavaScript code to display a dialog? Here is my jQuery code: $(document).read ...

What is the most effective way to retrieve the count of users who have logged in within the past three months by utilizing Jquery

I am seeking to retrieve the count of users who have logged in the last three months utilizing a JSON API and Jquery. This is my current progress: $.getJSON('users.json', function(data) { var numberOfUserLogged = 0; var d1 = ...

Tips for configuring a file using javascript and angular.js

I am in need of some assistance. I would like to create a configuration file that stores all the paths for JavaScript files, which can then be dynamically called and included in the index page using Angular.js or JavaScript. The structure of my files is ex ...

What is the best way to pause before executing the next npm command until the current command finishes running?

At first glance, creating an 'init' command seems like a straightforward task. However, as I dive deeper into the process, challenges start to surface. The goal is to develop an 'init' command that assists in preparing a cloned repo fo ...

AngularJS experiencing issues with Bootstrap multiselect functionality

I am currently integrating bootstrap-multiselect into my AngularJS application. To do this, I've included the following scripts in my master page (index.html). <script src="/bower_components/jquery/dist/jquery.min.js"></script> <scrip ...

Bootstrap allows for word wrapping on either one or three lines

I am currently utilizing Bootstrap 3 for my website, and I have a sentence composed of three parts at a certain point on the page. For example, it might read "Bud Spencer walks with Terence Hill." where X="Bud Spencer", Y="walks with", Z="Terence Hill." T ...

Guide to creating a dictionary array on-the-fly:

I have been working on updating years to make it more dynamic by using the starting year (2010) and ending year (2018). Originally, I used a for loop to address this, but I am curious if there is a more efficient way to refactor years. Current arrangemen ...

Unexpected request causes a dilemma during the karma test for an Angular directive

Let's discuss a straightforward directive: 'use strict'; angular .module('app') .directive('ngEmailMask', ngEmailMask); function ngEmailMask() { var directive = { replace: true, restrict: 'EA', ...

create a division in the organization of the identification numbers

Is there a way to always change pages when the id changes in a foreach loop to separate the printed pages? Take a look at this code snippet: var data = [ {Id: "552", valor: "50.00", Descricao: "Fraldas", }, {Id: "552", valor: "35.00", Descrica ...

"Implementing jQuery to dynamically set the href attribute for a link when

My website features a tabbed menu that displays content from various WordPress categories including Cars, Trucks, and Buses. The active tab is randomly selected each time the page is reloaded. However, there seems to be an issue with the "View More" button ...

Pure CSS tab system that prevents label propagation using anchors

I am in the process of creating a tab system using only CSS with the help of the :target and :checked pseudo classes. However, I have encountered an issue where an anchor inside the label is not triggering the :checked. When clicking on the anchor, the :c ...