In this section, we will explore two important types of scales in D3.js: time scales and logarithmic scales. These scales are essential for visualizing data that spans over time or data that varies exponentially.

Time Scales

What are Time Scales?

Time scales in D3.js are used to map dates and times to a visual representation. They are particularly useful for creating charts that display data over a period, such as line charts showing trends over months or years.

Creating a Time Scale

To create a time scale, you use the d3.scaleTime() function. Here’s a basic example:

// Define the time scale
const timeScale = d3.scaleTime()
    .domain([new Date(2020, 0, 1), new Date(2020, 11, 31)]) // Input domain
    .range([0, 500]); // Output range

// Example usage
const x = timeScale(new Date(2020, 5, 15)); // Maps to a value within the range
console.log(x); // Outputs a value between 0 and 500

Practical Example: Creating a Time Scale Axis

Let's create a simple line chart with a time scale on the x-axis.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Time Scale Example</title>
    <script src="https://d3js.org/d3.v6.min.js"></script>
</head>
<body>
    <svg width="600" height="400"></svg>
    <script>
        const svg = d3.select("svg");
        const margin = {top: 20, right: 30, bottom: 30, left: 40};
        const width = +svg.attr("width") - margin.left - margin.right;
        const height = +svg.attr("height") - margin.top - margin.bottom;

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

        const data = [
            {date: new Date(2020, 0, 1), value: 30},
            {date: new Date(2020, 1, 1), value: 50},
            {date: new Date(2020, 2, 1), value: 80},
            {date: new Date(2020, 3, 1), value: 65},
            {date: new Date(2020, 4, 1), value: 95}
        ];

        const x = d3.scaleTime()
            .domain(d3.extent(data, d => d.date))
            .range([0, width]);

        const y = d3.scaleLinear()
            .domain([0, d3.max(data, d => d.value)])
            .range([height, 0]);

        g.append("g")
            .attr("transform", `translate(0,${height})`)
            .call(d3.axisBottom(x));

        g.append("g")
            .call(d3.axisLeft(y));

        g.append("path")
            .datum(data)
            .attr("fill", "none")
            .attr("stroke", "steelblue")
            .attr("stroke-width", 1.5)
            .attr("d", d3.line()
                .x(d => x(d.date))
                .y(d => y(d.value))
            );
    </script>
</body>
</html>

Explanation

  • Data Preparation: We define an array of objects, each containing a date and a value.
  • Scales: We create a time scale for the x-axis and a linear scale for the y-axis.
  • Axes: We append the x-axis and y-axis to the SVG.
  • Line Path: We use d3.line() to create a line generator and bind the data to it.

Logarithmic Scales

What are Logarithmic Scales?

Logarithmic scales are used to map data that spans several orders of magnitude. They are useful for visualizing data that grows exponentially, such as population growth or financial data.

Creating a Logarithmic Scale

To create a logarithmic scale, you use the d3.scaleLog() function. Here’s a basic example:

// Define the logarithmic scale
const logScale = d3.scaleLog()
    .domain([1, 1000]) // Input domain
    .range([0, 500]); // Output range

// Example usage
const x = logScale(10); // Maps to a value within the range
console.log(x); // Outputs a value between 0 and 500

Practical Example: Creating a Logarithmic Scale Axis

Let's create a simple scatter plot with a logarithmic scale on the x-axis.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Logarithmic Scale Example</title>
    <script src="https://d3js.org/d3.v6.min.js"></script>
</head>
<body>
    <svg width="600" height="400"></svg>
    <script>
        const svg = d3.select("svg");
        const margin = {top: 20, right: 30, bottom: 30, left: 40};
        const width = +svg.attr("width") - margin.left - margin.right;
        const height = +svg.attr("height") - margin.top - margin.bottom;

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

        const data = [
            {x: 1, y: 30},
            {x: 10, y: 50},
            {x: 100, y: 80},
            {x: 1000, y: 65},
            {x: 10000, y: 95}
        ];

        const x = d3.scaleLog()
            .domain([1, d3.max(data, d => d.x)])
            .range([0, width]);

        const y = d3.scaleLinear()
            .domain([0, d3.max(data, d => d.y)])
            .range([height, 0]);

        g.append("g")
            .attr("transform", `translate(0,${height})`)
            .call(d3.axisBottom(x).ticks(10, ",.1s"));

        g.append("g")
            .call(d3.axisLeft(y));

        g.selectAll(".dot")
            .data(data)
            .enter().append("circle")
            .attr("class", "dot")
            .attr("cx", d => x(d.x))
            .attr("cy", d => y(d.y))
            .attr("r", 5)
            .attr("fill", "steelblue");
    </script>
</body>
</html>

Explanation

  • Data Preparation: We define an array of objects, each containing an x and y value.
  • Scales: We create a logarithmic scale for the x-axis and a linear scale for the y-axis.
  • Axes: We append the x-axis and y-axis to the SVG.
  • Scatter Plot: We bind the data to circles and position them using the scales.

Summary

In this section, we covered:

  • The basics of time scales and how to create them using d3.scaleTime().
  • How to create a simple line chart with a time scale.
  • The basics of logarithmic scales and how to create them using d3.scaleLog().
  • How to create a simple scatter plot with a logarithmic scale.

Understanding these scales is crucial for visualizing data that spans over time or varies exponentially. In the next module, we will dive into creating basic visualizations using these scales and more.

© Copyright 2024. All rights reserved