Two pie charts in one flexbox container - flexbox

I'm designing a dashboard in which I want to put two pie charts in one flexbox container ... it shows the second and the one is hidden and sometimes when I hover on the second the first appear on the top of the second ... my code
<section id="pie-charts">
<canvas id="trans-status-chart" width="400" height="300"></canvas>
<canvas id="targeted-gender-chart" width="400" height="300"></canvas>
</section>
My css:
#pie-charts{
width: 100%;
display: flex;
justify-content: space-between;
align-items: center;
}
#trans-status-chart{
width: 45% !important;
height: auto !important;
}
#targeted-gender-chart{
width: 45% !important;
height: auto !important;
}

Chart.js needs an 'exclusive' parent element to ensure that it can size itself correctly. This effectively means you need to wrap all canvas elements in a relatively positioned div.
Here's an example:
new Chart(document.getElementById('trans-status-chart'), {
type: 'pie',
data: {
datasets: [{
data: [43, 20, 44]
}]
},
options: {}
});
new Chart(document.getElementById('targeted-gender-chart'), {
type: 'pie',
data: {
datasets: [{
data: [55, 82, 32]
}]
},
options: {}
});
#pie-charts {
width: 100%;
display: flex;
justify-content: space-between;
align-items: center;
}
#pie-charts>div {
width: 45% !important;
height: auto !important;
position: relative;
}
<script src="https://cdn.jsdelivr.net/npm/chart.js#2.9.3/dist/Chart.min.js"></script>
<section id="pie-charts">
<div><canvas id="trans-status-chart" width="400" height="300"></canvas></div>
<div><canvas id="targeted-gender-chart" width="400" height="300"></canvas></div>
</section>

Related

Mobile menu and Media Query

