summaryrefslogtreecommitdiff
path: root/makima/frontend/src/components/charts/ChartRenderer.tsx
blob: 276b170492f6b18086ea0a197b9ce2e373e8d3e8 (plain) (blame)
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
import { useMemo } from "react";
import {
  LineChart,
  Line,
  BarChart,
  Bar,
  PieChart,
  Pie,
  AreaChart,
  Area,
  XAxis,
  YAxis,
  CartesianGrid,
  Tooltip,
  Legend,
  ResponsiveContainer,
  Cell,
} from "recharts";
import type { ChartType } from "../../lib/api";

interface ChartRendererProps {
  chartType: ChartType;
  data: Record<string, unknown>[];
  title?: string;
  config?: Record<string, unknown>;
}

// Default color palette
const COLORS = [
  "#9bc3ff",
  "#ff9b9b",
  "#9bffb3",
  "#ffeb9b",
  "#d49bff",
  "#9bfff0",
  "#ff9beb",
  "#b3ff9b",
];

export function ChartRenderer({
  chartType,
  data,
  title,
  config,
}: ChartRendererProps) {
  // Extract data keys (excluding 'name' which is used for labels)
  const dataKeys = useMemo(() => {
    if (data.length === 0) return [];
    const keys = Object.keys(data[0]).filter((key) => key !== "name");
    return keys;
  }, [data]);

  // Get colors from config or use defaults
  const colors = (config?.colors as string[]) || COLORS;

  const renderChart = () => {
    switch (chartType) {
      case "line":
        return (
          <LineChart data={data}>
            <CartesianGrid strokeDasharray="3 3" stroke="#333" />
            <XAxis dataKey="name" stroke="#9bc3ff" fontSize={12} />
            <YAxis stroke="#9bc3ff" fontSize={12} />
            <Tooltip
              contentStyle={{
                backgroundColor: "#111",
                border: "1px solid #9bc3ff",
                borderRadius: "0",
              }}
            />
            <Legend />
            {dataKeys.map((key, i) => (
              <Line
                key={key}
                type="monotone"
                dataKey={key}
                stroke={colors[i % colors.length]}
                strokeWidth={2}
                dot={{ fill: colors[i % colors.length] }}
              />
            ))}
          </LineChart>
        );

      case "bar":
        return (
          <BarChart data={data}>
            <CartesianGrid strokeDasharray="3 3" stroke="#333" />
            <XAxis dataKey="name" stroke="#9bc3ff" fontSize={12} />
            <YAxis stroke="#9bc3ff" fontSize={12} />
            <Tooltip
              contentStyle={{
                backgroundColor: "#111",
                border: "1px solid #9bc3ff",
                borderRadius: "0",
              }}
            />
            <Legend />
            {dataKeys.map((key, i) => (
              <Bar key={key} dataKey={key} fill={colors[i % colors.length]} />
            ))}
          </BarChart>
        );

      case "area":
        return (
          <AreaChart data={data}>
            <CartesianGrid strokeDasharray="3 3" stroke="#333" />
            <XAxis dataKey="name" stroke="#9bc3ff" fontSize={12} />
            <YAxis stroke="#9bc3ff" fontSize={12} />
            <Tooltip
              contentStyle={{
                backgroundColor: "#111",
                border: "1px solid #9bc3ff",
                borderRadius: "0",
              }}
            />
            <Legend />
            {dataKeys.map((key, i) => (
              <Area
                key={key}
                type="monotone"
                dataKey={key}
                stroke={colors[i % colors.length]}
                fill={colors[i % colors.length]}
                fillOpacity={0.3}
              />
            ))}
          </AreaChart>
        );

      case "pie":
        // For pie charts, use the first data key as value
        const valueKey = dataKeys[0] || "value";
        return (
          <PieChart>
            <Pie
              data={data}
              dataKey={valueKey}
              nameKey="name"
              cx="50%"
              cy="50%"
              outerRadius={80}
              label={({ name, percent }) =>
                `${name}: ${((percent ?? 0) * 100).toFixed(0)}%`
              }
              labelLine={{ stroke: "#9bc3ff" }}
            >
              {data.map((_, i) => (
                <Cell key={i} fill={colors[i % colors.length]} />
              ))}
            </Pie>
            <Tooltip
              contentStyle={{
                backgroundColor: "#111",
                border: "1px solid #9bc3ff",
                borderRadius: "0",
              }}
            />
            <Legend />
          </PieChart>
        );

      default:
        return <div className="text-red-400">Unknown chart type: {chartType}</div>;
    }
  };

  return (
    <div className="w-full">
      {title && (
        <h4 className="text-[#9bc3ff] font-mono text-sm mb-2">{title}</h4>
      )}
      <div className="h-64 w-full">
        <ResponsiveContainer width="100%" height="100%">
          {renderChart()}
        </ResponsiveContainer>
      </div>
    </div>
  );
}