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

feat(web): tooltip-improvement-and-line-draw #1651

Merged
merged 1 commit into from
Jul 18, 2024
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
6 changes: 6 additions & 0 deletions web/global.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,9 @@ declare module "styled-components" {
//eslint-disable-next-line @typescript-eslint/no-empty-interface
export interface DefaultTheme extends Theme {}
}

declare module "chart.js" {
interface TooltipPositionerMap {
custom: TooltipPositionerFunction<ChartType>;
}
}
20 changes: 20 additions & 0 deletions web/src/pages/Home/CourtOverview/Chart.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React, { useMemo, useState } from "react";
import styled from "styled-components";

import { Tooltip } from "chart.js";
import { formatUnits } from "viem";

import { DropdownSelect } from "@kleros/ui-components-library";
Expand Down Expand Up @@ -101,4 +102,23 @@ const Chart: React.FC = () => {
);
};

// custom positioner for tooltip, we need dynamic top positioning, which is not available by default.
Tooltip.positioners.custom = function (elements) {
const tooltip = this;
const height = tooltip.chart.chartArea.height;
const width = tooltip.chart.chartArea.width;

const x = elements[0]?.element.x;
const y = elements[0]?.element.y;
const isAtTop = height > y + tooltip.height;
const isAtEnd = width < x + tooltip.width;

return {
x: elements[0]?.element.x,
y: elements[0]?.element.y,
xAlign: isAtTop ? (isAtEnd ? "right" : "left") : "center",
yAlign: isAtTop ? "center" : "bottom",
};
};
Comment on lines +105 to +122
Copy link
Contributor

Choose a reason for hiding this comment

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

Custom Tooltip Positioner Implemented

The custom tooltip positioner dynamically calculates positions based on chart dimensions and hovered elements, which is a key feature for improving user interaction. However, the static analysis tool flagged an unnecessary aliasing of this in the arrow function, which should be corrected.

- Tooltip.positioners.custom = function (elements) {
+ Tooltip.positioners.custom = (elements) => {

This change ensures that this is correctly bound in the context of the arrow function, improving readability and adhering to best practices.

Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
// custom positioner for tooltip, we need dynamic top positioning, which is not available by default.
Tooltip.positioners.custom = function (elements) {
const tooltip = this;
const height = tooltip.chart.chartArea.height;
const width = tooltip.chart.chartArea.width;
const x = elements[0]?.element.x;
const y = elements[0]?.element.y;
const isAtTop = height > y + tooltip.height;
const isAtEnd = width < x + tooltip.width;
return {
x: elements[0]?.element.x,
y: elements[0]?.element.y,
xAlign: isAtTop ? (isAtEnd ? "right" : "left") : "center",
yAlign: isAtTop ? "center" : "bottom",
};
};
// custom positioner for tooltip, we need dynamic top positioning, which is not available by default.
Tooltip.positioners.custom = (elements) => {
const tooltip = this;
const height = tooltip.chart.chartArea.height;
const width = tooltip.chart.chartArea.width;
const x = elements[0]?.element.x;
const y = elements[0]?.element.y;
const isAtTop = height > y + tooltip.height;
const isAtEnd = width < x + tooltip.width;
return {
x: elements[0]?.element.x,
y: elements[0]?.element.y,
xAlign: isAtTop ? (isAtEnd ? "right" : "left") : "center",
yAlign: isAtTop ? "center" : "bottom",
};
};
Tools
Biome

[error] 107-107: This aliasing of this is unnecessary.

Arrow functions inherits this from their enclosing scope.
Safe fix: Use this instead of an alias.

(lint/complexity/noUselessThisAlias)


export default Chart;
45 changes: 42 additions & 3 deletions web/src/pages/Home/CourtOverview/TimeSeriesChart.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
import React from "react";
import styled, { useTheme } from "styled-components";

import { Chart as ChartJS, CategoryScale, LinearScale, PointElement, LineElement, TimeScale, Tooltip } from "chart.js";
import {
Chart as ChartJS,
CategoryScale,
LinearScale,
PointElement,
LineElement,
TimeScale,
Tooltip,
ScriptableContext,
} from "chart.js";
import { Line } from "react-chartjs-2";
import "chartjs-adapter-moment";

Expand Down Expand Up @@ -51,6 +60,7 @@ const TimeSeriesChart: React.FC<ITimeSeriesChart> = ({ data }) => {
},
plugins: {
tooltip: {
position: "custom",
backgroundColor: theme.whiteBackground,
titleColor: theme.primaryText,
borderColor: theme.stroke,
Expand All @@ -74,18 +84,47 @@ const TimeSeriesChart: React.FC<ITimeSeriesChart> = ({ data }) => {
datasets: [
{
data,
borderColor: theme.primaryBlue,
// borderColor: theme.primaryBlue,
stepped: true,
cubicInterpolationMode: "monotone",
borderColor: (context: ScriptableContext<"line">) => {
const ctx = context.chart.ctx;
const gradient = ctx.createLinearGradient(0, 0, 0, 200);
gradient.addColorStop(0, theme.primaryBlue);
gradient.addColorStop(1, theme.secondaryPurple);
return gradient;
},
},
],
},
options,
}}
plugins={[
{
id: "line-draw",
afterDatasetsDraw: (chart) => {
if (chart.tooltip?._active?.length) {
const x = chart.tooltip._active[0].element.x;
const y = chart.tooltip._active[0].element.y;
const yAxis = chart.scales.y;

const ctx = chart.ctx;
ctx.save();
ctx.beginPath();
ctx.moveTo(x, y);
ctx.lineTo(x, yAxis.bottom);
ctx.lineWidth = 1;
ctx.strokeStyle = theme.secondaryPurple;
ctx.setLineDash([4, 4]);
ctx.stroke();
ctx.restore();
}
},
},
]}
/>
}
</LineContainer>
);
};

export default TimeSeriesChart;
Loading