I have created a mobile version of my site with a hamburger menu.
The close icon and hamburger icon are not responding to media query selectors and neither can be made visible on top of the mobile menu.
.nav-open is to be applied to the .page-header by Javascript to open the mobile menu.
**Html:**
<header class="page-header">
<a href="index.html" class="menu-logo">
<img
src="images/header.footer/logo.png"
alt="Easy Eddy's Tree Service Logo"/>
</a>
<nav class="menu">
<ul class="menu-list">
<li>Home</li>
<li>Our History</li>
<li>About Us</li>
</ul>
</nav>
<button class="menu-btn">
<svg
class="icon-mobile-nav"
name="menu-outline"
</svg>
<svg
class="icon-mobile-nav"
name="close-outline"
</svg>
</button>
**
css:**
.page-header {
position: relative;
}
.menu-btn {
display: none;
border: none;
background: none;
cursor: pointer;
align-self: center;
}
.icon-mobile-nav[name="close-outline"] {
display: none;
}
**Media query css:**
.menu-btn {
display: block;
}
.menu {
background-color: rgb(189, 189, 189);
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100vh;
grid-column: 1 / -1;
/* flex-direction: column; */
transform: translateX(100%);
display: flex;
align-items: center;
justify-content: center;
opacity: 0;
transition: all 0.5s ease-in;
pointer-events: none;
visibility: hidden;
z-index: 1;
}
.nav-open .menu {
opacity: 1;
pointer-events: auto;
visibility: visible;
transform: translateX(0);
}
.nav-open .icon-mobile-nav[name="close-outline"] {
display: block;
}
.nav-open .icon-mobile-nav[id="menu-outline"] {
display: none;
}
}
the selectors at the bottom of media query are having no effect. - those reading .nav-open at the beginning.
Removing z-index from the page header did nothing to change visibility of menu icons
Changed "name" attribute on html and selectors to "title" and "id" - no change
changed the svg elements(which are hidden due to their size) to script-inserted icons- no change
edited .menu-btn style to display: block which only made the hamburger visible all the time(except when the menu was opened.
-changed the svg elements to p elements with text- no change in functionality

Align one element of a container at the top left corner and the other at the bottom right using flexbox?

The element in the image is styled using absolute positioning but when I resize the screen I have to slightly adjust the positioning of both the 'For Sale' element and the '$400,000' element so I am curious as to if there is a way to achieve the same layout using flexbox?
Here is my attempt (https://codepen.io/ob98/pen/eYVPJLJ)
<div class = 'container'>
<p class='item1'>Top Left</p>
<p class='item2'>Bottom Right</p>
</div>
css:
.container{
display: flex;
background: red;
width: 500px;
height: 200px;
color: white;
}
.item1{
align-self: flex-start;
}
.item2{
align-self: flex-end;
justify-self: flex-end; /* If align-self moved this to the bottom of the container vertically, I am thinking that this should move it to the end/right side of the container horizontally, but that is not working */
}
Just went to https://developer.mozilla.org/en-US/docs/Web/CSS/justify-self and saw that "In flexbox layouts, this property is ignored (more about alignment in Flexbox)", so I guess that explains why that is not working but I don't really understand why this property is being ignored... Anyway, is there a way to achieve this layout using flexbox or do I have to stick to absolute positioning? Thanks for any input.
Add to .container the declaration justify-content: space-between, to make both your p go to the container edges, and that's done!
.container {
display: flex;
justify-content: space-between;
background: red;
width: 500px;
height: 200px;
color: white;
}
.item1 {
align-self: flex-start;
}
.item2 {
align-self: flex-end;
justify-self: flex-end;
}
<div class = 'container'>
<p class='item1'>Top Left</p>
<p class='item2'>Bottom Right</p>
</div>
Adding justify-content to container and just using the item2 class like this would help.
.container{
display: flex;
justify-content: space-between;
background: red;
width: 500px;
height: 200px;
color: white;
}
.item2{
align-self: flex-end;
}
Can be the answer but im not sure. When i ran it worked.
.bg-flex {
width: 100vw;
height: 100vh;
background-image: url('https://media.istockphoto.com/photos/the-city-of-london-skyline-at-night-united-kingdom-picture-id1312550959');
display: flex;
flex-direction: column;
}
.for-sale {
height: 50%;
display: flex;
flex-direction: row;
align-items: flex-start;
justify-content: flex-start;
}
.k-400 {
height: 50%;
display: flex;
flex-direction: row;
align-items: flex-end;
justify-content: flex-end;
}
.example {
width: 300px;
height: 100px;
background-color: aqua;
}
<div class="bg-flex">
<div class="for-sale">
<div class="example">
</div>
</div>
<div class="k-400">
<div class="example">
</div>
</div>
</div>

Size of the map when redenring mapbox in React

I trying to use mapbox GL to render a map in React.
I've diveide my screen in 4(2 col/crow) with Grid with the following css:
.container {
font-family: sans-serif;
margin: 0;
display: grid;
grid-template-rows: 1fr 7fr;
grid-template-columns: auto;
gap: 5px;
grid-template-areas: "cr1 cr2"
"cr1 cr4";
height: 100vh;
width: 200vh;
background-color: chartreuse;
}
.table {
grid-area: cr1 ;
background-color: aqua;
display: flex;
justify-content: center;
align-items: center;
}
.tank-select {
grid-area: cr2;
background-color: grey;
}
.map {
grid-area: cr4;
display: flex;
justify-content:space-around;
align-items:stretch;
background-color: blueviolet;
}
i would like to have the map in the box cr4 with the following code:
<div className="container">
<div className="table">test</div>
<div className="tank-select">test2</div>
<div className="map">
<Map />
</div>
</div>
The map is compute this way:
const mapContainerRef = useRef(null)
const [lng, setLng] = useState(5)
const [lat, setLat] = useState(34)
const [zoom, setZoom] = useState(1.5)
// Initialize map when component mounts
useEffect(() => {
const map = new mapboxgl.Map({
container: mapContainerRef.current,
style: 'mapbox://styles/mapbox/streets-v11',
center: [lng, lat],
zoom: zoom,
})
// Add navigation control (the +/- zoom buttons)
map.addControl(new mapboxgl.NavigationControl(), 'top-right')
map.on('move', () => {
setLng(map.getCenter().lng.toFixed(4))
setLat(map.getCenter().lat.toFixed(4))
setZoom(map.getZoom().toFixed(2))
})
// const marker = new mapboxgl.Marker().setLngLat(TankLocationArray).addTo(map)
//
map.on('load', function () {
map.addSource('route', {
type: 'geojson',
data: '../../assets/data/test.geoJSON',
})
map.addLayer({
id: 'route',
type: 'symbol',
source: 'route',
paint: {
'circle-radius': 10,
'circle-color': '#ff0000',
},
})
})
//
// Clean up on unmount
return () => map.remove()
}, []) // eslint-disable-line react-hooks/exhaustive-deps
return (
<div>
<div className="sidebarStyle">
<div>
Longitude: {lng} | Latitude: {lat} | Zoom: {zoom}
</div>
</div>
<div className="map-container" ref={mapContainerRef} />
</div>
With the css:
.map-container {
position:relative;
top: 0;
bottom: 0;
left: 0;
right: 0;
}
.sidebarStyle {
display: inline-block;
position: absolute;
top: 0;
left: 0;
margin: 12px;
background-color: #404040;
color: #ffffff;
z-index: 1 !important;
padding: 6px;
font-weight: bold;
}
When i change the map-container position from relative to absolute, the map would take the whole screen, otherwise it's not visible.
I do not know how to style to get the map to appear only in the box .map(cr4).
thanks for the help
solved the issue using React Map GL library.
works really fine. https://www.npmjs.com/package/react-map-gl

Centering and removing white space from a SVG in an enclosing div

I am facing difficulty in stretching a SVG in the X-axis, so that there is no white space visible on either side. Also, I want the point (marked in red circle in the attached picture) to touch the starting of the second div with background color of black. I have tried changing the viewbox values, but I am not able to achieve the desired result.
The below is my code.
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
html {
font-size: 62.5%
}
body {
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;
}
.example {
width: 100%;
height: 100vh;
}
svg {
width: 100%;
height: 100%;
}
.section-two {
width: 100%;
height: 100vh;
background-color: black;
}
<div class="example">
<svg viewbox="0 0 3000 1750">
<path d="M2987.34,2.895l-2984.74,-0l0,1224.16l671.731,426.733l2313,-426.733l0,-1224.16Z" style="fill:#f8be46;"/>
</svg>
</div>
<div class="section-two">
</div>
enter image description here
For that to happen, you need to do two things:
Make sure that your viewBox is correct. It needs to match the dimensions of your <path>. It currently doesn't.
Add the following attribute to your SVG to turn off the automatic aspect-ratio-preserving scaling that SVG does.
preserveAspectRatio="none"
If we check the bounding box of your path, we get the values:
x: 2.600
y: 2.895
width: 2984.740
height: 1650.893
So that is what your viewBox needs to be set to, if you want the path to touch all four sides of the SVG's parent container.
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
html {
font-size: 62.5%
}
body {
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;
}
.example {
width: 100%;
height: 100vh;
}
svg {
width: 100%;
height: 100%;
}
.section-two {
width: 100%;
height: 100vh;
background-color: black;
}
<div class="example">
<svg viewbox="2.6 2.9 2984.7 1650.9" preserveAspectRatio="none">
<path d="M2987.34,2.895l-2984.74,-0l0,1224.16l671.731,426.733l2313,-426.733l0,-1224.16Z" style="fill:#f8be46;"/>
</svg>
</div>
<div class="section-two">
</div>

3 stacked divs with min-height

I've been scratching my head while thinking about this issue.
I have 3 divs. The top and bottom div have a min-height, the middle div has unknown height (expands by content) but should be centered in the page. The top and bottom div should then fill up the remaining top and bottom space.
http://oi43.tinypic.com/5lb3v5.jpg
I'd like to have a pure HTML/CSS solution. Refactoring the HTML structure is possible..
Thanks
Current HTML structure:
<div id="page">
<div id="top">top div</div
<div id="middle">middle div</div>
<div id="bottom">bottom div</div>
</div>
And CSS:
#page {
min-height: 100%;
height: 100%;
width: 100%;
}
#top, #bottom {
min-height: 17.5%;
height: auto !important; /* Set height to content height but keep it minimum 17.5% */
height: 17.5%; /* Some IE don't understand min-height... */
width: 100%;
}
#middle {
height: auto;
width: 100%;
}
You can use Jquery for that, like this: http://jsfiddle.net/rebeen/8YVzb/
Html:
<div id="page">
<div id="top">top div</div>
<div id="middle">middle div</div>
<div id="bottom">bottom div</div>
</div>
Css:
#page {
min-height: 300px;
}
#top, #bottom {
background: #000;
min-height: 17.5%;
width: 100%;
}
#middle {
background:#555;
height: auto;
width: 100%;
}
Jquery:
$(function(){
var pageHeight = $('#page').height();
var topHeight = $('#top').height();
var bottomHeight = $('#bottom').height();
var middleHeight = $('#middle').height();
if(middleHeight > (pageHeight-(topHeight+bottomHeight)))
$('#middle').css(height, 'auto');
else
$('#top, #bottom').height((pageHeight-middleHeight)/2);
})
The easiest way I see is table display but it depends on wich browser you have to support (http://caniuse.com/#feat=css-table).
http://jsfiddle.net/NukYE/
body,
html {
height: 100%;
}
#page {
display: table;
min-height: 100%;
height: 100%;
width: 100%;
}
#top, #bottom {
display: table-row;
min-height: 17.5%;
height: auto !important; /* Set height to content height but keep it minimum 17.5% */
height: 17.5%; /* Some IE don't understand min-height... */
width: 100%;
}
#middle {
display: table-row;
height: auto;
width: 100%;
}

Resources