Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's 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 it's URL and the proper URL extention.

+ 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

Save Automatically?

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="content" class="content">
        <h1 id="title">United States GDP</h1>
    </div>
    <footer>
        <p>Get more information at: <a href="http://www.bea.gov/national/pdf/nipaguid.pdf">http://www.bea.gov/national/pdf/nipaguid.pdf</a></p>
    </footer>
    <script src="https://d3js.org/d3.v7.min.js"></script>
    <script type="text/javascript" src="index.js"></script>
              
            
!

CSS

              
                body {
    height: 100vh;
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    background-color: #004c6d;
}

.content {
    background-color: white;
    padding: 10px;
    border-radius: 10px;
    box-shadow: 0 0 50px 10px black;
}

#title {
    text-align: center;
}

.bar {
    fill: #99d0f9;
}

.bar:hover {
    fill: white;
}

.tool-tip {
    height: 50px;
}

footer {
    margin-top: 50px;
}

a {
    color: white;
    text-decoration: none;
}
              
            
!

JS

              
                let dataSet;
const height = 600;
const width = 1000;

const timeParser = (time) => {
    let year = time.getFullYear();
    let month = time.getMonth();
    let quarter;

    switch (month) {
        case 0:
        case 1:
        case 2:
            quarter = "Q1";
            break;
        case 3: 
        case 4:
        case 5:
            quarter = "Q2";
            break;
        case 6:
        case 7:
        case 8:
            quarter = "Q3";
            break;
        case 9:
        case 10:
        case 11:
            quarter = "Q4";
            break;
    } 

    return `${year} ${quarter}`
}

const gdpParser = (gdp) => {
    return `$ ${gdp} Billion`
}

fetch("https://raw.githubusercontent.com/freeCodeCamp/ProjectReferenceData/master/GDP-data.json")
    .then(response => response.json())
    .then(data => {

        //Raw DataSET as Link
        dataSet = data;

        //Timestmaps for Tooltips
        const timeIntervalls = dataSet.data.map(data => {

            let timeStampModified = new Date(data[0]);
            return timeStampModified;
        });

        //all GDPs in arrays
        const gdp = dataSet.data.map(data => data[1]);

        //GDP max and min values
        const gdpMax = d3.max(gdp, (d) => d);
        const gdpMin = d3.min(gdp, (d) => d);
    

        //first and last year
        const yearMin = d3.min(timeIntervalls, (d) => d);
        const yearMax = d3.max(timeIntervalls, (d) => d);

        const xScale = d3.scaleTime().domain([yearMin, yearMax]).range([60, width - 60]);
        const yScale = d3.scaleLinear().domain([0, gdpMax]).range([height - 30, 30]);
        const linearScale = d3.scaleLinear().domain([0, gdpMax]).range([0, height - 60]);
        
        const xAxis = d3.axisBottom(xScale);
        const yAxis = d3.axisLeft(yScale);

        const svgBG = d3.select("#content")
                        .append("svg")
                        .attr('width', width)
                        .attr('height', height)
                        .attr('class', "blue" )

        svgBG.append("g")
            .attr("transform", "translate(0, " + (height - 30)+ ")")
            .attr("id", "x-axis")
            .call(xAxis);

        svgBG.append("g")
            .attr("transform", "translate(" + 60 + ", 0)")
            .attr("id", "y-axis")
            .call(yAxis);

        d3.select("svg")
            .selectAll("rect")
            .data(gdp)
            .enter()
            .append("rect")
            .attr("data-date", function (d, i) {
                return dataSet.data[i][0];
            })
            .attr("data-gdp", function(d, i) {
                return gdp[i]
            })
            .attr("x", function(d, i) {
                return xScale(timeIntervalls[i]);
            })
            .attr("y", function (d) {
                return height - 30 - linearScale(d);
            })
            .attr("height", function(d) {
                return linearScale(d)
            })
            .attr("width", 880 / 275)
            .attr("class", "bar")
            .append("title")
            .attr("id", "tool-tip")
            .attr("class", "tool-tip")
            .text((d, i) => {
                return `Year: ${timeParser(timeIntervalls[i])}, GDP: ${gdpParser(d)}`
            })

        svgBG.append("text")
            .attr('transform', 'rotate(-90)')
            .attr('x', -300)
            .attr('y', 80)
            .text('Gross Domestic Product');

        svgBG.append("text")
            .attr('x', 950)
            .attr('y', 600)
            .text('Years');
    });
              
            
!
999px

Console