【问题标题】:Use of .on("mouseover", ...) in d3.js [duplicate]在 d3.js 中使用 .on("mouseover", ...) [重复]
【发布时间】:2018-05-11 11:26:43
【问题描述】:

我正在学习 javascript,我正在尝试使用 .on("mouseovert", ...) 以便在光标位于图形上时获取图形的 x 值。

我的代码如下所示:

// do something as mouseover the graph
svg.select("svg")
    .on("mouseover", alert("mouse on graph"));

结果是:当我打开 html 文件(并加载我的 js 脚本)时出现警告,但悬停图表时什么也没有发生。

脚本中的其他所有内容都可以正常工作。

你知道为什么吗?

非常感谢您抽出宝贵的时间!

这是完整的脚本:

function draw_co2(url) {
    d3.select("svg").remove() //remove the old graph
    // set the dimensions and margins of the graph
    var margin = {
        top: 20,
        right: 20,
        bottom: 30,
        left: 50
    },
    width = 960 - margin.left - margin.right,
    height = 500 - margin.top - margin.bottom;
    // parse the date / time
    var parseTime = d3.timeParse("%Y-%m-%d");

    // Get the data
    d3.json(url, function (error, data) {
        if (error)
            throw ('There was an error while getting geoData: ' + error);
        data.forEach(function (d) {
            d.Date = parseTime(d.Date);
            d.Trend = +d.Trend;
        });

        // set the ranges // Scale the range of the data
        var x = d3.scaleTime().domain([new Date("1960"), new Date("2015")]).range([0, width]);
        var y = d3.scaleLinear()
            .domain([d3.min(data, function (d) {
                        return d.Trend;
                    }) - 1 / 100 * d3.min(data, function (d) {
                        return d.Trend;
                    }), d3.max(data, function (d) {
                        return d.Trend;
                    }) + 1 / 100 * d3.min(data, function (d) {
                        return d.Trend;
                    })])
            .range([height, 0]);

        // define the line
        var valueline = d3.line()
            .x(function (d) {
                return x(d.Date);
            })
            .y(function (d) {
                return y(d.Trend);
            });

        // append the svg obgect to the body of the page
        // appends a 'group' element to 'svg'
        // moves the 'group' element to the top left margin
        var svg = d3.select("#graph_draw").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 + ")");

        //Y Axis label
        svg.append("g")
        .call(d3.axisLeft(y))
        .append("text")
        .attr("fill", "#000")
        .attr("transform", "rotate(-90)")
        .attr("y", 6)
        .attr("dy", "0.71em")
        .attr("text-anchor", "end")
        .text("Carbon dioxide (ppm)");

        // Add the valueline path.
        svg.append("path")
        .data([data])
        .style("opacity", 0)
        .transition()
        .duration(1000)
        .style("opacity", 1)
        .attr("class", "line")
        .attr("d", valueline);

        // Add the X Axis
        svg.append("g")
        .attr("transform", "translate(0," + height + ")")
        .call(d3.axisBottom(x));

        // Add the Y Axis
        svg.append("g")
        .call(d3.axisLeft(y));

        // gridlines in x axis function
        function make_x_gridlines() {
            return d3.axisBottom(x)
            .ticks(10);
        };

        // add the X gridlines
        svg.append("g")
        .attr("class", "grid")
        .attr("transform", "translate(0," + height + ")")
        .call(make_x_gridlines()
            .tickSize(-height)
            .tickFormat(""));

        // do something as mouseover the graph
        svg.select("svg")
        .on("mouseover", alert("mouse on graph"));
    })
}

【问题讨论】:

  • 把它放在一个函数中。使用svg.on("mouseover", function(){alert("mouse on graph")});
  • 我试过了,但它不起作用。现在,当我加载 html 文件时,警报甚至都没有出现。我尝试将svg.select("svg") 替换为svg.select("body") 以查看问题是否不是来自选择,但结果是一样的。
  • 试试d3.select('svg').on("mouseover",function(){alert("mouse on graph")})
  • 有点用。当我悬停图表时会出现一个警报。但是,如果我将警报替换为:// do something as mouseover the graph var count=0; d3.select("svg").on("mouseover",function(){ count += 1; console.log(count);});,结果是控制台中会出现一个数字,但只是周期性地出现,比如每秒而不是每厘秒。也许有悬停在图表上的元素? (对不起,我不知道如何在回答时将代码放在块中)

标签: javascript d3.js mouseevent mouseover


【解决方案1】:

将鼠标悬停用作内联函数

svg.select("svg")
.on("mouseover", function () {
    alert("mouse on graph")
});

【讨论】:

    猜你喜欢
    • 2014-03-31
    • 1970-01-01
    • 2020-03-31
    • 1970-01-01
    • 1970-01-01
    • 2014-07-26
    • 2015-05-02
    • 2013-07-03
    • 2014-03-09
    相关资源
    最近更新 更多