1import { DataFrame, DataFrameFieldIndex, Field } from '@grafana/data'; 2import { XYFieldMatchers } from './types'; 3import React, { useCallback, useContext } from 'react'; 4 5/** @alpha */ 6interface GraphNGContextType { 7 mapSeriesIndexToDataFrameFieldIndex: (index: number) => DataFrameFieldIndex; 8 dimFields: XYFieldMatchers; 9 data: DataFrame; 10} 11 12/** @alpha */ 13export const GraphNGContext = React.createContext<GraphNGContextType>({} as GraphNGContextType); 14 15/** 16 * @alpha 17 * Exposes API for data frame inspection in Plot plugins 18 */ 19export const useGraphNGContext = () => { 20 const { data, dimFields, mapSeriesIndexToDataFrameFieldIndex } = useContext<GraphNGContextType>(GraphNGContext); 21 22 const getXAxisField = useCallback(() => { 23 const xFieldMatcher = dimFields.x; 24 let xField: Field | null = null; 25 26 for (let j = 0; j < data.fields.length; j++) { 27 if (xFieldMatcher(data.fields[j], data, [data])) { 28 xField = data.fields[j]; 29 break; 30 } 31 } 32 33 return xField; 34 }, [data, dimFields]); 35 36 return { 37 dimFields, 38 mapSeriesIndexToDataFrameFieldIndex, 39 getXAxisField, 40 alignedData: data, 41 }; 42}; 43