请帮帮我。有一个javascript代码,我在其中制作一个画布图。图表的数据是静态的,但我想从.txt文件加载这些数据。你知不知道怎么?谢谢!
<script type="text/javascript">
var canvas ;
var context ;
var Val_max;
var Val_min;
var sections;
var xScale;
var yScale;
// Values for the Data Plot, they can also be obtained from a external file
var data =[0,10,20,40,50,100,120,130,0];
function init() {
// set these values for your data
sections = 12;
Val_max = 130;
Val_min = -40;
var stepSize = 10;
var columnSize = 50;
var rowSize = 50;
var margin = 10;
var xAxis = [" ", "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]
//
canvas = document.getElementById("canvas");
context = canvas.getContext("2d");
context.fillStyle = "#0099ff"
context.font = "20 pt Verdana"
yScale = (canvas.height - columnSize - margin) / (Val_max - Val_min);
xScale = (canvas.width - rowSize) / sections;
context.strokeStyle="#009933"; // color of grid lines
context.beginPath();
// print Parameters on X axis, and grid lines on the graph
for (i=1;i<=sections;i++) {
var x = i * xScale;
context.fillText(xAxis[i], x,columnSize - margin);
context.moveTo(x, columnSize);
context.lineTo(x, canvas.height - margin);
}
答案 0 :(得分:1)
对于加载文件,请尝试:
function request (path, onload) {
var req = new XMLHttpRequest();
req.onload = function () {
return onload(this.responseText);
};
req.open("GET", path, true);
req.send();
}
并使用:
request('http://yousite.com/text.txt',function(response){
//code here
});