0
我有一個.js文件和一個.html文件,我希望html文檔可以從.js文件獲取數據並操作它以生成餅圖。我應該如何繼續?通過html查詢javascript
目前,我在.js文件中有一個函數。我想從html命令中調用它。
編輯: 我有圖表準備在HTML中,並已在js中查詢數據庫。我留下來鏈接它們。我想顯示MSSQL數據庫基於數據值的餅圖
這是我的html文件index12.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<script> </script>
<style>
rect{
stroke-width: 2;
}
.legend{
stroke:black;
opacity:1;
font-size: 12px;
}
text{
font-family: sans-serif;
font-size: 10px;
fill:black;
}
</style>
</head>
<body>
<p>hi how ru</p>
<div id="chart"></div>
<button type="button">Change Content</button>
<script src="ajax.googleapis.com/ajax/libs/jquery/1.12.2/jquery.min.js></script>
<script src="d3/d3.js"></script>
<select id ="slctmodel"></select>
<script>
var dataset;
$(document).ready(function() {
$.getJSON("app.js", success = function (data) {
dataset = data;
});
setTimeout(function() {
/* dataset = [
{ "module": "A", "errors": 50 },
{ "module": "B", "errors": 120 },
{ "module": "C", "errors": 10 },
{ "module": "D", "errors": 200 },
{ "module": "E", "errors": 27 },
{ "module": "F", "errors": 25 },
{ "module": "G", "errors": 40 }
];*/
console.log(dataset);
var width = 1500;
var height = 1500;
var radius = 150;
var legendRectSize = 16;
var legendSpacing = 4;
console.log("hi1");
var color = d3.scale.ordinal()
.domain(["A", "B", "C", "D", "E", "F", "G", ])
.range(["#FFEBAA", "#EEAB79", "#955C52", "#BE4C60",
"#B42E61", "#851362", "#5E0063"]);
var svg = d3.select('#chart')
.append('svg')
.attr('width', width)
.attr('height', height)
.append('g')
.attr('transform', 'translate(' + 850 + ',' + 250+')');
d3.select('#chart svg')
.append("text")
.attr("x", 850)
.attr("y", 30)
.attr("text-anchor", "middle")
.text("PROBABILITY OF FAILURE IN MODULES");
var arc = d3.svg.arc()
.innerRadius(30)
.outerRadius(radius);
var arcOver = d3.svg.arc()
.innerRadius(20)
.outerRadius(radius + 30);
console.log("hi2");
var pie = d3.layout.pie()
.value(function (d) { return d.errors; })
.sort(null);
var path = svg.selectAll('path')
.data(pie(dataset))
.enter()
.append('path')
.attr('d', arc)
.attr('fill', function (d, i) {
return color(d.data.module);
})
.attr("opacity", 1)
.attr("stroke", "black")
.attr("stroke-width", 2)
.on("mouseenter", function (d) {
d3.select(this)
.attr("stroke", "black")
.transition()
.duration(1000)
.attr("d", arcOver)
.attr("stroke-width", 4)
.attr("opacity", 1)
})
.on("mouseleave", function (d) {
d3.select(this).transition()
.attr("d", arc)
.attr("stroke", "black")
.attr("stroke-width", 2)
.attr("opacity", 1);
})
console.log("hi3");
svg.selectAll('text')
.data(pie(dataset))
.enter()
.append('text')
.attr("transform", function (d) {
return "translate(" + arc.centroid(d) + ")";
})
.attr("text-anchor", "middle")
.text(function (d) {
return d.value;
})
.style("pointer-events", "none");
var legend = svg.selectAll('.legend')
.data(color.domain())
.enter()
.append('g')
.attr('class', 'legend')
.attr('transform', function (d, i) {
var height = legendRectSize + legendSpacing;
var offset = height * color.domain().length/2;
var vert = i * height - offset;
return 'translate(' + 250 + ',' + vert + ')';
});
console.log("hi4");
legend.append('rect')
.attr('width', legendRectSize)
.attr('height', legendRectSize)
.style('fill', color)
.style('stroke', color);
legend.append('text')
.attr('x', legendRectSize + legendSpacing)
.attr('y', legendRectSize - legendSpacing)
.text(function (d) { return d; });
console.log("hi5");
}
, 500);
});
</script>
</body>
</html>
和我的APP.JS文件是
var http = require('http'); //connect with http
var sql = require('mssql'); //connect with sql
var express = require('express'); //connect with express
var path = require('path');
var app = express();
var jQ = require('jquery');
env: {
browser: true
}
var Connection = sql.Connection;
var Request = sql.Request;
var a; var recordSet;
var config = {
server: '10.2.13.211', //my IP address - obtained
//server: '.',
database: 'trialdb', //my table is within this
user: 'sa', //windows authentication
password: 'admin123#',
port: 1433 //deafault port number
};
function send404Response(response) {
response.writeHead(404, { "Context-Type": "text/plain" });
response.write("Error 404:Page not found");
response.end();
}
function loadEmployees() {
var dbConn = new sql.Connection(config);
dbConn.connect().then(function() {
var request = new sql.Request(dbConn);
request.query("select * from list").then(function (recordSet) {
// console.log(recordSet);
a = recordSet[1].errors;
return recordSet;
dbConn.close(); //close connection
}).catch(function (err) {
console.log(err);
dbConn.close();
});
}).catch(function (err) {
console.log(err);
});
}
app.use(express.static('D:/d3 project/project_part1/project_part1/'));
app.get('/', function (req, res) {
res.sendFile(path.join(__dirname, '../../index12.html'));
//res.send(recordSet);
});
app.get('/abt', function (req, res) {
res.sendFile(path.join(__dirname, '../../startpage.html'));
});
function onRequest(request, response) {
if (request.method == 'GET' && request.url == '/') {
console.log("user made a request" + request.url);
response.writeHead(200, { "Context-Type": "text/plain" });
loadEmployees();
setTimeout(function() {
// response.write("here is some data");
response.write("hiya" + a);
response.end();
}
, 200);
}
else {
send404Response(response); console.log('error 404');
}
}
http.createServer(onRequest).listen(8888);
console.log('server will run on requset to port 8888');
var server = app.listen(8081);
我的「數據'index12.html文件的jquery部分中的變量具有未定義的值。它應該在app.js文件中具有'recordSet'變量的值
如何繼續操作?
如何在index12.html中調用app.js的函數loadEmployees(),以便我可以存儲函數loadEmployees()返回的值,即index12的'data'變量中的'recordSet'。 html
形成了你的問題,我知道你只是想accces HTML中JS,只寫了''