te')); return $arr; } /* 遍历用户所有主题 * @param $uid 用户ID * @param int $page 页数 * @param int $pagesize 每页记录条数 * @param bool $desc 排序方式 TRUE降序 FALSE升序 * @param string $key 返回的数组用那一列的值作为 key * @param array $col 查询哪些列 */ function thread_tid_find_by_uid($uid, $page = 1, $pagesize = 1000, $desc = TRUE, $key = 'tid', $col = array()) { if (empty($uid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('uid' => $uid), array('tid' => $orderby), $page, $pagesize, $key, $col); return $arr; } // 遍历栏目下tid 支持数组 $fid = array(1,2,3) function thread_tid_find_by_fid($fid, $page = 1, $pagesize = 1000, $desc = TRUE) { if (empty($fid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('fid' => $fid), array('tid' => $orderby), $page, $pagesize, 'tid', array('tid', 'verify_date')); return $arr; } function thread_tid_delete($tid) { if (empty($tid)) return FALSE; $r = thread_tid__delete(array('tid' => $tid)); return $r; } function thread_tid_count() { $n = thread_tid__count(); return $n; } // 统计用户主题数 大数量下严谨使用非主键统计 function thread_uid_count($uid) { $n = thread_tid__count(array('uid' => $uid)); return $n; } // 统计栏目主题数 大数量下严谨使用非主键统计 function thread_fid_count($fid) { $n = thread_tid__count(array('fid' => $fid)); return $n; } ?>javascript - d3 - Change text label when data updates - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - d3 - Change text label when data updates - Stack Overflow

programmeradmin4浏览0评论

Using d3, I created a bar graph that displays the text value of each bar on it. I am toggling between two different data sets through a click event on a button. The data sets change successfully on mousedown, i.e. the bar graphs change in size as they should, but I am unable to change the text labels on the bars. My redrawText function does not do anything, and calling my drawText function again just redraws the data on top of the previous label (as one would expect). I am looking for a way to remove the old label and redraw the label reflecting the new data inside my removeText function.

Here is my drawText function, which is called initially to create the label. 'datachoose' is the name of the variable that is selected to graph the proper data set.

function drawText(dataChoose) {
     new_svg.selectAll("text.dataChoose")
        .data(dataChoose)
        .enter().append("text")
        .text(function(d) {
                return d;
                })
    /* removed some transform code */
     .attr("fill", "white")
     .attr("style", "font-size: 12; font-family: Garamond, sans-serif");
}

Here are the relevant parts of my mousedown event handler, which is used to update the data set and redraw the graph:

.on("mousedown", function() {

        if (dataChoose == data) {
            dataChoose = data2;
        }

        else {
        dataChoose = data;
        }

        redraw(dataChoose);
        redrawText(dataChoose);
    });

and here is my redrawText() function

function redrawText(dataChoose) {

    var new_text = new_svg.selectAll("text.dataChoose")
        .data(dataChoose);

    new_text.transition()
     .duration(1000)
     .text(function(d) {
        return d;
        })

/* removed transform code */

    .attr("fill", "white")
    .attr("style", "font-size: 16; font-family: Garamond, sans-serif");
}

Using d3, I created a bar graph that displays the text value of each bar on it. I am toggling between two different data sets through a click event on a button. The data sets change successfully on mousedown, i.e. the bar graphs change in size as they should, but I am unable to change the text labels on the bars. My redrawText function does not do anything, and calling my drawText function again just redraws the data on top of the previous label (as one would expect). I am looking for a way to remove the old label and redraw the label reflecting the new data inside my removeText function.

Here is my drawText function, which is called initially to create the label. 'datachoose' is the name of the variable that is selected to graph the proper data set.

function drawText(dataChoose) {
     new_svg.selectAll("text.dataChoose")
        .data(dataChoose)
        .enter().append("text")
        .text(function(d) {
                return d;
                })
    /* removed some transform code */
     .attr("fill", "white")
     .attr("style", "font-size: 12; font-family: Garamond, sans-serif");
}

Here are the relevant parts of my mousedown event handler, which is used to update the data set and redraw the graph:

.on("mousedown", function() {

        if (dataChoose == data) {
            dataChoose = data2;
        }

        else {
        dataChoose = data;
        }

        redraw(dataChoose);
        redrawText(dataChoose);
    });

and here is my redrawText() function

function redrawText(dataChoose) {

    var new_text = new_svg.selectAll("text.dataChoose")
        .data(dataChoose);

    new_text.transition()
     .duration(1000)
     .text(function(d) {
        return d;
        })

/* removed transform code */

    .attr("fill", "white")
    .attr("style", "font-size: 16; font-family: Garamond, sans-serif");
}
Share Improve this question asked Feb 18, 2014 at 3:19 Elaine BElaine B 2131 gold badge3 silver badges14 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 12

Without a full example it's hard to see exactly what you're doing but it looks like if the text label is a property of the data you might not be getting the label field correctly.

Here's a simple example of what I think you're describing as your desired behavior: (LINK): http://tributary.io/inlet/9064381

var svg = d3.select('svg');

var data = [{"tag":"abc","val":123}]
    data2 = [{"tag":"ijk","val":321}]

var dataChoose = data;

var myBarGraph = svg.selectAll('rect')
    .data(dataChoose)
    .enter()
    .append('rect')
    .attr({
      x: 160,
      y: 135,
      height: 20,
      width: function(d) { return d.val; },
      fill: 'black'
    });

var updateBarGraph = function() {
    myBarGraph
    .data(dataChoose)
    .transition()
    .duration(1000)
    .attr('width', function(d) { return d.val; })
}

var myText = svg.append('text')
    .data(dataChoose)
    .attr('x', 129)
    .attr('y', 150)
    .attr('fill', '#000')
    .classed('dataChoose', true)
    .text(function(d) { return d.tag })

svg.on("click", function() {
        if (dataChoose == data) {
            dataChoose = data2;
        } else {
        dataChoose = data;
        }
        redrawText();
        updateBarGraph();
    });

function redrawText() {
    myText
    .data(dataChoose)
    .transition()
    .duration(1000)
    .style("opacity", 0)
    .transition().duration(500)
    .style("opacity", 1)
    .text(function(d) { return d.tag })
}

EDIT: The other possibility is that your label transition wasn't working because you need to tell d3 how to do the transition for text (see the updated redrawText).

发布评论

评论列表(0)

  1. 暂无评论