Files
grafana/public/app/plugins/panel/barchart/module.tsx

258 lines
7.5 KiB
TypeScript
Raw Normal View History

import {
DataFrame,
FieldColorModeId,
FieldConfigProperty,
FieldType,
getFieldDisplayName,
identityOverrideProcessor,
PanelPlugin,
VizOrientation,
} from '@grafana/data';
import { config } from '@grafana/runtime';
import { GraphTransform, GraphThresholdsStyleMode, StackingMode, VisibilityMode } from '@grafana/schema';
import { graphFieldOptions, commonOptionsBuilder } from '@grafana/ui';
import { ThresholdsStyleEditor } from '../timeseries/ThresholdsStyleEditor';
import { BarChartPanel } from './BarChartPanel';
import { TickSpacingEditor } from './TickSpacingEditor';
import { changeToBarChartPanelMigrationHandler } from './migrations';
import { FieldConfig, Options, defaultFieldConfig, defaultOptions } from './panelcfg.gen';
VisualizationSelection: Real previews of suitable visualisation and options based on current data (#40527) * Initial pass to move panel state to it's own, and make it by key not panel.id * Progress * Not making much progress, having panel.key be mutable is causing a lot of issues * Think this is starting to work * Began fixing tests * Add selector * Bug fixes and changes to cleanup, and fixing all flicking when switching library panels * Removed console.log * fixes after merge * fixing tests * fixing tests * Added new test for changePlugin thunk * Initial struture in place * responding to state changes in another part of the state * bha * going in a different direction * This is getting exciting * minor * More structure * More real * Added builder to reduce boiler plate * Lots of progress * Adding more visualizations * More smarts * tweaks * suggestions * Move to separate view * Refactoring to builder concept * Before hover preview test * Increase line width in preview * More suggestions * Removed old elements of onSuggestVisualizations * Don't call suggestion suppliers if there is no data * Restore card styles to only borders * Changing supplier interface to support data vs option suggestion scenario * Renamed functions * Add dynamic width support * not sure about this * Improve suggestions * Improve suggestions * Single grid/list * Store vis select pane & size * Prep for option suggestions * more suggestions * Name/title option for preview cards * Improve barchart suggestions * Support suggestions when there are no data * Minor change * reverted some changes * Improve suggestions for stacking * Removed size option * starting on unit tests, hit cyclic dependency issue * muuu * First test for getting suggestion seems to work, going to bed * add missing file * A basis for more unit tests * More tests * More unit tests * Fixed unit tests * Update * Some extreme scenarios * Added basic e2e test * Added another unit test for changePanelPlugin action * More cleanup * Minor tweak * add wait to e2e test * Renamed function and cleanup of unused function * Adding search support and adding search test to e2e test
2021-10-25 13:55:06 +02:00
import { BarChartSuggestionsSupplier } from './suggestions';
import { prepareBarChartDisplayValues } from './utils';
export const plugin = new PanelPlugin<Options, FieldConfig>(BarChartPanel)
.setPanelChangeHandler(changeToBarChartPanelMigrationHandler)
.useFieldConfig({
standardOptions: {
[FieldConfigProperty.Color]: {
settings: {
byValueSupport: true,
preferThresholdsMode: false,
},
defaultValue: {
mode: FieldColorModeId.PaletteClassic,
},
},
},
useCustomConfig: (builder) => {
const cfg = defaultFieldConfig;
builder
.addSliderInput({
path: 'lineWidth',
name: 'Line width',
defaultValue: cfg.lineWidth,
settings: {
min: 0,
max: 10,
step: 1,
},
})
.addSliderInput({
path: 'fillOpacity',
name: 'Fill opacity',
defaultValue: cfg.fillOpacity,
settings: {
min: 0,
max: 100,
step: 1,
},
})
.addRadio({
path: 'gradientMode',
name: 'Gradient mode',
defaultValue: graphFieldOptions.fillGradient[0].value,
settings: {
options: graphFieldOptions.fillGradient,
},
});
builder.addSelect({
category: ['Graph styles'],
name: 'Transform',
path: 'transform',
settings: {
options: [
{
label: 'Constant',
value: GraphTransform.Constant,
description: 'The first value will be shown as a constant line',
},
{
label: 'Negative Y',
value: GraphTransform.NegativeY,
description: 'Flip the results to negative values on the y axis',
},
],
isClearable: true,
},
hideFromDefaults: true,
});
builder.addCustomEditor({
id: 'thresholdsStyle',
path: 'thresholdsStyle',
name: 'Show thresholds',
category: ['Thresholds'],
defaultValue: { mode: GraphThresholdsStyleMode.Off },
settings: {
options: graphFieldOptions.thresholdsDisplayModes,
},
editor: ThresholdsStyleEditor,
override: ThresholdsStyleEditor,
process: identityOverrideProcessor,
shouldApply: () => true,
});
2021-12-18 11:21:22 -08:00
commonOptionsBuilder.addAxisConfig(builder, cfg, false);
commonOptionsBuilder.addHideFrom(builder);
},
})
.setPanelOptions((builder, context) => {
const disp = prepareBarChartDisplayValues(context.data, config.theme2, context.options ?? ({} as Options));
let xaxisPlaceholder = 'First string or time field';
const viz = 'viz' in disp ? disp.viz[0] : undefined;
if (viz?.fields?.length) {
const first = viz.fields[0];
xaxisPlaceholder += ` (${getFieldDisplayName(first, viz)})`;
}
builder
.addFieldNamePicker({
path: 'xField',
name: 'X Axis',
settings: {
placeholderText: xaxisPlaceholder,
},
})
.addRadio({
path: 'orientation',
name: 'Orientation',
settings: {
options: [
{ value: VizOrientation.Auto, label: 'Auto' },
{ value: VizOrientation.Horizontal, label: 'Horizontal' },
{ value: VizOrientation.Vertical, label: 'Vertical' },
],
},
defaultValue: defaultOptions.orientation,
})
.addSliderInput({
path: 'xTickLabelRotation',
name: 'Rotate x-axis tick labels',
defaultValue: defaultOptions.xTickLabelRotation,
settings: {
min: -90,
max: 90,
step: 15,
marks: { '-90': '-90°', '-45': '-45°', 0: '0°', 45: '45°', 90: '90°' },
included: false,
},
})
.addNumberInput({
path: 'xTickLabelMaxLength',
name: 'X-axis tick label max length',
description: 'X-axis labels will be truncated to the length provided',
settings: {
2022-01-10 15:12:33 -08:00
placeholder: 'None',
min: 0,
},
showIf: (opts) => opts.xTickLabelRotation !== 0,
})
2022-01-10 15:12:33 -08:00
.addCustomEditor({
id: 'xTickLabelSpacing',
path: 'xTickLabelSpacing',
name: 'X-axis labels minimum spacing',
defaultValue: defaultOptions.xTickLabelSpacing,
2022-01-10 15:12:33 -08:00
editor: TickSpacingEditor,
})
.addRadio({
path: 'showValue',
name: 'Show values',
settings: {
options: [
{ value: VisibilityMode.Auto, label: 'Auto' },
{ value: VisibilityMode.Always, label: 'Always' },
{ value: VisibilityMode.Never, label: 'Never' },
],
},
defaultValue: defaultOptions.showValue,
})
.addRadio({
path: 'stacking',
name: 'Stacking',
settings: {
options: graphFieldOptions.stacking,
},
defaultValue: defaultOptions.stacking,
})
.addSliderInput({
path: 'groupWidth',
name: 'Group width',
defaultValue: defaultOptions.groupWidth,
settings: {
min: 0,
max: 1,
step: 0.01,
},
showIf: (c, data) => {
if (c.stacking && c.stacking !== StackingMode.None) {
return false;
}
return countNumberFields(data) !== 1;
},
})
.addSliderInput({
path: 'barWidth',
name: 'Bar width',
defaultValue: defaultOptions.barWidth,
settings: {
min: 0,
max: 1,
step: 0.01,
},
})
.addSliderInput({
path: 'barRadius',
name: 'Bar radius',
defaultValue: defaultOptions.barRadius,
settings: {
min: 0,
max: 0.5,
step: 0.05,
},
})
.addBooleanSwitch({
path: 'fullHighlight',
name: 'Highlight full area on hover',
defaultValue: defaultOptions.fullHighlight,
showIf: (c) => c.stacking === StackingMode.None,
});
builder.addFieldNamePicker({
path: 'colorByField',
name: 'Color by field',
description: 'Use the color value for a sibling field to color each bar value.',
});
commonOptionsBuilder.addTooltipOptions(builder);
commonOptionsBuilder.addLegendOptions(builder);
commonOptionsBuilder.addTextSizeOptions(builder, false);
VisualizationSelection: Real previews of suitable visualisation and options based on current data (#40527) * Initial pass to move panel state to it's own, and make it by key not panel.id * Progress * Not making much progress, having panel.key be mutable is causing a lot of issues * Think this is starting to work * Began fixing tests * Add selector * Bug fixes and changes to cleanup, and fixing all flicking when switching library panels * Removed console.log * fixes after merge * fixing tests * fixing tests * Added new test for changePlugin thunk * Initial struture in place * responding to state changes in another part of the state * bha * going in a different direction * This is getting exciting * minor * More structure * More real * Added builder to reduce boiler plate * Lots of progress * Adding more visualizations * More smarts * tweaks * suggestions * Move to separate view * Refactoring to builder concept * Before hover preview test * Increase line width in preview * More suggestions * Removed old elements of onSuggestVisualizations * Don't call suggestion suppliers if there is no data * Restore card styles to only borders * Changing supplier interface to support data vs option suggestion scenario * Renamed functions * Add dynamic width support * not sure about this * Improve suggestions * Improve suggestions * Single grid/list * Store vis select pane & size * Prep for option suggestions * more suggestions * Name/title option for preview cards * Improve barchart suggestions * Support suggestions when there are no data * Minor change * reverted some changes * Improve suggestions for stacking * Removed size option * starting on unit tests, hit cyclic dependency issue * muuu * First test for getting suggestion seems to work, going to bed * add missing file * A basis for more unit tests * More tests * More unit tests * Fixed unit tests * Update * Some extreme scenarios * Added basic e2e test * Added another unit test for changePanelPlugin action * More cleanup * Minor tweak * add wait to e2e test * Renamed function and cleanup of unused function * Adding search support and adding search test to e2e test
2021-10-25 13:55:06 +02:00
})
.setSuggestionsSupplier(new BarChartSuggestionsSupplier());
function countNumberFields(data?: DataFrame[]): number {
let count = 0;
if (data) {
for (const frame of data) {
for (const field of frame.fields) {
if (field.type === FieldType.number) {
count++;
}
}
}
}
return count;
}