Count issues group by custom field value

I have a custom field defined and in my Jira plugin I want to create a small bar chart with number of issues in a project per custom field value.

So far I am creating one JQL search per custom field value like project = JRA and cf[10490] = xss and get the number of issues via SearchService.searchCount.

Is there an easier way to get the counts per all values of custom field in one call like in SQL you would have a select count(*) group by customfield?

Is the custom field text or select. If it is a select field, then you should be able to get values group by using gadget on a dashboard. If it is text field, i don’t know if there is any way to do it.

I want to do it for a select field, but not via Gadget, but via Java API call in my addon Java code.

Did you try looking into how gadget does it, you maybe able to use the same API.