2022-05-26 23:06:25 +08:00
---
2023-05-13 02:33:49 +08:00
title: Add features to Explore queries
2022-05-26 23:06:25 +08:00
---
2020-05-12 23:06:14 +08:00
2023-05-13 02:33:49 +08:00
# Add features for Explore queries
2020-05-12 23:06:14 +08:00
2023-05-13 02:33:49 +08:00
[Explore ]({{< relref "../../explore/" >}} ) allows users can make ad-hoc queries without the use of a dashboard. This is useful when they want to troubleshoot or learn more about the data.
2020-05-12 23:06:14 +08:00
2023-05-13 02:33:49 +08:00
Your data source supports Explore by default and uses the existing query editor for the data source. This guide explains how to extend functionality for Explore queries in a data source plugin.
2020-05-12 23:06:14 +08:00
2022-05-05 17:36:50 +08:00
## Add an Explore-specific query editor
2020-05-12 23:06:14 +08:00
2023-05-13 02:33:49 +08:00
To extend Explore functionality for your data source, define an Explore-specific query editor.
2020-05-12 23:06:14 +08:00
2023-05-13 02:33:49 +08:00
1. Create a file `ExploreQueryEditor.tsx` in the `src` directory of your plugin, with content similar to this:
2020-05-12 23:06:14 +08:00
```ts
import React from 'react';
2021-09-15 23:26:23 +08:00
import { QueryEditorProps } from '@grafana/data';
2020-05-12 23:06:14 +08:00
import { QueryField } from '@grafana/ui';
import { DataSource } from './DataSource';
import { MyQuery, MyDataSourceOptions } from './types';
2022-05-05 17:36:50 +08:00
type Props = QueryEditorProps< DataSource , MyQuery , MyDataSourceOptions > ;
2020-05-12 23:06:14 +08:00
export default (props: Props) => {
2022-05-05 17:36:50 +08:00
return < h2 > My Explore-specific query editor< / h2 > ;
2020-05-12 23:06:14 +08:00
};
```
2022-05-05 17:36:50 +08:00
1. Modify your base query editor in `QueryEditor.tsx` to render the Explore-specific query editor. For example:
2020-05-12 23:06:14 +08:00
```ts
2022-05-05 17:36:50 +08:00
// [...]
import { CoreApp } from '@grafana/data';
2020-05-12 23:06:14 +08:00
import ExploreQueryEditor from './ExploreQueryEditor';
2022-05-05 17:36:50 +08:00
type Props = QueryEditorProps< DataSource , MyQuery , MyDataSourceOptions > ;
2020-05-12 23:06:14 +08:00
export default (props: Props) => {
2022-05-05 17:36:50 +08:00
const { app } = props;
switch (app) {
case CoreApp.Explore:
return < ExploreQueryEditor { . . . props } / > ;
default:
return < div > My base query editor< / div > ;
}
2020-05-12 23:06:14 +08:00
};
```
2023-05-13 02:33:49 +08:00
## Select a preferred visualization type
2020-05-12 23:06:14 +08:00
2023-05-13 02:33:49 +08:00
By default, Explore should select an appropriate and useful visualization for your data. It can figure out whether the returned data is time series data or logs or something else, and creates the right type of visualization.
2020-05-12 23:06:14 +08:00
2023-05-13 02:33:49 +08:00
However, if you want a custom visualization, you can add a hint to your returned data frame by setting the `meta' attribute to ` preferredVisualisationType`.
2020-05-12 23:06:14 +08:00
2023-05-13 02:33:49 +08:00
Construct a data frame with specific metadata like this:
2021-08-06 21:52:36 +08:00
2020-05-12 23:06:14 +08:00
```
2021-03-17 00:25:10 +08:00
const firstResult = new MutableDataFrame({
fields: [...],
meta: {
preferredVisualisationType: 'logs',
},
});
2020-05-12 23:06:14 +08:00
```
2021-03-17 00:25:10 +08:00
2022-09-02 00:15:44 +08:00
For possible options, refer to [PreferredVisualisationType ](https://github.com/grafana/grafana/blob/main/packages/grafana-data/src/types/data.ts#L25 ).