Trend: Add support for a logarithmic x axis (#101433)

Co-authored-by: Leon Sorokin <leeoniya@gmail.com>
This commit is contained in:
Kristina
2025-08-29 00:02:49 -05:00
committed by GitHub
co-authored by Leon Sorokin
parent c62ba51d68
commit c1edba6d8f
3 changed files with 79 additions and 23 deletions
@@ -18,8 +18,8 @@
"editable": true, "editable": true,
"fiscalYearStartMonth": 0, "fiscalYearStartMonth": 0,
"graphTooltip": 0, "graphTooltip": 0,
"id": 21370,
"links": [], "links": [],
"liveNow": false,
"panels": [ "panels": [
{ {
"datasource": { "datasource": {
@@ -29,16 +29,16 @@
"defaults": { "defaults": {
"color": { "color": {
"fixedColor": "blue", "fixedColor": "blue",
"mode": "fixed" "mode": "palette-classic"
}, },
"custom": { "custom": {
"axisBorderShow": false,
"axisCenteredZero": false, "axisCenteredZero": false,
"axisColorMode": "series", "axisColorMode": "series",
"axisLabel": "Torque (NM)", "axisLabel": "",
"axisPlacement": "auto", "axisPlacement": "auto",
"axisSoftMax": 280,
"axisSoftMin": 80,
"barAlignment": 0, "barAlignment": 0,
"barWidthFactor": 0.6,
"drawStyle": "line", "drawStyle": "line",
"fillOpacity": 0, "fillOpacity": 0,
"gradientMode": "none", "gradientMode": "none",
@@ -47,6 +47,7 @@
"tooltip": false, "tooltip": false,
"viz": false "viz": false
}, },
"insertNulls": false,
"lineInterpolation": "smooth", "lineInterpolation": "smooth",
"lineStyle": { "lineStyle": {
"fill": "solid" "fill": "solid"
@@ -73,7 +74,7 @@
"steps": [ "steps": [
{ {
"color": "green", "color": "green",
"value": null "value": 0
}, },
{ {
"color": "red", "color": "red",
@@ -89,24 +90,24 @@
"options": "Power (kW)" "options": "Power (kW)"
}, },
"properties": [ "properties": [
{
"id": "custom.axisPlacement",
"value": "right"
},
{ {
"id": "custom.axisLabel", "id": "custom.axisLabel",
"value": "Power (kW)" "value": "Power (kW)"
}, },
{
"id": "custom.axisSoftMax",
"value": 120
},
{ {
"id": "color", "id": "color",
"value": { "value": {
"fixedColor": "red", "fixedColor": "red",
"mode": "fixed" "mode": "fixed"
} }
},
{
"id": "custom.axisSoftMin",
"value": 20
},
{
"id": "custom.axisSoftMax",
"value": 120
} }
] ]
}, },
@@ -119,6 +120,41 @@
{ {
"id": "custom.axisLabel", "id": "custom.axisLabel",
"value": "Speed (RPM)" "value": "Speed (RPM)"
},
{
"id": "custom.axisSoftMin",
"value": 600
},
{
"id": "custom.axisSoftMax",
"value": 6800
}
]
},
{
"matcher": {
"id": "byName",
"options": "Torque (NM)"
},
"properties": [
{
"id": "custom.axisLabel",
"value": "Torque (NM)"
},
{
"id": "color",
"value": {
"fixedColor": "blue",
"mode": "fixed"
}
},
{
"id": "custom.axisSoftMin",
"value": 80
},
{
"id": "custom.axisSoftMax",
"value": 280
} }
] ]
} }
@@ -139,14 +175,16 @@
"showLegend": true "showLegend": true
}, },
"tooltip": { "tooltip": {
"hideZeros": false,
"mode": "single", "mode": "single",
"sort": "none" "sort": "none"
}, },
"xField": "Speed (RPM)" "xField": "Speed (RPM)"
}, },
"pluginVersion": "12.2.0-pre",
"targets": [ "targets": [
{ {
"csvContent": "Speed (RPM),Torque (NM),Power (kW)\n0,,\n500,,\n800,180,20\n1500,215,40\n2000,227,50\n2500,230,62\n3000,233,75\n3500,236,86\n4000,234,92\n4500,230,100\n5000,220,107\n5500,210,110\n6000,178,106\n6500,120,92\n7000,,\n", "csvContent": "Speed (RPM),Torque (NM),Power (kW)\n800,180,20\n1500,215,40\n2000,227,50\n2500,230,62\n3000,233,75\n3500,236,86\n4000,234,92\n4500,230,100\n5000,220,107\n5500,210,110\n6000,178,106\n6500,120,92",
"datasource": { "datasource": {
"type": "grafana-testdata-datasource" "type": "grafana-testdata-datasource"
}, },
@@ -158,8 +196,9 @@
"type": "trend" "type": "trend"
} }
], ],
"preload": false,
"refresh": "", "refresh": "",
"schemaVersion": 38, "schemaVersion": 41,
"tags": ["gdev", "panel-tests", "graph-ng", "trend"], "tags": ["gdev", "panel-tests", "graph-ng", "trend"],
"templating": { "templating": {
"list": [] "list": []
@@ -172,6 +211,5 @@
"timezone": "", "timezone": "",
"title": "Panel Tests - Trend", "title": "Panel Tests - Trend",
"uid": "b36b5576-2e3d-4b0c-8dce-e79514d99345", "uid": "b36b5576-2e3d-4b0c-8dce-e79514d99345",
"version": 4, "version": 16
"weekStart": ""
} }
@@ -22,6 +22,8 @@ export interface ScaleProps {
centeredZero?: boolean; centeredZero?: boolean;
decimals?: DecimalCount; decimals?: DecimalCount;
stackingMode?: StackingMode; stackingMode?: StackingMode;
padMinBy?: number;
padMaxBy?: number;
} }
export class UPlotScaleBuilder extends PlotConfigBuilder<ScaleProps, Scale> { export class UPlotScaleBuilder extends PlotConfigBuilder<ScaleProps, Scale> {
@@ -45,6 +47,8 @@ export class UPlotScaleBuilder extends PlotConfigBuilder<ScaleProps, Scale> {
centeredZero, centeredZero,
decimals, decimals,
stackingMode, stackingMode,
padMinBy = 0.1,
padMaxBy = 0.1,
} = this.props; } = this.props;
if (stackingMode === StackingMode.Percent) { if (stackingMode === StackingMode.Percent) {
@@ -144,13 +148,13 @@ export class UPlotScaleBuilder extends PlotConfigBuilder<ScaleProps, Scale> {
const rangeConfig: Range.Config = { const rangeConfig: Range.Config = {
min: { min: {
pad: 0.1, pad: padMinBy,
hard: hardMin ?? -Infinity, hard: hardMin ?? -Infinity,
soft: softMin || 0, soft: softMin || 0,
mode: softMinMode, mode: softMinMode,
}, },
max: { max: {
pad: 0.1, pad: padMaxBy,
hard: hardMax ?? Infinity, hard: hardMax ?? Infinity,
soft: softMax || 0, soft: softMax || 0,
mode: softMaxMode, mode: softMaxMode,
+17 -3
View File
@@ -27,6 +27,7 @@ import {
AxisColorMode, AxisColorMode,
GraphGradientMode, GraphGradientMode,
VizOrientation, VizOrientation,
ScaleDistributionConfig,
} from '@grafana/schema'; } from '@grafana/schema';
// unit lookup needed to determine if we want power-of-2 or power-of-10 axis ticks // unit lookup needed to determine if we want power-of-2 or power-of-10 axis ticks
@@ -180,20 +181,33 @@ export const preparePlotConfigBuilder: UPlotConfigPrepFn = ({
}); });
} }
} else { } else {
let custom = xField.config.custom;
let scaleDistr: ScaleDistributionConfig = { ...custom?.scaleDistribution };
builder.addScale({ builder.addScale({
scaleKey: xScaleKey, scaleKey: xScaleKey,
orientation: isHorizontal ? ScaleOrientation.Horizontal : ScaleOrientation.Vertical, orientation: isHorizontal ? ScaleOrientation.Horizontal : ScaleOrientation.Vertical,
direction: isHorizontal ? ScaleDirection.Right : ScaleDirection.Up, direction: isHorizontal ? ScaleDirection.Right : ScaleDirection.Up,
range: (u, dataMin, dataMax) => [xField.config.min ?? dataMin, xField.config.max ?? dataMax], distribution: scaleDistr?.type,
log: scaleDistr?.log,
linearThreshold: scaleDistr?.linearThreshold,
min: xField.config.min,
max: xField.config.max,
softMin: custom?.axisSoftMin,
softMax: custom?.axisSoftMax,
centeredZero: custom?.axisCenteredZero,
decimals: xField.config.decimals,
padMinBy: 0,
padMaxBy: 0,
}); });
builder.addAxis({ builder.addAxis({
scaleKey: xScaleKey, scaleKey: xScaleKey,
placement: xFieldAxisPlacement, placement: xFieldAxisPlacement,
show: xFieldAxisShow, show: xFieldAxisShow,
label: xField.config.custom?.axisLabel, label: custom?.axisLabel,
theme, theme,
grid: { show: xField.config.custom?.axisGridShow }, grid: { show: custom?.axisGridShow },
formatValue: (v, decimals) => formattedValueToString(xField.display!(v, decimals)), formatValue: (v, decimals) => formattedValueToString(xField.display!(v, decimals)),
}); });
} }