Rafa447 Rafa447 - 13 days ago 6
CSS Question

Code works fine on jsfiddle but doesn´t work on local host

I´ve been adapting some jsfiddle files that I found, so that the result fits to my needs.
It happens that, my code works fine on jsfiddle but doesn´t work on my localhost (used python to start an HTTPServer). More specifically the HTML file only gets the number 100 that is in the div.
I´m only copying the files to my computer, it´s anything I need to add/change more?
I´m new on coding, I apreciate any help.

The link for jsfiddle:
https://jsfiddle.net/sgfcusk4/

The HTML file:

<div id="tooltip" class="hidden">
<p><span id="value">100</span>
</p>
</div>


The JavaScript File:

var margins = {
top: 12,
left: 55,
right: 24,
bottom: 24
},
legendPanel = {
width: 180
},
width = 500 - margins.left - margins.right - legendPanel.width,
height = 100 - margins.top - margins.bottom,
dataset = [{
data: [{
month: 'Portugal',
count: 60
}, {
month: 'Espanha',
count: 40
}],
name: 'Diesel'
}, {
data: [{
month: 'Portugal',
count: 35
}, {
month: 'Espanha',
count: 50
}],
name: 'Petrol'
}, {
data: [{
month: 'Portugal',
count: 5
}, {
month: 'Espanha',
count: 10
}],
name: 'Alternative'
}],
series = dataset.map(function(d) {
return d.name;
}),
dataset = dataset.map(function(d) {
return d.data.map(function(o, i) {
// Structure it so that your numeric
// axis (the stacked amount) is y
return {
y: o.count,
x: o.month
};
});
}),
stack = d3.layout.stack();

stack(dataset);

var dataset = dataset.map(function(group) {
return group.map(function(d) {
// Invert the x and y values, and y0 becomes x0
return {
x: d.y,
y: d.x,
x0: d.y0
};
});
}),
svg = d3.select('body')
.append('svg')
.attr('width', width + margins.left + margins.right + legendPanel.width)
.attr('height', height + margins.top + margins.bottom)
.append('g')
.attr('transform', 'translate(' + margins.left + ',' + margins.top + ')'),
xMax = d3.max(dataset, function(group) {
return d3.max(group, function(d) {
return d.x + d.x0;
});
}),
xScale = d3.scale.linear()
.domain([0, xMax])
.range([0, width]),
months = dataset[0].map(function(d) {
return d.y;
}),
_ = console.log(months),
yScale = d3.scale.ordinal()
.domain(months)
.rangeRoundBands([0, height], .1),
xAxis = d3.svg.axis()
.scale(xScale)
.orient('bottom'),
yAxis = d3.svg.axis()
.scale(yScale)
.orient('left'),
colours = d3.scale.category10(),
groups = svg.selectAll('g')
.data(dataset)
.enter()
.append('g')
.style('fill', function(d, i) {
return colours(i);
}),
rects = groups.selectAll('rect')
.data(function(d) {
return d;
})
.enter()
.append('rect')
.attr('x', function(d) {
return xScale(d.x0);
})
.attr('y', function(d, i) {
return yScale(d.y);
})
.attr('height', function(d) {
return yScale.rangeBand();
})
.attr('width', function(d) {
return xScale(d.x);
})
.on('mouseover', function(d) {
var xPos = parseFloat(d3.select(this).attr('x')) / 2 + width / 2;
var yPos = parseFloat(d3.select(this).attr('y')) + yScale.rangeBand() / 2;

d3.select('#tooltip')
.style('left', xPos + 'px')
.style('top', yPos + 'px')
.select('#value')
.text(d.x);

d3.select('#tooltip').classed('hidden', false);
})
.on('mouseout', function() {
d3.select('#tooltip').classed('hidden', true);
})

svg.append('g')
.attr('class', 'axis')
.attr('transform', 'translate(0,' + height + ')')
.call(xAxis);

svg.append('g')
.attr('class', 'axis')
.call(yAxis);
/* Rectangulo amarelo a volta dos tipos de combustivel
svg.append('rect')
.attr('fill', 'yellow')
.attr('width', 160)
.attr('height', 30 * dataset.length)
.attr('x', width + margins.left)
.attr('y', 0);
*/

//Texto do lado direito
series.forEach(function(s, i) {
svg.append('text')
.attr('fill', 'black')
.attr('x', width + margins.left + 8)
.attr('y', i * 24 + 24)
.text(s);

//Barras do lado direito
svg.append('rect')
.attr('fill', colours(i))
.attr('width', 60)
.attr('height', 20)
.attr('x', width + margins.left + 90)
.attr('y', i * 24 + 6);
});


And the css file:

.axis path,
.axis line {
fill: none;
stroke: black;
shape-rendering: crispEdges;
}

.axis text {
font-family: sans-serif;
font-size: 11px;
}

#tooltip {
position: absolute;
text-align: center;
width: 40px;
height: auto;
padding: 10px;
background-color: white;
-webkit-border-radius: 10px;
-moz-border-radius: 10px;
border-radius: 10px;
-webkit-box-shadow: 4px 4px 10px rgba(0, 0, 0, 0.4);
-moz-box-shadow: 4px 4px 10px rgba(0, 0, 0, 0.4);
box-shadow: 4px 4px 10px rgba(0, 0, 0, 0.4);
pointer-events: none;
}

#tooltip.hidden {
display: none;
}

#tooltip p {
margin: 0;
font-family: sans-serif;
font-size: 16px;
line-height: 20px;
}


The only thing I did was copying the content of the files to new files on my computer and named "sketch.html", "sketch.css" and "sketch.js".

When I open the HTML on localhost the only thing I see is the number "100". It doesn´t show any error message.

Please help.

Answer

You need to include the css and javascript files in your html file.

The css belongs in a link element: <link rel="stylesheet" href="sketch.css" />

And js in a script tag: <script src="sketch.js"></script>

Basic example:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Chart</title>
    <link rel="stylesheet" href="sketch.css" />
</head>
<body>
    <div id="tooltip" class="hidden">
        <p><span id="value">100</span></p>
    </div>
    <script src="sketch.js"></script>
</body>
</html>
Comments