michi-vz
v0.5.44
Published
a visualization library based on d3 and react
Readme
Overview
A React-based data visualization library built with D3.js and TypeScript.
Recent Updates (v0.5.2)
- ✅ Fixed infinite loop issues in LineChart and GapChart components
- ✅ Improved stability - resolved "Maximum update depth exceeded" errors
- ✅ Better debugging - added source maps for clearer stack traces in consuming projects
- ✅ Code cleanup - removed unused Redux/worker files for smaller bundle size
- ✅ Better performance - optimized useEffect dependencies across components
Tech Stack
react/typescriptstorybookfor component developmenteslintandprettierto harmonize the code basestyled-componentsfor stylingd3for data visualization
Project structure
- `/src`: all source files go here
- `/components`: individual components shared across the project
- `/components/__tests__`: test files for components
- `/node_modules`: handled by npm, don't touch this
- `stories`: contains stories for storybookConfigure your project
Install dependencies:
npm installConfigure TypeScript:
- The
tsconfig.jsonfile is already set up to handle both source code and Storybook stories - Make sure your stories match the component interfaces to avoid type errors
- The
Setup Storybook:
- Storybook is configured to run on port 6006
- Component stories should be placed in the
stories/directory - Use the MichiVzProvider to provide theme and color settings for your components
Linting and Formatting:
- Run
npm run lintto check for linting errors - Run
npm run lint:fixto automatically fix linting issues
- Run
Testing
Running Tests:
npm run test # Run all tests npm run test:watch # Run tests in watch mode npm run test -- -u # Update snapshotsTest Structure:
- Tests are located in
src/components/__tests__/ - Each component should have a corresponding
ComponentName.test.tsxfile - Utility functions and mocks are in
test-utils.tsx - Global mocks for React 19 are in
setupTests.js
- Tests are located in
React 19 Compatibility:
- Tests are set up to handle React 19's concurrent rendering features
- We use longer timeouts for async operations due to React 19's rendering behavior
- Some React 19 APIs are mocked in the test environment for stability
- Make sure to use the latest
@testing-library/react(v15+) for compatibility
Writing Tests:
- Use the
customRenderfunction from test-utils to render components with the MichiVzProvider - SVG methods used by D3 are automatically mocked
- Sample data is provided for common chart types
- Use
waitForwith increased timeouts for React 19 asynchronous rendering
- Use the
Code Coverage:
npm run test -- --coverage- This will generate a coverage report in the
/coveragedirectory
- This will generate a coverage report in the
Color Management & State Integration
Self-Generated Colors
Both AreaChart and LineChart support automatic color generation:
// Automatic color generation
<AreaChart
keys={["Sales", "Marketing", "Development"]}
colors={["#1f77b4", "#ff7f0e", "#2ca02c"]} // Optional custom palette
onColorMappingGenerated={(colors) => {
// colors = { "Sales": "#1f77b4", "Marketing": "#ff7f0e", "Development": "#2ca02c" }
console.log("Generated colors:", colors);
}}
{...otherProps}
/>Redux Integration Pattern
Use onColorMappingGenerated to store colors in Redux for cross-chart synchronization:
// Redux slice
const chartSlice = createSlice({
name: 'charts',
initialState: { colorMappings: {}, highlightItems: [] },
reducers: {
setColorMapping: (state, action) => {
const { chartId, colors } = action.payload;
state.colorMappings[chartId] = colors;
},
setHighlightItems: (state, action) => {
state.highlightItems = action.payload;
}
}
});
// Component usage
const Dashboard = () => {
const dispatch = useDispatch();
const { colorMappings, highlightItems } = useSelector(state => state.charts);
return (
<div>
{/* Master chart generates colors */}
<AreaChart
onColorMappingGenerated={(colors) =>
dispatch(setColorMapping({ chartId: 'global', colors }))
}
onHighlightItem={(items) => dispatch(setHighlightItems(items))}
{...chart1Props}
/>
{/* Slave charts use generated colors */}
<AreaChart
colorsMapping={colorMappings.global || {}}
{...chart2Props}
/>
<LineChart
colorsMapping={colorMappings.global || {}}
{...chart3Props}
/>
</div>
);
};Color Management Options
- Self-Generated (Default): Charts automatically assign colors from palette
- Explicit Mapping: Provide specific colors via
colorsMappingprop - Context-Based: Use
MichiVzProviderfor shared colors across charts - Hybrid: Combine context + component-specific overrides
Interactive Features
- Highlighting: Use
onHighlightItemcallback andhighlightItemsprop/context - Disabled Items: Use
disabledItemsprop/context to hide categories - Cross-Chart Sync: Store state in Redux/context for synchronized interactions
See Storybook examples for detailed implementation patterns.
Console commands
Development
npm run storybook: start dev servernpm run test: run testsnpm run test:watch: run tests in watch mode
Building
npm run build: standard build with prettier outputnpm run build:files: build with file listing (shows all processed files)npm run build:debug: build with performance metrics and timing breakdownnpm run build:trace: build with module resolution tracingnpm run type-check: type checking only (no file output)
Linting
npm run lint: check for linting errorsnpm run lint:fix: automatically fix linting issues
Publishing
npm publish: publish the package to npm (update package.json version first)
