basic plot graph by javascript,

자바스크립트로 그래프그리기 초간단

 

 

Question: How do I plot a graph of a mathematical function using JavaScript?

Answer: To plot a graph of a function on your webpage, use the canvas element, as shown in the example below. The canvas element is supported in all major browsers: Firefox, Opera, Safari, Google Chrome, and Microsoft Internet Explorer 9 or newer.

Example: Here is a graph of the functions sin x (green) and cos 3x (blue).

The graph has been created using the following JavaScript code:

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 = .5 + .5*canvas.width;  // x0 pixels from left to x=0
 axes.y0 = .5 + .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);
 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=4, 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);
  if (i==iMin) ctx.moveTo(x0+xx,y0-yy);
  else         ctx.lineTo(x0+xx,y0-yy);
 }
 ctx.stroke();
}

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();
}
It is safe to call the draw() function after the entire body of the webpage is loaded. The source code of an HTML page containing the canvas element may look like this:
<!DOCTYPE html>
<html>
<head><title>Canvas code example</title>
<script type="text/javascript">
// JavaScript source code goes here
</script>
</head>
<body onload="draw()">
<canvas id="canvas" width="502" height="108"></canvas>
</body>
</html>

Note: If you omit the DOCTYPE element at the beginning of the page, then the graphics might not show up in IE browsers: without the DOCTYPE element, IE may switch to the backward compatibility mode and treat canvas as an unsupported tag.

경축! 아무것도 안하여 에스천사게임즈가 새로운 모습으로 재오픈 하였습니다.
어린이용이며, 설치가 필요없는 브라우저 게임입니다.
https://s1004games.com

See also:
Mathematical functions
Mathematical constants
Mathematical and special symbols
List of JavaScript Graphics Libraries (at open-libraries.com)

 

[출처] http://www.javascripter.net/faq/plotafunctiongraph.htm

 

 

 

본 웹사이트는 광고를 포함하고 있습니다.
광고 클릭에서 발생하는 수익금은 모두 웹사이트 서버의 유지 및 관리, 그리고 기술 콘텐츠 향상을 위해 쓰여집니다.
번호 제목 글쓴이 날짜 조회 수
21 [UX 디자인] 직관적 디자인(Intuitive Design) file 졸리운_곰 2016.10.08 609
20 [UX 디자인] 천재적 디자인(Genius Design) file 졸리운_곰 2016.10.08 549
19 [UX 디자인] 목표 지향 디자인 (Goal-Directed Design) file 졸리운_곰 2016.10.08 364
18 [UX 디자인] 사용자 중심 디자인(User Centered Design) file 졸리운_곰 2016.10.08 407
17 [UX 디자인] 기능 중심 디자인(Feature Centered Design) file 졸리운_곰 2016.10.08 408
16 [UX 디자인] 인터랙션 디자인의 7가지 접근 방법 졸리운_곰 2016.10.08 555
15 [웹 기획] 성공적인 웹사이트 구축을 위한 9가지 역할 file 졸리운_곰 2016.10.08 487
14 [웹 기획] 웹기획의 기본 - 사용자 경험(UX)의 5가지 계층 file 졸리운_곰 2016.10.08 408
13 [웹 기획] 8) 사용자 행동 방식 - 평균 사용자의 행동 패턴 file 졸리운_곰 2016.10.08 367
12 [웹 기획] 7) 사용자 행동 방식 - F자 형태의 웹 콘텐트 읽기 패턴 file 졸리운_곰 2016.10.08 491
11 [웹 기획] 6) 사용자 행동 방식 -[퀴즈] 사용자는 광고처럼 보이는 것을 무시한다 file 졸리운_곰 2016.10.08 526
10 [웹 기획] 4) 사용자 행동 방식 - 사용자는 광고를 피하고 싶어한다. Q file 졸리운_곰 2016.10.08 346
9 [웹 기획} 3) 웹기획의 기본 - 조직 규모에 따른 웹기획자의 역할 file 졸리운_곰 2016.10.08 451
8 [웹 기획] 2) 웹기획의 기본 - 웹사이트 구축을 위한 조직 형태와 역할 file 졸리운_곰 2016.10.08 641
7 [웹 기획] 1) 웹기획의 기본 - 웹기획자는 누구인가 file 졸리운_곰 2016.10.08 601
6 반응형 웹 기획 file 졸리운_곰 2016.10.08 699
5 한국형웹콘텐츠접근성지침2.1 file 졸리운_곰 2016.10.08 675
4 웹표준 가이드 firefox 사이트 crossbrowsing-v2 file 졸리운_곰 2016.10.08 396
3 홈페이지 평가 방법론 file 졸리운_곰 2014.12.09 529
2 웹사이트 평가 지표 졸리운_곰 2014.12.09 743
대표 김성준 주소 : 경기 용인 분당수지 U타워 등록번호 : 142-07-27414
통신판매업 신고 : 제2012-용인수지-0185호 출판업 신고 : 수지구청 제 123호 개인정보보호최고책임자 : 김성준 sjkim70@stechstar.com
대표전화 : 010-4589-2193 [fax] 02-6280-1294 COPYRIGHT(C) stechstar.com ALL RIGHTS RESERVED