HTML5のキャンバス:
Javascriptを:
var hpms = {};
hpms.Graph = function(canvasId) {
this.MARGIN = 30;
this.canvas = document.getElementById(canvasId);
this.ctx = this.canvas.getContext("2d");
this.offsetY = this.canvas.height - this.MARGIN;
};
hpms.Graph.prototype.drawAxis = function(canvas) {
this.ctx.beginPath();
this.ctx.moveTo(0 , this.offsetY);
this.ctx.lineTo(this.canvas.width, this.offsetY);
this.ctx.moveTo(this.MARGIN, this.canvas.height);
this.ctx.lineTo(this.MARGIN, 0);
var dx = (this.canvas.width - 2*this.MARGIN)/3;
var yText = this.offsetY + 3*this.MARGIN/4;
var x = this.MARGIN + dx;
var legend = "1 year";
this.ctx.moveTo(x, 0);
this.ctx.lineTo(x, this.offsetY + this.MARGIN/2);
x -= this.ctx.measureText(legend).width/2;
this.ctx.strokeText(legend, x, yText);
x = this.MARGIN + 2*dx;
this.ctx.moveTo(x, 0);
this.ctx.lineTo(x, this.offsetY + this.MARGIN/2);
legend = "2 years";
x -= this.ctx.measureText(legend).width/2;
this.ctx.strokeText(legend, x, yText);
x = this.MARGIN + 3*dx;
this.ctx.moveTo(x, 0);
this.ctx.lineTo(x, this.offsetY + this.MARGIN/2);
legend = "3 years";
x -= this.ctx.measureText(legend).width/2;
this.ctx.strokeText(legend, x, yText);
var dy = 4;
for(var y = 0; y < this.offsetY; y += 100) {
this.ctx.moveTo(this.MARGIN/2 , this.offsetY - y);
this.ctx.lineTo(this.canvas.width, this.offsetY - y);
legend = "" + y;
this.ctx.strokeText(legend, 0, this.offsetY - y + dy);
}
this.ctx.closePath();
this.ctx.lineCap = "round";
this.ctx.lineWidth = 1;
this.ctx.strokeStyle = "lightgray";
this.ctx.stroke();
};
hpms.Graph.prototype.drawLinear = function(fn, color) {
this.ctx.beginPath();
this.ctx.strokeStyle = color;
this.ctx.lineWidth = 5;
this.ctx.moveTo(this.MARGIN, this.offsetY - fn.b);
var x = this.canvas.width - 2*this.MARGIN;
var y = fn.a*x + fn.b;
this.ctx.lineTo(this.MARGIN + x, this.offsetY - y);
this.ctx.closePath();
this.ctx.stroke();
}
function draw() {
var g = new hpms.Graph("canvas");
g.drawAxis ();
g.drawLinear({a:0.25, b:200}, "darkgray");
g.drawLinear({a:0.80, b: 30}, "rgb(16,65,96)");
}
HTML:
<!doctype html>
<html lang="en-US">
<head>
<meta charset="UTF-8" />
<title>Canvas</title>
<script type="text/javascript" src="canvas.js"></script>
</head>
<body>
<canvas id="canvas" width="1000" height="400"></canvas>
<script type="text/javascript">
draw();
</script>
</body>
</html>
あなたが回帰を実行する方法を見つけたいですか?または曲線を描く方法? – kennytm
@kennytm、正直言って、わかりませんが、私があなたに伝えることは、現時点では、配列の内容の結果であるポリラインを描き、同じデータを表示したいと思います。曲線。 – SPlatten
このようなものを描きたいですか? https://i.stack.imgur.com/ZIljM.pngポリラインを描くためにどのライブラリを使用していますか? – kennytm