Creating a pie chart with a legend in the realm of the dojo

Apologies for any language errors.

I am looking to develop a web application where users can fill out a form and submit it to the server. The server will then respond with the requested data in JSON format. Using this data, I want to create a diagram and a legend using Dojo. The generated graph and legend should resemble this image:

1 Question: How can I display custom text in the legend instead of numbers (47, 14, 11, etc.) based on the JSON object?

2 Question: I am currently using the Claro theme in Dojo which has only 5 colors that repeat. How can I assign unique colors to each sector of a circle in the chart?

3 Question: When a new query is made by the user, I need to clear the old chart and legend and display the new ones. While this works for the chart, the legend remains hidden and does not update. What could be causing this issue?

The client-side code appears as follows:

<script>
require(["dojo/dom", 
         "dojo/on", 
     "dojo/request", 
     "dojo/dom-form",
     "statsDiagramme/kreisDiagramm",
     "statsDiagramme/stabDiagramm",
     "dojo/json", 
     "dojox/json/query", 
     "dijit/Dialog", 
     "dijit/form/Button",
      "dojo/domReady!"],

function(dom, on, request, domForm, kreisdiagramm, stabdiagramm, json){

     var form = dom.byId('sqlOptForm');      
     on(form, "submit", function(evt){                      
         evt.stopPropagation();         
         evt.preventDefault();      

         request.post("ServletStatsSQLOPT", {       

    data: domForm.toObject("sqlOptForm"),   
    handleAs: "json"

     }).then(function(response){

    var error = dojox.json.query("fehlermeldung", response);

    if(error === ""){   
                                dojo.html._emptyNode("statsKreisDiagramm");
                                dojo.html._emptyNode("statsStabDiagramm");
                                dojo.html._emptyNode("legende");

                                stabdiagramm.setStabDiagramm(response); 
                                kreisdiagramm.setKreisDiagramm(response);
                                dom.byId("statsKreisDiagramm").style.visibility = 'visible';
                                dom.byId("statsStabDiagramm").style.visibility = 'hidden';
                                dom.byId("statsMenuButton").style.visibility = 'visible';
                                dom.byId("legende").style.visibility = 'visible';                   

    }
    else {                  
        // ERROR 

    }

    }, function(error) {    
        // ERROR 

        });
        });
    }
    );
</script>

The chart creation script is defined as:

define([
"dojox/charting/Chart",
"dojox/charting/themes/Claro",
"dojox/charting/plot2d/Pie",
"dojox/charting/action2d/Tooltip",
"dojox/charting/action2d/MoveSlice",
"dojox/charting/widget/Legend",
"dojox/charting/plot2d/Markers",
"dojox/charting/widget/Legend",
"dojox/charting/axis2d/Default",

"dojo/domReady!"
], 
function(Chart, theme, PiePlot, Tooltip, MoveSlice, Legend){

    return{
        setKreisDiagramm: function(response) {

            var data = new Array(response.summeArray.length);          
            // response => JSON object
            for(var i=0; i < response.summeArray.length; i++) {
                data[i] = { x: 1, y: response.summeArray[i].summe };   
            }

            var pieChart = new Chart("statsKreisDiagramm");        

            pieChart.setTheme(theme);          


            pieChart.addPlot("default", {
                type: PiePlot,                  
                radius: 180,
                fontColor: "black",
                labelOffset: 30,                
                markers: true
            });

            pieChart.addSeries("Summary", data);      

            var tooltip = new Tooltip(pieChart, "default");        

            var moveSliceAction = new MoveSlice(pieChart,"default");       

            pieChart.render();

            var chartLegend = new Legend({ chart: pieChart, horizontal: false },   "legende");


        }
    };
}
);

Any assistance would be greatly appreciated. Thank you!

Answer №1

To address question 2:

There are options to either create your own custom theme or use a preexisting dojo theme for your pie chart.

If you're looking for examples and explanations, check out this link: specifically in the Point Chart Themes section

For more information on available themes, visit the API documentation here:

You can also preview these themes by visiting:

I hope this information proves helpful.

Best regards, Miriam

Answer №2

To eliminate a myth, it must be dismantled completely. Here is the solution to question 3:

var myth = dijit.byId("myth");
if (myth) {
  myth.destroyRecursive(true);
}

var legend = new dojox.charting.widget.Legend({ chart: pieChart, horizontal: false }, "myth"); 

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Encountering Axios CanceledError while attempting to forward a POST request using Axios

While attempting to relay a POST request from an express backend to another backend using axios, I encountered an axios error stating "CanceledError: Request stream has been aborted". Interestingly, this issue does not arise when dealing with GET requests. ...

Issue with passing reactive property to component in Vue 3 application

I am currently working on a Vue 3 application and I am in the process of setting up a store for state management. Within this application, I have several important files that play different roles: app.vue component.vue main.js store.js These files contai ...

