简体   繁体   English

如何同时使用Express和Ampersand?

[英]How do I use Express and Ampersand simultaneously?

I'm creating a D3 app that's going to display some simple data using an Express server, and Ampersand on the client side. 我正在创建一个D3应用程序,该应用程序将使用Express服务器和客户端上的与号来显示一些简单数据。 Eventually, I'll want to send the data to the browser dynamically, as the entire dataset could get very large. 最终,由于整个数据集可能变得非常大,因此我想将数据动态发送到浏览器。 For now, though, I'm having trouble getting even the simplest of things working. 不过,就目前而言,即使是最简单的事情也无法解决。

I've set up a simple Express server, and it's rather simple: 我已经设置了一个简单的Express服务器,它非常简单:

// Boilerplate express code 
var express = require('express'); 
var app = express(); 
var path = require('path'); 

// Make the Public directory available to the browser 
app.use(express.static(__dirname + '/public')); 

app.get('/', function(req, res) { 
  res.sendFile('d3.html', {'root': path.join(__dirname, '/views') }); 
}); 

// Listen on port 3000 
app.listen(3000); 

That works rather well. 效果很好。 When I visit the page, it loads a blank D3 bar graph (just the axis), but shows no data. 当我访问该页面时,它会加载一个空白的D3条形图(仅是轴),但没有显示任何数据。 This is because I've not sent the data to the browser, and, as I understand it, this needs to be done using either AJAX, jQuery, or something else. 这是因为我尚未将数据发送到浏览器,据我所知,这需要使用AJAX,jQuery或其他方式完成。 I'm not sure how to proceed with this. 我不确定如何进行此操作。

This is in my d3.html; 这是在我的d3.html中; it reads its data from data.tsv, which I've placed in the public folder of my Express server: 它读取data.tsv它的数据,我已经放置在public我的Express服务器的文件夹:

d3.tsv("data.tsv", type, function(error, data) {
  x.domain(data.map(function(d) { return d.letter; }));
  y.domain([0, d3.max(data, function(d) { return d.frequency; })]);

This is data.tsv : 这是data.tsv

letter  frequency
A .08167
B .01492
C .02782
D .04253
E .12702
F .02288
G .02015
H .06094
I .06966
J .00153
K .00772
L .04025
M .02406
N .06749
O .07507
P .01929
Q .00095
R .05987
S .06327
T .09056
U .02758
V .00978
W .02360
X .00150
Y .01974
Z .00074

How do I pass the data to the browser so that d3.html can read in the data? 如何将数据传递到浏览器,以便d3.html可以读入数据? I don't care if the data is in data.tsv or not; 我不在乎数据是否在data.tsv I just need to send the equivalent data somehow. 我只需要以某种方式发送等效数据。 I'm not familiar with it, but I have a hunch that Ampersand can handle the requesting/receiving of this data, but I'm not sure how. 我不熟悉它,但是我有一种直觉,即“&”号可以处理该数据的请求/接收,但是我不确定如何处理。

You shouldn't need to use ampersand for this specific example, in fact, I wouldn't use it at all for this specific problem (and I'm an ampersand author). 对于这个特定的示例,您不需要使用&符,实际上,对于这个特定的问题,我根本不会使用它(而且我是&符的作者)。

What you're trying to do is load the data.tsv file off the server, and get the data in to d3. 您要做的是从服务器上加载data.tsv文件,并将数据导入d3。 Normally you'll grab data from a server from the browser with an ajax request (as you identified). 通常,您将通过ajax请求(如您所确定的)从浏览器中获取服务器中的数据。 And in fact, d3 is already doing that for you. 实际上,d3已经为您做到了。 That d3.tsv function you're calling is trying to load the data.tsv file from the server and parse it for you. 您正在调用的d3.tsv函数正在尝试从服务器加载data.tsv文件并为您解析。

Now, this is obviously not working for you for some reason, so things to look at: 现在,由于某些原因,这显然对您不起作用,因此需要注意以下事项:

  1. If you visit http://localhost:3000/data.tsv , can you see the data in your browser? 如果访问http:// localhost:3000 / data.tsv ,可以在浏览器中看到数据吗? This checks that you have express + express static setup to properly serve the data. 这将检查您是否具有Express + Express静态设置以正确提供数据。

  2. If you open up the d3.html page and open your browser developer tools, do you see any errors? 如果打开d3.html页面并打开浏览器开发人员工具,是否看到任何错误?

  3. This code is where you are grabbing the data from the server and parsing it. 此代码是您从服务器获取数据并进行解析的地方。 Try logging the error to the console and check the developer tools logs to see if there's an error. 尝试将error记录到控制台,并检查开发人员工具日志以查看是否存在错误。 Maybe d3 is struggling to grab or parse the data? 也许d3正在努力获取或解析数据? You could log the data too and see if it looks correct at that point: 您也可以记录数据,看看此时它是否正确:

     d3.tsv("data.tsv", type, function(error, data) { console.log("The error is:", error); //add these console.log("The data is:", error); //two lines x.domain(data.map(function(d) { return d.letter; })); y.domain([0, d3.max(data, function(d) { return d.frequency; })]); 
  4. Beyond that, you'll need to post more of your d3 code to help people figure out what's going on, and why you aren't getting data. 除此之外,您还需要发布更多的d3代码,以帮助人们了解正在发生的事情以及为什么您没有获取数据。 You may even wish to tag this as a d3 question, and remove ampersand from the title as it's probably not relevant to your issue here. 您甚至可能希望将此问题标记为d3问题,并从标题中删除“&”号,因为它可能与此处的问题无关。

Side note: apologies that the ampersand website wasn't helpful. 旁注:抱歉,“&”网站没有帮助。 These things can always be better huh? 这些东西总可以更好吧? :) :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM