Forum Discussion

jmn3's avatar
jmn3
Cloud Apps
06-07-2022

Change Legend Category Names

Does anyone know of a script that can be used to change the names of category values for a pie chart?

I have the chart below which uses a binary value and I would like to replace these values with text.

 

5 Replies

Replies have been turned off for this discussion
  • harikm007's avatar
    harikm007
    Data Warehouse

    Hi jmn3 ,

    Try this script:

    Refer: https://www.binextlevel.com/post/replace-x-axis-labels-in-a-widget

    newItemMapping = { '1':'Jan',
    				  '2':'Feb',
    				  '3':'Mar',
    				  '4':'Apr',
    				  '5':'May'
    				}
    
    widget.on("queryend", function(se, ev){
    	
    	var panelName = 'Day Number' //Items in this panel will be replaced with new items
    	
    	panelIndex = ev.rawResult.headers.indexOf(panelName)
    	$.each(ev.rawResult.values, function(index, value){
    
    		
    		if(newItemMapping[value[panelIndex].text] != undefined)
    		{
    			value[panelIndex].data = newItemMapping[value[panelIndex].data]
    			value[panelIndex].text = newItemMapping[value[panelIndex].text]
    		}
    	})	
    })

    - Hari

     

    • jmn3's avatar
      jmn3
      Cloud Apps

      Thank you harikm007.

      I can't see to get this to work on the pie chart. I modified the code to match my data and category name but can't see to get it to work on my pie chart widget:

      newItemMapping = { '0':'no scanback',
      				  '1':'scanback'
      				}
      
      widget.on("queryend", function(se, ev){
      	
      	var panelName = 'scanback_uploaded' //Items in this panel will be replaced with new items
      	
      	panelIndex = ev.rawResult.headers.indexOf(panelName)
      	$.each(ev.rawResult.values, function(index, value){
      
      		
      		if(newItemMapping[value[panelIndex].text] != undefined)
      		{
      			value[panelIndex].data = newItemMapping[value[panelIndex].data]
      			value[panelIndex].text = newItemMapping[value[panelIndex].text]
      		}
      	})	
      })

       

       

       

       

       

      • harikm007's avatar
        harikm007
        Data Warehouse

        Can you share a screenshot of left-side panel of widget? Just to confirm how you added the panel (if there is multiple value panels)?

        -Hari

  • Ophir_Buchman's avatar
    Ophir_Buchman
    Data Integration

    Hi jmn3 

    One approach is to code the solution (as harikm007 suggested). The other, is in the data model:

    Create a dimension table that contains two columns of 0-false and 1-true and connect it to the binary field. Then - use that field for the pie chart grouping 

  • Clay's avatar
    Clay
    Cloud Apps

    Hello.  This script works, however it breaks the slice/filter option of the pie charts by adding the custom values to the filter list instead of using the original values when clicked.  Is there a way to modify the script to use the original values for the "click to filter"?

    Edit: Removing the line that updates the data portion of the widget seems to have fixed this.  Haven't extensively tested to see if it affects anything else though.

    Remove this line: 

    value[panelIndex].data = newItemMapping[value[panelIndex].data]