# getCategories

A Category model is a data model that represents a list of labeled data points for categorical data. Suitable for charts such as grouped bar charts, pie charts, or tree charts.

## **Usage**

```typescript
const categories = await dataSource.getCategories({
    column: 'categories_column',
    operation: 'count',
    operationColumn: 'values_column',
    operationExp?: string
    // + base options...
});
```

## **Options**

The `getCategories` model inherits all options from the [base options](/carto-for-developers/reference/carto-widgets-reference/models.md#model-base-options), plus:

```typescript
export interface CategoryRequestOptions extends BaseRequestOptions {
  column: string;
  operation: 'count' | 'avg' | 'min' | 'max' | 'sum' | 'custom';
  operationColumn?: string | '*';
  operationExp?: string;
  othersThreshold?: number;
}
```

* **column:** the name of the column that will be used to generate the categories.
* **operation:** the aggregation that will be performed on the `operationColumn`. Accepted values are:
  * `count`
  * `avg`
  * `min`
  * `max`
  * `sum`
  * `custom`
* **operationColumn** (optional): this is the column that will be aggregated for each category in the specified `column` , and it is required when using an operation other than custom.
* **operationExp** (optional): a valid SQL expression to perform a custom aggregation. For example `sum(sales)/sum(area)` . This is only applicable when using a `custom` operation.
* **othersThreshold** (optional): use this parameter to let CARTO group the values in categories after the threshold as "others". This value will be returned in the response as `_carto_others`.

## **Response**

An array of objects where each object is a pair of category name and its corresponding value:

```typescript
type CategoriesModelResponse = {name: string; value: number}[];
/* example response 
    [
        {
            name: 'category_1',
            value: '245'
        },
        {
            name: 'category_2',
            value: '673'
        },
        {
            name: 'category_3',
            value: '987'
        }
    ]  
*/    
```

The response can be then mapped to any charting library to create data visualizations like a bar chart.

<figure><img src="/files/h5xHUdcUtgSRzSoJcmH8" alt="" width="563"><figcaption></figcaption></figure>


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.carto.com/carto-for-developers/reference/carto-widgets-reference/models/getcategories.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
