下面我发布了一些代码和评论,用于制作带有标签悬停效果的简单 gRaphael 折线图(包含的脚本文件可以在http://raphaeljs.com和http://g.raphaeljs.com找到)。该代码按应有的方式工作(因此对于其他从 gRaphael 线图开始的人来说是一个很好的例子),但我有一个关于标记函数的文本参数的问题/请求:
照原样,标签将在当前列显示每个点的 Y 值(this.values[i]),但我希望同时显示 X 值和 Y 值(X,Y)。我敢肯定这很简单,但到目前为止我还无法弄清楚如何去做。添加逗号和空格没问题,只是', ' + this.values[i]
,但我不知道如何处理 X 值。this.attr("x")
是我到目前为止最接近的,但那是纸上的 X 坐标,而不是图表 X 轴上的 X 值;-)
谁能帮帮我?
// make tags at every point on the current column
for (var i = 0; i < this.y.length; i++) {
this.tags.push(
// make tag (x, y, text, degree, radius)
paper.tag(this.x, this.y[i], this.values[i], 0, 4).insertBefore(this).attr([{ fill: "#ffffff" }, { fill: this.symbols[i].attr("fill") }])
);
<html>
<head>
<title></title>
<script src="raphael-min.js"></script>
<script src="g.raphael-min.js"></script>
<script src="g.line-min.js"></script>
<script language="JavaScript">
function graphael() {
var paper = Raphael("paper");
var chartwidth = 800;
var chartheight = 300;
var charttopleftx = 20;
var charttoplefty = 0;
// The chart
var linechart = paper.linechart(charttopleftx, charttoplefty, chartwidth, chartheight,
// The X-coordinate sets
[
[0, 1, 2, 3, 4, 5],
[0, 1, 2, 3, 4, 5],
[0, 1, 2, 3, 4, 5]
],
// The Y-coordinate sets
[
[500, 550, 540, 510, 600, 570],
[500, 525, 400, 450, 390, 490],
[500, 425, 500, 430, 650, 425]
],
// Chart config
{ axis: "0 0 1 1", symbol: "circle", smooth: false, axisxstep: 5 });
// The dots
linechart.symbols.attr({ r: 4 });
// The tags
linechart.hoverColumn(
// show
function onmousein() {
this.tags = paper.set(); // creates a set of tags (to be able to hide them all in one operation)
// make tags at every point on the current column
for (var i = 0; i < this.y.length; i++) {
this.tags.push(
// make tag (x, y, text, degree, radius)
paper.tag(this.x, this.y[i], this.values[i], 0, 4).insertBefore(this).attr([{ fill: "#ffffff" }, { fill: this.symbols[i].attr("fill") }])
);
}
}
,
// hide
function onmouseout() {
this.tags.hide();
}
);
}
</script>
</head>
<body onload="graphael()">
<div id='paper' style='width:900;height:320;'></div>
</body>
</html>