I'm learning d3 and try to open a html file in my local machine.
When I use local server using Python then there is no problem in opening the file.
However, I want to skip this step.
So in Sublime, I built a shortcut in Tools > Build System > New Build System as below.
{
"cmd": ["C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe", "$file"]
}
Once I set up, I could open the file by pressing ctr+b. But from time to time it doesn't seem working. When it doesn't work, it shows this error message:
Failed to load _path_of_file_: Cross origin requests are only supported for protocol
schemes: http, data, chrome, chrome-extension, https.send @ d3.v4.min.js:2
I'm confused if it doesn't work because my file contains errors or the set-up doesn't work.
One more weird thing is, when I open console (ctr+shift+j), the console shows where errors in my file are. For example,
Uncaught TypeError: Cannot read property 'forEach' of null
at index.html:32
I understand 32th line in my code is probably wrong.
But when I open the same file through Python local server, where errors are is different. For example,
Uncaught TypeError: svg.append(...).attr(...).calls is not a function
at index.html:59
Like this, the error lies in 59th line.
It maybe because my code contains a lot of errors? and console shows any errors randomly?
Or, it is something wrong with the setting in sublime?
Basically, I'm worried if the setting in Sublime is reliable. If anyone knows other ways to open a html file without using local server, please let me know as well.
Thanks so much for reading this,
--
Here is my code in d3
<!DOCTYPE html>
<meta charset = 'utf-8'>
<body>
<script src="https://d3js.org/d3.v4.min.js"></script>
<script>
var w = 960,
h = 500;
var parseTime = d3.timeParse('%Y');
var xScale = d3.scaleTime().range([0, w]);
var yScale1 = d3.scaleLinear().range([h, 0]);
var yScale2 = d3.scaleLinear().range([h, 0]);
var line1 = d3.line()
.x(function(d) {return xScale(d.time); })
.y(function(d) {return yScale1(d.value); });
var line2 = d3.line()
.x(function(d) {return xScale(d.time); })
.y(function(d) {return yScale2(d.gdp/1000); });
var svg = d3.select('body')
.append('svg')
.attr('width', w)
.attr('height', h);
d3.csv("data.csv", function(d) {
d.forEach(function(d) {
d.date = parseTime(d.time),
d.value = + d.value;
d.gdp = + d.gdp_per_capita;
});
xScale.domain(d3.extent(d, function(d) { return d.date; }));
yScale1.domain(d3.extent(d, function(d) { return d.value;}));
yScale2.domain(d3.extent(d, function(d) { return d.gdp/1000; }));
svg.append('path')
.data([d])
.attr('class', 'line')
.attr('d', line1);
svg.append('path')
.data([d])
.attr('class', 'line')
.style('stroke', 'red')
.attr('d', line2);
svg.append('g')
.attr('transform', 'translate(0,' + h + ')')
.call(d3.axisBottom(xScale));
svg.append('g')
.attr('class', 'axisLeft')
.calls(d3.axisLeft(yScale1));
svg.append('g')
.attr('class', 'axisRight')
.attr('transform', 'translate(' + w + ', 0)')
.calls(d3.axisRight(yScale2));
});
</script>
</body>