hiding the zero value in a chart legend

Hi - I have been trying to figure out how to hide the zero value and label in my legend of the pie chart. I found the below code in the am chart documentation. Isn’t working for me. Does anyone have any ideas?

“legend”: {
“useGraphSettings”: true,
“labelFunction”: function(graphDataItem, labelText) {
if (graphDataItem.values.value == 0) {
return “”;
}
return labelText;
}
}
});

When I put this in the chart properties source tab I get an error saying -'Expected ‘a’ at 5.23 (that location is the ‘u’ in the word Function).

So then I try to place in the script. Obviously not doing it right. I get an error saying ‘Invalid Return’ -

else if (json.type == “pie”) {
// Pie chart
json.valueField = “value”;
json.titleField = “title”;
json.colorField = “color”;
json.dataProvider = ;

let rec = detailTable && record.tables[detailTable] ? record.tables[detailTable][0] : record;
if (rec) {
	for (let graphIndex in graphs) {
		let graph = graphs[graphIndex];
		let data = getCopy(dataTemplate, graphIndex);
		data.value = rec.fields[graph.field];
		data.title = graph.title;
		if (!json.theme && !json.baseColor && !json.colors) {
			data.color = graph.color;
		}
		if (graphDataItem.values.value == 0) {
        return "";
        }
        return labelText;

		json.dataProvider.push(data);
	}
}

Expanding the Bar Script to a Standard Script and modifying the JavaScript code as applied to the Standard Script sounds like the easiest way to me.

Example

// [...]
for (let graphIndex in graphs) {
	let field = record.fields[graphs[graphIndex].field];
	
	if (field > 0) {
		let data = getCopy(dataTemplate, 0);
		
		data.value = field;
		data.category = graphs[graphIndex].title;
		
		if (!json.theme && !json.colors) {
			data.color = graphs[graphIndex].color;
		}
		
		json.dataProvider.push(data);
	}
}

results.attr("data-amchart", JSON.stringify(json));

Example of modified JavaScript code, by which only graphs with a data value greater than 0 will be added (Example_20240920.zip).

Hi - I tried this and it gives me the following error on this line -

let field = record.fields[graphs[graphIndex].field];

error - 'Unknown field - ‘test1’, 'Unknown field - ‘test2’,'Unknown field - ‘test3’,'Unknown field - ‘test4’,'Unknown field - ‘test5’

That’s correct. The reason why is because your Data Model most certainly doesn’t contain the record fields test1 until test5.

Please note that the shared JavaScript code is just an example. You will have to expand your own Bar Script to a Standard Script and change the JavaScript code, as applied to the Standard Script, so that the code will first check if the data value is greater than 0 before creating a graph for it.

Attached you will find a simple example. Please note that this is just an example and that you will to change the JavaScript code differently to get it work in your situation.

Example_20240920.OL-package

Thank you. I should have also said that I received the same errors when I used my data. I haven’t been updated to the newest version of OL yet. Any change of getting the sample in in 2022.2? Sorry.

Sure, no problem.

example_20240924.OL-datamapper
example_20240924.OL-template

Thank you for your sample. I really appreciate it. I can get the zero’s to hide in the chart. My issue is getting the zero’s to hide in the actual legend. The zero’s are not showing in the pie chart but are in the legend. Any ideas?

image

Thanks

Hi - just wondering if anyone has any ideas? Is the only solution to hide the legend and build a table to mimic the legend? Thanks

It is possible to hide both bars and the corresponding information in the legend by expanding the Chart Script to a Standard Script and by applying changes to the JavaScript code applied to the expanded Chart Script.

Caution: I would not recommend to expand a Chart Script to a Standard Script and make any changes to the JavaScript code of the expanded Chart Script unless you understand what the applied JavaScript code (better).

Example

Before

bar-chart-before

After

bar-chart-after

Example_20241001.OL-datamapper
Example_20241001.OL-template

Thank you for this. I will definitely save this and learn from this! For now I will accept the zero’s in the legend. One last question, I use percentPrecision = 1 to display the percents to one decimal point in the pie chart but can’t figure out how to show the percents to one decimal point. I have tried using the percentPrecision in a few different places. The legends are giving me trouble!! UGH.

Thanks

Please note that the most recent provided example does not include ‘zero values’ in the legend. :slight_smile:

Yes - I saw that. I look at the code. But as you indicated if you don’t understand the scripting better to be careful. I am learning but wasn’t sure how to use it with the data i have. I saved it and am using it to learn what it’s doing so I can do that in the future. Currently, I am hiding the value’s below 2% by using the ‘hideLabelsPercent = 3’. Now I just need to get the below to one decimal point. I tried 'percentPrecision = 1 for the legend, it doesn’t work. Works for the chart but not the legend. For example:
2.14 to 2
.053 to .5

image