Implement Slider Question

by asmith7013

skill

Create D3 questions with interactive sliders and live visualization updates. Students adjust continuous values and observe dynamic feedback.

Skill Details

Repository Files

2 files in this skill directory


name: Implement Slider Question description: Create D3 questions with interactive sliders and live visualization updates. Students adjust continuous values and observe dynamic feedback.

Implement Slider Question

Use this skill when creating questions where students:

  • Adjust continuous values using sliders or range inputs
  • Observe live updates to visualizations as they change parameters
  • Explore relationships between variables interactively

When to Use This Pattern

Perfect for:

  • Parameter exploration (adjust slope, intercept, etc.)
  • Continuous value adjustments (temperature, speed, ratio)
  • Interactive simulations with live feedback
  • "Adjust until..." type questions

Not suitable for:

Components Required

Copy these from .claude/skills/question-types/snippets/:

Required

  • cards/standard-card.jscreateStandardCard()
  • svg-basics.js → For visualization rendering

Optional

  • cards/explanation-card.jscreateExplanationCard()
  • cards/video-accordion.jscreateVideoAccordion()

Quick Start

  1. Review the pattern guide: PATTERN.md
  2. Study the working example:
    cat courses/IM-8th-Grade/modules/Unit-3/assignments/161-Proportion-Graphs/questions/11/attachments/chart.js
    

Key Implementation Decisions

  1. Slider ranges - What are min, max, step values?
  2. Visualization type - What updates as slider changes? (graph, diagram, numbers)
  3. State structure - Which slider values to track
  4. Update frequency - Real-time updates or debounced?

State Shape

function createDefaultState() {
  return {
    sliderValue1: 50,  // Initial slider position
    sliderValue2: 25,
    explanation: ""
  };
}

Core Pattern

function renderSlider(container, options) {
  const { min, max, step, value, onChange, label, locked } = options;

  const sliderGroup = container.append("div")
    .style("margin", "20px 0");

  sliderGroup.append("label")
    .style("display", "block")
    .style("margin-bottom", "8px")
    .style("font-weight", "600")
    .text(label);

  const slider = sliderGroup.append("input")
    .attr("type", "range")
    .attr("min", min)
    .attr("max", max)
    .attr("step", step)
    .property("value", value)
    .property("disabled", locked)
    .style("width", "100%")
    .on("input", function() {
      onChange(+this.value);
    });

  const valueDisplay = sliderGroup.append("span")
    .style("margin-left", "10px")
    .style("font-weight", "bold")
    .text(value);

  return { slider, valueDisplay };
}

// Usage:
renderSlider(container, {
  label: "Adjust temperature:",
  min: 0,
  max: 100,
  step: 1,
  value: chartState.temperature,
  onChange: (newValue) => {
    chartState.temperature = newValue;
    updateVisualization();
    sendChartState();
  },
  locked: interactivityLocked
});

Live Visualization Updates

function updateVisualization() {
  // Re-render SVG based on current slider values
  svg.selectAll("circle")
    .attr("r", chartState.sliderValue1)
    .attr("fill", getColorFromValue(chartState.sliderValue2));
}

// Call after slider changes:
slider.on("input", function() {
  chartState.value = +this.value;
  updateVisualization();  // ← Live update
  sendChartState();
});

Working Examples

In codebase:

  • Check for slider-based questions in the curriculum

In this skill:

Common Variations

Multiple Sliders

function createDefaultState() {
  return {
    slope: 1,
    intercept: 0,
    amplitude: 5,
    explanation: ""
  };
}

Slider with Value Labels

const sliderContainer = container.append("div");
const valueLabel = sliderContainer.append("span");

slider.on("input", function() {
  const val = +this.value;
  valueLabel.text(val);
  chartState.value = val;
  updateVisualization();
  sendChartState();
});

Styled Range Input

