Skip to content

Commit

Permalink
add chart
Browse files Browse the repository at this point in the history
  • Loading branch information
samfairbairn committed May 5, 2024
1 parent 400847c commit 24b1856
Show file tree
Hide file tree
Showing 3 changed files with 150 additions and 78 deletions.
2 changes: 1 addition & 1 deletion packages/storykit/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@storyprotocol/storykit",
"author": "storyprotocol engineering <eng@storyprotocol.xyz>",
"version": "0.1.6-mrtest.5",
"version": "0.1.6-mrtest.6",
"type": "module",
"exports": {
".": {
Expand Down
224 changes: 148 additions & 76 deletions packages/storykit/src/components/IPARoyaltyChart/IPARoyaltyChart.tsx
Original file line number Diff line number Diff line change
@@ -1,92 +1,164 @@
import { shortenAddress } from "@/lib/utils"
import { useIPAssetContext } from "@/providers"
import Chart from "react-apexcharts"
import { useEffect, useRef, useState } from "react"
import type { Props as ReactApexChartsProps } from "react-apexcharts"
import { Address } from "viem"

import "../../global.css"

function IPARoyaltyChart() {
// const Chart = dynamic(() => import("react-apexcharts"), { ssr: false })
// import { useLoadReactApexCharts } from "./useLoadApexChart"

function IPARoyaltyChart() {
const { royaltyData } = useIPAssetContext()

if (royaltyData?.targetAncestors) {
const chart = {
options: {
chart: {
width: 300,
type: "pie" as `pie`,
},
dataLabels: {
style: {
fontSize: "20px",
// fontFamily: "Inter, ui-sans-serif",
fontWeight: "400",
colors: ["#fff", "#fff", "#1f2937"],
},
dropShadow: {
enabled: false,
},
formatter: (value: number) => `${value.toFixed(1)} %`,
},
// labels: ["Direct", "Organic search"],
plotOptions: {
pie: {
dataLabels: {
offset: -20,
},
},
},
colors: ["#5f6060", "#494848"],
tooltip: {
enabled: true,
y: {
title: {
formatter: (value: Address) => shortenAddress(value),
},
},
},
legend: {
show: false,
},
labels: royaltyData?.targetAncestors,
responsive: [
{
breakpoint: 480,
options: {
chart: {
width: 300,
},
legend: {
position: "bottom",
},
},
},
],
const ApexChart = useLoadReactApexCharts()

// eslint-disable-next-line @typescript-eslint/no-explicit-any
// const [Chart, setChart] = useState<any>(null)

// useEffect(() => {
// // ForceGraph will break SSR, and needs to be loaded dynamically
// async function importChartsModule() {
// const ch = await import("react-apexcharts")
// console.log(ch)
// setChart(ch)
// // setChart(fg.default)
// }
// importChartsModule()
// }, [])

if (!ApexChart) return null

// const ChartComponent = ApexChart.default
const ChartComponent = ApexChart.default

// if (royaltyData?.targetAncestors) {
const chart = {
options: {
chart: {
id: "apexchart-example",
},
series: royaltyData?.targetRoyaltyAmount.map(Number),
}
xaxis: {
categories: [1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999],
},
},
series: [
{
name: "series-1",
data: [30, 40, 35, 50, 49, 60, 70, 91, 125],
},
],

return (
<div className="flex flex-col items-center justify-between">
<div className="min-h-[230px]">
<Chart options={chart.options} series={chart.series} type="pie" height={250} width="300" />
</div>
<div className="w-full min-w-[300px] px-2">
<dl className="divide-y divide-gray-100 overflow-x-hidden text-sm leading-6">
{royaltyData?.targetAncestors.map((target, i) => (
<div key={target} className="flex justify-between gap-x-4 py-1">
<dt className="text-xs capitalize text-gray-500">{shortenAddress(target)}</dt>
<dd className="truncate text-gray-700">{royaltyData.targetRoyaltyAmount[i]}</dd>
</div>
))}
</dl>
</div>
</div>
)
// chart: {
// width: 300,
// type: "pie" as `pie`,
// },
// dataLabels: {
// style: {
// fontSize: "20px",
// // fontFamily: "Inter, ui-sans-serif",
// fontWeight: "400",
// colors: ["#fff", "#fff", "#1f2937"],
// },
// dropShadow: {
// enabled: false,
// },
// formatter: (value: number) => `${value.toFixed(1)} %`,
// },
// // labels: ["Direct", "Organic search"],
// plotOptions: {
// pie: {
// dataLabels: {
// offset: -20,
// },
// },
// },
// colors: ["#5f6060", "#494848"],
// tooltip: {
// enabled: true,
// y: {
// title: {
// formatter: (value: Address) => shortenAddress(value),
// },
// },
// },
// legend: {
// show: false,
// },
// labels: royaltyData?.targetAncestors,
// responsive: [
// {
// breakpoint: 480,
// options: {
// chart: {
// width: 300,
// },
// legend: {
// position: "bottom",
// },
// },
// },
// ],
// },
// series: royaltyData?.targetRoyaltyAmount.map(Number),
}

return <div className="flex h-60 flex-col items-center justify-center text-slate-400">No Royalty Data</div>
return (
<div className="flex flex-col items-center justify-between">
<div className="min-h-[230px]">
{/* <ChartComponent options={chart.options} series={chart.series} type="pie" height={250} width="300" /> */}
<ChartComponent options={chart.options} series={chart.series} type="bar" width={500} height={320} />
</div>
<div className="w-full min-w-[300px] px-2">
<dl className="divide-y divide-gray-100 overflow-x-hidden text-sm leading-6">
{royaltyData?.targetAncestors.map((target, i) => (
<div key={target} className="flex justify-between gap-x-4 py-1">
<dt className="text-xs capitalize text-gray-500">{shortenAddress(target)}</dt>
<dd className="truncate text-gray-700">{royaltyData.targetRoyaltyAmount[i]}</dd>
</div>
))}
</dl>
</div>
</div>
)
// }

// return <div className="flex h-60 flex-col items-center justify-center text-slate-400">No Royalty Data</div>
}

export default IPARoyaltyChart

type ReactApexChartsType = React.NamedExoticComponent<ReactApexChartsProps>

type ReactApexChartModule = {
__esModule: true
default: ReactApexChartsType
}

function useLoadReactApexCharts() {
const appexRef = useRef<boolean>(false)

const [apexChartModule, setApexChartModule] = useState<ReactApexChartModule | null>(null)

useEffect(() => {
async function loadAppex() {
const module = (await import("react-apexcharts")) as unknown as ReactApexChartModule

/**
* For some reason, the type inference here is not working correctly.
*
* It assumes that the `default` property is the component itself, but
* when we console log it, it's another module. My gut feeling is that
* due the module transpilation, it converts to an object that contains the shape
* specified in the `ReactApexChartModule` type.
*/
setApexChartModule(module)
}

if (appexRef.current === false) {
loadAppex()
}
}, [appexRef])

return apexChartModule
}
2 changes: 1 addition & 1 deletion packages/storykit/src/components/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ export * from "./Button"
// export * from "./IPAssetWidget"
export * from "./IPAGraph"
export * from "./IPAPolicies"
// export * from "./IPARoyaltyChart"
export * from "./IPARoyaltyChart"

0 comments on commit 24b1856

Please sign in to comment.