Open In App

D3.js pie.sortValues() Function

The pie.sortValues() function in D3,js is used to set the value comparator to the given function. It returns the current value comparator when the comparator function is not specified which defaults to the descending order. 

The difference between a value and data comparator is that the two arguments, a and b are values which are derived from the data array using the value accessor. If the arc for b must come before the arc for a then comparator function must return a value that is greater than 0.



Syntax:

pie.sortValues( compare(a, b) )

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



Return Values: This function returns the current value comparator.

Below example illustrates the pie.sortValues() function in D3.js:

Example 1:




<!DOCTYPE html>
<html>
  
<head>
    <script src="https://d3js.org/d3.v6.min.js">
    </script>
</head>
  
<body>
    <div style="width:300px;
              height:300px;">
        <center>
            <h1 style="color:green">
                GeeksforGeeks
            </h1>
            <h2>
                pie.sortValues()
            </h2>
        </center>
        <svg width="300" height="250">
        </svg>
        <button>Click Me</button>
    </div>
    <script>
        // Data to be added in the pie chart
        var data = [
            { "property": "fp5", "value": 5 },
            { "property": "gp5", "value": 16 },
            { "property": "bp4", "value": 10 },
            { "property": "ap3", "value": 12 },
            { "property": "mp2", "value": 19 },
            { "property": "kp1", "value": 21 },
        ]
  
        // Selecting SVG using d3.select()
        var svg = d3.select("svg");
  
        // Creating Pie generator
        var pie = d3.pie()
            // Use of pie.value() Function
            .value((d) => { return d.value })
            (data);
  
        function updatePie() {
  
            // Creating Pie generator
            var pie = 0
            var pie = d3.pie()
                .value((d) => { return d.value })
  
                // Use of pie.sortValues() Function
                .sortValues(function (a, b) {
                    return a - b;
                })
                (data);
            // Creating arc
            var arc = d3.arc()
                .innerRadius(0)
                .outerRadius(100);
  
            let g = svg.append("g")
                .attr("transform", "translate(150,120)");
  
            // Grouping different arcs
            var arcs = g.selectAll("arc")
                .data(pie)
                .enter()
                .append("g");
  
            // Appending path 
            arcs.append("path")
                .attr("fill", (data, i) => {
                    return d3.schemeSet2[i];
                })
                .attr("d", arc);
  
            arcs.append("text")
                .attr("transform", (d) => {
                    return "translate(" +
                        arc.centroid(d) + ")";
                })
                .text(function (d) {
                    return d.value;
                });
        }
        let btn = document.querySelector("button")
        btn.addEventListener("click", updatePie);
  
        // Creating arc
        var arc = d3.arc()
            .innerRadius(0)
            .outerRadius(100);
  
        let g = svg.append("g")
            .attr("transform", "translate(150,120)");
  
        // Grouping different arcs
        var arcs = g.selectAll("arc")
            .data(pie)
            .enter()
            .append("g");
  
        // Appending path 
        arcs.append("path")
            .attr("fill", (data, i) => {
                return d3.schemeSet2[i];
            })
            .attr("d", arc);
  
        arcs.append("text")
            .attr("transform", (d) => {
                return "translate(" +
                    arc.centroid(d) + ")";
            })
            .text(function (d) {
                return d.value;
            });
    </script>
</body>
  
</html>

Output:

Example 2:




<!DOCTYPE html>
<html>
  
<head>
    <script src="https://d3js.org/d3.v6.min.js">
    </script>
</head>
  
<body>
    <div style="width:300px; 
              height:300px;">
        <center>
            <h1 style="color:green">
                GeeksforGeeks
            </h1>
            <h2>
                pie.sortValues();
            </h2>
        </center>
        <svg width="300" height="250">
        </svg>
        <button>Clickme</button>
    </div>
    <script>
        // Data to be added in the pie chart
        var data = [
            { "property": "fp5", "value": "5" },
            { "property": "gp5", "value": "6" },
            { "property": "bp4", "value": "b" },
            { "property": "ap3", "value": "a" },
            { "property": "mp2", "value": 9 },
            { "property": "kp1", "value": "8" },
        ]
  
        // Selecting SVG using d3.select()
        var svg = d3.select("svg");
  
        // Creating Pie generator
        var pie = d3.pie()
            // Use of pie.value() Function
            .value((d) => { return d.value })
            (data);
  
        function updatePie() {
            // Creating Pie generator
            var pie = 0
            var pie = d3.pie()
                .value((d) => { return d.value })
  
                // Use of pie.sortValues() Function
                .sortValues(function (b, a) {
                    console.log("a: " + a + ",
                      b: " + b + ",
                        a - b: ", b - a);
          return b - a;
                })
                (data);
            // Creating arc
            var arc = d3.arc()
                .innerRadius(30)
                .outerRadius(80);
  
            let g = svg.append("g")
                .attr("transform", "translate(150,120)");
  
            // Grouping different arcs
            var arcs = g.selectAll("arc")
                .data(pie)
                .enter()
                .append("g");
  
            // Appending path 
            arcs.append("path")
                .attr("fill", (data, i) => {
                    return d3.schemeSet1[i];
                })
                .attr("d", arc);
  
            arcs.append("text")
                .attr("transform", (d) => {
                    return "translate(" +
                        arc.centroid(d) + ")";
                })
                .text(function (d) {
                    return d.value;
                });
        }
        let btn = document.querySelector("button")
        btn.addEventListener("click", updatePie);
        // Creating arc
        var arc = d3.arc()
            .innerRadius(30)
            .outerRadius(80);
  
        let g = svg.append("g")
            .attr("transform", "translate(150,120)");
  
        // Grouping different arcs
        var arcs = g.selectAll("arc")
            .data(pie)
            .enter()
            .append("g");
  
        // Appending path 
        arcs.append("path")
            .attr("fill", (data, i) => {
                return d3.schemeSet1[i];
            })
            .attr("d", arc);
  
        arcs.append("text")
            .attr("transform", (d) => {
                return "translate(" +
                    arc.centroid(d) + ")";
            })
            .text(function (d) {
                return d.value;
            });
    </script>
</body>
  
</html>

Output:


Article Tags :