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 class="container"></div>

              
            
!

CSS

              
                html, body {
  height: 100%;
}

.container {
  height: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
}

.axis {
  color: #676767;
}

              
            
!

JS

              
                // Article: https://dev.to/chooblarin/d3js-chart-with-gradient-color-4j71

const margin = { top: 20, right: 20, bottom: 160, left: 45 }
const svgWidth = 420;
const svgHeight = 320;
const width = svgWidth - margin.left - margin.right
const height = svgHeight - margin.top - margin.bottom

const svg = d3
  .select('.container')
  .append('svg')
  .attr('width', svgWidth)
  .attr('height', svgHeight);

const defs = svg.append('defs');

const bgGradient = defs
  .append('linearGradient')
  .attr('id', 'bg-gradient')
  .attr('gradientTransform', 'rotate(90)');
bgGradient
  .append('stop')
  .attr('stop-color', '#F2C66B')
  .attr('offset', '0%');
bgGradient
  .append('stop')
  .attr('stop-color', '#D13D73')
  .attr('offset', '100%');

const graphArea = svg
  .append('g')
  .attr('transform', `translate(${margin.left}, ${margin.top})`);

// Donloaded from https://www.imdb.com/list/ls021348496/
const dataURL = 'https://s3-us-west-2.amazonaws.com/s.cdpn.io/721250/2018%20Movie%20Rankings.csv';

(async () => {
  let data = await d3.csv(dataURL);

  const getX = d => d["Title"];
  const getY = d => +d["Num Votes"]; // convert to number from string

  // filter Top 10 data, and sort by num of votes
  data = data
    .slice(0, 10)
    .sort( (a, b) => -(getY(a) - getY(b)));

  const x = d3.scaleBand()
    .rangeRound([0, width])
    .domain(data.map(getX))
    .padding(0.1);

  const y = d3.scaleLinear()
    .range([height, 0])
    .domain([
      d3.min(data, getY),
      d3.max(data, getY)
    ])
    .nice();

  const xAxis = d3.axisBottom(x);
  const yAxis = d3.axisLeft(y).ticks(5);

  const ax = graphArea
    .append('g')
    .attr('class', 'axis')
    .attr('transform', `translate(0, ${height})`)
    .call(xAxis);

  ax.selectAll('text')
    .style("text-anchor", "start")
    .style("alignment-baseline", "middle")
    .attr("transform", `translate(${x.bandwidth() / 2}, 10) rotate(90)`)

  graphArea
    .append('g')
    .attr('class', 'axis')
    .call(yAxis);

  graphArea
     .selectAll("bar")
     .data(data)
     .enter().append("rect")
        .style("fill", "url(#bg-gradient)")
        .attr("x", d => x(getX(d)))
        .attr("y", d => y(getY(d)))
        .attr("width", x.bandwidth())
        .attr("height", d => height - y(getY(d)));
})();

              
            
!
999px

Console