Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[fix] Persist chart legend filters #92

Merged
merged 7 commits into from
Nov 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ interface HeaderProps {
}

export function Header({ digest }: HeaderProps) {
console.log(digest.start, typeof digest.start)
return (
<Flex as="header" align="center">
<LogoIcon />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// SPDX-FileCopyrightText: 2023 Raintank, Inc. dba Grafana Labs
//
// SPDX-License-Identifier: AGPL-3.0-only

import { useState } from "react"
import uPlot, { type Options, type Series } from "uplot"

import { mergeRightProps } from "utils"

import { createOptions, CreateOptionsProps } from "./Chart.utils"

const mergeRightShowProp = mergeRightProps(["show"])

const mergeSeries = (plotSeries: Series[] = [], stateSeries: Series[] = []) => {
return plotSeries.map((series, i) => mergeRightShowProp(series, stateSeries[i]))
}

export const useOptions = ({ plot, theme, width }: CreateOptionsProps): Options => {
const [series, setSeries] = useState<Series[]>(plot.series)
const newPlot = { ...plot, series: mergeSeries(plot.series, series) }
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@szkiba this might be overkill, is it possible for plot.series to change over time?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have no idea (uPlot documentation is a little bit limited)

const hooks = { setSeries: [(self: uPlot) => setSeries(self.series)] }

return createOptions({ hooks, plot: newPlot, theme, width })
}
4 changes: 2 additions & 2 deletions dashboard/assets/packages/ui/src/components/Chart/Chart.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import { Icon } from "components/Icon"
import { Paper } from "components/Paper"
import { Tooltip } from "components/Tooltip"

import { createOptions } from "./Chart.utils"
import { useOptions } from "./Chart.hooks"
import * as styles from "./Chart.css"

interface ChartProps {
Expand All @@ -34,7 +34,7 @@ export default function Chart({ panel, container }: ChartProps) {
const plot = new SeriesPlot(digest, panel, createColorScheme(theme))
const hasData = !plot.empty && plot.data[0].length > 1
const plotData = (hasData ? plot.data : []) as AlignedData
const options = createOptions({ plot, theme, width })
const options = useOptions({ plot, theme, width })

const Wrapper = container ? Fragment : Paper

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
//
// SPDX-License-Identifier: AGPL-3.0-only

import uPlot, { type Axis, type Options, type Series } from "uplot"
import uPlot, { type Axis, type Hooks, type Options, type Series } from "uplot"
import { type UnitType } from "@xk6-dashboard/model"
import { format, tooltipPlugin, type SeriesPlot } from "@xk6-dashboard/view"
import { format, tooltipPlugin, SeriesPlot } from "@xk6-dashboard/view"

import { type Theme } from "store/theme"
import { common, grey, midnight } from "theme/colors.css"
Expand Down Expand Up @@ -63,13 +63,14 @@ const createAxis = (colors: ReturnType<typeof createChartTheme>, length: number)
}
}

interface CreateOptionsProps {
plot: SeriesPlot
export interface CreateOptionsProps {
hooks: Hooks.Arrays
plot: Omit<SeriesPlot, "series"> & { series: Series[] }
theme: Theme
width: number
}

export const createOptions = ({ plot, theme, width }: CreateOptionsProps): Options => {
export const createOptions = ({ hooks, plot, theme, width }: CreateOptionsProps): Options => {
const colors = createChartTheme(theme)
const units = plot.samples.units
const axes = units.map(createAxis(colors, units.length))
Expand All @@ -78,6 +79,7 @@ export const createOptions = ({ plot, theme, width }: CreateOptionsProps): Optio
class: styles.uplot,
width: width,
height: CHART_HEIGHT,
hooks,
cursor: { sync: { key: sync.key } },
legend: { live: false },
series: plot.series as Series[],
Expand Down
25 changes: 25 additions & 0 deletions dashboard/assets/packages/ui/src/utils/object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,28 @@ export const omitUndefined = <T extends object>(styles: T) => {
{}
)
}

/**
* This method is used to pick the specified properties from the object
*/
export const pick = (props: string[], obj: object) =>
Object.entries(obj).reduce(
(acc, [key, value]) => {
if (props.includes(key)) {
acc[key] = value
}

return acc
},
{} as { [key: string]: unknown }
)

/**
* This method is used to merge two objects together. It will overwrite the values of the first object with the values from the second object
*/
export const mergeRight = (a: object, b: object) => ({ ...a, ...b })

/**
* This method is used to merge two objects together. It will overwrite the values of the first object with the specified properties from the second object
*/
export const mergeRightProps = (props: string[]) => (a: object, b: object) => mergeRight(a, pick(props, b))
Loading