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. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ 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

              
                <h1>Poll Results with Chart.js</h1>
<div class="chart-container">
  <canvas id="chart"></canvas> <!-- <canvas id="chart" width="300" height="150"></canvas> -->
</div>
<form id="pollForm">
  <div class="poll">
    <div class="poll-heading">
      <h3 class="poll-title">Favorite Superhero</h3>
    </div>
    <div class="poll-body">
      <ul>
        <li>
          <label><input type="radio" name="pollOptions" value="Spider-Man"><span>Spider-Man</span></label>
        </li>
        <li>
          <label><input type="radio" name="pollOptions" value="Superman"><span>Superman</span></label>
        </li>
        <li>
          <label><input type="radio" name="pollOptions" value="Batman"><span>Batman</span></label>
        </li>
        <li>
          <label><input type="radio" name="pollOptions" value="Son Goku"><span>Son Goku</span></label>
        </li>
        <li>
          <label><input type="radio" name="pollOptions" value="Hulk"><span>Hulk</span></label>
        </li>
        <li>
          <label><input type="radio" name="pollOptions" value="Wolverine"><span>Wolverine</span></label>
        </li>
        <li>
          <label><input type="radio" name="pollOptions" value="Other"><span>Other</span></label>
        </li>
      </ul>
    </div>
    <div class="poll-footer">
      <button type="submit">Vote</button>
    </div>
  </div>
</form>
              
            
!

CSS

              
                * {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}
body {
  font-family: "Comic Sans MS", cursive, sans-serif;
  background-color: #eee;
  color: #111;
}
h1 {
  text-align: center;
  margin: 1.25rem 0 0.25rem 0;
}
.chart-container {
  position: relative;
  width: 45vw;
  height: 22.5vw;
  margin: auto;
}

.poll {
  background-color: #fff;
  color: #333;
  width: 30vw;
  margin: 1.75rem auto 1rem auto;
  border: 1px solid #ccc;
  border-radius: 0.25rem;
}
.poll-heading {
  padding: 1rem;
  border-bottom: 1px solid;
  border-image: linear-gradient(to right, transparent, #333, transparent) 1;
}
.poll-body {
  padding: 1rem;
}
.poll-body ul {
  list-style-type: none;
  border: 1px solid #ccc;
  border-radius: 0.5rem;
}
.poll-body li label {
  display: block;
  width: 100%;
  padding: 0.5rem;
}
.poll-body li label:hover {
  background-color: #eee;
}
.poll-body li:first-child label {
  border-top-left-radius: 0.5rem;
  border-top-right-radius: 0.5rem;  
}
.poll-body li:last-child label {
  border-bottom-left-radius: 0.5rem;
  border-bottom-right-radius: 0.5rem;  
}
.poll-body li label input[type="radio"] {
  margin-right: 0.5rem;
}
.poll-body li label input[type="radio"]:checked + span {
  font-weight: bold;
}
.poll-footer {
  padding-bottom: 1rem;
  text-align: center;
}
.poll-footer button {
  background-color: #777;
  color: #fff;
  padding: 0.5rem;
  font-size: 1.25rem;
  font-weight: bold;
  border: none;
  outline: none;
  width: 7rem;
  border-radius: 0.25rem;
  cursor: pointer;
  transition: background-color 0.2s;
}
.poll-footer button:hover {
  background-color: #333;
}
.poll-footer button:active {
  background-color: #111;
}

@media(max-width: 1200px) {
  .chart-container {
    width: 65vw;
    height: 32.5vw;
  }
  .poll {
    width: 50%;
  }
}
@media(max-width: 992px) {
  .chart-container {
    width: 75vw;
    height: 37.5vw;
  }
  .poll {
    width: 60%;
  }
}
@media(max-width:767px) {
  html {
    font-size: 14px;
  }
  h1 {
    font-size: 1.75rem;
  }
  .chart-container {
    width: 95vw;
    height: 47.5vw;
  }
  .poll {
    width: 80%;
  }
}
              
            
!

JS

              
                const pollData = [
  {
    option: "Spider-Man",
    votes: 11,
    color: "rgb(255, 99, 132)"
  },
  {
    option: "Superman",
    votes: 8,
    color: "rgb(54, 162, 235)"
  },
  {
    option: "Batman",
    votes: 11,
    color: "rgb(36, 36, 36)"
  },
  {
    option: "Son Goku",
    votes: 5,
    color: "rgb(255, 159, 64)"
  },
  {
    option: "Hulk",
    votes: 3,
    color: "rgb(75, 192, 192)"
  },
  {
    option: "Wolverine",
    votes: 8,
    color: "rgb(255, 206, 86)"
  },
  {
    option: "Other",
    votes: 10,
    color: "rgb(153, 102, 255)"
  }
];

const pollForm = document.querySelector("#pollForm");

pollForm.addEventListener("submit", pollFormSubmit);

function pollFormSubmit(event) {
  event.preventDefault();
  const pollOptionInput = pollForm.querySelector("input[name='pollOptions']:checked");
  if(pollOptionInput) {
    const pollOptionValue = pollOptionInput.value;
    pollData.find(pollOption => pollOption.option === pollOptionValue).votes++;
    pollChart.data.datasets[0].data = pollData.map(pollOption => pollOption.votes);
    pollChart.update();
    pollForm.reset();
  }
}

function rgbToRgba(rgb, alpha=1) {
  return `rgba(${rgb.substring(rgb.indexOf('(')+1, rgb.length-1).split(',').join()}, ${alpha})`;
}

Chart.defaults.global.defaultFontFamily = '"Comic Sans MS", cursive, sans-serif';

const ctx = document.getElementById('chart').getContext('2d');
const pollChart = new Chart(ctx, {
  type: 'bar',
  data: {
    labels: pollData.map(pollOption => pollOption.option),
    datasets: [{
      label: '# of Votes',
      data: pollData.map(pollOption => pollOption.votes),
      backgroundColor: pollData.map(pollOption => rgbToRgba(pollOption.color, 0.75)),
      borderWidth: 3
    }]
  },
  options: {
    scales: {
      yAxes: [{
        ticks: {
          beginAtZero: true
        }
      }]
    },
    title: {
      display: true,
      text: 'Favorite Superheroes',
      fontColor: "#333",
      fontSize: 20,
      padding: 20
    },
    legend: {
      display: false,
    }
  }
});
              
            
!
999px

Console