d3.line.context()方法可讓您在畫布元素的上下文中渲染線。當調用線生成器時,將在當前上下文中渲染線。我們可以使用顏色,描邊,填充等方法自行設置行的上下文。默認值為null。
用法:
d3.line.context(_context);
參數:
- _context:用戶設置的上下文。
返回值:此方法返回當前上下文。
範例1:在此示例中,使用此方法更改顏色,筆觸。
<!DOCTYPE html>
<html>
<meta charset="utf-8">
<head>
<title>d3.line.defined()</title>
</head>
<script src=
"https://cdnjs.cloudflare.com/ajax/libs/d3/4.2.2/d3.min.js">
</script>
<body>
<h1 style="text-align:center;
color:green;">
GeeksforGeeks
</h1>
<center>
<canvas id="gfg" width="200" height="200">
</canvas>
</center>
<script>
var points = [
{x:0, y:0},
{x:1, y:3},
{x:2, y:15},
{x:3, y:8},
{x:4, y:11},
{x:5, y:15},
{x:6, y:20},
{x:7, y:25}];
var xp = d3.scaleLinear().domain([0, 7]).range([25, 200]);
var yp = d3.scaleLinear().domain([0, 25]).range([175, 25]);
var cont = d3.select("#gfg").node().getContext("2d");
var line = d3.line()
.x(d => xp(d.x))
.y(d => yp(d.y))
.context(cont);
line(points);
cont.strokeStyle = "green";
cont.stroke();
</script>
</body>
</html>
輸出:
範例2:在此示例中,更改顏色,使用此方法填充。
<!DOCTYPE html>
<html>
<meta charset="utf-8">
<head>
<title>d3.line.defined()</title>
</head>
<script src=
"https://cdnjs.cloudflare.com/ajax/libs/d3/4.2.2/d3.min.js">
</script>
<body>
<h1 style="text-align:center; color:green;">
GeeksforGeeks
</h1>
<center>
<canvas id="gfg" width="200" height="200"></canvas>
</center>
<script>
var points = [
{x:0, y:0},
{x:1, y:3},
{x:2, y:15},
{x:3, y:8},
{x:4, y:11},
{x:5, y:15},
{x:6, y:20},
{x:7, y:25}];
var xp = d3.scaleLinear().domain([0, 7]).range([25, 200]);
var yp = d3.scaleLinear().domain([0, 25]).range([175, 25]);
var cont = d3.select("#gfg").node().getContext("2d");
var line = d3.line()
.x(d => xp(d.x))
.y(d => yp(d.y))
.context(cont);
line(points);
cont.strokeStyle = "green";
cont.fill();
</script>
</body>
</html>
輸出:
相關用法
- Lodash _.method()用法及代碼示例
- Javascript dataView.getInt16()用法及代碼示例
- Javascript RegExp toString()用法及代碼示例
- Node.js URLSearchParams.has()用法及代碼示例
- JavaScript Math cosh()用法及代碼示例
- HTML DOM isEqualNode()用法及代碼示例
- JavaScript Date toLocaleTimeString()用法及代碼示例
- Node.js crypto.createHash()用法及代碼示例
- Node.js writeStream.clearLine()用法及代碼示例
- Javascript Number isSafeInteger()用法及代碼示例
- HTML DOM History go()用法及代碼示例
- Node.js fs.link()用法及代碼示例
- JavaScript Math random()用法及代碼示例
- JavaScript Math round()用法及代碼示例
- Javascript toString()用法及代碼示例
- Javascript Number.isInteger( )用法及代碼示例
- Javascript Number.isFinite()用法及代碼示例
- Javascript toFixed()用法及代碼示例
注:本文由純淨天空篩選整理自taran910大神的英文原創作品 D3.js line.context() method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。