Spaces:
Sleeping
Sleeping
File size: 9,452 Bytes
c21180c 8ac8526 c21180c 8ac8526 c21180c 8ac8526 c21180c 8ac8526 c21180c 8ac8526 c21180c 8ac8526 c21180c 8ac8526 c21180c 8ac8526 c21180c 8ac8526 c21180c 8ac8526 c21180c | 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 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 | import React, { useState } from 'react';
import { useSessionStore } from '../store/session';
import { useNotebookStore } from '../store/notebook';
import { CHART_TYPES, getChartType, isChartAvailable, countColumnTypes } from '../lib/chartTypes';
export function VisualizationPanel() {
const { columns } = useSessionStore();
const { addCell, updateCell } = useNotebookStore();
const [selectedType, setSelectedType] = useState<string>('');
const [params, setParams] = useState<Record<string, any>>({});
const colNames = columns.map((c) => c.name);
const numericCols = columns.filter((c) => c.column_type === 'numeric').map((c) => c.name);
const categoricalCols = columns.filter((c) => c.column_type === 'categorical').map((c) => c.name);
const datetimeCols = columns.filter((c) => c.column_type === 'datetime').map((c) => c.name);
const geoCols = columns.filter((c) => c.column_type === 'geo_lat_lon').map((c) => c.name);
const typeCounts = countColumnTypes(columns);
const chartType = getChartType(selectedType);
const handleCreate = () => {
if (!selectedType) return;
const cellId = addCell(selectedType === 'map' ? 'map' : 'chart', `${selectedType} chart`, {
chartType: selectedType,
params,
});
updateCell(cellId, { config: { chartType: selectedType, params } });
};
const renderColumnSelect = (name: string, relevantCols: string[], required: boolean) => (
<div key={name}>
<label className="block text-xs font-medium text-gray-600 mb-1">
{name} {required && <span className="text-red-500">*</span>}
</label>
<select
className="input-field"
value={params[name] || ''}
onChange={(e) => setParams({ ...params, [name]: e.target.value })}
>
<option value="">Select column...</option>
{relevantCols.map((c) => (
<option key={c} value={c}>{c}</option>
))}
{relevantCols.length === 0 && colNames.map((c) => (
<option key={c} value={c}>{c}</option>
))}
</select>
</div>
);
const getRelevantCols = (param: string): string[] => {
if (param.includes('x_col') || param.includes('y_col') || param.includes('value') || param === 'column') {
return numericCols.length > 0 ? numericCols : colNames;
}
if (param === 'columns') {
return numericCols.length > 0 ? numericCols : colNames;
}
if (param.includes('category')) {
return categoricalCols.length > 0 ? categoricalCols : colNames;
}
if (param.includes('date')) {
return datetimeCols.length > 0 ? datetimeCols : colNames;
}
if (param.includes('lat') || param.includes('lon')) {
return geoCols.length > 0 ? geoCols : numericCols.length > 0 ? numericCols : colNames;
}
if (param.includes('group')) {
return categoricalCols.length > 0 ? categoricalCols : colNames;
}
return colNames;
};
// Render a multi-select for columns (used by radar chart)
const renderMultiColumnSelect = (name: string, relevantCols: string[], required: boolean) => (
<div key={name}>
<label className="block text-xs font-medium text-gray-600 mb-1">
{name} (select multiple) {required && <span className="text-red-500">*</span>}
</label>
<div className="max-h-32 overflow-y-auto border border-gray-300 rounded-lg p-2 space-y-1 bg-white">
{relevantCols.map((c) => {
const selected = (params[name] || []).includes(c);
return (
<label key={c} className="flex items-center gap-2 text-xs cursor-pointer hover:bg-gray-50 px-1 py-0.5 rounded">
<input
type="checkbox"
checked={selected}
onChange={(e) => {
const current: string[] = params[name] || [];
const next = e.target.checked
? [...current, c]
: current.filter((x: string) => x !== c);
setParams({ ...params, [name]: next });
}}
className="rounded border-gray-300"
/>
<span>{c}</span>
</label>
);
})}
{relevantCols.length === 0 && (
<p className="text-xs text-gray-400 italic">No columns available</p>
)}
</div>
{(params[name] || []).length > 0 && (
<p className="text-[10px] text-gray-400 mt-1">{(params[name] || []).length} selected</p>
)}
</div>
);
return (
<div className="p-4">
<h4 className="text-sm font-semibold text-gray-900 mb-3">Create Visualization</h4>
{/* Chart type grid */}
<div className="grid grid-cols-2 gap-1.5 mb-4">
{CHART_TYPES.map((ct) => {
const available = isChartAvailable(ct, columns);
return (
<div key={ct.key} className="relative group">
<button
onClick={() => {
if (available) {
setSelectedType(ct.key);
setParams({});
}
}}
disabled={!available}
className={`w-full p-2 rounded-lg border text-left transition-colors ${
selectedType === ct.key
? 'border-brand-500 bg-brand-50 text-brand-700'
: available
? 'border-gray-200 hover:border-gray-300 text-gray-600'
: 'border-gray-100 bg-gray-50 text-gray-300 cursor-not-allowed'
}`}
>
<div className="font-medium text-xs">{ct.label}</div>
<div className="text-[10px] text-gray-400 mt-0.5">{ct.description}</div>
</button>
{!available && (
<div className="absolute bottom-full left-1/2 -translate-x-1/2 mb-1 px-2 py-1 bg-gray-800 text-white text-[10px] rounded whitespace-nowrap opacity-0 group-hover:opacity-100 transition-opacity pointer-events-none z-10">
{ct.unavailableMessage}
</div>
)}
</div>
);
})}
</div>
{/* Available types summary */}
<div className="mb-3 text-[10px] text-gray-400 flex flex-wrap gap-x-3 gap-y-0.5">
{typeCounts.numeric > 0 && <span># Numeric: {typeCounts.numeric}</span>}
{typeCounts.categorical > 0 && <span>Aa Categorical: {typeCounts.categorical}</span>}
{typeCounts.datetime > 0 && <span>DT Datetime: {typeCounts.datetime}</span>}
{typeCounts.geo_lat_lon > 0 && <span>G Geo: {typeCounts.geo_lat_lon}</span>}
</div>
{/* Parameter form */}
{chartType && (
<div className="space-y-3 mb-4">
{chartType.requiredParams.map((param) => {
if (param === 'columns') {
const relevantCols = getRelevantCols(param);
return renderMultiColumnSelect(param, relevantCols, true);
}
const relevantCols = getRelevantCols(param);
return renderColumnSelect(param, relevantCols, true);
})}
{chartType.optionalParams.map((param) => {
if (param === 'color_col' || param === 'label_col' || param === 'group_col') {
return renderColumnSelect(param, colNames, false);
}
if (param === 'bins' || param === 'n') {
return (
<div key={param}>
<label className="block text-xs font-medium text-gray-600 mb-1">{param}</label>
<input
type="number"
className="input-field"
value={params[param] || (param === 'bins' ? 20 : 15)}
onChange={(e) => setParams({ ...params, [param]: parseInt(e.target.value) || 0 })}
/>
</div>
);
}
if (param === 'sort_desc') {
return (
<label key={param} className="flex items-center gap-2 text-sm">
<input
type="checkbox"
checked={params[param] !== false}
onChange={(e) => setParams({ ...params, [param]: e.target.checked })}
className="rounded border-gray-300"
/>
Sort descending
</label>
);
}
if (param === 'agg') {
return (
<div key={param}>
<label className="block text-xs font-medium text-gray-600 mb-1">Aggregation</label>
<select
className="input-field"
value={params[param] || 'sum'}
onChange={(e) => setParams({ ...params, [param]: e.target.value })}
>
<option value="sum">Sum</option>
<option value="mean">Mean</option>
<option value="count">Count</option>
<option value="min">Min</option>
<option value="max">Max</option>
<option value="median">Median</option>
</select>
</div>
);
}
return null;
})}
</div>
)}
<button
onClick={handleCreate}
disabled={!selectedType}
className="btn-primary w-full"
>
Create Chart
</button>
</div>
);
}
|