简体   繁体   English

如何创建交互式d3折线图以在悬停时显示数据图/标签

[英]How to create a interactive d3 line chart to show data plots / labels on hover

Code Fiddle link : http://jsfiddle.net/sedhuait/8eep9kpe/9/ 代码小提琴链接: http : //jsfiddle.net/sedhuait/8eep9kpe/9/

 ![function InitChart() {
     var data = \[
         \[
         1413154800000,
         0\],
         \[
         1413158400000,
         0\],
         \[
         1413162000000,
         0\],
         \[
         1413165600000,
         0\],
         \[
         1413169200000,
         0\],
         \[
         1413172800000,
         0\],
         \[
         1413176400000,
         0\],
         \[
         1413180000000,
         27\],
         \[
         1413183600000,
         37\],
         \[
         1413187200000,
         12\],
         \[
         1413190800000,
         13\],
         \[
         1413194400000,
         67\],
         \[
         1413198000000,
         18\],
         \[
         1413201600000,
         1\],
         \[
         1413205200000,
         0\],
         \[
         1413208800000,
         0\],
         \[
         1413212400000,
         0\],
         \[
         1413216000000,
         0\],
         \[
         1413219600000,
         0\],
         \[
         1413223200000,
         0\],
         \[
         1413226800000,
         0\],
         \[
         1413230400000,
         0\],
         \[
         1413234000000,
         0\],
         \[
         1413237600000,
         0\]
     \],
         data1 = \[
             \[
             1413154800000,
             0\],
             \[
             1413158400000,
             0\],
             \[
             1413162000000,
             0\],
             \[
             1413165600000,
             0\],
             \[
             1413169200000,
             0\],
             \[
             1413172800000,
             0\],
             \[
             1413176400000,
             0\],
             \[
             1413180000000,
             1\],
             \[
             1413183600000,
             5\],
             \[
             1413187200000,
             20\],
             \[
             1413190800000,
             16\],
             \[
             1413194400000,
             8\],
             \[
             1413198000000,
             2\],
             \[
             1413201600000,
             3\],
             \[
             1413205200000,
             2\],
             \[
             1413208800000,
             0\],
             \[
             1413212400000,
             0\],
             \[
             1413216000000,
             0\],
             \[
             1413219600000,
             0\],
             \[
             1413223200000,
             0\],
             \[
             1413226800000,
             0\],
             \[
             1413230400000,
             0\],
             \[
             1413234000000,
             0\],
             \[
             1413237600000,
             0\]
         \];





     data.forEach(function (d) {
         d.time = new Date(d\[0\]);
     });

     data1.forEach(function (d) {
         d.time = new Date(d\[0\]);
     });

     var xmax = d3.max(data, function (d) {
         return d.time;
     }),
         xmin = d3.min(data, function (d) {
             return d.time;
         }),
         ymax = d3.max(data, function (d) {
             return d\[1\];
         }),
         ymin = d3.min(data, function (d) {
             return d\[1\];
         });

     var timeFormat = d3.time.format("%I:%M %p ");




     var vis = d3.select("#visualisation"),
         WIDTH = 1000,
         HEIGHT = 500,
         MARGINS = {
             top: 20,
             right: 20,
             bottom: 20,
             left: 50
         },
         xScale = d3.time.scale().range(\[MARGINS.left, WIDTH - MARGINS.right\]).domain(\[xmin, xmax\]),
         yScale = d3.scale.linear().range(\[HEIGHT - MARGINS.top, MARGINS.bottom\]).domain(\[ymin, ymax\]),
         xAxis = d3.svg.axis()
             .scale(xScale)
             .ticks(7)
             .tickPadding(5)
             .tickFormat(timeFormat),
         yAxis = d3.svg.axis()
             .scale(yScale)
             .orient("left");

     vis.append("svg:g")
         .attr("class", "x axis")
         .attr("transform", "translate(0," + (HEIGHT - MARGINS.bottom) + ")")
         .call(xAxis);
     vis.append("svg:g")
         .attr("class", "y axis")
         .attr("transform", "translate(" + (MARGINS.left) + ",0)")
         .call(yAxis);
     var lineGen = d3.svg.line()
         .x(function (d) {
         return xScale(d.time);
     })
         .y(function (d) {
         return yScale(d\[1\]);
     })
         .interpolate("basis");
     vis.append('svg:path')
         .attr('d', lineGen(data))
         .attr('stroke', '#206a82')
         .attr('stroke-width', 5)
         .attr('fill', 'none');

     vis.append('svg:path')
         .attr('d', lineGen(data1))
         .attr('stroke', 'black')
         .attr('stroke-width', 5)
         .attr('fill', 'none');



 }

 InitChart();][1]

Output should be like this. 输出应该是这样的。 Tooltips should appear only on mouse hover 工具提示仅应在鼠标悬停时出现

实际产量

And I'm new to d3. 我是d3的新手。 I tried to find it out from this tut ( http://www.d3noob.org/2013/01/adding-tooltips-to-d3js-graph.html ). 我试图从此教程中找到它( http://www.d3noob.org/2013/01/adding-tooltips-to-d3js-graph.html )。

But due to .interpolate("basis"); 但是由于.interpolate(“ basis”); , its not getting aligned properly as in the following fiddle http://jsfiddle.net/sedhuait/8eep9kpe/10/ ,如以下小提琴中未正确对齐http://jsfiddle.net/sedhuait/8eep9kpe/10/

Please help me the steps to achieve it. 请帮助我实现它的步骤。

Update 更新

Finally I got it. 终于我明白了。 But still facing issue with interpolate('basis'). 但是仍然面临interpolate('basis')问题。 I want my lines to have smooth edges. 我希望我的线条边缘光滑。 Multiline http://jsfiddle.net/sedhuait/jg4p89c0/ 多行http://jsfiddle.net/sedhuait/jg4p89c0/

Single Line http://jsfiddle.net/sedhuait/772jwy4w/ 单行http://jsfiddle.net/sedhuait/772jwy4w/

1)For a circle to follow the path the below code might help 1)让一个圆圈遵循以下代码可能会有所帮助

                var pathEl = path.node();
                var pathLength = pathEl.getTotalLength();
                console.log("Length" + pathLength);
                var BBox = pathEl.getBBox();
                console.log("BBox" + BBox);
                var scale = pathLength/BBox.width;
                console.log("scale" + scale);
                var offsetLeft = document.getElementById("visualisation").offsetLeft;
                console.log("offset" + offsetLeft);



                vis.on("mousemove", function() {
                var x = d3.event.pageX - offsetLeft-90; 
                var beginning = x, end = pathLength, target;
                while (true) {
                target = Math.floor((beginning + end) / 2);
                pos = pathEl.getPointAtLength(target);
                if ((target === end || target === beginning) && pos.x !== x) {
                break;
                }
                if (pos.x > x)      end = target;
                else if (pos.x < x) beginning = target;
                else                break; //position found

}}); }});

For further queries refer http://bl.ocks.org/duopixel/3824661 有关更多查询,请参见http://bl.ocks.org/duopixel/3824661

2)I think tooltips with hover line will look better. 2)我认为带有悬停线的工具提示会看起来更好。 For that refer the below link 为此,请参考以下链接

http://jsfiddle.net/BvuBV/4/ http://jsfiddle.net/BvuBV/4/

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

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