# Custom metric for aggregation based visualizations

**URL:** https://discuss.elastic.co/t/custom-metric-for-aggregation-based-visualizations/284210
**Category:** Kibana
**Created:** [September 14, 2021, 5:19pm UTC](https://discuss.elastic.co/t/custom-metric-for-aggregation-based-visualizations/284210 "2021-09-14T17:19:43Z")
**Posts on this page:** 15
**Page:** 1

<div class="post-metadata">

### Author: ![pchakour](https://avatars.discourse-cdn.com/v4/letter/p/e36b37/32.png) [@pchakour](https://discuss.elastic.co/u/pchakour)
#### Post date: [September 14, 2021, 5:19pm UTC](https://discuss.elastic.co/t/custom-metric-for-aggregation-based-visualizations/284210/1 "2021-09-14T17:19:43Z")

</div>

Hello 👋

I'm trying to migrate the famous plugin [datasweet formula](https://github.com/datasweet/kibana-datasweet-formula) but I'm stuck.

I created a custom metric using the registerMetric function from the data plugin and the metric appears except options. I want to create "custom options" like select and textarea but I can't find a way to do it.

Any ideas ? 🙂

---

<div class="post-metadata">

### Author: ![warkolm](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/warkolm/32/39224_2.png) [@warkolm](https://discuss.elastic.co/u/warkolm)
#### Post date: [September 14, 2021, 10:26pm UTC](https://discuss.elastic.co/t/custom-metric-for-aggregation-based-visualizations/284210/2 "2021-09-14T22:26:54Z")

</div>

That's a pretty old plugin by the looks of it, and it's not a core one, so you'd need to approach the developer.

I don't know Vega intimately, but I'd suggest that might be a better approach as it can do custom functions.

---

<div class="post-metadata">

### Author: ![stephenb](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/stephenb/32/40856_2.png) [@stephenb](https://discuss.elastic.co/u/stephenb)
#### Post date: [September 14, 2021, 10:39pm UTC](https://discuss.elastic.co/t/custom-metric-for-aggregation-based-visualizations/284210/3 "2021-09-14T22:39:13Z")

</div>

Have you looked at custom formulas in Lens?

> **[Lens | Kibana Guide \[7.14\] | Elastic](https://www.elastic.co/guide/en/kibana/current/lens.html#lens-formulas)**

> **[What’s new in Kibana 7.14: More visualization capabilities for ad hoc analysis](https://www.elastic.co/blog/whats-new-kibana-7-14-0)**
>
> Kibana 7.14 delivers more capabilities to easily compare data from different time periods and reshape your data by authoring metrics and editing maps. Elastic Agent, the unified agent, and Fleet, the Kibana app for centralized management, are GA.

---

<div class="post-metadata">

### Author: ![warkolm](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/warkolm/32/39224_2.png) [@warkolm](https://discuss.elastic.co/u/warkolm)
#### Post date: [September 14, 2021, 10:40pm UTC](https://discuss.elastic.co/t/custom-metric-for-aggregation-based-visualizations/284210/4 "2021-09-14T22:40:25Z")

</div>

Ahh yeah, even easier!

---

<div class="post-metadata">

### Author: ![pchakour](https://avatars.discourse-cdn.com/v4/letter/p/e36b37/32.png) [@pchakour](https://discuss.elastic.co/u/pchakour)
#### Post date: [September 15, 2021, 9:48am UTC](https://discuss.elastic.co/t/custom-metric-for-aggregation-based-visualizations/284210/5 "2021-09-15T09:48:19Z")

</div>

This is a good alternative to datasweet, thank you.

But, I have the same problem with an other custom metric and I don't understand why I'm not able to create specific params for my metrics anymore 😭

Do you know if the lens team plans to add the ability to create custom functions and visualizations ?

---

<div class="post-metadata">

### Author: ![pchakour](https://avatars.discourse-cdn.com/v4/letter/p/e36b37/32.png) [@pchakour](https://discuss.elastic.co/u/pchakour)
#### Post date: [September 15, 2021, 9:50am UTC](https://discuss.elastic.co/t/custom-metric-for-aggregation-based-visualizations/284210/6 "2021-09-15T09:50:42Z")

</div>

Vega is a visualization plugin. When you create a visualization plugin, you have the ability to create your own configuration panel.

In my case, I want only create a metric accessible by all agregations based visualization.

---

<div class="post-metadata">

### Author: ![stephenb](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/stephenb/32/40856_2.png) [@stephenb](https://discuss.elastic.co/u/stephenb)
#### Post date: [September 15, 2021, 1:38pm UTC](https://discuss.elastic.co/t/custom-metric-for-aggregation-based-visualizations/284210/7 "2021-09-15T13:38:54Z")

</div>

> [@pchakour](#):
>
> But, I have the same problem with an other custom metric and I don't understand why I'm not able to create specific params for my metrics anymore 😭

Perhaps if you give more detail / example we could help, I am not sure what you are asking..

Can you provide an example and the equation

---

<div class="post-metadata">

### Author: ![pchakour](https://avatars.discourse-cdn.com/v4/letter/p/e36b37/32.png) [@pchakour](https://discuss.elastic.co/u/pchakour)
#### Post date: [September 15, 2021, 2:20pm UTC](https://discuss.elastic.co/t/custom-metric-for-aggregation-based-visualizations/284210/8 "2021-09-15T14:20:11Z")

</div>

You're aright, it will be easier with an example 🙂

To illustrate my problem, I created a custom metric that display a specific value.  
This custom metric has two parameters. The first one is an string input to provide the value to display and the second one is a select containing some options.

To set up this example, I used a MetricAggType object as following :

```auto
new MetricAggType({
    name: 'custom_metric',
    expressionName: aggCustomFnName,
    title: 'Custom metric',
    type: 'metrics',
    subtype: 'Customs',
    hasNoDsl: true,
    makeLabel(aggConfig) {
      return 'Custom ' + aggConfig.id;
    },
    params: [
      {
        name: 'value',
        displayName: 'Value',
        type: 'string',
      },
      {
        name: 'option',
        displayName: 'Option',
        type: 'optioned',
        options: [
          {
            text: i18n.translate('option1', {
              defaultMessage: 'Option1',
            }),
            isCompatible: true,
            disabled: false,
            value: 'option1',
          },
          {
            text: i18n.translate('option2', {
              defaultMessage: 'Option2',
            }),
            isCompatible: true,
            disabled: false,
            value: 'option2',
          },
        ],
        write: _.noop,
      },
    ],
    getSerializedFormat(agg) {
      return fieldFormats.getDefaultInstance(KBN_FIELD_TYPES.NUMBER);
    },
    getValue(agg: any, bucket: any) {
      return agg.params.value;
    },
  });

```

Unfortunately, the select is not displayed :

 ![Capture d’écran du 2021-09-15 16-12-38](https://us1.discourse-cdn.com/elastic/original/3X/5/c/5c00dd5de2bbb80f76e5043705a84d9258995d04.png)

In previous version of Kibana, I can use the editorComponent property to render parameters but this property was removed.

---

<div class="post-metadata">

### Author: ![stephenb](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/stephenb/32/40856_2.png) [@stephenb](https://discuss.elastic.co/u/stephenb)
#### Post date: [September 15, 2021, 2:45pm UTC](https://discuss.elastic.co/t/custom-metric-for-aggregation-based-visualizations/284210/9 "2021-09-15T14:45:14Z")

</div>

Apologies I am a bit dense this AM I am not sure what the resulting Visualization is supposed to look like.

Were is the select option supposed to be on the display? Does it change the Color.. is used at the label?

What you are showing is not Lens and not the Custom Formula Section which you should be able to access multiple fields

That said Vega is extremely flexible perhaps take a look at that.

---

<div class="post-metadata">

### Author: ![toog](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/toog/32/94659_2.png) [@toog](https://discuss.elastic.co/u/toog)
#### Post date: [September 16, 2021, 9:30am UTC](https://discuss.elastic.co/t/custom-metric-for-aggregation-based-visualizations/284210/10 "2021-09-16T09:30:48Z")

</div>

I'm currently in the same use case as pchakour, I made a custom metric named stats to use the power of elasticsearch to make a single query and retrieve all results from the extended\_stats metric. The user can select the part of the answer they don't want or all the results.

This metric helps to reduce the visualization setup when the user wants multiple results on a single field.

With the new version of Kibana, we cannot insert this custom metric without patching the code. It's sad to close to the community the ability to enrich the solution.

 ![stats](https://us1.discourse-cdn.com/elastic/original/3X/5/a/5a5fd04382075ed144c4f5be1dd5305d3d26ac74.png)

---

<div class="post-metadata">

### Author: ![toog](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/toog/32/94659_2.png) [@toog](https://discuss.elastic.co/u/toog)
#### Post date: [September 16, 2021, 9:33am UTC](https://discuss.elastic.co/t/custom-metric-for-aggregation-based-visualizations/284210/11 "2021-09-16T09:33:58Z")

</div>

Would it be possible to open the solution to make a custom metric integration in Lens or in all visualizations as before?

---

<div class="post-metadata">

### Author: ![stephenb](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/stephenb/32/40856_2.png) [@stephenb](https://discuss.elastic.co/u/stephenb)
#### Post date: [September 16, 2021, 3:46pm UTC](https://discuss.elastic.co/t/custom-metric-for-aggregation-based-visualizations/284210/12 "2021-09-16T15:46:10Z")

</div>

@ghudgins Any Thoughts on the Above? I am unclear?

---

<div class="post-metadata">

### Author: ![ghudgins](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ghudgins/32/138532_2.png) [@ghudgins](https://discuss.elastic.co/u/ghudgins)
#### Post date: [September 16, 2021, 8:01pm UTC](https://discuss.elastic.co/t/custom-metric-for-aggregation-based-visualizations/284210/13 "2021-09-16T20:01:35Z")

</div>

> In previous version of Kibana, I can use the editorComponent property to render parameters but this property was removed.

I don't think we _intentionally_ closed anything off, I'm not familiar with this plugin or how it works but if it's targeting DOM elements via CSS to add the select option there's a chance the CSS changed and the plugin needs updating. I'll see if I can try it out

Also generally will be looking into this plugin's unique abilities over things like [runtime fields](https://www.elastic.co/guide/en/elasticsearch/reference/current/runtime.html) and lens custom formula to see if there's an enhancement request in there.

> The user can select the part of the answer they don't want or all the results.

it sounds like adding the controls to the editor is the key difference?

---

<div class="post-metadata">

### Author: ![pchakour](https://avatars.discourse-cdn.com/v4/letter/p/e36b37/32.png) [@pchakour](https://discuss.elastic.co/u/pchakour)
#### Post date: [September 20, 2021, 12:48pm UTC](https://discuss.elastic.co/t/custom-metric-for-aggregation-based-visualizations/284210/14 "2021-09-20T12:48:54Z")

</div>

> [@ghudgins](#):
>
> I'll see if I can try it out

Ok thanks ! I will continue on my side... I'm also taking a look to Lens Formula to replace Datasweet formula.

> [@toog](#):
>
> Would it be possible to open the solution to make a custom metric integration in Lens or in all visualizations as before?

It will be great !! Lens will become more popular and extensibility will be the key for the community to improve Lens.

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)
#### Post date: [October 18, 2021, 12:49pm UTC](https://discuss.elastic.co/t/custom-metric-for-aggregation-based-visualizations/284210/15 "2021-10-18T12:49:28Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
