From c4cfa7eee0e42fe1abae770d84a1b1837b370673 Mon Sep 17 00:00:00 2001 From: severinlandolt Date: Wed, 11 Dec 2024 21:02:25 +0100 Subject: [PATCH] spark charts --- .../SparkAreaChart/SparkAreaChart.tsx | 67 ++++++++-------- .../SparkBarChart/SparkBarChart.tsx | 43 +++++----- .../SparkLineChart/SparkLineChart.tsx | 56 +++++++------ .../spark-elements/SparkLineChart/index.ts | 2 +- src/components/spark-elements/common/style.ts | 79 +++++++++++++++++++ .../spark-elements/SparkLineChart.stories.tsx | 2 +- .../spark-elements/SparkAreaChart.test.tsx | 2 +- 7 files changed, 161 insertions(+), 90 deletions(-) create mode 100644 src/components/spark-elements/common/style.ts diff --git a/src/components/spark-elements/SparkAreaChart/SparkAreaChart.tsx b/src/components/spark-elements/SparkAreaChart/SparkAreaChart.tsx index 0c2e23034..ca801e946 100644 --- a/src/components/spark-elements/SparkAreaChart/SparkAreaChart.tsx +++ b/src/components/spark-elements/SparkAreaChart/SparkAreaChart.tsx @@ -2,12 +2,13 @@ import React from "react"; import { Area, AreaChart as ReChartsAreaChart, ResponsiveContainer, XAxis, YAxis } from "recharts"; -import { BaseColors, colorPalette, getColorClassNames, themeColorRange, tremorTwMerge } from "lib"; -import { CurveType } from "../../../lib/inputTypes"; -import BaseSparkChartProps from "../common/BaseSparkChartProps"; -import { constructCategoryColors, getYAxisDomain } from "components/chart-elements/common/utils"; import NoData from "components/chart-elements/common/NoData"; +import { constructCategoryColors, getYAxisDomain } from "components/chart-elements/common/utils"; +import { BaseColors, colorPalette, getColorClassNames, themeColorRange, tremorTwMerge } from "lib"; import { AxisDomain } from "recharts/types/util/types"; +import { Color, CurveType } from "../../../lib/inputTypes"; +import BaseSparkChartProps from "../common/BaseSparkChartProps"; +import { strokeColors, textColors } from "../common/style"; export interface SparkAreaChartProps extends BaseSparkChartProps { stack?: boolean; @@ -24,7 +25,7 @@ const AreaChart = React.forwardRef((props, stack = false, colors = themeColorRange, showAnimation = false, - animationDuration = 900, + animationDuration = 600, showGradient = true, curveType = "linear", connectNulls = false, @@ -46,16 +47,12 @@ const AreaChart = React.forwardRef((props, {categories.map((category) => { + const color = categoryColors.get(category) as Color; return ( {showGradient ? ( ((props, ); })} - {categories.map((category) => ( - - ))} + {categories.map((category) => { + const color = categoryColors.get(category) as Color; + return ( + + ); + })} ) : ( diff --git a/src/components/spark-elements/SparkBarChart/SparkBarChart.tsx b/src/components/spark-elements/SparkBarChart/SparkBarChart.tsx index 433378c8d..565cb7538 100644 --- a/src/components/spark-elements/SparkBarChart/SparkBarChart.tsx +++ b/src/components/spark-elements/SparkBarChart/SparkBarChart.tsx @@ -1,14 +1,15 @@ "use client"; -import { colorPalette, getColorClassNames, tremorTwMerge } from "lib"; +import { Color, tremorTwMerge } from "lib"; import React from "react"; import { Bar, BarChart as ReChartsBarChart, ResponsiveContainer, XAxis, YAxis } from "recharts"; -import { BaseColors, themeColorRange } from "lib"; -import BaseSparkChartProps from "../common/BaseSparkChartProps"; -import { constructCategoryColors, getYAxisDomain } from "components/chart-elements/common/utils"; import NoData from "components/chart-elements/common/NoData"; +import { constructCategoryColors, getYAxisDomain } from "components/chart-elements/common/utils"; +import { themeColorRange } from "lib"; import { AxisDomain } from "recharts/types/util/types"; +import BaseSparkChartProps from "../common/BaseSparkChartProps"; +import { fillColors } from "../common/style"; export interface SparkBarChartProps extends BaseSparkChartProps { stack?: boolean; @@ -46,24 +47,22 @@ const SparkBarChart = React.forwardRef((prop > - {categories.map((category) => ( - - ))} + {categories.map((category) => { + const color = categoryColors.get(category) as Color; + return ( + + ); + })} ) : ( diff --git a/src/components/spark-elements/SparkLineChart/SparkLineChart.tsx b/src/components/spark-elements/SparkLineChart/SparkLineChart.tsx index b49af3ada..03140af54 100644 --- a/src/components/spark-elements/SparkLineChart/SparkLineChart.tsx +++ b/src/components/spark-elements/SparkLineChart/SparkLineChart.tsx @@ -2,14 +2,15 @@ import React from "react"; import { Line, LineChart as ReChartsLineChart, ResponsiveContainer, XAxis, YAxis } from "recharts"; -import { BaseColors, colorPalette, getColorClassNames, themeColorRange, tremorTwMerge } from "lib"; -import { CurveType } from "../../../lib/inputTypes"; -import BaseSparkChartProps from "../common/BaseSparkChartProps"; -import { constructCategoryColors, getYAxisDomain } from "components/chart-elements/common/utils"; import NoData from "components/chart-elements/common/NoData"; +import { constructCategoryColors, getYAxisDomain } from "components/chart-elements/common/utils"; +import { themeColorRange, tremorTwMerge } from "lib"; import { AxisDomain } from "recharts/types/util/types"; +import { Color, CurveType } from "../../../lib/inputTypes"; +import BaseSparkChartProps from "../common/BaseSparkChartProps"; +import { strokeColors } from "../common/style"; -export interface SparkLineChartProps extends BaseSparkChartProps { +interface SparkLineChartProps extends BaseSparkChartProps { curveType?: CurveType; connectNulls?: boolean; } @@ -41,29 +42,26 @@ const SparkLineChart = React.forwardRef((pr - {categories.map((category) => ( - - ))} + {categories.map((category) => { + const color = categoryColors.get(category) as Color; + return ( + + ); + })} ) : ( @@ -75,4 +73,4 @@ const SparkLineChart = React.forwardRef((pr SparkLineChart.displayName = "SparkLineChart"; -export default SparkLineChart; +export { SparkLineChart, type SparkLineChartProps }; diff --git a/src/components/spark-elements/SparkLineChart/index.ts b/src/components/spark-elements/SparkLineChart/index.ts index 5e93d8e5c..658dbc4aa 100644 --- a/src/components/spark-elements/SparkLineChart/index.ts +++ b/src/components/spark-elements/SparkLineChart/index.ts @@ -1,2 +1,2 @@ -export { default as SparkLineChart } from "./SparkLineChart"; +export { SparkLineChart } from "./SparkLineChart"; export type { SparkLineChartProps } from "./SparkLineChart"; diff --git a/src/components/spark-elements/common/style.ts b/src/components/spark-elements/common/style.ts new file mode 100644 index 000000000..b059b5fca --- /dev/null +++ b/src/components/spark-elements/common/style.ts @@ -0,0 +1,79 @@ +import { Color } from "lib"; + +export const strokeColors: { [color in Color]: string } = { + brand: "stroke-tremor-brand-default", + slate: "stroke-slate-500", + gray: "stroke-gray-500", + zinc: "stroke-zinc-500", + neutral: "stroke-neutral-500", + stone: "stroke-stone-500", + red: "stroke-red-500", + orange: "stroke-orange-500", + amber: "stroke-amber-500", + yellow: "stroke-yellow-500", + lime: "stroke-lime-500", + green: "stroke-green-500", + emerald: "stroke-emerald-500", + teal: "stroke-teal-500", + cyan: "stroke-cyan-500", + sky: "stroke-sky-500", + blue: "stroke-blue-500", + indigo: "stroke-indigo-500", + violet: "stroke-violet-500", + purple: "stroke-purple-500", + fuchsia: "stroke-fuchsia-500", + pink: "stroke-pink-500", + rose: "stroke-rose-500", +}; + +export const fillColors: { [color in Color]: string } = { + brand: "fill-tremor-brand-default", + slate: "fill-slate-500", + gray: "fill-gray-500", + zinc: "fill-zinc-500", + neutral: "fill-neutral-500", + stone: "fill-stone-500", + red: "fill-red-500", + orange: "fill-orange-500", + amber: "fill-amber-500", + yellow: "fill-yellow-500", + lime: "fill-lime-500", + green: "fill-green-500", + emerald: "fill-emerald-500", + teal: "fill-teal-500", + cyan: "fill-cyan-500", + sky: "fill-sky-500", + blue: "fill-blue-500", + indigo: "fill-indigo-500", + violet: "fill-violet-500", + purple: "fill-purple-500", + fuchsia: "fill-fuchsia-500", + pink: "fill-pink-500", + rose: "fill-rose-500", +}; + +export const textColors: { [color in Color]: string } = { + brand: "text-tremor-brand-default", + slate: "text-slate-500", + gray: "text-gray-500", + zinc: "text-zinc-500", + neutral: "text-neutral-500", + stone: "text-stone-500", + red: "text-red-500", + orange: "text-orange-500", + amber: "text-amber-500", + yellow: "text-yellow-500", + lime: "text-lime-500", + green: "text-green-500", + emerald: "text-emerald-500", + teal: "text-teal-500", + cyan: "text-cyan-500", + sky: "text-sky-500", + blue: "text-blue-500", + indigo: "text-indigo-500", + violet: "text-violet-500", + purple: "text-purple-500", + fuchsia: "text-fuchsia-500", + pink: "text-pink-500", + rose: "text-rose-500", +}; diff --git a/src/stories/spark-elements/SparkLineChart.stories.tsx b/src/stories/spark-elements/SparkLineChart.stories.tsx index 3ba2cf50e..ef21ec6b8 100644 --- a/src/stories/spark-elements/SparkLineChart.stories.tsx +++ b/src/stories/spark-elements/SparkLineChart.stories.tsx @@ -2,13 +2,13 @@ import React from "react"; import type { Meta, StoryObj } from "@storybook/react"; +import { SparkLineChart } from "components/spark-elements"; import { simpleBaseChartData as data, longBaseChartData, simpleBaseChartDataWithNulls, singleAndMultipleData, } from "../chart-elements/helpers/testData"; -import { SparkLineChart } from "components/spark-elements"; import ExampleCard from "./helpers/ExampleCard"; import { smallVariantionDatas } from "./helpers/testData"; diff --git a/src/tests/spark-elements/SparkAreaChart.test.tsx b/src/tests/spark-elements/SparkAreaChart.test.tsx index 925d1ff25..322180cd8 100644 --- a/src/tests/spark-elements/SparkAreaChart.test.tsx +++ b/src/tests/spark-elements/SparkAreaChart.test.tsx @@ -1,6 +1,6 @@ -import React from "react"; import { render } from "@testing-library/react"; import AreaChart from "components/spark-elements/SparkAreaChart/SparkAreaChart"; +import React from "react"; describe("AreaChart", () => { beforeAll(() => {