【发布时间】:2021-12-17 05:59:35
【问题描述】:
<canvas id="chart" width="700" height="550"></canvas>
<script>
const canvas = document.getElementById('chart');
const context = canvas.getContext('2d');
/* Draw a line from (fromX, fromY) to (toX, toY) */
function drawLine(fromX, fromY, toX, toY) {
context.beginPath();
context.moveTo(toX, toY);
context.lineTo(fromX, fromY);
context.stroke();
}
/* Draw a text (string) on (x, y) */
function drawText(text, x, y) {
context.fillStyle = 'black';
context.fillText(text, x, y);
}
/* Draw a text and with a line to its right */
function drawLineWithText(text, fromX, fromY, toX, toY) {
drawText(text, fromX - 50, fromY + 3);
drawLine(fromX, fromY, toX, toY);
}
for (var fromY = 50; fromY < 500; fromY += 50, toY = 50 toY < 500; toY += 50, fromX = 70, toX =700) {
drawLineWithText(text, fromX, fromY, toX, toY);
}
</script>
**强文本**
我实际上不知道如何为此做一个 for 语句,我试图玩弄,但只有在我手动完成时才设法使它工作,编写自己的参数示例; drawLineWithText(1000, 20, 30, 100, 30)
【问题讨论】:
-
你想做什么?如果我们确切地知道您想要什么,我们可以做出更好的回应。
-
最终目标是制作条形图,现在我只想制作线条,每条线左侧有一个y值
-
每一步应该怎么做?文字在哪里?
标签: javascript html loops for-loop canvas