如何使用画布使用javascript绘制实时图表。我必须使用配置文件绘制上传和下载值 画布代码示例 //JavaScript源代码在这里 函数fun1(x){return Math.sin(x);} 函数fun2(x){return Math.cos(3*x);} 函数绘图(){ var canvas=document.getElementById(“canvas”); if(null==canvas | |!canvas.getContext)返回; var axes={},ctx=canvas.getContext(“2d”); axes.x0=0+.5*canvas.width;//x0像素从左到x=0 //文件写入(axes.x0); axes.y0=0+.5*canvas.height;//从顶部到y=0的y0像素 axes.scale=40;//从x=0到x=1的40个像素 axes.doNegativeX=真; 显示轴(ctx,轴);//可能显示,即仅显示轴:P 真菌图(ctx,轴,fun1,“rgb(11153,11)”,1); 真菌图(ctx,Axis,fun2,“rgb(66,44255)”,2); } 函数funGraph(ctx、轴、函数、颜色、厚度){ 变量xx,yy,dx=0.5,x0=axes.x0,y0=axes.y0,scale=axes.scale; var iMax=数学圆((ctx.canvas.width-x0)/dx); var iMin=axes.doNegativeX?数学圆(-x0/dx):0; ctx.beginPath(); ctx.线宽=厚; ctx.strokeStyle=颜色; 对于(var i=iMin;i
您可以使用如何使用画布使用javascript绘制实时图表。我必须使用配置文件绘制上传和下载值 画布代码示例 //JavaScript源代码在这里 函数fun1(x){return Math.sin(x);} 函数fun2(x){return Math.cos(3*x);} 函数绘图(){ var canvas=document.getElementById(“canvas”); if(null==canvas | |!canvas.getContext)返回; var axes={},ctx=canvas.getContext(“2d”); axes.x0=0+.5*canvas.width;//x0像素从左到x=0 //文件写入(axes.x0); axes.y0=0+.5*canvas.height;//从顶部到y=0的y0像素 axes.scale=40;//从x=0到x=1的40个像素 axes.doNegativeX=真; 显示轴(ctx,轴);//可能显示,即仅显示轴:P 真菌图(ctx,轴,fun1,“rgb(11153,11)”,1); 真菌图(ctx,Axis,fun2,“rgb(66,44255)”,2); } 函数funGraph(ctx、轴、函数、颜色、厚度){ 变量xx,yy,dx=0.5,x0=axes.x0,y0=axes.y0,scale=axes.scale; var iMax=数学圆((ctx.canvas.width-x0)/dx); var iMin=axes.doNegativeX?数学圆(-x0/dx):0; ctx.beginPath(); ctx.线宽=厚; ctx.strokeStyle=颜色; 对于(var i=iMin;i,javascript,charts,Javascript,Charts,您可以使用flot进行此操作,请参见页面上的图形:或示例下的图形。签出。d3js是另一个很好的选项。以下是一些示例。我不能将画布用于实时图形。例如从一个点进行打印(移动到和线到)还有一点。我可以每隔一段时间动态获取值并绘制相同的图吗?是否可以使用画布。是的,这是可能的,但是flot以一种更简单的方式为您做同样的事情。这只是一个建议 <!DOCTYPE html> <html> <head><title>Canvas code example<
flot
进行此操作,请参见页面上的图形:或示例下的图形。签出。d3js是另一个很好的选项。以下是一些示例。我不能将画布用于实时图形。例如从一个点进行打印(移动到和线到)还有一点。我可以每隔一段时间动态获取值并绘制相同的图吗?是否可以使用画布。是的,这是可能的,但是flot
以一种更简单的方式为您做同样的事情。这只是一个建议
<!DOCTYPE html>
<html>
<head><title>Canvas code example</title>
<script type="text/javascript">
// JavaScript source code goes here
function fun1(x) {return Math.sin(x); }
function fun2(x) {return Math.cos(3*x);}
function draw() {
var canvas = document.getElementById("canvas");
if (null==canvas || !canvas.getContext) return;
var axes={}, ctx=canvas.getContext("2d");
axes.x0 = 0 + .5*canvas.width; // x0 pixels from left to x=0
//document.writeln(axes.x0);
axes.y0 = 0 + .5*canvas.height; // y0 pixels from top to y=0
axes.scale = 40; // 40 pixels from x=0 to x=1
axes.doNegativeX = true;
showAxes(ctx,axes); // maybe to show , ie display the axes only :P
funGraph(ctx,axes,fun1,"rgb(11,153,11)",1);
funGraph(ctx,axes,fun2,"rgb(66,44,255)",2);
}
function funGraph (ctx,axes,func,color,thick) {
var xx, yy, dx=.5, x0=axes.x0, y0=axes.y0, scale=axes.scale;
var iMax = Math.round((ctx.canvas.width-x0)/dx);
var iMin = axes.doNegativeX ? Math.round(-x0/dx) : 0;
ctx.beginPath();
ctx.lineWidth = thick;
ctx.strokeStyle = color;
for (var i=iMin;i<=iMax;i++) {
xx = dx*i; yy = scale*func(xx/scale); //scale is the magnification factor
if (i==iMin) ctx.moveTo(x0+xx,y0-yy); //if min ..then start drawing..so moveTo ..else CONTINUE drawing..so lineTo
else ctx.lineTo(x0+xx,y0-yy);
}
ctx.stroke(); //this is used to draw it
}
function showAxes(ctx,axes) {
var x0=axes.x0, w=ctx.canvas.width;
var y0=axes.y0, h=ctx.canvas.height;
var xmin = axes.doNegativeX ? 0 : x0;
ctx.beginPath();
ctx.strokeStyle = "rgb(128,128,128)";
ctx.moveTo(xmin,y0); ctx.lineTo(w,y0); // X axis
ctx.moveTo(x0,0); ctx.lineTo(x0,h); // Y axis
ctx.stroke();
}
</script>
</head>
<body onload="draw()">
<canvas id="canvas" width="502" height="108"></canvas>
</body>
</html>