html点击散点图的点显示一个文本,javascript – D3.js中的简单散点图示例?

这应该让你开始。你可以在

http://bl.ocks.org/2595950看到它。

// data that you want to plot, I've used separate arrays for x and y values

var xdata = [5, 10, 15, 20],

ydata = [3, 17, 4, 6];

// size and margins for the chart

var margin = {top: 20, right: 15, bottom: 60, left: 60}

, width = 960 - margin.left - margin.right

, height = 500 - margin.top - margin.bottom;

// x and y scales, I've used linear here but there are other options

// the scales translate data values to pixel values for you

var x = d3.scale.linear()

.domain([0, d3.max(xdata)]) // the range of the values to plot

.range([ 0, width ]); // the pixel range of the x-axis

var y = d3.scale.linear()

.domain([0, d3.max(ydata)])

.range([ height, 0 ]);

// the chart object, includes all margins

var chart = d3.select('body')

.append('svg:svg')

.attr('width', width + margin.right + margin.left)

.attr('height', height + margin.top + margin.bottom)

.attr('class', 'chart')

// the main object where the chart and axis will be drawn

var main = chart.append('g')

.attr('transform', 'translate(' + margin.left + ',' + margin.top + ')')

.attr('width', width)

.attr('height', height)

.attr('class', 'main')

// draw the x axis

var xAxis = d3.svg.axis()

.scale(x)

.orient('bottom');

main.append('g')

.attr('transform', 'translate(0,' + height + ')')

.attr('class', 'main axis date')

.call(xAxis);

// draw the y axis

var yAxis = d3.svg.axis()

.scale(y)

.orient('left');

main.append('g')

.attr('transform', 'translate(0,0)')

.attr('class', 'main axis date')

.call(yAxis);

// draw the graph object

var g = main.append("svg:g");

g.selectAll("scatter-dots")

.data(ydata) // using the values in the ydata array

.enter().append("svg:circle") // create a new circle for each value

.attr("cy", function (d) { return y(d); } ) // translate y value to a pixel

.attr("cx", function (d,i) { return x(xdata[i]); } ) // translate x value

.attr("r", 10) // radius of circle

.style("opacity", 0.6); // opacity of circle

这样使用:

The d3 test
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值