Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

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.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

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.

+ add another resource

Packages

Add Packages

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.

Behavior

Auto Save

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <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>

              
            
!

CSS

              
                            .knopf{
                position: absolute;
                top: 50px;
                left: 100px;
            }
            .knopf2{
                position: absolute;
                top: 50px;
                left: 200px;
            }
            .auswahl_class{
                position: absolute;
                top: 50px;
                left: 300px;
            }
              
            
!

JS

              
                            // 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);
            }
              
            
!
999px

Console