-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add first working draft of chartjs islands (#15)
- Loading branch information
Showing
8 changed files
with
229 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { Chart as default } from "$fresh_charts/island.tsx"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
import ChartJS, { | ||
type ChartConfiguration, | ||
type ChartType, | ||
type DefaultDataPoint, | ||
} from "chart.js"; | ||
import type { JSX } from "preact"; | ||
import { useEffect, useRef } from "preact/hooks"; | ||
|
||
export type { ChartType, DefaultDataPoint }; | ||
|
||
export type ChartProps< | ||
Type extends ChartType, | ||
Data = DefaultDataPoint<Type>, | ||
Label = unknown, | ||
> = ChartConfiguration<Type, Data, Label> & { | ||
canvas?: JSX.HTMLAttributes<HTMLCanvasElement>; | ||
}; | ||
|
||
/** | ||
* A hook which takes in a Chart.js configuration object and returns `canvasRef` and `chartRef`. | ||
* | ||
* `canvasRef` is a reference to the canvas element which the chart is rendered to. | ||
* `chartRef` is a reference to the Chart.js instance. | ||
*/ | ||
function useChart< | ||
Type extends ChartType, | ||
Data = DefaultDataPoint<Type>, | ||
Label = unknown, | ||
>(options: ChartConfiguration<Type, Data, Label>) { | ||
const canvasRef = useRef<HTMLCanvasElement | null>(null); | ||
const chartRef = useRef<ChartJS<Type, Data, Label> | null>(null); | ||
|
||
useEffect(() => { | ||
if (canvasRef.current === null) { | ||
throw new Error("canvas is null"); | ||
} | ||
if (chartRef.current) { | ||
chartRef.current.destroy(); | ||
} | ||
|
||
chartRef.current = new ChartJS( | ||
canvasRef.current, | ||
options, | ||
); | ||
|
||
return () => { | ||
chartRef.current?.destroy(); | ||
}; | ||
}, [canvasRef, options]); | ||
|
||
return { canvasRef, chartRef }; | ||
} | ||
|
||
/** | ||
* A JSX component which can be used to client side render a chart inline | ||
* within a page. | ||
* | ||
* View {@linkcode ChartProps} for a list of properties that can be set | ||
* on the component. | ||
* | ||
* ### Example | ||
* | ||
* ```tsx | ||
* import { Chart } from "https://deno.land/x/fresh_charts/island.tsx"; | ||
* import { ChartColors } from "https://deno.land/x/fresh_charts/utils.ts"; | ||
* | ||
* export default App() { | ||
* return <> | ||
* <h1>Chart Example</h1> | ||
* <Chart | ||
* type="line" | ||
* options={{ | ||
* scales: { yAxes: [{ ticks: { beginAtZero: true } }] }, | ||
* }} | ||
* data={{ | ||
* labels: ["1", "2", "3"], | ||
* datasets: [{ | ||
* label: "Sessions", | ||
* data: [123, 234, 234], | ||
* borderColor: ChartColors.Red, | ||
* borderWidth: 1, | ||
* }, { | ||
* label: "Users", | ||
* data: [346, 233, 123], | ||
* borderColor: ChartColors.Blue, | ||
* borderWidth: 1, | ||
* }], | ||
* }} | ||
* /> | ||
* <>; | ||
* } | ||
* ``` | ||
*/ | ||
export function Chart<Type extends ChartType>(props: ChartProps<Type>) { | ||
const { canvasRef, chartRef } = useChart<Type>(props); | ||
|
||
useEffect(() => { | ||
chartRef.current?.render(); | ||
}, []); | ||
|
||
return <canvas ref={canvasRef} />; | ||
} |