mirror of
https://github.com/lensapp/lens.git
synced 2025-05-20 05:10:56 +00:00
Fix PieChart tooltips (#5223)
* Add tooltipLabels field to ChartData Signed-off-by: Alex Andreev <alex.andreev.email@gmail.com> * Use tooltipLabels in ClusterPieCharts for pods Signed-off-by: Alex Andreev <alex.andreev.email@gmail.com> * Check for tooltipLabels field to assign tooltip text Signed-off-by: Alex Andreev <alex.andreev.email@gmail.com> * Use tooltipLabels inside overview charts Signed-off-by: Alex Andreev <alex.andreev.email@gmail.com> * Expand workload overview charts to fit tooltips Signed-off-by: Alex Andreev <alex.andreev.email@gmail.com> * Move tooltipLabels into chart datasets Signed-off-by: Alex Andreev <alex.andreev.email@gmail.com> * Move tooltipLabels prop to PieCharts Signed-off-by: Alex Andreev <alex.andreev.email@gmail.com> * Little clean up Signed-off-by: Alex Andreev <alex.andreev.email@gmail.com> * Getting back id field to PieChartData interface Signed-off-by: Alex Andreev <alex.andreev.email@gmail.com> * Id fix Signed-off-by: Alex Andreev <alex.andreev.email@gmail.com> * More clean up Signed-off-by: Alex Andreev <alex.andreev.email@gmail.com>
This commit is contained in:
parent
423ffd44dc
commit
542cbe9ebf
@ -12,7 +12,7 @@ import { MetricNodeRole } from "./cluster-overview-store/cluster-overview-store"
|
|||||||
import { Spinner } from "../spinner";
|
import { Spinner } from "../spinner";
|
||||||
import { Icon } from "../icon";
|
import { Icon } from "../icon";
|
||||||
import { nodesStore } from "../+nodes/nodes.store";
|
import { nodesStore } from "../+nodes/nodes.store";
|
||||||
import type { ChartData } from "../chart";
|
import type { PieChartData } from "../chart";
|
||||||
import { PieChart } from "../chart";
|
import { PieChart } from "../chart";
|
||||||
import { ClusterNoMetrics } from "./cluster-no-metrics";
|
import { ClusterNoMetrics } from "./cluster-no-metrics";
|
||||||
import { bytesToUnits, cssNames } from "../../utils";
|
import { bytesToUnits, cssNames } from "../../utils";
|
||||||
@ -49,7 +49,7 @@ const NonInjectedClusterPieCharts = observer(({ clusterOverviewStore }: Dependen
|
|||||||
const defaultColor = ThemeStore.getInstance().activeTheme.colors.pieChartDefaultColor;
|
const defaultColor = ThemeStore.getInstance().activeTheme.colors.pieChartDefaultColor;
|
||||||
|
|
||||||
if (!memoryCapacity || !cpuCapacity || !podCapacity || !memoryAllocatableCapacity || !cpuAllocatableCapacity || !podAllocatableCapacity) return null;
|
if (!memoryCapacity || !cpuCapacity || !podCapacity || !memoryAllocatableCapacity || !cpuAllocatableCapacity || !podAllocatableCapacity) return null;
|
||||||
const cpuData: ChartData = {
|
const cpuData: PieChartData = {
|
||||||
datasets: [
|
datasets: [
|
||||||
{
|
{
|
||||||
data: [
|
data: [
|
||||||
@ -96,7 +96,7 @@ const NonInjectedClusterPieCharts = observer(({ clusterOverviewStore }: Dependen
|
|||||||
["Capacity", cpuCapacity],
|
["Capacity", cpuCapacity],
|
||||||
]),
|
]),
|
||||||
};
|
};
|
||||||
const memoryData: ChartData = {
|
const memoryData: PieChartData = {
|
||||||
datasets: [
|
datasets: [
|
||||||
{
|
{
|
||||||
data: [
|
data: [
|
||||||
@ -143,7 +143,7 @@ const NonInjectedClusterPieCharts = observer(({ clusterOverviewStore }: Dependen
|
|||||||
`Capacity: ${bytesToUnits(memoryCapacity)}`,
|
`Capacity: ${bytesToUnits(memoryCapacity)}`,
|
||||||
],
|
],
|
||||||
};
|
};
|
||||||
const podsData: ChartData = {
|
const podsData: PieChartData = {
|
||||||
datasets: [
|
datasets: [
|
||||||
{
|
{
|
||||||
data: [
|
data: [
|
||||||
@ -156,6 +156,10 @@ const NonInjectedClusterPieCharts = observer(({ clusterOverviewStore }: Dependen
|
|||||||
],
|
],
|
||||||
id: "podUsage",
|
id: "podUsage",
|
||||||
label: "Usage",
|
label: "Usage",
|
||||||
|
tooltipLabels: [
|
||||||
|
(percent) => `Usage: ${percent}`,
|
||||||
|
(percent) => `Available: ${percent}`,
|
||||||
|
],
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
labels: [
|
labels: [
|
||||||
|
|||||||
@ -13,10 +13,4 @@
|
|||||||
--workload-status-failed: #{$pod-status-failed-color};
|
--workload-status-failed: #{$pod-status-failed-color};
|
||||||
--workload-status-terminated: #{$pod-status-terminated-color};
|
--workload-status-terminated: #{$pod-status-terminated-color};
|
||||||
--workload-status-unknown: #{$pod-status-unknown-color};
|
--workload-status-unknown: #{$pod-status-unknown-color};
|
||||||
|
|
||||||
.PieChart {
|
|
||||||
.chart-container {
|
|
||||||
width: 110px
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -8,9 +8,9 @@ import "./overview-workload-status.scss";
|
|||||||
import React from "react";
|
import React from "react";
|
||||||
import capitalize from "lodash/capitalize";
|
import capitalize from "lodash/capitalize";
|
||||||
import { observer } from "mobx-react";
|
import { observer } from "mobx-react";
|
||||||
|
import type { DatasetTooltipLabel, PieChartData } from "../chart";
|
||||||
import { PieChart } from "../chart";
|
import { PieChart } from "../chart";
|
||||||
import { cssVar } from "../../utils";
|
import { cssVar } from "../../utils";
|
||||||
import type { ChartData } from "chart.js";
|
|
||||||
import { ThemeStore } from "../../theme.store";
|
import { ThemeStore } from "../../theme.store";
|
||||||
|
|
||||||
export interface OverviewWorkloadStatusProps {
|
export interface OverviewWorkloadStatusProps {
|
||||||
@ -27,7 +27,7 @@ export class OverviewWorkloadStatus extends React.Component<OverviewWorkloadStat
|
|||||||
}
|
}
|
||||||
|
|
||||||
const cssVars = cssVar(this.elem);
|
const cssVars = cssVar(this.elem);
|
||||||
const chartData: Required<ChartData> = {
|
const chartData: Required<PieChartData> = {
|
||||||
labels: [],
|
labels: [],
|
||||||
datasets: [],
|
datasets: [],
|
||||||
};
|
};
|
||||||
@ -43,10 +43,12 @@ export class OverviewWorkloadStatus extends React.Component<OverviewWorkloadStat
|
|||||||
} else {
|
} else {
|
||||||
const data: number[] = [];
|
const data: number[] = [];
|
||||||
const backgroundColor: string[] = [];
|
const backgroundColor: string[] = [];
|
||||||
|
const tooltipLabels: DatasetTooltipLabel[] = [];
|
||||||
|
|
||||||
for (const [status, value] of statuses) {
|
for (const [status, value] of statuses) {
|
||||||
data.push(value);
|
data.push(value);
|
||||||
backgroundColor.push(cssVars.get(`--workload-status-${status.toLowerCase()}`).toString());
|
backgroundColor.push(cssVars.get(`--workload-status-${status.toLowerCase()}`).toString());
|
||||||
|
tooltipLabels.push(percent => `${capitalize(status)}: ${percent}`);
|
||||||
chartData.labels.push(`${capitalize(status)}: ${value}`);
|
chartData.labels.push(`${capitalize(status)}: ${value}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -54,6 +56,7 @@ export class OverviewWorkloadStatus extends React.Component<OverviewWorkloadStat
|
|||||||
data,
|
data,
|
||||||
backgroundColor,
|
backgroundColor,
|
||||||
label: "Status",
|
label: "Status",
|
||||||
|
tooltipLabels,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -16,6 +16,17 @@ import { ThemeStore } from "../../theme.store";
|
|||||||
export interface PieChartProps extends ChartProps {
|
export interface PieChartProps extends ChartProps {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export interface PieChartData extends ChartJS.ChartData {
|
||||||
|
datasets?: PieChartDataSets[];
|
||||||
|
}
|
||||||
|
|
||||||
|
export type DatasetTooltipLabel = (percent: string) => string | string;
|
||||||
|
|
||||||
|
interface PieChartDataSets extends ChartJS.ChartDataSets {
|
||||||
|
id?: string;
|
||||||
|
tooltipLabels?: DatasetTooltipLabel[];
|
||||||
|
}
|
||||||
|
|
||||||
@observer
|
@observer
|
||||||
export class PieChart extends React.Component<PieChartProps> {
|
export class PieChart extends React.Component<PieChartProps> {
|
||||||
render() {
|
render() {
|
||||||
@ -28,15 +39,24 @@ export class PieChart extends React.Component<PieChartProps> {
|
|||||||
mode: "index",
|
mode: "index",
|
||||||
callbacks: {
|
callbacks: {
|
||||||
title: () => "",
|
title: () => "",
|
||||||
label: (tooltipItem, data) => {
|
label: (tooltipItem, data: PieChartData) => {
|
||||||
const dataset: any = data["datasets"][tooltipItem.datasetIndex];
|
const dataset = data.datasets[tooltipItem.datasetIndex];
|
||||||
const metaData = Object.values<{ total: number }>(dataset["_meta"])[0];
|
const datasetData = dataset.data as number[];
|
||||||
const percent = Math.round((dataset["data"][tooltipItem["index"]] / metaData.total) * 100);
|
const total = datasetData.reduce((acc, cur) => acc + cur, 0);
|
||||||
const label = dataset["label"];
|
const percent = Math.round((datasetData[tooltipItem.index] as number / total) * 100);
|
||||||
|
const percentLabel = isNaN(percent) ? "N/A" : `${percent}%`;
|
||||||
|
const tooltipLabel = dataset.tooltipLabels?.[tooltipItem.index];
|
||||||
|
let tooltip = `${dataset.label}: ${percentLabel}`;
|
||||||
|
|
||||||
if (isNaN(percent)) return `${label}: N/A`;
|
if (tooltipLabel) {
|
||||||
|
if (typeof tooltipLabel === "function") {
|
||||||
|
tooltip = tooltipLabel(percentLabel);
|
||||||
|
} else {
|
||||||
|
tooltip = tooltipLabel;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return `${label}: ${percent}%`;
|
return tooltip;
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
filter: ({ datasetIndex, index }, { datasets }) => {
|
filter: ({ datasetIndex, index }, { datasets }) => {
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user