简体   繁体   English

如何设置包含D3热图的svg元素的背景颜色?

[英]How can I set the background color of a svg element which contains a D3 heat map?

I am building a heat map which displays the tweeting activity of an account during the week. 我正在构建一个热图,其中显示了该帐户在一周内的推文活动。 My dataset is made of an array of dictonaries containing the weekday, the hour and the count of how many times the weekday+hour combination repeats. 我的数据集由一系列字典组成,其中包含工作日,小时以及工作日+小时组合重复多少次的计数。 Since usually people stop tweeting during the night, there are some weekday+hour combinations which do not exist in the dataset. 由于通常人们在晚上不发推文,因此数据集中不存在某些平日+小时组合。 My problem is that when I draw the rectangles, I can only draw them for the existing combinations, and the non-existing ones remain blank. 我的问题是,当我绘制矩形时,只能为现有组合绘制它们,而不存在的组合则保持​​空白。 This is a link to the result, hopefully it'll make things clearer: 这是结果的链接,希望可以使事情更清楚:

在此处输入图片说明

As you can see, there are blank spaces between 2:00 and 8:00 am. 如您所见,在凌晨2:00至8:00之间有空白。

I have no idea how to solve this problem, but I thought I could give a white background to the whole heat map before coloring the rectangles containing the existing combinations, so that the non-existing ones become white instead of being blank. 我不知道如何解决这个问题,但是我想我可以给整个热图赋予白色背景,然后再对包含现有组合的矩形着色,以使不存在的矩形变为白色而不是空白。 How can i do this? 我怎样才能做到这一点? If this is not possible, is there another solution to my problem? 如果这不可能,那么我的问题是否还有其他解决方案?

This is my code: 这是我的代码:

[var data = timestamps_final;

var days = \["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"\], times = \["00", "01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23"\];

var margin = { top: 40, right: 50, bottom: 40, left: 50 };

var w = Math.max(Math.min(window.innerWidth, 1000), 500) - margin.left - margin.right - 20,ngridSize = Math.floor(w / times.length), h = gridSize * (days.length + 2);

var svg = d3.select("#heat_map")
        .append("svg")
        .attr("width", w + margin.top + margin.bottom)
        .attr("height", h + margin.left + margin.right)
        .append("g")
        .attr("transform", "translate(" + margin.left + "," + margin.top + ")");

var colours = d3.scaleLinear()
        .range(\["#F0C881", "#F244C7", "#173490",\])
        .domain(\[1, 50, 100\]);

var dayLabels = svg.selectAll(".dayLabel")
        .data(days)
        .enter()
        .append("text")
        .text(function (d) { return d; })
        .attr("x", 0)
        .attr("y", function (d, i) { return i * gridSize; })
        .style("text-anchor", "end")
        .style('fill', 'white')
        .attr("transform", "translate(-6," + gridSize / 1.5 + ")")

var timeLabels = svg.selectAll(".timeLabel")
        .data(times)
        .enter()
        .append("text")
        .text(function (d) { return d; })
        .attr("x", function (d, i) { return i * gridSize; })
        .attr("y", 0)
        .style("text-anchor", "middle")
        .style('fill', 'white')
        .attr("transform", "translate(" + gridSize / 2 + ", -6)");

var rectangles = svg.selectAll("rect")
        .data(data)
        .enter()
        .append("rect")
        .attr("x", function (d) { return d.hour * 36; })
        .attr("y", function (d) { return d.weekday * 36; })
        .attr("width", 36)
        .attr("height", 36)
        .style("stroke-width", 1)
        .style("stroke", "white")
        .style("fill", function (d) { return colours(d.value); });

Thank you for your help. 谢谢您的帮助。

Add a big white rect before you add the week+hour rects 在添加周+小时rect之前先添加一个大白色rect

svg.append("rect")
    .style("fill", "white")
    .attr("width", 36 * 24)
    .attr("height", 36 * 7);

var rectangles = svg.selectAll("rect")
        .data(data)
        .enter()
        .append("rect")
        .attr("x", function (d) { return d.hour * 36; })
        .attr("y", function (d) { return d.weekday * 36; })
        .attr("width", 36)
        .attr("height", 36)
        .style("stroke-width", 1)
        .style("stroke", "white")
        .style("fill", function (d) { return colours(d.value); });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM