Skip to content

Commit

Permalink
Merge pull request #10 from andymchugh/bgcolor
Browse files Browse the repository at this point in the history
Bgcolor
  • Loading branch information
andymchugh authored Apr 12, 2024
2 parents 0060c9b + 8311489 commit 891df79
Show file tree
Hide file tree
Showing 8 changed files with 234 additions and 9 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ Fixes x-scaling ratio when the SVG x-dimension is smaller than the available
window. Now it scales with the window whereas before it scaled at twice the rate
resulting in it being much smaller than necessary.

Adds the ability to drive the SVG background. New panelConfig terms:
- background.darkThemeColor
- background.lightThemeColor
Can be used with the normal color-names, rbg, hex values. When the relevant term is
undefined the background color is not driven.

## 1.5.0
Fixes the grafana variable threshold matching to break out on first rule
match for a given variable/cell tuple. Before it was continuing through
Expand Down
4 changes: 4 additions & 0 deletions provisioning/dashboardData/backgroundColor.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
182 changes: 182 additions & 0 deletions provisioning/dashboards/backgroundColor.json

Large diffs are not rendered by default.

7 changes: 7 additions & 0 deletions src/components/Config.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ export type Link = {
params: LinkUrlParams;
};

export type Background = {
darkThemeColor: string | undefined;
lightThemeColor: string | undefined;
}