<style>
input[type="range"] {
  -webkit-appearance: none;
  height: 8px;
  border-radius: 4px;
  background: #e5e7eb;
  outline: none;
}
input[type="range"]::-webkit-slider-thumb {
  -webkit-appearance: none;
  width: 20px;
  height: 20px;
  border-radius: 50%;
  background: #3b82f6;
  cursor: pointer;
}
</style>

Implementation Checklist

  • Defined slider ranges (min, max, step)
  • Created createDefaultState() with slider values
  • Rendered slider inputs with labels
  • Implemented onChange handlers to update state
  • Created visualization that responds to slider values
  • Called updateVisualization() on slider input
  • Added explanation card (if needed)
  • Implemented setInteractivity() to disable sliders when locked
  • Implemented applyInitialState() to restore slider positions
  • Tested real-time visualization updates
  • Tested state restoration
  • Tested locking/unlocking

Tips

  1. Provide visual feedback - Show current value next to slider
  2. Use appropriate step sizes - Whole numbers for integers, 0.1 for decimals
  3. Label clearly - Explain what the slider controls
  4. Update efficiently - For expensive renders, consider debouncing
  5. Show range - Display min/max values near slider
  6. Test on mobile - Sliders work on touch but test carefully

Debouncing for Performance

If visualization updates are expensive:

let updateTimeout;
slider.on("input", function() {
  const val = +this.value;
  chartState.value = val;

  // Debounce expensive visualization updates
  clearTimeout(updateTimeout);
  updateTimeout = setTimeout(() => {
    updateVisualization();
  }, 100);

  // Still send state immediately
  sendChartState();
});

Related Skills

Additional Resources

Related Skills

Attack Tree Construction

Build comprehensive attack trees to visualize threat paths. Use when mapping attack scenarios, identifying defense gaps, or communicating security risks to stakeholders.

skill

Grafana Dashboards

Create and manage production Grafana dashboards for real-time visualization of system and application metrics. Use when building monitoring dashboards, visualizing metrics, or creating operational observability interfaces.

skill

Matplotlib

Foundational plotting library. Create line plots, scatter, bar, histograms, heatmaps, 3D, subplots, export PNG/PDF/SVG, for scientific visualization and publication figures.

skill

Scientific Visualization

Create publication figures with matplotlib/seaborn/plotly. Multi-panel layouts, error bars, significance markers, colorblind-safe, export PDF/EPS/TIFF, for journal-ready scientific plots.

skill

Seaborn

Statistical visualization. Scatter, box, violin, heatmaps, pair plots, regression, correlation matrices, KDE, faceted plots, for exploratory analysis and publication figures.

skill

Shap

Model interpretability and explainability using SHAP (SHapley Additive exPlanations). Use this skill when explaining machine learning model predictions, computing feature importance, generating SHAP plots (waterfall, beeswarm, bar, scatter, force, heatmap), debugging models, analyzing model bias or fairness, comparing models, or implementing explainable AI. Works with tree-based models (XGBoost, LightGBM, Random Forest), deep learning (TensorFlow, PyTorch), linear models, and any black-box model

skill

Pydeseq2

Differential gene expression analysis (Python DESeq2). Identify DE genes from bulk RNA-seq counts, Wald tests, FDR correction, volcano/MA plots, for RNA-seq analysis.

skill

Query Writing

For writing and executing SQL queries - from simple single-table queries to complex multi-table JOINs and aggregations

skill

Pydeseq2

Differential gene expression analysis (Python DESeq2). Identify DE genes from bulk RNA-seq counts, Wald tests, FDR correction, volcano/MA plots, for RNA-seq analysis.

skill

Scientific Visualization

Meta-skill for publication-ready figures. Use when creating journal submission figures requiring multi-panel layouts, significance annotations, error bars, colorblind-safe palettes, and specific journal formatting (Nature, Science, Cell). Orchestrates matplotlib/seaborn/plotly with publication styles. For quick exploration use seaborn or plotly directly.

skill

Skill Information

Category:Skill
Last Updated:12/1/2025