CxD Archive
Code: Web App Data Visualization
Code: Web App Data Visualization
  • Getting Started
  • Dashboard Template Tutorial
    • 1 - First Look at Template
    • 2 - SQL Introduction
    • 3 - Producing Charts
    • 4 - Creating Your Own Database (Google Sheet)
  • SQL Examples
    • WHERE
    • LIKE
    • ORDER BY
    • LIMIT
  • Chart Examples
    • Bar Charts
    • Column Charts
    • Pie/Donut Chart
    • Histograms
    • Scatter Charts
    • Line Charts
    • Bubble Charts
    • Geo Charts
    • Geo Charts (Markers)
    • Tree Maps
  • Dashboard Layout Examples
    • Selectable Chart Data
    • Custom Grid 1
  • Viz Library Source (GitHub)
  • Google Charts Reference
  • Data Visualization Project Guide Book
  • Open Data Sources
Powered by GitBook
On this page
  • HTML
  • Javascript
Export as PDF
  1. Dashboard Layout Examples

Selectable Chart Data

PreviousDashboard Layout ExamplesNextCustom Grid 1

Last updated 6 years ago

This example demonstrates how you can use a dropdown menu to allow a user to select, or filter, the data displayed on a column chart.

Uses jQuery Selectors, the jQuery val function, and the jQuery change function.

HTML

The select element will be used to filter the data for a column chart that will be inserted in the "chart2" div.

<div class="card">
  <select id="salary-select">
    <option value="StartingMedianSalary" selected>Starting Salary</option>
    <option value="MidCareerMedianSalary">Mid-Career Salary</option>
  </select>
  <div id="chart2"></div>
</div>

Javascript

Add the following code to the bottom of the setup() function. This will "listen" for changes to the dropdown selector and re-run the composeCharts() function when it is changed.

$("#salary-select").change(function () {
    composeCharts();
  });

The following "viz.chart" function defines a column chart that will use the value selected by the user in the SQL to retrieve the right column of data to chart.

viz.chart({
    sql: `SELECT Major, ${$("#salary-select").val()} FROM Degrees`,
    chartType: "ColumnChart",
    containerId: "chart2"
});