Thursday, September 4, 2025
HomeLanguagesJavascriptD3.js stack.order() Method

D3.js stack.order() Method

The stack.order() method takes an ordering function as an argument and returns the stack generator.

Syntax:

stack.order(orderingFunc)

Parameters: This function accepts a single parameter as mentioned above and described below:

  • orderingFunc: This is a function that orders the stack content.

Return Value: This method returns the stack generator.

Example 1: Ordering the stack using d3.stackOrderNone function.

HTML




<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
  
    <script src=
        "https://d3js.org/d3.v5.min.js">
    </script>
</head>
  
<body>
    <h1 style="text-align: center; color: green;">
        neveropen
    </h1>
  
    <center>
        <canvas id="gfg" width="200" height="200">
        </canvas>
    </center>
  
    <script>
        var data = [
              {letter: {a: 3840, b: 1920, c: 960, d: 400}},
              {letter: {a: 1600, b: 1440, c: 960, d: 400}},
              {letter: {a:  640, b:  960, c: 640, d: 400}},
              {letter: {a:  320, b:  480, c: 640, d: 400}}
            ];
  
        var stackGen = d3.stack()
            // Defining keys
            .keys(["a", "b", "c", "d"])
            // Ordering
            .order(d3.stackOrderNone);
  
        var stack = stackGen(data);
          
        console.log(stack);
  
    </script>
</body>
  
</html>


Output:

Example 2: Ordering the stack using d3.stackOrderReverse function.

HTML




<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
  
    <script src=
        "https://d3js.org/d3.v5.min.js">
    </script>
</head>
  
<body>
    <h1 style="text-align: center; color: green;">
        neveropen
    </h1>
  
    <center>
        <canvas id="gfg" width="200" height="200">
        </canvas>
    </center>
  
    <script>
        var data = [
              {letter: {a: 3840, b: 1920, c: 960, d: 400}},
              {letter: {a: 1600, b: 1440, c: 960, d: 400}},
              {letter: {a:  640, b:  960, c: 640, d: 400}},
              {letter: {a:  320, b:  480, c: 640, d: 400}}
            ];
  
        var stackGen = d3.stack()
            // Defining keys
            .keys(["a", "b", "c", "d"])
            // Ordering
            .order(d3.stackOrderReverse);
        
        var stack = stackGen(data);
          
        console.log(stack);
  
    </script>
</body>
  
</html>


Output:

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!
RELATED ARTICLES

Most Popular

Dominic
32263 POSTS0 COMMENTS
Milvus
81 POSTS0 COMMENTS
Nango Kala
6626 POSTS0 COMMENTS
Nicole Veronica
11799 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11857 POSTS0 COMMENTS
Shaida Kate Naidoo
6749 POSTS0 COMMENTS
Ted Musemwa
7025 POSTS0 COMMENTS
Thapelo Manthata
6696 POSTS0 COMMENTS
Umr Jansen
6716 POSTS0 COMMENTS