Skip to content

Commit 5e128cb

Browse files
authored
Merge pull request lichess-org#14409 from allanjoseph98/perf-chart-single-point
More chart fixes.
2 parents e3019aa + 453b551 commit 5e128cb

File tree

2 files changed

+35
-37
lines changed

2 files changed

+35
-37
lines changed

ui/chart/src/lag.ts

+4-23
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,9 @@ import {
66
ChartType,
77
DoughnutController,
88
Title,
9-
Tooltip,
109
} from 'chart.js';
1110
import dataLabels from 'chartjs-plugin-datalabels';
12-
import { fontColor, fontFamily, tooltipBgColor, resizePolyfill } from './common';
11+
import { fontColor, fontFamily, resizePolyfill } from './common';
1312

1413
declare module 'chart.js' {
1514
// eslint-disable-next-line @typescript-eslint/no-unused-vars
@@ -21,7 +20,7 @@ declare module 'chart.js' {
2120
}
2221

2322
resizePolyfill();
24-
Chart.register(DoughnutController, ArcElement, Tooltip, dataLabels, Title);
23+
Chart.register(DoughnutController, ArcElement, dataLabels, Title);
2524
Chart.defaults.font = fontFamily();
2625

2726
const v = {
@@ -40,7 +39,6 @@ export async function initModule() {
4039
hoverBackgroundColor: colors,
4140
borderColor: '#d9d9d9',
4241
borderWidth: 3,
43-
hoverBorderWidth: 4,
4442
circumference: 180,
4543
rotation: 270,
4644
},
@@ -52,30 +50,13 @@ export async function initModule() {
5250
datasets: dataset,
5351
},
5452
options: {
55-
interaction: {
56-
mode: 'dataset',
57-
axis: 'r',
58-
},
53+
events: [],
5954
plugins: {
6055
title: {
6156
display: true,
6257
text: '',
6358
padding: { top: 100 },
64-
},
65-
tooltip: {
66-
backgroundColor: tooltipBgColor,
67-
titleColor: fontColor,
68-
borderColor: fontColor,
69-
borderWidth: 1,
70-
callbacks: {
71-
title: () => {
72-
const lat = index ? v.network : v.server;
73-
const text = index ? 'Ping:' : 'Server Latency:';
74-
if (lat <= 0) return '';
75-
return `${text} ${lat}` + ' milliseconds';
76-
},
77-
label: () => '',
78-
},
59+
color: fontColor,
7960
},
8061
needle: {
8162
value: index ? v.network : v.server,

ui/chart/src/ratingHistory.ts

+31-14
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import noUiSlider, { Options, PipsMode } from 'nouislider';
1919
import dayjs from 'dayjs';
2020
import duration from 'dayjs/plugin/duration';
2121
import dayOfYear from 'dayjs/plugin/dayOfYear';
22+
import { memoize } from 'common';
2223

2324
interface Opts {
2425
data: PerfRatingHistory[];
@@ -66,6 +67,21 @@ const styles: ChartPerf[] = [
6667

6768
const oneDay = 24 * 60 * 60 * 1000;
6869

70+
const dateFormat = memoize(() =>
71+
window.Intl && Intl.DateTimeFormat
72+
? new Intl.DateTimeFormat(
73+
document.documentElement.lang.startsWith('ar-') ? 'ar-ly' : document.documentElement.lang,
74+
{
75+
month: 'short',
76+
day: '2-digit',
77+
year: 'numeric',
78+
},
79+
).format
80+
: (d: Date) => d.toLocaleDateString(),
81+
);
82+
83+
const utcOffset = -dayjs().utcOffset() * 60 * 1000;
84+
6985
export function initModule({ data, singlePerfName }: Opts) {
7086
$('.spinner').remove();
7187

@@ -76,9 +92,8 @@ export function initModule({ data, singlePerfName }: Opts) {
7692
return;
7793
}
7894
const allData = makeDatasets(1, { data, singlePerfName }, singlePerfIndex);
79-
let startDate = dayjs(allData.startDate);
80-
const endDate = dayjs(allData.endDate);
81-
if (startDate.diff(endDate, 'D') < 1) startDate = startDate.subtract(1, 'day');
95+
const startDate = allData.startDate;
96+
const endDate = allData.endDate;
8297
const weeklyData = makeDatasets(7, { data, singlePerfName }, singlePerfIndex);
8398
const biweeklyData = makeDatasets(14, { data, singlePerfName }, singlePerfIndex);
8499
const threeMonthsAgo = endDate.subtract(3, 'M');
@@ -106,10 +121,6 @@ export function initModule({ data, singlePerfName }: Opts) {
106121
max: endDate.valueOf(),
107122
type: 'time',
108123
display: false,
109-
time: {
110-
tooltipFormat: 'MMM DD, YYYY',
111-
minUnit: 'day',
112-
},
113124
clip: false,
114125
ticks: {
115126
source: 'data',
@@ -175,6 +186,10 @@ export function initModule({ data, singlePerfName }: Opts) {
175186
borderWidth: 1,
176187
yAlign: 'center',
177188
caretPadding: 10,
189+
rtl: document.dir == 'rtl',
190+
callbacks: {
191+
title: items => dateFormat()(new Date(items[0].parsed.x + utcOffset)),
192+
},
178193
},
179194
},
180195
},
@@ -266,23 +281,23 @@ function makeDatasets(step: number, { data, singlePerfName }: Opts, singlePerfIn
266281
const minMax = (d: PerfRatingHistory) => [getDate(d.points[0]), getDate(d.points[d.points.length - 1])];
267282
const filteredData = data.filter(indexFilter);
268283
const dates = filteredData.filter(p => p.points.length).flatMap(minMax);
269-
const startDate = Math.min(...dates);
270-
const endDate = Math.max(...dates);
284+
let startDate = dayjs(Math.min(...dates));
285+
let endDate = dayjs(Math.max(...dates));
271286
const ds: ChartDataset<'line'>[] = filteredData.map((serie, i) => {
272287
const originalDatesAndRatings = serie.points.map(r => ({
273288
ts: getDate(r),
274289
rating: r[3],
275290
}));
276291
const perfStyle = styles.filter(indexFilter)[i];
277-
const data = smoothDates(originalDatesAndRatings, step, startDate);
292+
const data = smoothDates(originalDatesAndRatings, step, startDate.valueOf());
278293
return {
279294
indexAxis: 'x',
280295
type: 'line',
281296
label: serie.name,
282297
borderColor: perfStyle.color,
283298
hoverBorderColor: hoverBorderColor,
284299
backgroundColor: perfStyle.color,
285-
pointRadius: 0,
300+
pointRadius: data.length == 1 ? 3 : 0,
286301
pointHoverRadius: 6,
287302
data: data,
288303
pointStyle: perfStyle.symbol,
@@ -293,20 +308,22 @@ function makeDatasets(step: number, { data, singlePerfName }: Opts, singlePerfIn
293308
animation: false,
294309
};
295310
});
311+
if (endDate.diff(startDate, 'day') < 1) {
312+
startDate = startDate.subtract(1, 'day');
313+
endDate = endDate.add(1, 'day');
314+
}
296315
return { ds: ds.filter(ds => ds.data.length), startDate, endDate };
297316
}
298317
function smoothDates(data: TsAndRating[], step: number, begin: number) {
299318
const oneStep = oneDay * step;
300319
if (!data.length) return [];
301-
if (data.length == 1) return [{ x: begin, y: data[0].rating }];
302320

303321
const end = data[data.length - 1].ts;
304322
const reversed = data.slice().reverse();
305323
const allDates: number[] = [];
306324
for (let i = begin; i <= end; i += oneStep) allDates.push(i);
307-
if (!allDates.find(d => d == end)) allDates.push(end);
308325
const result: Point[] = [];
309-
for (let j = 1; j < allDates.length; j++) {
326+
for (let j = 0; j < allDates.length; j++) {
310327
const match = reversed.find(x => x.ts <= allDates[j]);
311328
if (match) result.push({ x: allDates[j], y: match.rating });
312329
}

0 commit comments

Comments
 (0)