export type PanelConfigCellLabel = {
dataRef: string | undefined;
separator: LabelSeparator;
Expand Down Expand Up @@ -49,6 +54,7 @@ export type SiteConfig = {
};

export type PanelConfig = {
background: Background;
variableThresholdScalars: Map<string, VariableThresholdScalars[]>;
gradientMode: ColorGradientMode;
cellIdPreamble: string;
Expand All @@ -60,6 +66,7 @@ export type PanelConfig = {
export function panelConfigFactory(config: any) {
config = config || {};
return {
background: config.background || {},
variableThresholdScalars: new Map<string, VariableThresholdScalars[]>(Object.entries(config.variableThresholdScalars || {})),
gradientMode: config.gradientMode || 'none',
cellIdPreamble: config.cellIdPreamble || '',
Expand Down
5 changes: 2 additions & 3 deletions src/components/FlowPanel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { svgInit, svgUpdate, SvgHolder } from 'components/SvgUpdater';
import { seriesExtend, seriesInterpolate , seriesTransform } from 'components/TimeSeries';
import { TimeSliderFactory } from 'components/TimeSlider';
import { displayColorsInner, displayDataInner, displayMappingsInner, displaySvgInner } from 'components/DebuggingEditor';
import { primeColorCache, appendUrlParams, getInstrumenter } from 'components/Utils';
import { appendUrlParams, getInstrumenter } from 'components/Utils';
import { addHook, sanitize } from 'dompurify';

interface Props extends PanelProps<FlowOptions> {}
Expand Down Expand Up @@ -133,8 +133,7 @@ export const FlowPanel: React.FC<Props> = ({ options, data, width, height, timeZ
configInit(siteConfig, panelConfig);

const svgDoc = new DOMParser().parseFromString(sanitizeSvgStr(svgStr), "text/xml");
const svgAttribs = svgInit(svgDoc, panelConfig, siteConfig);
primeColorCache(grafanaTheme.current, svgAttribs);
const svgAttribs = svgInit(svgDoc, grafanaTheme.current, panelConfig, siteConfig);
svgHolderRef.current = {
doc: svgDoc,
attribs: svgAttribs,
Expand Down
22 changes: 18 additions & 4 deletions src/components/SvgUpdater.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { getValueFormatterIndex, formattedValueToString } from '@grafana/data';
import { getValueFormatterIndex, formattedValueToString, GrafanaTheme2 } from '@grafana/data';
import {
LabelSeparator, Link,
PanelConfig, PanelConfigCell, PanelConfigCellColor, PanelConfigCellLabel,
SiteConfig, VariableThresholdScalars } from 'components/Config';
import { TimeSeriesData } from 'components/TimeSeries';
import {
cellIdFactory, CellIdMaker, getColor,
cellIdFactory, CellIdMaker, colorLookup, getColor,
primeColorCache,
variableThresholdScalarsInit, variableThresholdScaleValue } from 'components/Utils';

// Defines the metadata stored against each drivable svg cell
Expand Down Expand Up @@ -109,7 +110,7 @@ function recurseElements(el: HTMLElement, cellData: SvgCell, cellIdMaker: CellId
return false;
}

export function svgInit(doc: Document, panelConfig: PanelConfig, siteConfig: SiteConfig): SvgAttribs {
export function svgInit(doc: Document, grafanaTheme: GrafanaTheme2, panelConfig: PanelConfig, siteConfig: SiteConfig): SvgAttribs {
let cells = new Map<string, SvgCell>();
const cellIdPreamble = panelConfig.cellIdPreamble;
panelConfig.cells.forEach((cellProps, cellIdShort) => {
Expand Down Expand Up @@ -155,14 +156,27 @@ export function svgInit(doc: Document, panelConfig: PanelConfig, siteConfig: Sit
// image won't scale and center corrently
let dimensions = dimensionCoherence(doc);

return {


const svgAttribs = {
width: dimensions.width,
height: dimensions.height,
scaleDrive: dimensions.scaleDrive,
cells: cells,
elementLinks: elementLinks,
variableValues: variableValues,
};

// Initialie the color cache and setup the background
primeColorCache(grafanaTheme, svgAttribs, panelConfig.background);

// Set background according to theme if defined in config
const bgColor = grafanaTheme.isDark ? panelConfig.background.darkThemeColor : panelConfig.background.lightThemeColor;
if (bgColor) {
doc.documentElement.style.backgroundColor = colorLookup(bgColor);
}

return svgAttribs;
}

function getCellValue(tsName: string, tsData: TimeSeriesData) {
Expand Down
11 changes: 9 additions & 2 deletions src/components/Utils.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { GrafanaTheme2, colorManipulator } from '@grafana/data';
import { SvgAttribs, SvgCell } from 'components/SvgUpdater'
import { PanelConfigCellColor, Threshold ,VariableThresholdScalars } from 'components/Config';
import { Background, PanelConfigCellColor, Threshold ,VariableThresholdScalars } from 'components/Config';



Expand Down Expand Up @@ -66,7 +66,7 @@ function rgbToString(rgb: number[]) {
return `rgb(${rgb[0]}, ${rgb[1]}, ${rgb[2]})`;
}

export function primeColorCache(theme: GrafanaTheme2, svgAttribs: SvgAttribs) {
export function primeColorCache(theme: GrafanaTheme2, svgAttribs: SvgAttribs, background: Background) {
function initCache(thresholds: Threshold[] | undefined) {
if (thresholds) {
thresholds.forEach(function(threshold) {
Expand All @@ -79,6 +79,13 @@ export function primeColorCache(theme: GrafanaTheme2, svgAttribs: SvgAttribs) {
initCache(cellData.cellProps.fillColor && cellData.cellProps.fillColor.thresholds);
initCache(cellData.cellProps.labelColor && cellData.cellProps.labelColor.thresholds);
});

if (background.darkThemeColor) {
colorStringToRgb(theme, background.darkThemeColor);
}
if (background.lightThemeColor) {
colorStringToRgb(theme, background.lightThemeColor);
}
}

export function colorStringToRgb(theme: GrafanaTheme2, colorStr: string) {
Expand Down
6 changes: 6 additions & 0 deletions yaml_defs/panelConfig.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ variableThresholdScalars:
#------------------------------------------------------------------------------
# Panel Config

# 1.6.0 onwards: When undefined the background color is not driven. When defined the color for
# the current theme is used. Colors can be entered in all the normal ways of name, hex, hsl, etc.
background:
darkThemeColor: "yellow"
lightThemeColor: "green"

# This defines the default color gradientMode. It defines the default value on cell labelColor.gradientMode
# and fillColor.gradientMode for when the field is undefined. Values are "hue" and "none" with
# "none" being the default when undefined.
Expand Down

0 comments on commit 891df79

Please sign in to comment.