尝试选择Google图表SVG,然后附加通过单击并拖动鼠标绘制的线条。
我可以通过var graph = d3.select("body").append("svg")
但是当我尝试选择谷歌图表SVG时,就像我在下面的代码中所做的那样,当我点击并拖动鼠标时,我没有得到任何线条。
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script src="http://d3js.org/d3.v3.min.js"></script>
<style>
line {
stroke: black;
stroke-width: 1px;
}
svg {
border: 1px;
}
</style>
</head>
<body>
<!--<div id="regions_div" style="width: 900px; height: 500px;"></div> -->
<div id="regions_div"></div>
<script>
google.load("visualization", "1", {packages:["geochart"]});
google.setOnLoadCallback(drawRegionsMap);
function drawRegionsMap() {
var data = google.visualization.arrayToDataTable([
['Country', 'Popularity'],
['Germany', 200],
['United States', 300],
['Brazil', 400],
['Canada', 500],
['France', 600],
['RU', 700]
]);
var options = {};
var chart = new google.visualization.GeoChart(document.getElementById('regions_div'));
chart.draw(data, options);
// Draw lines
var line;
var graph = d3.select("svg")
.on("mousedown", mousedown)
.on("mouseup", mouseup);
function mousedown() {
var m = d3.mouse(this);
line = graph.append("line")
.attr("x1", m[0])
.attr("y1", m[1])
.attr("x2", m[0])
.attr("y2", m[1]);
graph.on("mousemove", mousemove);
}
function mousemove() {
var m = d3.mouse(this);
line.attr("x2", m[0])
.attr("y2", m[1]);
}
function mouseup() {
vis.on("mousemove", null);
}
}
</script>
</body>
</html>
答案 0 :(得分:1)
所以我稍微改了一下。我添加了库jquery,所以我可以做一个简单的文档就绪事件。接下来我做的是提取要绘制的数据并将其放入一个名为points的变量中。从那以后我告诉d3使用那些数据,当它发生变化时,做你的事。
HTH
<!DOCTYPE html>
<html>
<head>
<script src="https://code.jquery.com/jquery-2.1.1.js"></script>
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script src="http://d3js.org/d3.v3.min.js"></script>
<style>
line {
stroke: black;
stroke-width: 1px;
}
svg {
border: 1px;
}
</style>
</head>
<body>
<!--<div id="regions_div" style="width: 900px; height: 500px;"></div> -->
<div id="regions_div"></div>
<script>
google.load("visualization", "1", {packages:["geochart"]});
google.setOnLoadCallback(drawRegionsMap);
points = [];
mousedown = false;
function drawRegionsMap() {
var data = google.visualization.arrayToDataTable([
['Country', 'Popularity'],
['Germany', 200],
['United States', 300],
['Brazil', 400],
['Canada', 500],
['France', 600],
['RU', 700]
]);
var options = {};
var chart = new google.visualization.GeoChart(document.getElementById('regions_div'));
chart.draw(data, options);
}
$(function() {
$(document).mousedown(function () {
mousedown = true
});
$(document).mouseup(function () {
mousedown = false
});
$(document).mousemove(function (e) {
if (mousedown) {
points.push({x: e.pageX, y: e.pageY});
drawLines();
}
});
// Draw lines
function drawLines() {
if (points.length > 1) {
var lineUpdate = d3.select("svg").selectAll("line").data(points);
lineUpdate.enter().append("line")
.attr("x1", function (d, i) {
return points[i - 1].x
})
.attr("y1", function (d, i) {
return points[i - 1].y
})
.attr("x2", function (d, i) {
return points[i].x
})
.attr("y2", function (d, i) {
return points[i].y
})
.attr("style", "stroke:rgb(255,0,0);stroke-width:2");
}
}
});
</script>
</body>
</html>