Hovering in Javascript

Imagine I have the following code snippet: <div class="class1"> ... random content </div> I want to use JavaScript so that when I hover over that div, a CSS attribute is added: background-color:#ffffe0; border:1px solid #bfbfbf; This is a ...

What steps do I need to follow to execute React/Next code that I have downloaded from GitHub?

I recently obtained a zip file from https://github.com/prgrms-web-devcourse/Team_Price_Offer_FE and extracted its contents. When attempting to launch the program in Visual Studio Code, I inputted the command npm start but encountered an issue. Team_Price_ ...

Utilizing React JS to call a static function within another static function when an HTML button is clicked

Can you please analyze the following code snippet: var ResultComponent = React.createClass({ getInitialState: function () { // … Some initial state setup ……. }, handleClick: function(event) { // … Handling click event logic …… // Including ...

Oops! The system encountered a problem while trying to identify the value `Han` for the property `Script

I'm attempting to extract Chinese characters from a string. According to this particular answer, the following code snippet should work: const nonChinese = /[^\p{Script=Han}]/gimu; const text = "asdP asfasf这些年asfagg 我开源的 几 ...

Combining two or more arrays containing similar values into a single array

Within my array, there are 100 subarrays, each containing 3160 values of an object with two attributes: a sequence number (which only appears if it is present as 1), and another value of either 0 or 1. My goal is to merge all 100 arrays into one single ar ...

Determine if a JSON object is void

Using jQuery, I am checking whether the object returned from an AJAX call is empty or not. In the first example, the AJAX call is successful and returns some data. console.log("obj before JSON parse:", response); var test = $.isEmptyObject(response); con ...

Getting the initial date of the upcoming month in JavaScript

I'm trying to figure out how to get the first day of the next month for a datePicker in dd/mm/yyyy format. Can anyone help? Here's the code I currently have: var now = new Date(); if (now.getMonth() == 11) { var current = new Date(now.getFu ...

Looking to optimize the JavaScript code for improved performance speed

Is there a more efficient way to avoid writing the same lines of code repeatedly without compromising performance? I've attempted using a for loop to categorize fields as 'mandatory' or 'optional', but it still requires duplicating ...

Strategies for disabling middle mouse button default behavior in Vue

When I use @click.middle.stop.prevent="test", the default scroll wheel still shows up despite it detecting the middle mouse click and calling my function. Is there a way to prevent this from happening? ...

Sending HTML input data to jQuery form field

Is there a way to pass HTML text input values into a Stripe form? Here is an example of what I currently have: <input type="text" name="trip" id="trip" value=""> JS: (part of the form) .append(jQuery('<input>', { 'nam ...

The issue with the Hidden Content feature in the Slick Carousel is that it does not function correctly on the

There are some related topics worth exploring, such as: Slick carousel center class not working when going from last item to first item Despite trying various solutions, the issue still persists in my code. My goal is to have each item displayed in the ce ...

The custom SerializationBinder does not get invoked during the deserialization process

The code I have seems simple, but for some reason it's not working as expected: var customBinder = new TypeNameSerializationBinder("MyNamespace.{0}, MyAssembly"); JsonSerializerSettings settings = new JsonSerializerSettings { TypeNameHandling = ...

Establish a secondary resource group specifically for the nested template

Attempting to deploy a template for setting up a site-to-site VPN and connecting a virtual machine to Azure using nested templates. Each template functions independently as well as together when nested. The issue arises when trying to deploy the site-to-s ...

Decrease the space between slide items by reducing margins or increasing their width

I'm having trouble achieving the desired spacing between items, I would like more space between each item but they are currently too close together. I am looking for a margin of 10px or a width of 32% for each item. I have already looked into some re ...

Locate a specific item in an array using AngularJs based on a key and present its value on the View

Imagine you have an array of objects: $scope.objArr = [{key:1,value:'value1'},{key:2,value:'value2'},{key:3,value:'value3'}]; And a variable that corresponds to key. For instance: $scope.a = 3; In the Controller, you want ...

NodeJs - Issue: Headers cannot be changed once they are sent & TypeError: req.next is undefined

My goal is to retrieve data from a MySQL database by calling methods to insert and read information. The reason I am doing this is because node.js operates asynchronously. Here is my code: exports.list = function(req, res){ var moduleRows; req.getCo ...

Upon invoking the useEffect function, the default value can be seamlessly established within the input field of the antd library

I have a function called loadProfile that I need to execute within the useEffect hook. When this function is triggered, I want the name Mario to be automatically displayed in the input field labeled name. How can I set this default value using the antd lib ...

Tips for breaking up array elements across multiple "tr" tags

In this particular example, I have a Fiddle set up with multiple tr elements displayed within an ng-repeat. My goal is to have three td elements per row. Would it be necessary to use an inner angularjs ng-repeat and split the iconDets array for this purpos ...