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

              
                <div id="graph-container">
        <svg id="graph" width="600" height="300"></svg>
        <div id="tooltip" class="tooltip"></div>
    </div>
              
            
!

CSS

              
                        body {
            font-family: Arial, sans-serif;
            background-color: #f5f5f5;
        }

        #graph-container {
            width: 800px;
            margin: 50px auto;
            position: relative;
        }

        #graph {
            background-color: #fff;
            border: 1px solid #ddd;
        }

        .grid line {
            stroke: #e0e0e0;
            stroke-dasharray: 5,5;
        }

        .axis path,
        .axis line {
            stroke: #000;
        }

        .data-line {
            fill: none;
            stroke: #3498db;
            stroke-width: 2;
        }

        .data-area {
            fill: url(#gradient);
            opacity: 0.7;
        }

        .data-point {
            fill: #3498db;
            stroke: #fff;
            stroke-width: 2;
            cursor: pointer;
            transition: transform 0.2s;
        }

          .tooltip {
            position: absolute;
            background: rgba(0,0,0,0.7);
            color: #fff;
            padding: 6px 10px;
            border-radius: 4px;
            pointer-events: none;
            opacity: 0;
            transition: opacity 0.3s;
            font-size: 12px;
        }
              
            
!

JS

              
                document.addEventListener("DOMContentLoaded", function() {
            const svg = document.getElementById("graph");
            const tooltip = document.getElementById("tooltip");
            const width = svg.clientWidth;
            const height = svg.clientHeight;
            const padding = { top: 50, right: 50, bottom: 50, left: 50 };

            // Sample Data: Replace this with your actual data
            const data = [
                { month: "January", value: 30 },
                { month: "February", value: 45 },
                { month: "March", value: 20 },
                { month: "April", value: 60 },
                { month: "May", value: 50 },
                { month: "June", value: 70 },
                { month: "July", value: 80 },
                { month: "August", value: 55 },
                { month: "September", value: 65 },
                { month: "October", value: 40 },
                { month: "November", value: 35 },
                { month: "December", value: 50 },
            ];

            // Determine the maximum value for scaling
            const maxValue = Math.max(...data.map(d => d.value));

            // Scales for X and Y axes
            const xScale = (index) => padding.left + (index * (width - padding.left - padding.right)) / (data.length - 1);
            const yScale = (value) => padding.top + ((maxValue - value) * (height - padding.top - padding.bottom)) / maxValue;

            // Create Grid Lines
            const numRows = 5; // Number of horizontal grid lines
            const gridGroup = document.createElementNS("http://www.w3.org/2000/svg", "g");
            gridGroup.setAttribute("class", "grid");
            for (let i = 0; i <= numRows; i++) {
                const y = padding.top + i * (height - padding.top - padding.bottom) / numRows;
                const line = document.createElementNS("http://www.w3.org/2000/svg", "line");
                line.setAttribute("x1", padding.left);
                line.setAttribute("y1", y);
                line.setAttribute("x2", width - padding.right);
                line.setAttribute("y2", y);
                gridGroup.appendChild(line);
            }
            svg.appendChild(gridGroup);

            // Create Axes
            const axisGroup = document.createElementNS("http://www.w3.org/2000/svg", "g");
            axisGroup.setAttribute("class", "axis");

            // Y Axis
            const yAxis = document.createElementNS("http://www.w3.org/2000/svg", "line");
            yAxis.setAttribute("x1", padding.left);
            yAxis.setAttribute("y1", padding.top);
            yAxis.setAttribute("x2", padding.left);
            yAxis.setAttribute("y2", height - padding.bottom);
            axisGroup.appendChild(yAxis);

            // X Axis
            const xAxis = document.createElementNS("http://www.w3.org/2000/svg", "line");
            xAxis.setAttribute("x1", padding.left);
            xAxis.setAttribute("y1", height - padding.bottom);
            xAxis.setAttribute("x2", width - padding.right);
            xAxis.setAttribute("y2", height - padding.bottom);
            axisGroup.appendChild(xAxis);

            // Y Axis Labels
            for (let i = 0; i <= numRows; i++) {
                const value = Math.round(maxValue - (i * maxValue) / numRows);
                const y = padding.top + i * (height - padding.top - padding.bottom) / numRows;
                const text = document.createElementNS("http://www.w3.org/2000/svg", "text");
                text.setAttribute("x", padding.left - 10);
                text.setAttribute("y", y + 5);
                text.setAttribute("text-anchor", "end");
                text.setAttribute("font-size", "12");
                text.textContent = value;
                axisGroup.appendChild(text);
            }

            // X Axis Labels (Months)
            data.forEach((d, index) => {
                const x = xScale(index);
                const y = height - padding.bottom + 20;
                const text = document.createElementNS("http://www.w3.org/2000/svg", "text");
                text.setAttribute("x", x);
                text.setAttribute("y", y);
                text.setAttribute("text-anchor", "middle");
                text.setAttribute("font-size", "12");
                text.textContent = d.month.substring(0, 3); // Short month name
                axisGroup.appendChild(text);
            });

            svg.appendChild(axisGroup);

            // Define Gradient for Data Area
            const defs = document.createElementNS("http://www.w3.org/2000/svg", "defs");
            const gradient = document.createElementNS("http://www.w3.org/2000/svg", "linearGradient");
            gradient.setAttribute("id", "gradient");
            gradient.setAttribute("x1", "0%");
            gradient.setAttribute("y1", "0%");
            gradient.setAttribute("x2", "0%");
            gradient.setAttribute("y2", "100%");
            const stop1 = document.createElementNS("http://www.w3.org/2000/svg", "stop");
            stop1.setAttribute("offset", "0%");
            stop1.setAttribute("stop-color", "#3498db");
            stop1.setAttribute("stop-opacity", "0.7");
            const stop2 = document.createElementNS("http://www.w3.org/2000/svg", "stop");
            stop2.setAttribute("offset", "100%");
            stop2.setAttribute("stop-color", "#3498db");
            stop2.setAttribute("stop-opacity", "0");
            gradient.appendChild(stop1);
            gradient.appendChild(stop2);
            defs.appendChild(gradient);
            svg.appendChild(defs);

            // Create Data Area (Under the Line)
            const areaPath = document.createElementNS("http://www.w3.org/2000/svg", "path");
            let areaData = "M";
            data.forEach((d, index) => {
                const x = xScale(index);
                const y = yScale(d.value);
                areaData += `${x} ${y} `;
            });

            // Close the path to create the filled area
            areaData += `L ${xScale(data.length - 1)} ${height - padding.bottom} L ${xScale(0)} ${height - padding.bottom} Z`;
            areaPath.setAttribute("d", areaData);
            areaPath.setAttribute("class", "data-area");
            svg.appendChild(areaPath);

            // Create Data Line
            const linePath = document.createElementNS("http://www.w3.org/2000/svg", "path");
            let pathData = "";
            data.forEach((d, index) => {
                const x = xScale(index);
                const y = yScale(d.value);
                pathData += `${index === 0 ? 'M' : 'L'} ${x} ${y} `;
            });
            linePath.setAttribute("d", pathData);
            linePath.setAttribute("class", "data-line");
            svg.appendChild(linePath);

            // Create Data Points
            data.forEach((d, index) => {
                const x = xScale(index);
                const y = yScale(d.value);
                const circle = document.createElementNS("http://www.w3.org/2000/svg", "circle");
                circle.setAttribute("cx", x);
                circle.setAttribute("cy", y);
                circle.setAttribute("r", 5);
                circle.setAttribute("class", "data-point");
                
                // Tooltip Interactivity
                circle.addEventListener("mouseover", () => {
                    tooltip.style.opacity = 1;
                    tooltip.textContent = `${d.month}: ${d.value}`;
                });
                circle.addEventListener("mousemove", (event) => {
                    const rect = document.getElementById("graph-container").getBoundingClientRect();
                    tooltip.style.left = (event.clientX - rect.left + 10) + "px";
                    tooltip.style.top = (event.clientY - rect.top - 25) + "px";
                });
                circle.addEventListener("mouseout", () => {
                    tooltip.style.opacity = 0;
                });

                svg.appendChild(circle);
            });

        });
              
            
!
999px

Console