【问题标题】:Svg line not displayed on google charts?谷歌图表上未显示 Svg 线?
【发布时间】:2013-12-05 10:23:56
【问题描述】:

我正在尝试在谷歌折线图顶部添加一条线。

我正在使用 svg 来转换线。如果我不使用google.load('visualization', '1', {packages: ['corechart'], callback: drawChart}); 来画线,那么一条移动的垂直线正在工作,但如果我绘制图表,则不会显示垂直线。

以下是我在谷歌折线图上绘制移动垂直线的代码。

 <!DOCTYPE html>
 <html>
<head>
    <script type="text/javascript" src="http://www.google.com/jsapi?.js"></script>  
    <script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
    <script type="text/javascript" src="http://d3js.org/d3.v2.js"></script>
    <script type="text/javascript" src="underscore.js"></script>
    <style>
        .selection_line
        {
            stroke: rgba(0, 0, 0, 0.6);
            shape-rendering: crispEdges;
        }
    </style>
    <script type="text/javascript">
        $(function() {
            // Handler for .ready() called.
            var graph = d3.select('#graph')
                    .append('svg')
                    .attr('width', '100%')
                    .attr('height', 600);


            var line = graph.append('line')
                    .attr('transform', 'translate(0, 50)')
                    .attr({'x1': 0, 'y1': 0, 'x2': 0, 'y2': 300})
                    .attr('class', 'selection_line');

            var x = 0;

            graph.on('mousemove', function() {
                x = d3.mouse(this)[0];
            });

            var draw = function() {
                line
                        .transition()
                        .duration(18)
                        .attrTween('transform', d3.tween('translate(' + x + ', 50)', d3.interpolateString))
                        .each('end', draw);
            };


            draw();
            google.load('visualization', '1', {packages: ['corechart'], callback: drawChart});

        });

        function drawChart() {
            // Create and populate the data table.
            var data = new google.visualization.DataTable();
            data.addColumn('number', 'x');
            // add an "annotation" role column to the domain column
            data.addColumn({type: 'string', role: 'annotation'});
            data.addColumn('number', 'y');

            // add 100 rows of pseudorandom data
            var y = 50;
            for (var i = 0; i < 100; i++) {
                y += Math.floor(Math.random() * 5) * Math.pow(-1, Math.floor(Math.random() * 2));
                data.addRow([i, null, y]);
            }
            // add a blank row to the end of the DataTable to hold the annotations
            data.addRow([null, null, null]);
            // get the index of the row used for the annotations
            var annotationRowIndex = data.getNumberOfRows() - 1;

            var options = {
                height: 400,
                width: 600
            };

            // create the chart
            var chart = new google.visualization.LineChart(document.getElementById('graph'));


            // draw the chart
            chart.draw(data, options);

        }

    </script>
</head>
<body>
    <div id="graph"></div>
</body>

【问题讨论】:

  • 你想做什么?动画线条的绘制? stackoverflow.com/questions/13893127/…
  • 我正在尝试绘制 svg 线,该线将在谷歌折线图上随鼠标移动
  • 类似this?
  • 谢谢,但我想要的是用鼠标移动垂直线我已经更新了小提琴现在它显示垂直线但它没有用鼠标移动jsfiddle.net/uT7Hc你能帮我吗?跨度>
  • 我仍然不完全确定您的意思。 This?

标签: javascript svg d3.js google-visualization underscore.js


【解决方案1】:

为此,您必须在 Google 图表生成的 SVG 上附加一条线,并根据鼠标指针的位置移动它,忽略 x 组件:

var line = graph.append('line')
                .attr('transform', 'translate(100, 50)')
                .attr({'x1': 0, 'y1': 0, 'x2': 400, 'y2': 0})
                .attr('class', 'selection_line');

graph.on('mousemove', function() {
            line.attr("y1", d3.event.y - 50);
            line.attr("y2", d3.event.y - 50);
        });

完整示例here

【讨论】:

  • 您可能必须在此处使用d3.event.clientY。不幸的是,这因浏览器而异。
  • 关于如何找到线切割图形的点(坐标)的任何想法?
  • intersection library 在这里可能会有所帮助。
  • 是否可以使用这个库找到与谷歌图表的交点?
  • 我不知道。我自己没有使用过那个库。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-07-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多