【发布时间】:2018-01-15 13:50:19
【问题描述】:
我是 Django 和 Web 开发的新手。我被困在一个点上。我需要使用 Javascript 制作图表,并且我在位于 app/templates/graphs/BarGraph.js 的文件中成功完成了该操作,并且我的 HTML 模板文件 analysis.html 也在同一个文件夹中。使用Web server Chrome 运行良好,但是当我尝试使用我的应用程序运行我的 HTML 文件时,它找不到我的 BarGraph.js
analysis.html
{% extends "header.html" %}
{% block content %}
<h3 style="text-align:center;">Graph</h3>
<script type="text/javascript" src= "templates/graphs/BarGraph.js" ></script>
{% endblock %}
我还尝试将所有 JS 代码放入我的 HTML 中,然后它确实处理了代码但没有找到frequent_words.tsv 文件并说
BarGraph.js
// See D3 margin convention: http://bl.ocks.org/mbostock/3019563
var margin = {top: 20, right: 10, bottom: 100, left:50},
width = 700 - margin.right - margin.left,
height = 500 - margin.top - margin.bottom;
var svg = d3.select("body")
.append("svg")
.attr ({
"width": width + margin.right + margin.left,
"height": height + margin.top + margin.bottom
})
.append("g")
.attr("transform","translate(" + margin.left + "," + margin.right + ")");
// define x and y scales
var xScale = d3.scale.ordinal()
.rangeRoundBands([0,width], 0.2, 0.2);
var yScale = d3.scale.linear()
.range([height, 0]);
// define x axis and y axis
var xAxis = d3.svg.axis()
.scale(xScale)
.orient("bottom");
var yAxis = d3.svg.axis()
.scale(yScale)
.orient("left");
d3.tsv("templates/graphs/frequent_words.tsv", function(error,data) {
if(error) console.log("Error: data not loaded!");
data.forEach(function(d) {
d.words = d.words;
d.frequency = +d.frequency; // try removing the + and see what the console prints
console.log(d.frequency); // use console.log to confirm
});
// sort the frequency values
data.sort(function(a,b) {
return b.frequency - a.frequency;
});
// Specify the domains of the x and y scales
xScale.domain(data.map(function(d) { return d.words; }) );
yScale.domain([0, d3.max(data, function(d) { return d.frequency; } ) ]);
svg.selectAll('rect')
.data(data)
.enter()
.append('rect')
.attr("height", 0)
.attr("y", height)
.transition().duration(3000)
.delay( function(d,i) { return i * 200; })
// attributes can be also combined under one .attr
.attr({
"x": function(d) { return xScale(d.words); },
"y": function(d) { return yScale(d.frequency); },
"width": xScale.rangeBand(),
"height": function(d) { return height - yScale(d.frequency); }
})
.style("fill", function(d,i) { return 'rgb(20, 20, ' + ((i * 30) + 100) + ')'});
svg.selectAll('text')
.data(data)
.enter()
.append('text')
.text(function(d){
return d.frequency;
})
.attr({
"x": function(d){ return xScale(d.words) + xScale.rangeBand()/2; },
"y": function(d){ return yScale(d.frequency)+ 12; },
"font-family": 'sans-serif',
"font-size": '13px',
"font-weight": 'bold',
"fill": 'white',
"text-anchor": 'middle'
});
// Draw xAxis and position the label
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis)
.selectAll("text")
.attr("dx", "-.8em")
.attr("dy", ".25em")
.attr("transform", "rotate(-60)" )
.style("text-anchor", "end")
.attr("font-size", "10px");
// Draw yAxis and postion the label
svg.append("g")
.attr("class", "y axis")
.call(yAxis)
.append("text")
.attr("transform", "rotate(-90)")
.attr("x", -height/2)
.attr("dy", "-3em")
.style("text-anchor", "middle")
.text("Trillions of US Dollars ($)");
});
我也尝试了static 路径,但它们也不起作用。请帮忙。谢谢
【问题讨论】:
-
静态文件位于静态目录中,与模板不同。
-
是的,但是当我尝试
static时,我将所有文件放在 {% static js/BarGraph.js%} 目录中。但它也没有工作 -
我不明白你的意思。你把这些放在你的项目结构中的什么地方?您的 STATIC_ROOT、STATIC_URL 和 STATICFILES_DIRS 设置的值是多少?您是在开发还是生产中运行?调试是否开启?您是否设置了任何东西来实际提供文件?
标签: javascript html django