Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8692481
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T00:19:12+00:00 2026-06-13T00:19:12+00:00

I am trying to draw a heatmap with d3 using data from a csv:

  • 0

I am trying to draw a heatmap with d3 using data from a csv: this is what I have so far

Given a csv file:

row,col,score
0,0,0.5
0,1,0.7
1,0,0.2
1,1,0.4

I have an svg and code as follows:

<svg id="heatmap-canvas" style="height:200px"></svg>

<script>
d3.csv("sgadata.csv", function(data) {
    data.forEach(function(d) { 
    d.score = +d.score;
    d.row = +d.row;
d.col = +d.col;
});
//height of each row in the heatmap
//width of each column in the heatmap
var h = gridSize;
var w = gridSize;
var rectPadding = 60;

$('#heatmap-canvas').empty();

var mySVG = d3.select("#heatmap-canvas")
.style('top',0)
.style('left',0);

var colorScale = d3.scale.linear()
.domain([-1, 0, 1])
.range([colorLow, colorMed, colorHigh]);

rowNest = d3.nest()
.key(function(d) { return d.row; })
.key(function(d) { return d.col; });

dataByRows = rowNest.entries(data);
mySVG.forEach(function(){
var heatmapRow = mySVG.selectAll(".heatmap")
    .data(dataByRows, function(d) { return d.key; })
    .enter().append("g");

    //For each row, generate rects based on columns - this is where I get stuck
    heatmapRow.forEach(function(){
    var heatmapRects = heatmapRow
        .selectAll(".rect")
        .data(function(d) {return d.score;})
        .enter().append("svg:rect")
                    .attr('width',w)
        .attr('height',h)
        .attr('x', function(d) {return (d.row * w) + rectPadding;})
        .attr('y', function(d) {return (d.col * h) + rectPadding;})
        .style('fill',function(d) {
            if(d.score == NaN){return colorNA;}
            return colorScale(d.score);
                 })

})
</script>

My problem is with the nesting. My nesting is based on 2 keys, row first (used to generate the rows) then for each row, there are multiple nested keys for the columns and each of these contain my score.
I am not sure how to proceed i.e. loop over columns and add rectangles with the colors

Any help would be appreciated

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T00:19:13+00:00Added an answer on June 13, 2026 at 12:19 am

    While you could used a subselect (see d3.js building a grid of rectangles) to work with nested data in d3 it’s not really needed in this case. I put together an example using your data at http://jsfiddle.net/QWLkR/2/. This is the key part:

    var heatMap = svg.selectAll(".heatmap")
        .data(data, function(d) { return d.col + ':' + d.row; })
      .enter().append("svg:rect")
        .attr("x", function(d) { return d.row * w; })
        .attr("y", function(d) { return d.col * h; })
        .attr("width", function(d) { return w; })
        .attr("height", function(d) { return h; })
        .style("fill", function(d) { return colorScale(d.score); });
    

    Basically you can just use the row and col to calculate the correct position of the squares in your heatmap.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to plot heatmap in ggplot2 using csv data following casbon's solution in
I'm trying to draw a chart using JFreeChart on Java! The data in which
I'm trying to draw a line using ctx.lineTo in loop. I have small function
I am trying to draw a waveform. I have a precalculated amount of datapoints
I am trying to draw in a web worker using html5 canvas. The worker
I'm trying to draw custom UI (a path in this case) on top of
I'm trying to draw an open rectangle using a Polygon: int[] xPoints = {1,1,3,3};
Backstory: I'm trying to draw as many squares the the screen as possible using
I am trying to draw a Google Line Chart, where each line may have
I am trying to draw a line using the Graphics 2D but then the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.