Converting coordinates to pixel-based fixed positioning in CSS

After creating an animated square pie chart using basic CSS to display it in a grid format, I am now looking to transform the larger squares into a state map grid. Any ideas on how to achieve this?

In my JavaScript code snippet below, I believe there is a way to reorganize the blocks with CSS by translating coordinates to fixed pixel positioning, but I'm struggling to make that connection:

<script id="field_details">
var field_details = {
  "ME" : { "state": "ME", "row": 0, "col": 10 },
  ...

The challenge lies in integrating the existing "field_details" setup which generates a grid from state data onto a canvas and using it to rearrange the grid as per the state map layout.

...

If you have any insights or guidance on this transition, I would greatly appreciate your help!

UPDATE 2

HTML

 </style>
 </head>
<div id="main-wrapper">
... (omitted for brevity)

Javascript

var valfields = d3.keys(field_details);
// Make data accessible by grp key
data.forEach(function(o) {
    grp_vals["grp" + o.agegrp] = o;
});
...

CSS

#charts {
  position: relative;
  margin-top: 20px;
  width: 100%;
}
#map {
  position: relative;
  width: 100%;
  height: 100%
}
... (additional CSS rules omitted for brevity)

Answer №1

It seems like the challenging part of converting the geographical map layout into rows and columns has already been completed by you; therefore, it's just a matter of multiplying the 'row' and 'col' values by the block's width / height and using the outcomes as the pixel position.

I'll be overlooking the sections of your code that aren't pertinent to the inquiry and illustrating the "blocks" as singular elements based on your 'field_details' object:

