mirror of
https://github.com/grafana/grafana.git
synced 2025-02-15 10:03:33 -06:00
* move to core where possible * update imports * ignore import order for now * add graveyard files * update codeowners
45 lines
1.1 KiB
TypeScript
45 lines
1.1 KiB
TypeScript
import React, { useCallback, useContext } from 'react';
|
|
|
|
import { DataFrame, DataFrameFieldIndex, Field } from '@grafana/data';
|
|
|
|
import { XYFieldMatchers } from './types';
|
|
|
|
/** @alpha */
|
|
interface GraphNGContextType {
|
|
mapSeriesIndexToDataFrameFieldIndex: (index: number) => DataFrameFieldIndex;
|
|
dimFields: XYFieldMatchers;
|
|
data: DataFrame;
|
|
}
|
|
|
|
/** @alpha */
|
|
export const GraphNGContext = React.createContext<GraphNGContextType>({} as GraphNGContextType);
|
|
|
|
/**
|
|
* @alpha
|
|
* Exposes API for data frame inspection in Plot plugins
|
|
*/
|
|
export const useGraphNGContext = () => {
|
|
const { data, dimFields, mapSeriesIndexToDataFrameFieldIndex } = useContext<GraphNGContextType>(GraphNGContext);
|
|
|
|
const getXAxisField = useCallback(() => {
|
|
const xFieldMatcher = dimFields.x;
|
|
let xField: Field | null = null;
|
|
|
|
for (let j = 0; j < data.fields.length; j++) {
|
|
if (xFieldMatcher(data.fields[j], data, [data])) {
|
|
xField = data.fields[j];
|
|
break;
|
|
}
|
|
}
|
|
|
|
return xField;
|
|
}, [data, dimFields]);
|
|
|
|
return {
|
|
dimFields,
|
|
mapSeriesIndexToDataFrameFieldIndex,
|
|
getXAxisField,
|
|
alignedData: data,
|
|
};
|
|
};
|