简体   繁体   English

如何在文件路径中使用变量? - javascript,d3

[英]how to use a variable in file path? - javascript, d3

d3.tsv("//localhost/wordpress/" + my_var + ".tsv", function(d) {
d.frequency = +d.frequency;
return d;
}, function(error, data) {
if (error) throw error;

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

I am trying to integrate the my_var variable successfully in the file path but it is not working. 我试图在文件路径中成功集成my_var变量,但它无法正常工作。 Can someone explain me why? 有人能解释一下为什么吗?

var my_var = <?php echo json_encode($title); ?>;

This 这个

var my_var = <?php echo json_encode($title); ?>;

evaluates to: 评估为:

var my_var = sometitle;

and sometitle is probably not defined. sometitle可能没有定义。 You want it to be a string: 你希望它是一个字符串:

var my_var = "<?php echo json_encode($title); ?>";

That is because my_var is not being treated as a placeholder for a string . 这是因为my_var不被视为string的占位符。 Instead, my_var is being treated as a place holder for another variable named <?php echo json_encode($title); ?> 相反,my_var被视为另一个名为<?php echo json_encode($title); ?>变量的占位符<?php echo json_encode($title); ?> <?php echo json_encode($title); ?> . <?php echo json_encode($title); ?> Make the following change and everything should work: 进行以下更改,一切都应该有效:

var my_var = "<?php echo json_encode($title); ?>";

var url = "//localhost/wordpress/"
url += my_var
url += ".tsv"

d3.tsv( url, function(d) {
d.frequency = +d.frequency;
return d;
}, function(error, data) {
if (error) throw error;

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

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

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