# Horizontal Line on Visualization Based on Max of Query Result

**URL:** <https://discuss.elastic.co/t/horizontal-line-on-visualization-based-on-max-of-query-result/309559>\
**Category:** Kibana\
**Tags:** timelion\
**Created:** [July 13, 2022, 4:56pm UTC](https://discuss.elastic.co/t/horizontal-line-on-visualization-based-on-max-of-query-result/309559 "2022-07-13T16:56:49Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![LisaJ](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/lisaj/32/108271_2.png) [@LisaJ](https://discuss.elastic.co/u/LisaJ)\
**Post date:** [July 13, 2022, 4:56pm UTC](https://discuss.elastic.co/t/horizontal-line-on-visualization-based-on-max-of-query-result/309559/1 "2022-07-13T16:56:49Z")

</div>

I am looking for some way chart a horizontal line at some value determined by a query. I have a time series line chart showing dB loss values across the time series. There is another index that stores _baseline_ dB loss values for the same locations. I want to draw a horizontal line at the baseline value for the location -- so someone can quickly see if the current scans are pretty close to "as good as it's ever going to get" or if the current scans show a potential problem.

I've tried both Vega and TimeLion (we're using ES7, so MathLion doesn't appear to be an option). In TimeLion, I can use .value(10) to get a horizontal line at 10, so that seemed like a good direction. I just need to populate .value() based on a search instead of a static value. But my attempts haven't yielded anything.

```auto
.es(index= db_traces-*,
    timefield='@timestamp',
    q='locationname:SampleLocation1',
    metric='max:KeyEvents.Summary.loss')
  .label('Total Loss')
  .title('Loss History')
  .yaxis(1,position=left)
  .lines(width=2).color(purple)
  .fit(none)
,
.value(.es(index= db_baselines-*,
    q='locationname:SampleLocation1',
    metric='max:KeyEvents.Summary.loss'))
  .lines(width=3)
  .color(maroon)
  .label('Baseline Loss')

```

---

<div class="post-metadata">

**Author:** ![LeeDr](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/leedr/32/9289_2.png) [@LeeDr](https://discuss.elastic.co/u/LeeDr)\
**Post date:** [July 15, 2022, 8:16pm UTC](https://discuss.elastic.co/t/horizontal-line-on-visualization-based-on-max-of-query-result/309559/2 "2022-07-15T20:16:09Z")

</div>

Hi Lisa,

I'm not sure what version of Kibana you're on. The most recent release is 8.3.2. On that version, in Lens, you can add layers of data from different indices. Looks like this feature was available at least as far back as 7.17, maybe further.  
There's an option for Reference lines, but to use data from another index I think you would need to use the `Visualization` option.

 ![image](https://us1.discourse-cdn.com/elastic/original/3X/9/2/924f53e3b22b8a241f2d3edd0e0735d75df8b722.png)

I don't really have good data in different indices to demo this but here's an example where there are two layers that both come from the same index;

 ![image](https://us1.discourse-cdn.com/elastic/original/3X/d/1/d1b92ed05aab4e8c945a249f1d2e9662ecdd9016.jpeg)

---

<div class="post-metadata">

**Author:** ![LisaJ](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/lisaj/32/108271_2.png) [@LisaJ](https://discuss.elastic.co/u/LisaJ)\
**Post date:** [July 18, 2022, 7:25pm UTC](https://discuss.elastic.co/t/horizontal-line-on-visualization-based-on-max-of-query-result/309559/3 "2022-07-18T19:25:45Z")

</div>

Thank you for the information -- we're running 7.7.0, but we wanted to upgrade to the latest 7.x iteration in the mid-term ... so I'll try this out in my lab and see if "wait until we upgrade" is a viable answer (or, more realistically, expedite this upgrade because we want our graphs!).

---

<div class="post-metadata">

**Author:** ![LisaJ](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/lisaj/32/108271_2.png) [@LisaJ](https://discuss.elastic.co/u/LisaJ)\
**Post date:** [July 22, 2022, 8:09pm UTC](https://discuss.elastic.co/t/horizontal-line-on-visualization-based-on-max-of-query-result/309559/4 "2022-07-22T20:09:51Z")

</div>

I was finally able to figure out a set of data contortions that allowed me to draw a horizontal line over the extent of the graph. It's a bit convoluted, but I figured I'd post the approach here in case anyone else with older iterations of ES gets hung up trying to draw a horizontal line.

My data elements look like this -- for both scans and baselines:

```auto
            { "@timestamp": "2022-05-26T15:10:14.395Z", "loss": 13.558},

```

I added transforms to my scan data to ensure each record has a _max_ datetime and then used a lookup from the baseline data to retrieve the value from my scan table. Since the lookup method needs to find an exact match, I appended a static value to all of the records too. Pi ... because mmm, pie!

```auto
        "transform":[
                {"type": "formula", "expr": "datetime(datum['@timestamp'])", "as": "transformedtimestamp"}
               , {"type": "joinaggregate", "fields": ["@timestamp"], "ops": ["max"], "as": ["maxtime"]}
               , {"type": "formula", "as": "pi", "expr": "PI"}
    ] 

```

Now my scan data includes a few extra elements:

 ![image](https://us1.discourse-cdn.com/elastic/original/3X/d/c/dcd89a7b0b227941326efa39cd0aac4129b20386.png)

Then I used a lookup from the baseline data -- fortunately this seems to work fine even though there are multiple matching records.

```auto
{"type": "lookup", "from": "scandata", "key": "pi", "fields": ["pi"], "values": ["maxtime"], "as": ["maxtime"]}

```

Since my baseline data contains the max date for the x-axis:

 ![image](https://us1.discourse-cdn.com/elastic/original/3X/8/2/8236664f9b4b39e551174798fe2f948d5db4d619.png)

I can use a 'rule' mark to draw the line at _loss value_ from 0 to _max time value_:

```auto
 {
            "type": "rule",
            "from": {"data": "baseline"},
            "encode": {
              "enter": {
                "stroke": {"value": "#652c90"},
                "x": {"scale": "x", "value": 0},
                "y": {"scale": "y", "type": "quantitative","field": "loss"},
                "x2": {"scale": "x","field": "maxtime", "type": "temporal"},
                "strokeWidth": {"value": 4},
                "opacity": {"value": 0.3}
              }
            }
          }

```

---

<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:** [August 19, 2022, 8:09pm UTC](https://discuss.elastic.co/t/horizontal-line-on-visualization-based-on-max-of-query-result/309559/5 "2022-08-19T20:09:58Z")

</div>

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