You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

63 lines
1.9 KiB

import { Options } from '../models/options';
import { Series } from '../models/series';
import { PanelOptions } from '../types';
import { DataProcessor } from '../grafana/data_processor';
3 years ago
import { ChartwerkGaugePod } from '@chartwerk/gauge-pod';
3 years ago
import { PanelData, TimeRange, PanelProps } from '@grafana/data';
3 years ago
import React, { useRef } from 'react';
import { css } from 'emotion';
import * as _ from 'lodash';
3 years ago
interface Props extends PanelProps<PanelOptions> {}
export function Panel({ options, data, width, height, timeZone, timeRange, onChangeTimeRange }: Props) {
console.log('options', options);
const grafanaSeriesList = getGrafanaSeriesList(data, timeRange);
const series = new Series(grafanaSeriesList, options.gauge.value).getChartwerkSeries();
console.log('series', series);
const chartwerkOptions = new Options(grafanaSeriesList, options).getChartwerkOptions();
3 years ago
let chartContainer = useRef(null);
3 years ago
// we request animation frame here because we need an existing DOM-element at the moment we render the pod
3 years ago
window.requestAnimationFrame(() => {
// TODO: switch / case pod type
3 years ago
const pod = new ChartwerkGaugePod((chartContainer as any).current, series, chartwerkOptions);
3 years ago
pod.render();
});
3 years ago
const isLinkActive = !_.isEmpty(options.gauge.link);
const chartClickHandler = (event: React.MouseEvent<HTMLDivElement>) => {
event.preventDefault();
console.log('click', options);
if (!isLinkActive) {
return;
}
window.open(options.gauge.link, "_self");
};
return (
<div
ref={chartContainer}
className={css`
3 years ago
width: ${width}px;
height: ${height}px;
3 years ago
cursor: ${isLinkActive ? 'pointer' : 'default'}
`}
3 years ago
onClick={chartClickHandler}
></div>
);
}
function getGrafanaSeriesList(grafanaData: PanelData, timeRange: TimeRange): any[] {
const processor = new DataProcessor({});
return processor.getSeriesList({
dataList: grafanaData.series,
range: timeRange,
});
}