返回 presentation-ai
ag-chart-wrapper.tsx
根目录 / src / components / notebook / presentation / editor / custom-elements / charts / ag-chart-wrapper.tsx
1 "use client";
2
3 import { cn } from "@/lib/utils";
4 import { type AgChartOptions } from "ag-charts-community";
5 import { AgCharts } from "ag-charts-react";
6
7 export interface AgChartWrapperProps {
8 options: AgChartOptions;
9 className?: string;
10 style?: React.CSSProperties;
11 }
12
13 // Default chart colors - vibrant, visible colors that work well on both light and dark backgrounds
14 // These match the shadcn chart color palette
15 const CHART_COLORS = [
16 "#2563eb", // Blue (chart-1)
17 "#16a34a", // Green (chart-2)
18 "#ea580c", // Orange (chart-3)
19 "#8b5cf6", // Purple (chart-4)
20 "#ec4899", // Pink (chart-5)
21 "#14b8a6", // Teal
22 "#f59e0b", // Amber
23 "#ef4444", // Red
24 "#06b6d4", // Cyan
25 "#84cc16", // Lime
26 ];
27
28 /**
29 * Get chart color from index (cycles through chart colors)
30 */
31 export function getChartColor(index: number): string {
32 return CHART_COLORS[index % CHART_COLORS.length] ?? CHART_COLORS[0]!;
33 }
34
35 /**
36 * Get all chart colors array
37 */
38 export function getChartColors(): string[] {
39 return CHART_COLORS;
40 }
41
42 /**
43 * Wrapper component for AG Charts with shared styling
44 * Provides consistent theming that respects presentation CSS variables
45 */
46 export function AgChartWrapper({
47 options,
48 className,
49 style,
50 }: AgChartWrapperProps) {
51 // Merge default theme settings with provided options
52 const themedOptions: AgChartOptions = {
53 ...options,
54 background: {
55 visible: false,
56 },
57 // Apply theme overrides
58 theme: {
59 baseTheme: "ag-default-dark",
60 palette: {
61 fills: CHART_COLORS,
62 strokes: CHART_COLORS,
63 },
64 overrides: {
65 common: {
66 legend: {
67 item: {
68 label: {
69 color: "#e5e5e5",
70 },
71 },
72 },
73 },
74 },
75 },
76 };
77
78 return (
79 <div
80 className={cn("h-full w-full", className)}
81 style={{
82 backgroundColor: "var(--presentation-background)",
83 color: "var(--presentation-text)",
84 ...style,
85 }}
86 >
87 <AgCharts options={themedOptions} />
88 </div>
89 );
90 }
91
92 /**
93 * Build base chart options with common settings
94 */
95 export function buildBaseChartOptions(
96 dataArray: Record<string, unknown>[],
97 options: {
98 showLegend?: boolean;
99 showGrid?: boolean;
100 disableAnimation?: boolean;
101 primaryColor?: string;
102 } = {},
103 ): Partial<AgChartOptions> {
104 const { showLegend = true, disableAnimation = false } = options;
105
106 return {
107 data: dataArray,
108 legend: {
109 enabled: showLegend,
110 },
111 animation: {
112 enabled: !disableAnimation,
113 },
114 };
115 }
116
116 lines Plain Text