From 3ca44334f6c4b9e983d7b61ce0a11ee87142d899 Mon Sep 17 00:00:00 2001 From: cyclane Date: Mon, 19 Jul 2021 02:26:37 +0100 Subject: [PATCH] Multiple series support --- src/lib/api.ts | 2 +- .../api/graphs/[graph]/[profile]/[player].ts | 39 ++++++++++++++++++- .../member/[member]/graph/[graph].svelte | 34 ++++++++++++---- 3 files changed, 65 insertions(+), 10 deletions(-) diff --git a/src/lib/api.ts b/src/lib/api.ts index a1baef5..6227997 100644 --- a/src/lib/api.ts +++ b/src/lib/api.ts @@ -229,7 +229,7 @@ export async function getGraph( graph: string, profile: string, member: string -): Promise<[number, number][]> { +): Promise<([number, number] | [string, number, number][])[]> { let response = await request( `/api/graphs/${graph}/${profile}/${member}`, token ? { headers: { Authorization: token } } : {} diff --git a/src/routes/api/graphs/[graph]/[profile]/[player].ts b/src/routes/api/graphs/[graph]/[profile]/[player].ts index a032e8c..d5bef61 100644 --- a/src/routes/api/graphs/[graph]/[profile]/[player].ts +++ b/src/routes/api/graphs/[graph]/[profile]/[player].ts @@ -40,7 +40,44 @@ export async function get( }) ).all(); res.writeHead(200); - res.end(JSON.stringify(data)); + if (graph.data.style === "line") { + if (data[0].length === 2) { + res.end( + JSON.stringify( + data.filter((value, idx) => { + return ( + idx === 0 || + idx === data.length - 1 || + data[idx - 1][1] !== value || + data[idx + 1][1] !== value + ); + }) + ) + ); + } else { + let d: [string, number, number][][] = data; + res.end( + JSON.stringify( + d.map((values, idx) => { + return values.filter(value => { + return ( + idx === 0 || + idx === d.length - 1 || + d[idx - 1].find( + v => v[0] === value[0] + )?.[2] !== value[2] || + d[idx + 1].find( + v => v[0] === value[0] + )?.[2] !== value[2] + ); + }); + }) + ) + ); + } + } else { + res.end(JSON.stringify(data)); + } return; default: res.writeHead(404); diff --git a/src/routes/profile/[profile]/member/[member]/graph/[graph].svelte b/src/routes/profile/[profile]/member/[member]/graph/[graph].svelte index faf9e48..ad508b3 100644 --- a/src/routes/profile/[profile]/member/[member]/graph/[graph].svelte +++ b/src/routes/profile/[profile]/member/[member]/graph/[graph].svelte @@ -25,7 +25,6 @@ getUsername } from "../../../../../../lib/api"; import { login, extraNav } from "../../../../../../components/stores"; - import type { Graph } from "../../../../../api/_types"; $extraNav; @@ -79,13 +78,32 @@ yAxis: g.data.y_axis || { type: "value" }, - series: [ - { - name: g.data.name, - type: g.data.style, - data: data - } - ], + series: + data[0].length === 2 + ? [ + { + name: g.data.name, + type: g.data.style, + data: data + } + ] + : (<[string, number, number][]>data[0]).map( + ([name]) => { + return { + name, + type: g?.data.style, + data: (<[string, number, number][][]>( + data + )) + .map(is => + is + .find(i => i[0] === name) + ?.slice(1) + ) + .filter(v => v !== undefined) + }; + } + ), dataZoom: [ { id: "dataZoomX",