-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathrender.ts
57 lines (55 loc) · 1.72 KB
/
render.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
import { chart, type ChartConfiguration } from "./core.ts";
import { type ChartJs } from "./deps.ts";
/** Render a chart, based on the configuration, returning the chart as an SVG
* {@linkcode Response}.
*
* View {@linkcode ChartConfiguration} for information on how to configure a
* chart to be rendered.
*
* ```ts
* import { type Handlers } from "$fresh/server.ts";
* import { renderChart } from "https://deno.land/x/fresh_charts/mod.ts";
* import {
* ChartColors,
* transparentize
* } from "https://deno.land/x/fresh_charts/utils.ts";
*
* export const handler: Handlers = {
* GET() {
* return renderChart({
* type: "line",
* data: {
* labels: ["1", "2", "3"],
* datasets: [{
* label: "Sessions",
* data: [123, 234, 234],
* borderColor: ChartColors.Red,
* backgroundColor: transparentize(ChartColors.Red, 0.5),
* borderWidth: 1,
* }, {
* label: "Users",
* data: [346, 233, 123],
* borderColor: ChartColors.Blue,
* backgroundColor: transparentize(ChartColors.Blue, 0.5),
* borderWidth: 1,
* }],
* },
* options: {
* devicePixelRatio: 1,
* scales: { yAxes: [{ ticks: { beginAtZero: true } }] },
* },
* });
* },
* };
* ```
*/
export function renderChart<
TType extends ChartJs.ChartType = ChartJs.ChartType,
TData = ChartJs.DefaultDataPoint<TType>,
TLabel = unknown,
>(configuration?: ChartConfiguration<TType, TData, TLabel>): Response {
return new Response(chart(configuration), {
headers: { "content-type": "image/svg+xml" },
});
}