HTML preprocessors can make writing HTML more powerful or convenient. For instance, Markdown is designed to be easier to write and read for text documents and you could write a loop in Pug.
In CodePen, whatever you write in the HTML editor is what goes within the <body>
tags in a basic HTML5 template. So you don't have access to higher-up elements like the <html>
tag. If you want to add classes there that can affect the whole document, this is the place to do it.
In CodePen, whatever you write in the HTML editor is what goes within the <body>
tags in a basic HTML5 template. If you need things in the <head>
of the document, put that code here.
The resource you are linking to is using the 'http' protocol, which may not work when the browser is using https.
CSS preprocessors help make authoring CSS easier. All of them offer things like variables and mixins to provide convenient abstractions.
It's a common practice to apply CSS to a page that styles elements such that they are consistent across all browsers. We offer two of the most popular choices: normalize.css and a reset. Or, choose Neither and nothing will be applied.
To get the best cross-browser support, it is a common practice to apply vendor prefixes to CSS properties and values that require them to work. For instance -webkit-
or -moz-
.
We offer two popular choices: Autoprefixer (which processes your CSS server-side) and -prefix-free (which applies prefixes via a script, client-side).
Any URLs added here will be added as <link>
s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.
You can apply CSS to your Pen from any stylesheet on the web. Just put a URL to it here and we'll apply it, in the order you have them, before the CSS in the Pen itself.
You can also link to another Pen here (use the .css
URL Extension) and we'll pull the CSS from that Pen and include it. If it's using a matching preprocessor, use the appropriate URL Extension and we'll combine the code before preprocessing, so you can use the linked Pen as a true dependency.
JavaScript preprocessors can help make authoring JavaScript easier and more convenient.
Babel includes JSX processing.
Any URL's added here will be added as <script>
s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.
You can apply a script from anywhere on the web to your Pen. Just put a URL to it here and we'll add it, in the order you have them, before the JavaScript in the Pen itself.
If the script you link to has the file extension of a preprocessor, we'll attempt to process it before applying.
You can also link to another Pen here, and we'll pull the JavaScript from that Pen and include it. If it's using a matching preprocessor, we'll combine the code before preprocessing, so you can use the linked Pen as a true dependency.
Search for and use JavaScript packages from npm here. By selecting a package, an import
statement will be added to the top of the JavaScript editor for this package.
Using packages here is powered by esm.sh, which makes packages from npm not only available on a CDN, but prepares them for native JavaScript ESM usage.
All packages are different, so refer to their docs for how they work.
If you're using React / ReactDOM, make sure to turn on Babel for the JSX processing.
If active, Pens will autosave every 30 seconds after being saved once.
If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.
If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.
Visit your global Editor Settings.
<script src="https://d3js.org/d3.v5.min.js"></script>
<svg width="1900" height="900">
<g></g>
</svg>
<div class = "knopf">
<button onclick="resetZoom()">Reset zoom</button>
</div>
<div class = "knopf2">
<button onclick="NumtoPerc()">Num to Perc</button>
</div>
<div id="auswahl_id" class = "auswahl_class">
<select id="changeAxisYLabel" onChange = "update()">
<option value ="absoluteValue">Absolute Value</option>
<option value ="percentage">Percentage</option>
</select>
</div>
.knopf{
position: absolute;
top: 50px;
left: 100px;
}
.knopf2{
position: absolute;
top: 50px;
left: 200px;
}
.auswahl_class{
position: absolute;
top: 50px;
left: 300px;
}
// Step 1
let min = 0;
let max = 200;
let x_arr = [];
let y_arr = [];
let s_arr = [];
let z_arr = [];
for (let i = 0; i < 360; i++) {
var r = Math.round(Math.random() * (max - min)) + min;
x_arr[i]= i;
y_arr[i]= r;
z_arr.push([x_arr[i],y_arr[i]]);
}
s_arr = y_arr.sort(function(a, b){return a - b});
let neu_arr = [];
let zz_arr = [];
for (let i = 0; i < 360; i++) {
neu_arr[i]= i;
zz_arr.push([neu_arr[i], s_arr[i]]);
}
// console.log(z_arr);
// console.log(zz_arr);
// Zweiter und dritter Datensatz
let x2_arr = [];
let y2_arr = [];
let s2_arr = [];
let neu2_arr = [];
let zz2_arr = [];
for (let i = 0; i < 360; i++) {
var r = Math.round(Math.random() * (max - min)) + min;
x2_arr[i]= i;
y2_arr[i]= r;
}
s2_arr = y2_arr.sort(function(a, b){return b - a});
for (let i = 0; i < 360; i++) {
neu2_arr[i]= i;
zz2_arr.push([neu2_arr[i], s2_arr[i]]);
}
console.log(zz2_arr);
let x3_arr = [];
let y3_arr = [];
let s3_arr = [];
let neu3_arr = [];
let zz3_arr = [];
for (let i = 0; i < 360; i++) {
var r = Math.round(Math.pow(Math.random(), 3) * (max - min)) + min;
x3_arr[i]= i;
y3_arr[i]= r;
}
s3_arr = y3_arr.sort(function(a, b){return a - b});
for (let i = 0; i < 360; i++) {
neu3_arr[i]= i;
zz3_arr.push([neu3_arr[i], s3_arr[i]]);
}
console.log(zz3_arr);
var dataset1 = zz_arr;
var dataset2 = zz2_arr;
var dataset3 = zz3_arr;
// Step 3
var svg = d3.select("svg"),
margin = 200,
width = svg.attr("width") - margin, //1700
height = svg.attr("height") - margin //700
const offsetX = margin/2 + width/2;
const offsetY = margin/2 + height/2;
// Step 4
let xScale = d3.scaleLinear().domain([0 , 365]).range([0, width]),
yScale = d3.scaleLinear().domain([0, 210]).range([height, 0]),
newX = xScale,
newY = yScale;
// Margins, dadurch gruppiert
var g = svg.append("g")
.attr("transform", "translate(" + 100 + "," + 100 + ")");
// Step 5
// Title
svg.append('text')
.attr('x', width/2 + 100)
.attr('y', 75)
.attr('text-anchor', 'middle')
.style('font-family', 'Helvetica')
.style('font-size', 20)
.text('Line Chart');
// X label
svg.append('text')
.attr('x', width/2 + 85)
.attr('y', height + 150)
.attr('text-anchor', 'middle')
.style('font-family', 'Helvetica')
.style('font-size', 12)
.text('Zeitachse');
// Y label
svg.append('text')
.attr('text-anchor', 'middle')
.attr('transform', 'translate(60,' + 435 + ')rotate(-90)')
.style('font-family', 'Helvetica')
.style('font-size', 12)
.text('Wert');
// var axBot = d3.axisBottom(xScale).ticks(7).tickValues([0, 60, 120, 180, 240, 300, 360]);
// var axLft = d3.axisLeft(yScale);
// Step 6
var axBot = g.append("g")
.attr("transform", "translate(0," + height + ")")
.attr('id', 'x-axis')
.call(d3.axisBottom(xScale).ticks(7).tickValues([0, 60, 120, 180, 240, 300, 360]));
var axLft = g.append("g")
.attr('id', 'y-axis')
.call(d3.axisLeft(yScale));
// Don't draw beyond the axes
var clip = svg.append("defs")
.append("SVG:clipPath")
.attr("id", "clip")
.append("SVG:rect")
.attr("width", width)
.attr("height", height)
.attr("x", 100)
.attr("y", 100);
var scatter = svg.append('g')
.attr("clip-path", "url(#clip)")
// Step 7
scatter
// svg.append('g')
// .select('#points')
.selectAll("dot")
.data(dataset1)
.enter()
.append("circle")
.attr("cx", function (d) { return xScale(d[0]); } )
.attr("cy", function (d) { return yScale(d[1]); } )
.attr("r", 5)
.attr("transform", "translate(" + 100 + "," + 100 + ")")
.style("fill", "#CC0000")
.on("mouseover",movetooltip)
.on("mouseout", mouseout);
scatter
// svg.append('g')
// .select('#points')
.selectAll("dot")
.data(dataset2)
.enter()
.append("circle")
.attr("cx", function (d) { return xScale(d[0]); } )
.attr("cy", function (d) { return yScale(d[1]); } )
.attr("r", 5)
.attr("transform", "translate(" + 100 + "," + 100 + ")")
.style("fill", "#008800")
.on("mouseover",movetooltip)
.on("mouseout", mouseout);
scatter
// svg.append('g')
// .select('#points')
.selectAll("dot")
.data(dataset3)
.enter()
.append("circle")
.attr("cx", function (d) { return xScale(d[0]); } )
.attr("cy", function (d) { return yScale(d[1]); } )
.attr("r", 5)
.attr("transform", "translate(" + 100 + "," + 100 + ")")
.style("fill", "#0000FF")
.on("mouseover",movetooltip)
.on("mouseout", mouseout);
// Step 8
var line = d3.line()
.x(function(d) { return xScale(d[0]); })
.y(function(d) { return yScale(d[1]); })
.curve(d3.curveMonotoneX)
var scatterline = svg.append("g")
.attr("clip-path", "url(#clip)")
scatterline
.append("path")
.datum(dataset1)
.attr("id", "pointline")
.attr("class", "line")
.attr("transform", "translate(" + 100 + "," + 100 + ")")
.attr("d", line)
.style("fill", "none")
.style("stroke", "#CC0000")
.style("stroke-width", "2")
scatterline
.append("path")
.datum(dataset2)
.attr("id", "pointline")
.attr("class", "line")
.attr("transform", "translate(" + 100 + "," + 100 + ")")
.attr("d", line)
.style("fill", "none")
.style("stroke", "#008800")
.style("stroke-width", "2")
scatterline
.append("path")
.datum(dataset3)
.attr("id", "pointline")
.attr("class", "line")
.attr("transform", "translate(" + 100 + "," + 100 + ")")
.attr("d", line)
.style("fill", "none")
.style("stroke", "#0000FF")
.style("stroke-width", "2")
var zoomCallback = function(){
newX = d3.event.transform.rescaleX(xScale);
newY = d3.event.transform.rescaleY(yScale);
axBot.call(d3.axisBottom(newX));
let axleft = d3.axisLeft(newY);
if (document.getElementById("changeAxisYLabel").value == "percentage") {
const maxval = d3.max(dataset1, x => x[1]);
axleft.tickFormat(d => Math.round(d*100/maxval) + "%")
}
axLft.call(axleft);
d3.selectAll('circle')
.attr("cx", (d) => { return newX(d[0]); })
.attr("cy", (d) => { return newY(d[1]); });
d3.selectAll('#pointline')
.attr("d",
d3.line()
.x(function(d) { return newX(d[0]); })
.y(function(d) { return newY(d[1]); })
.curve(d3.curveMonotoneX)
);
}
let zoom = d3.zoom()
.scaleExtent([0.5, 9])
.extent([[0, 0], [width, height]])
.translateExtent([[0, 0], [width, height]])
.on('zoom', zoomCallback);
d3.select('svg')
.call(zoom);
function resetZoom() {
d3.select('svg')
.transition()
.call(zoom.transform, d3.zoomIdentity)
}
function NumtoPerc(){
const maxval = d3.max(dataset1, x => x[1]);
axLft.call(
d3.axisLeft(yScale)
.tickFormat(d => Math.round(d*100/maxval) + "%")
)
}
function update() {
var select = document.getElementById('changeAxisYLabel');
var option = select.options[select.selectedIndex];
let vv = option.value;
let tt = option.text;
if (vv == "percentage") {
console.log("In Prozent angegeben");
const maxval = d3.max(dataset1, x => x[1]);
axLft.call(
d3.axisLeft(newY)
.tickFormat(d => Math.round(d*100/maxval) + "%")
)
} else if (vv == "absoluteValue") {
console.log("In Zahlen angegeben");
const maxval = d3.max(dataset1, x => x[1]);
axLft.call(
d3.axisLeft(newY)
)
}
}
update();
// Tooltip
let tooltip = svg.append("g")
.attr("transform","translate(100,100)")
.style("visibility","hidden");
tooltip.append("rect")
.attr("width",70)
.attr("height",30)
.attr("fill", "#FFFFFF")
.attr("stroke", "black")
.attr("rx",10);
tooltip.append("text")
.attr("y", 19)
.attr("x", 5)
.attr("font-family","Verdana")
.attr("font-size", 10)
.text("I'm a Tooltip");
function movetooltip(d,i) {
tooltip.attr("transform","translate("+(110+newX(d[0]))+","+(70+newY(d[1]))+")")
// .transition()
// .duration(1000)
.style("visibility","visible")
// .style("opacity", 0.5)
var select = document.getElementById('changeAxisYLabel');
var option = select.options[select.selectedIndex];
let vv = option.value;
const maxval = d3.max(dataset1, x => x[1]);
tooltip.select("text")
.text((vv == "percentage") ? d[0] + " : "+ Math.round(d[1]*100/maxval) + "%" : d[0] + " : "+ d[1])
}
function mouseout(){
tooltip
.style("visibility", "hidden")
.transition()
.duration(5000);
}
Also see: Tab Triggers