簡體   English   中英

如何使用D3更新熱圖中的文本?

[英]How to update text in heatmap using D3?

我已經基於此示例創建了一個D3熱圖,並為其編寫了一個更新函數。 這是基本熱圖的相關代碼:

<!DOCTYPE html>
<html lange = "en">
<head>
    <meta charset="UTF-8">
    <title>Heatmap</title>
    <script type ="text/javascript" src="d3/d3.v3.js"></script>
    <script type ="text/javascript" src="updateHeatmap.js"> </script>
    <style type ="text/css">  

    .btn { 
        display: inline; 
    }


        rect.bordered {
        stroke: #E6E6E6;
        stroke-width:2px;   
      }

      text.mono {
        font-size: 9pt;
        font-family: Consolas, courier;
        fill: #aaa;
      }

      text.axis-workweek {
        fill: #000;
      }

      text.axis-worktime {
        fill: #000;
      }</style>
</head>


<body>

    <div id="n1" class="btn">
        <input name="updateButton" 
               type="button" 
               value="1" 
        />
    </div>
    <div id="n2" class="btn">
        <input name="updateButton" 
               type="button" 
               value="2" 
        />
    </div>



    <div id="chart"></div>
    <script type="text/javascript">
    var margin = {top:50, right:0, bottom:100, left:30},
        width=960-margin.left-margin.right,
        height=430-margin.top-margin.bottom,
        gridSize=Math.floor(width/24),
        legendElementWidth=gridSize*2.665,
        buckets = 10,
        colors = ["#f7fcf0","#e0f3db","#ccebc5","#a8ddb5","#7bccc4","#4eb3d3","#2b8cbe","#0868ac","#084081"],
        days = ["Mo", "Tu", "We", "Th", "Fr", "Sa", "Su"],
    times = ["12am", "1am", "2am", "3am", "4am", "5am", "6am", "7am", "8am", "9am", "10am", "11am", "12am", "1pm", "2pm", "3pm", "4pm", "5pm", "6pm", "7pm", "8pm", "9pm", "10pm", "11pm"];

    var heatmap;
    var legend;

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

    d3.csv("1.csv", function(d){
        return {
            day:+d.day2,
            hour:+d.hour,
            value:+d.per_day_per_hour
            };
        },
        function(error, data){

            console.log(data);

            var colorScale = d3.scale.quantile()
                .domain([0, (d3.max(data, function(d){return d.value;})/2), d3.max(data, function(d){return d.value;})])
                .range(colors);


            var dayLabels = svg.selectAll(".dayLabel")
                .data(days)
                .enter().append("text")
                .text(function (d) {return d; })
                .attr("y", function (d, i){ return i*gridSize;})
                .style("text-anchor", "end")
                .attr("transform", "translate(-6," + gridSize/1.5+")")
                .attr("class", function(d, i) { return ((i>=0 && i<=4) ? "dayLabel mono axis axis-workweek": "dayLabel mono axis"); });

            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")
                .attr("transform", "translate(" + gridSize/2+", -6)")
                .attr("class", function(d, i) { return ((i>=9 && i<= 17) ? "timeLabel mono axis axis-worktime": "timeLabel mono axis"); });

            var heatMap = svg.selectAll(".hour")
                .data(data)
                .enter().append("rect")
                .attr("x", function(d) {return (d.hour) * gridSize;})
                .attr("y", function(d) {return (d.day) * gridSize;})
                .attr("rx", 4)
                .attr("ry", 4)
                .attr("class", "hour bordered")
                .attr("width", gridSize)
                .attr("height", gridSize)
                .style("fill", colors[0]);

            heatMap.transition().duration(1000)
                .style("fill", function(d){ return colorScale(d.value);});

            heatMap.append("title").text(function(d) {return d.value;});

            var legend = svg.selectAll(".legend")
                .data([0].concat(colorScale.quantiles()), function(d) {return d;})
                .enter().append("g")
                .attr("class", "legend");

            legend.append("rect")
                .attr("x", function(d, i){ return legendElementWidth * i;})
                .attr("y", height)
                .attr("width", legendElementWidth)
                .attr("height", gridSize/2)
                .style("fill", function(d, i) {return colors[i]; });

            legend.append("text")
                .attr("class", "mono")
                .text(function(d) {return "≥ "+d.toString().substr(0,4);})
                .attr("x", function(d, i){ return legendElementWidth *i;})
                .attr("y", height+ gridSize);



            d3.select("#n1")
                .on("click", function() {
                    updateHeatmap("1_1.csv");
                });


            d3.select("#n2")
                .on("click", function() {
                    updateHeatmap("1_2.csv");
                });     

        ;       
        }
);
</script>

<script>

</script>

</body>
</html>

上面的代碼與我鏈接到小提琴的小提琴基本上相同, 除了它包括2個按鈕, 並且具有全局聲明的圖例,svg和heatmap變量。

這是我的問題的關鍵,這與我創建的用於加載兩個新CSV的更新功能有關:

function updateHeatmap(x){
    svg.selectAll(".legend").attr("opacity", 0);
    d3.csv(x, function(d){
        return {
            day:+d.day2,
            hour:+d.hour,
            value:+d.per_day_per_hour
            };
        },

        function(error, data){


        colorScale = d3.scale.quantile()
            .domain([0, (d3.max(data, function(d){return d.value;})/2), d3.max(data, function(d){return d.value;})])
            .range(colors);


        var heatMap = svg.selectAll(".hour")
            .data(data)
            .transition().duration(1000)
            .style("fill", function(d){ return colorScale(d.value);});

        heatMap.selectAll("title").text(function(d) {return d.value;});

        var legend = svg.selectAll(".legend")
            .data([0].concat(colorScale.quantiles()), function(d) {return d;})
            .enter().append("g")
            .attr("class", "legend");

        legend.append("rect")
            .attr("x", function(d, i){ return legendElementWidth * i;})
            .attr("y", height)
            .attr("width", legendElementWidth)
            .attr("height", gridSize/2)
            .style("fill", function(d, i) {return colors[i]; });

        legend.append("text")
            .attr("class", "mono")
            .text(function(d) {return "≥ "+d.toString().substr(0,4);})
            .attr("x", function(d, i){ return legendElementWidth *i;})
            .attr("y", height+ gridSize);



        }
    )
}

我設法更新了熱圖方塊的顏色(煙絲),但無法獲得熱圖下方的圖例進行協作,也無法像在初始時那樣在懸停時顯示每個方塊下面的值加載。 我感覺這是因為我的JS非常不整齊(按原樣,我的D3面對它),但不能確定-我認為這可能與我為選擇文本元素選擇適當的語法有關(即,我不確定如何正確執行操作)。

綜上所述此熱圖中的圖例(此處為要點框)未正確更新,並且每個正方形的懸停值未在更新時顯示。 讓人驚訝。 有什么建議么?

我已經更新了示例,以允許在數據集之間切換-這應該有所幫助。

http://bl.ocks.org/tjdecke/5558084

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM