Make named colors usable in angular code pt 1

This commit is contained in:
Dominik Prokop 2019-01-21 12:17:23 +01:00
parent 597cbb5b9b
commit 84caf0bc9d
10 changed files with 80 additions and 24 deletions

View File

@ -26,6 +26,7 @@ import ReactDOM from 'react-dom';
import { Legend, GraphLegendProps } from './Legend/Legend'; import { Legend, GraphLegendProps } from './Legend/Legend';
import { GraphCtrl } from './module'; import { GraphCtrl } from './module';
import { GrafanaTheme } from '@grafana/ui';
class GraphElement { class GraphElement {
ctrl: GraphCtrl; ctrl: GraphCtrl;
@ -51,7 +52,10 @@ class GraphElement {
this.panelWidth = 0; this.panelWidth = 0;
this.eventManager = new EventManager(this.ctrl); this.eventManager = new EventManager(this.ctrl);
this.thresholdManager = new ThresholdManager(this.ctrl); this.thresholdManager = new ThresholdManager(this.ctrl);
this.timeRegionManager = new TimeRegionManager(this.ctrl); this.timeRegionManager = new TimeRegionManager(
this.ctrl,
config.bootData.user.lightTheme ? GrafanaTheme.Light : GrafanaTheme.Dark
);
this.tooltip = new GraphTooltip(this.elem, this.ctrl.dashboard, this.scope, () => { this.tooltip = new GraphTooltip(this.elem, this.ctrl.dashboard, this.scope, () => {
return this.sortedSeries; return this.sortedSeries;
}); });

View File

@ -1,6 +1,7 @@
import 'vendor/flot/jquery.flot'; import 'vendor/flot/jquery.flot';
import $ from 'jquery'; import $ from 'jquery';
import _ from 'lodash'; import _ from 'lodash';
import { getColorFromHexRgbOrName } from '@grafana/ui/src/utils/colorsPalette';
export class ThresholdManager { export class ThresholdManager {
plot: any; plot: any;
@ -225,12 +226,12 @@ export class ThresholdManager {
if (threshold.yaxis === 'right' && this.hasSecondYAxis) { if (threshold.yaxis === 'right' && this.hasSecondYAxis) {
options.grid.markings.push({ options.grid.markings.push({
y2axis: { from: threshold.value, to: limit }, y2axis: { from: threshold.value, to: limit },
color: fillColor, color: getColorFromHexRgbOrName(fillColor),
}); });
} else { } else {
options.grid.markings.push({ options.grid.markings.push({
yaxis: { from: threshold.value, to: limit }, yaxis: { from: threshold.value, to: limit },
color: fillColor, color: getColorFromHexRgbOrName(fillColor),
}); });
} }
} }
@ -238,12 +239,12 @@ export class ThresholdManager {
if (threshold.yaxis === 'right' && this.hasSecondYAxis) { if (threshold.yaxis === 'right' && this.hasSecondYAxis) {
options.grid.markings.push({ options.grid.markings.push({
y2axis: { from: threshold.value, to: threshold.value }, y2axis: { from: threshold.value, to: threshold.value },
color: lineColor, color: getColorFromHexRgbOrName(lineColor),
}); });
} else { } else {
options.grid.markings.push({ options.grid.markings.push({
yaxis: { from: threshold.value, to: threshold.value }, yaxis: { from: threshold.value, to: threshold.value },
color: lineColor, color: getColorFromHexRgbOrName(lineColor),
}); });
} }
} }

View File

@ -1,7 +1,13 @@
import 'vendor/flot/jquery.flot'; import 'vendor/flot/jquery.flot';
import _ from 'lodash'; import _ from 'lodash';
import moment from 'moment'; import moment from 'moment';
import config from 'app/core/config'; import { GrafanaTheme } from '@grafana/ui';
import { getColorFromHexRgbOrName } from '@grafana/ui/src/utils/colorsPalette';
type TimeRegionColorDefinition = {
fill: string;
line: string;
};
export const colorModes = { export const colorModes = {
gray: { gray: {
@ -38,31 +44,35 @@ export function getColorModes() {
}); });
} }
function getColor(timeRegion) { function getColor(timeRegion, theme: GrafanaTheme): TimeRegionColorDefinition {
if (Object.keys(colorModes).indexOf(timeRegion.colorMode) === -1) { if (Object.keys(colorModes).indexOf(timeRegion.colorMode) === -1) {
timeRegion.colorMode = 'red'; timeRegion.colorMode = 'red';
} }
if (timeRegion.colorMode === 'custom') { if (timeRegion.colorMode === 'custom') {
return { return {
fill: timeRegion.fillColor, fill: getColorFromHexRgbOrName(timeRegion.fillColor, theme),
line: timeRegion.lineColor, line: getColorFromHexRgbOrName(timeRegion.lineColor, theme),
}; };
} }
const colorMode = colorModes[timeRegion.colorMode]; const colorMode = colorModes[timeRegion.colorMode];
if (colorMode.themeDependent === true) { if (colorMode.themeDependent === true) {
return config.bootData.user.lightTheme ? colorMode.lightColor : colorMode.darkColor; return theme === GrafanaTheme.Light ? colorMode.lightColor : colorMode.darkColor;
} }
return colorMode.color; return {
fill: getColorFromHexRgbOrName(colorMode.color.fill, theme),
line: getColorFromHexRgbOrName(colorMode.color.line, theme),
};
} }
export class TimeRegionManager { export class TimeRegionManager {
plot: any; plot: any;
timeRegions: any; timeRegions: any;
constructor(private panelCtrl) {} constructor(private panelCtrl, private theme: GrafanaTheme = GrafanaTheme.Dark) {}
draw(plot) { draw(plot) {
this.timeRegions = this.panelCtrl.panel.timeRegions; this.timeRegions = this.panelCtrl.panel.timeRegions;
@ -76,7 +86,7 @@ export class TimeRegionManager {
const tRange = { from: moment(this.panelCtrl.range.from).utc(), to: moment(this.panelCtrl.range.to).utc() }; const tRange = { from: moment(this.panelCtrl.range.from).utc(), to: moment(this.panelCtrl.range.to).utc() };
let i, hRange, timeRegion, regions, fromStart, fromEnd, timeRegionColor; let i, hRange, timeRegion, regions, fromStart, fromEnd, timeRegionColor: TimeRegionColorDefinition;
const timeRegionsCopy = panel.timeRegions.map(a => ({ ...a })); const timeRegionsCopy = panel.timeRegions.map(a => ({ ...a }));
@ -200,7 +210,7 @@ export class TimeRegionManager {
} }
} }
timeRegionColor = getColor(timeRegion); timeRegionColor = getColor(timeRegion, this.theme);
for (let j = 0; j < regions.length; j++) { for (let j = 0; j < regions.length; j++) {
const r = regions[j]; const r = regions[j];

View File

@ -35,6 +35,9 @@ export class TimeRegionFormCtrl {
colorMode: 'background6', colorMode: 'background6',
fill: true, fill: true,
line: false, line: false,
// Default colors for new
fillColor: 'rgba(234, 112, 112, 0.12)',
lineColor: 'rgba(237, 46, 24, 0.60)'
}); });
this.panelCtrl.render(); this.panelCtrl.render();
} }

View File

@ -5,6 +5,8 @@ import { contextSrv } from 'app/core/core';
import { tickStep } from 'app/core/utils/ticks'; import { tickStep } from 'app/core/utils/ticks';
import { getColorScale, getOpacityScale } from './color_scale'; import { getColorScale, getOpacityScale } from './color_scale';
import coreModule from 'app/core/core_module'; import coreModule from 'app/core/core_module';
import { getColorFromHexRgbOrName } from '@grafana/ui/src/utils/colorsPalette';
import { GrafanaTheme } from '@grafana/ui';
const LEGEND_HEIGHT_PX = 6; const LEGEND_HEIGHT_PX = 6;
const LEGEND_WIDTH_PX = 100; const LEGEND_WIDTH_PX = 100;
@ -247,7 +249,10 @@ function drawSimpleOpacityLegend(elem, options) {
.attr('width', rangeStep) .attr('width', rangeStep)
.attr('height', legendHeight) .attr('height', legendHeight)
.attr('stroke-width', 0) .attr('stroke-width', 0)
.attr('fill', options.cardColor) .attr(
'fill',
getColorFromHexRgbOrName(options.cardColor, contextSrv.user.lightTheme ? GrafanaTheme.Light : GrafanaTheme.Dark)
)
.style('opacity', d => legendOpacityScale(d)); .style('opacity', d => legendOpacityScale(d));
} }
} }

View File

@ -304,6 +304,7 @@ export class HeatmapCtrl extends MetricsPanelCtrl {
} }
onCardColorChange(newColor) { onCardColorChange(newColor) {
console.log(newColor)
this.panel.color.cardColor = newColor; this.panel.color.cardColor = newColor;
this.render(); this.render();
} }

View File

@ -8,6 +8,8 @@ import * as ticksUtils from 'app/core/utils/ticks';
import { HeatmapTooltip } from './heatmap_tooltip'; import { HeatmapTooltip } from './heatmap_tooltip';
import { mergeZeroBuckets } from './heatmap_data_converter'; import { mergeZeroBuckets } from './heatmap_data_converter';
import { getColorScale, getOpacityScale } from './color_scale'; import { getColorScale, getOpacityScale } from './color_scale';
import { getColorFromHexRgbOrName } from '@grafana/ui/src/utils/colorsPalette';
import { GrafanaTheme } from '@grafana/ui';
const MIN_CARD_SIZE = 1, const MIN_CARD_SIZE = 1,
CARD_PADDING = 1, CARD_PADDING = 1,
@ -521,7 +523,6 @@ export class HeatmapRenderer {
const maxValueAuto = this.data.cardStats.max; const maxValueAuto = this.data.cardStats.max;
const maxValue = this.panel.color.max || maxValueAuto; const maxValue = this.panel.color.max || maxValueAuto;
const minValue = this.panel.color.min || 0; const minValue = this.panel.color.min || 0;
const colorScheme = _.find(this.ctrl.colorSchemes, { const colorScheme = _.find(this.ctrl.colorSchemes, {
value: this.panel.color.colorScheme, value: this.panel.color.colorScheme,
}); });
@ -662,7 +663,12 @@ export class HeatmapRenderer {
getCardColor(d) { getCardColor(d) {
if (this.panel.color.mode === 'opacity') { if (this.panel.color.mode === 'opacity') {
return this.panel.color.cardColor;
return getColorFromHexRgbOrName(
this.panel.color.cardColor,
contextSrv.user.lightTheme ? GrafanaTheme.Light : GrafanaTheme.Dark
);
} else { } else {
return this.colorScale(d.count); return this.colorScale(d.count);
} }

View File

@ -8,6 +8,8 @@ import kbn from 'app/core/utils/kbn';
import config from 'app/core/config'; import config from 'app/core/config';
import TimeSeries from 'app/core/time_series2'; import TimeSeries from 'app/core/time_series2';
import { MetricsPanelCtrl } from 'app/plugins/sdk'; import { MetricsPanelCtrl } from 'app/plugins/sdk';
import { getColorFromHexRgbOrName } from '@grafana/ui/src/utils/colorsPalette';
import { GrafanaTheme } from '@grafana/ui';
class SingleStatCtrl extends MetricsPanelCtrl { class SingleStatCtrl extends MetricsPanelCtrl {
static templateUrl = 'module.html'; static templateUrl = 'module.html';
@ -479,6 +481,7 @@ class SingleStatCtrl extends MetricsPanelCtrl {
plotCanvas.css(plotCss); plotCanvas.css(plotCss);
const thresholds = []; const thresholds = [];
for (let i = 0; i < data.thresholds.length; i++) { for (let i = 0; i < data.thresholds.length; i++) {
thresholds.push({ thresholds.push({
value: data.thresholds[i], value: data.thresholds[i],
@ -586,7 +589,10 @@ class SingleStatCtrl extends MetricsPanelCtrl {
fill: 1, fill: 1,
zero: false, zero: false,
lineWidth: 1, lineWidth: 1,
fillColor: panel.sparkline.fillColor, fillColor: getColorFromHexRgbOrName(
panel.sparkline.fillColor,
config.bootData.user.lightTheme ? GrafanaTheme.Light : GrafanaTheme.Dark
),
}, },
}, },
yaxes: { show: false }, yaxes: { show: false },
@ -603,7 +609,10 @@ class SingleStatCtrl extends MetricsPanelCtrl {
const plotSeries = { const plotSeries = {
data: data.flotpairs, data: data.flotpairs,
color: panel.sparkline.lineColor, color: getColorFromHexRgbOrName(
panel.sparkline.lineColor,
config.bootData.user.lightTheme ? GrafanaTheme.Light : GrafanaTheme.Dark
),
}; };
$.plot(plotCanvas, [plotSeries], options); $.plot(plotCanvas, [plotSeries], options);
@ -619,12 +628,17 @@ class SingleStatCtrl extends MetricsPanelCtrl {
data.thresholds = panel.thresholds.split(',').map(strVale => { data.thresholds = panel.thresholds.split(',').map(strVale => {
return Number(strVale.trim()); return Number(strVale.trim());
}); });
data.colorMap = panel.colors;
// Map panel colors to hex or rgb/a values
data.colorMap = panel.colors.map(color =>
getColorFromHexRgbOrName(color, config.bootData.user.lightTheme ? GrafanaTheme.Light : GrafanaTheme.Dark)
);
const body = panel.gauge.show ? '' : getBigValueHtml(); const body = panel.gauge.show ? '' : getBigValueHtml();
if (panel.colorBackground) { if (panel.colorBackground) {
const color = getColorForValue(data, data.value); const color = getColorForValue(data, data.value);
console.log(color);
if (color) { if (color) {
$panelContainer.css('background-color', color); $panelContainer.css('background-color', color);
if (scope.fullscreen) { if (scope.fullscreen) {

View File

@ -1,10 +1,12 @@
import _ from 'lodash'; import _ from 'lodash';
import $ from 'jquery'; import $ from 'jquery';
import { MetricsPanelCtrl } from 'app/plugins/sdk'; import { MetricsPanelCtrl } from 'app/plugins/sdk';
import config from 'app/core/config';
import { transformDataToTable } from './transformers'; import { transformDataToTable } from './transformers';
import { tablePanelEditor } from './editor'; import { tablePanelEditor } from './editor';
import { columnOptionsTab } from './column_options'; import { columnOptionsTab } from './column_options';
import { TableRenderer } from './renderer'; import { TableRenderer } from './renderer';
import { GrafanaTheme } from '@grafana/ui';
class TablePanelCtrl extends MetricsPanelCtrl { class TablePanelCtrl extends MetricsPanelCtrl {
static templateUrl = 'module.html'; static templateUrl = 'module.html';
@ -129,7 +131,8 @@ class TablePanelCtrl extends MetricsPanelCtrl {
this.table, this.table,
this.dashboard.isTimezoneUtc(), this.dashboard.isTimezoneUtc(),
this.$sanitize, this.$sanitize,
this.templateSrv this.templateSrv,
config.bootData.user.lightTheme ? GrafanaTheme.Light : GrafanaTheme.Dark,
); );
return super.render(this.table); return super.render(this.table);

View File

@ -1,12 +1,21 @@
import _ from 'lodash'; import _ from 'lodash';
import moment from 'moment'; import moment from 'moment';
import kbn from 'app/core/utils/kbn'; import kbn from 'app/core/utils/kbn';
import { getColorFromHexRgbOrName } from '@grafana/ui/src/utils/colorsPalette';
import { GrafanaTheme } from '@grafana/ui';
export class TableRenderer { export class TableRenderer {
formatters: any[]; formatters: any[];
colorState: any; colorState: any;
constructor(private panel, private table, private isUtc, private sanitize, private templateSrv) { constructor(
private panel,
private table,
private isUtc,
private sanitize,
private templateSrv,
private theme?: GrafanaTheme
) {
this.initColumns(); this.initColumns();
} }
@ -49,10 +58,10 @@ export class TableRenderer {
} }
for (let i = style.thresholds.length; i > 0; i--) { for (let i = style.thresholds.length; i > 0; i--) {
if (value >= style.thresholds[i - 1]) { if (value >= style.thresholds[i - 1]) {
return style.colors[i]; return getColorFromHexRgbOrName(style.colors[i], this.theme);
} }
} }
return _.first(style.colors); return getColorFromHexRgbOrName(_.first(style.colors), this.theme);
} }
defaultCellFormatter(v, style) { defaultCellFormatter(v, style) {