var field_details = {
ME: {row: 0, col: 10}, WI: {row: 1, col: 5}, VT: {row: 1, col: 9}, NH: {row: 1, col: 10}, WA: {row: 2, col: 0}, ID: {row: 2, col: 1}, MT: {row: 2, col: 2}, ND: {row: 2, col: 3}, MN: {row: 2, col: 4}, IL: {row: 2, col: 5}, MI: {row: 2, col: 6}, NY: {row: 2, col: 8}, MA: {row: 2, col: 9}, OR: {row: 3, col: 0}, NV: {row: 3, col: 1}, WY: {row: 3, col: 2}, SD: {row: 3, col: 3}, IA: {row: 3, col: 4}, IN: {row: 3, col: 5}, OH: {row: 3, col: 6}, PA: {row:...
#map {
  position: relative;
  width: 100%;
  height: 100%
}

.block {
  font-size: 10px;
  width: 30px;
  height: 30px;
  border: 1px solid #000;
  position: absolute;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div id="map">
</div>

(Since this uses absolute positioning it doesn't need to be on a strict grid; that's just a consequence of your row/col data. You could improve the layout by adjusting the positioning of some states a bit -- for example doubling all the row/col values in the data and multiplying by half the width/height would let you nudge FL and TX by half a step left or right, or the northeastern states a half-step south...)

Update

Tying this in to your existing code:

You're already looping through field_details and creating DOM elements, just as in my sample above. So you're most of the way there; just add the positioning while you're at it:

valfields.forEach(function(v,i) {
    // ...
    var div = d3.select("#charts").append("div")
    // ...

    // New code: position the elements:
    div.style("top",(/* math */) + "px"); 
    div.style("left",(/* math */) + "px");          
    // (make sure those divs get a `position:absolute`, and #charts or some other containing element has `position:relative`
    // ...
}

Final update

These exact lines, replacing your existing div.style lines, work for me in your code:

div.style("position", "absolute"); // or set this in CSS on .holder
div.style("top", field_details[v].row * 95 + "px");
div.style("left", field_details[v].col * 95 + "px");

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

Conceal the scrollbar when the expansion panel is in its collapsed state

One issue I am encountering is that the scroll-bar appears when the expansion panel is collapsed, but not when it's expanded. Here are the references: Collapsed Expanded <mat-expansion-panel class="panel"> <mat-expansion-panel-he ...

I am attempting to adjust the color of the active tab, but I seem to be encountering issues in my code. Can anyone help me

The currently active tab should change along with the text inside the box, but it's not working as expected. I'm struggling to find out why. Here is a fiddle to demonstrate my progress so far: var btn1 = document.getElementById("btn1"); va ...

Lock GridView headers when only scrolling vertically

I am facing an issue with my gridview on an aspx page. The gridview is wider than the page itself, so I want the headers of the gridview to scroll horizontally along with the content, while remaining fixed vertically. Can anyone help me achieve this? I hav ...

Is it possible for me to reply to packets that are transmitted to a website?

I'm currently working on a Python program that sends a 'hello' packet to the server and I'm wondering if I can get the server to respond based on that. Here's the code snippet I'm using to send the packet: import requests hL = ...

Select value not updating as expected

I have a select box that is linked to a switch statement in PHP, which changes the order of the results returned from the database based on the value selected in the select box. However, I am facing an issue with my JavaScript not working correctly. Can an ...

Is it possible to deactivate dynamic binding in Vue.js?

I'm curious if there's a way to disable dynamic binding for a specific instance of an attribute that I'm displaying. Imagine I have the following code, utilizing two-way binding: this.$children[0].$data.hits In this scenario, I have a vac ...

Incorporate a vertical scrollbar in the tbody while keeping the thead fixed for smooth vertical scrolling

I'm seeking a solution to implement horizontal and vertical scroll for my table. Currently, the horizontal scroll is working fine, but when trying to add vertical scroll, the table header also moves along with it. Is there a way to keep the table hea ...

Is it possible to trigger a winform function from an HTML page?

Looking for assistance with my WinForm application that includes a C# WebBrowser control. The WebBrowser control loads an HTML page which features a button. I am seeking a way to trigger a function in the WinForm (such as Form1.cs) when this button is cl ...

Divide a string into an array starting from the end

I have a unique phrase. var phrase = "LoremipsumdolorsitametconsectetuadipiscingelitSeddoeiusmodtemporincididuntutlaboreetaliqua"; I am interested in dividing it into chunks of 11 characters starting from the end. Currently, I use: function splitPhrase ...

Having trouble importing font-face in scss

Currently, I am working on incorporating a Google font into my project. This is the desired outcome: https://i.sstatic.net/jIO7q.png However, the actual result is different: https://i.sstatic.net/V2KhW.png The code snippet in App.vue looks like this ...

Guide on sharing Photo Blogs on Tumblr using the "tumblr.js" NodeJS module

I've been using the tumblr.js node module to interact with the Tumblr API, but I'm having trouble understanding what exactly should be included in the "options" when posting on my blog. So far, I've only used this module to retrieve my follo ...

Creating a React button animation with CSS vibes

When I click a button, I want a subtle sparkle to appear and disappear quickly, within half a second. I've thought about using a gif, but it requires a lot of manual artistic work to achieve the desired effect. Is there a way to utilize the Animatio ...

Mastering Protractor's end-to-end control flow and managing time outs

When testing an angular app using protractor, I encountered a strange issue recently. Every now and then, or since a recent update, protractor seems to stall or slow down significantly. After investigating the problem, I discovered that a simple someEleme ...

Create a setup page upon initial login using express.js

Currently, I am implementing Passport.js for user authentication on my express.js application. I aim to display a setup page upon the initial login of the user. Is it possible to achieve this using Passport? ...

Tips for Adjusting the Size of a Search Bar

I've encountered an issue with a form I was developing. I decided to start fresh on an old project that included recreating the Google homepage design. While I aimed for an exact replica, some elements were unnecessary. The search box (form) moved to ...

What could be the reason my code isn't successfully performing addition within the input field?

As a novice, I am practicing by attempting to retrieve a number from a text field, prompting the user to click a button that adds 2 to that number, and then displaying the result through HTML. However, I keep encountering an issue where NaN is returned whe ...

Retrieve the text content from a JavaScript alert using WebDriver

Currently, I am utilizing Selenium WebDriver in C# to enhance my knowledge and create automated tests. I recently encountered a particular scenario that has left me puzzled: Imagine you have a website similar to this one: . When attempting to register wit ...

Choose the appropriate button when two buttons have the identical class

Here is the HTML code I am working with: <a class="action_btn recommend_btn" act="recommend" href="recommend.php"> Recommend </a> Below is my jQuery implementation: $(".action_btn").click(function() { }); However, a problem arises beca ...

Validating object keys

I am dealing with an array of objects and I need to find a way to pass multiple keys in the function checkArray to validate these keys within each object. var test = [ { // Object details here... }, { // Another object details here... } ...

What causes the toggle effect in my jQuery onclick function to alternate between on and off when the initialization is repeated multiple times?

I am facing an issue with my website where icons/buttons trigger a menu when clicked. I need to load more data by adding more buttons, so I tried re-initializing the existing buttons using a jQuery onclick function whenever the number of buttons changes. ...