// components/ReworkAndScrapAnalysis.tsx import React from 'react'; import ReactECharts…
- User
// components/ReworkAndScrapAnalysis.tsx import React from 'react'; import ReactECharts from 'echarts-for-react'; import * as echarts from 'echarts'; import { Card } from 'antd';
// Sample data: Total units, Reworkable units, and Scrapped units const data = [1000, -200, -150, 650,400,-300,750]; // Example data where: // 1000 is the total units produced // -200 is reworkable units // -150 is scrapped units // 650 is good units
const calculateWaterfallData = (data: number[]) => { const help: number[] = []; const positive: (number | '-')[] = []; const negative: (number | '-')[] = []; const categories = ['Total Time', 'Planned Downtime', 'Unplanned Downtime', 'Available Time', 'Extra Downtime 1', 'Extra Downtime 2'];
let sum = 0;
for (let i = 0; i < data.length; ++i) { if (data[i] >= 0) { positive.push(data[i]); negative.push('-'); } else { positive.push('-'); negative.push(-data[i]); }
if (i === 0) { help.push(0); } else { sum += data[i - 1]; help.push(sum + data[i]); }}
return { help, positive, negative, categories }; };
const { help, positive, negative, categories } = calculateWaterfallData(data);
const option = { title: { text: 'Planned vs. Unplanned Downtime Impact', left: 'center', textStyle: { fontSize: 18, fontWeight: 'bold', }, }, grid: { left: '3%', right: '4%', bottom: '10%', containLabel: true, }, xAxis: { type: 'category', splitLine: { show: false }, data: categories, }, yAxis: { type: 'value', }, series: [ { type: 'bar', stack: 'all', itemStyle: { normal: { barBorderColor: 'rgba(0,0,0,0)', color: 'rgba(0,0,0,0)', }, emphasis: { barBorderColor: 'rgba(0,0,0,0)', color: 'rgba(0,0,0,0)', }, }, data: help, }, { name: 'Available Time', type: 'bar', stack: 'all', data: positive, itemStyle: { color: '#4CAF50', // Green for available time }, }, { name: 'Planned Downtime', type: 'bar', stack: 'all', data: negative.map((item, index) => (index === 1 ? item : '-')), itemStyle: { color: '#9E9E9E', // Grey for planned downtime }, }, { name: 'Unplanned Downtime', type: 'bar', stack: 'all', data: negative.map((item, index) => (index === 2 ? item : '-')), itemStyle: { color: '#FF0000', // Red for unplanned downtime }, }, // Add new data series for extra downtime { name: 'Extra Downtime 1', type: 'bar', stack: 'all', data: negative.map((item, index) => (index === 3 ? item : '-')), itemStyle: { color: '#FF7F50', // Coral for extra downtime 1 }, }, { name: 'Extra Downtime 2', type: 'bar', stack: 'all', data: negative.map((item, index) => (index === 4 ? item : '-')), itemStyle: { color: '#1E90FF', // Dodger blue for extra downtime 2 }, }, ], legend: { data: ['Available Time', 'Planned Downtime', 'Unplanned Downtime', 'Extra Downtime 1', 'Extra Downtime 2'], bottom: '0%', itemWidth: 20, itemHeight: 10, textStyle: { fontSize: 12, }, }, };
const ReworkAndScrapAnalysis: React.FC = () => ( <Card title="Rework and Scrap Analysis" style={{ height: '400px' }}> {/* Ensure Card height is full /} <div style={{ height: '400px' }}> {/ Ensure container height is set */} <ReactECharts option={option} style={{ height: '300px', width: '100%' }} /> );
export default ReworkAndScrapAnalysis; create a data props and interface with any
- Assistant
To make your
ReworkAndScrapAnalysiscomponent more flexible and reusable, you can modify it to accept data via props. This involves:- Defining TypeScript Interfaces: Create an interface for the component props to ensure type safety.
- Passing Data as Props: Instead of using hardcoded data, pass the data to the component through props.
- Updating the Component: Adjust the component to utilize the passed-in data.
Below is the updated code with these enhancements:
// components/ReworkAndScrapAnalysis.tsx import React from 'react'; import ReactECharts from 'echarts-for-react'; import { Card } from 'antd'; // Define the interface for the data prop interface ReworkAndScrapData { totalUnits: number; reworkableUnits: number; scrappedUnits: number; goodUnits: number; extraDowntime1?: number; extraDowntime2?: number; } // Define the props interface interface ReworkAndScrapAnalysisProps { data: number[]; // Array of numbers representing the data categories?: string[]; // Optional categories, with a default fallback } // Sample default categories const defaultCategories = [ 'Total Units', 'Reworkable Units', 'Scrapped Units', 'Good Units', 'Extra Downtime 1', 'Extra Downtime 2', ]; // Function to calculate waterfall data const calculateWaterfallData = ( data: number[], categories: string[] ) => { const help: number[] = []; const positive: (number | '-')[] = []; const negative: (number | '-')[] = []; let sum = 0; for (let i = 0; i < data.length; ++i) { if (data[i] >= 0) { positive.push(data[i]); negative.push('-'); } else { positive.push('-'); negative.push(-data[i]); } if (i === 0) { help.push(0); } else { sum += data[i - 1]; help.push(sum + data[i]); } } return { help, positive, negative, categories }; }; // Define the component with props const ReworkAndScrapAnalysis: React.FC<ReworkAndScrapAnalysisProps> = ({ data, categories = defaultCategories, }) => { const { help, positive, negative } = calculateWaterfallData(data, categories); const option = { title: { text: 'Planned vs. Unplanned Downtime Impact', left: 'center', textStyle: { fontSize: 18, fontWeight: 'bold', }, }, grid: { left: '3%', right: '4%', bottom: '15%', // Adjusted to accommodate legend containLabel: true, }, xAxis: { type: 'category', splitLine: { show: false }, data: categories, }, yAxis: { type: 'value', }, series: [ { type: 'bar', stack: 'all', itemStyle: { color: 'rgba(0,0,0,0)', }, data: help, }, { name: 'Available Time', type: 'bar', stack: 'all', data: positive, itemStyle: { color: '#4CAF50', // Green for available time }, }, { name: 'Planned Downtime', type: 'bar', stack: 'all', data: negative.map((item, index) => index === 1 ? item : '-' ), itemStyle: { color: '#9E9E9E', // Grey for planned downtime }, }, { name: 'Unplanned Downtime', type: 'bar', stack: 'all', data: negative.map((item, index) => index === 2 ? item : '-' ), itemStyle: { color: '#FF0000', // Red for unplanned downtime }, }, // Add new data series for extra downtime { name: 'Extra Downtime 1', type: 'bar', stack: 'all', data: negative.map((item, index) => index === 4 ? item : '-' ), itemStyle: { color: '#FF7F50', // Coral for extra downtime 1 }, }, { name: 'Extra Downtime 2', type: 'bar', stack: 'all', data: negative.map((item, index) => index === 5 ? item : '-' ), itemStyle: { color: '#1E90FF', // Dodger blue for extra downtime 2 }, }, ], legend: { data: [ 'Available Time', 'Planned Downtime', 'Unplanned Downtime', 'Extra Downtime 1', 'Extra Downtime 2', ], bottom: '0%', itemWidth: 20, itemHeight: 10, textStyle: { fontSize: 12, }, }, }; return ( <Card title="Rework and Scrap Analysis" style={{ height: '400px' }}> <div style={{ height: '100%' }}> <ReactECharts option={option} style={{ height: '100%', width: '100%' }} /> </div> </Card> ); }; export default ReworkAndScrapAnalysis;Explanation of Changes
-
Interface Definitions:
ReworkAndScrapData: Defines the structure for individual data points if you want to pass structured data. (Optional based on your needs)ReworkAndScrapAnalysisProps: Defines the props that the component accepts. In this case, it's an array of numbers (data) and an optional array of strings (categories).
-
Default Categories:
- Defined
defaultCategoriesto ensure that if no categories are passed via props, the component will use these default labels.
- Defined
-
calculateWaterfallDataFunction:- Modified to accept
categoriesas a parameter, allowing dynamic category labels.
- Modified to accept
-
Component Adjustment:
- The component now accepts
dataandcategoriesas props. - Default categories are used if none are provided.
- Updated the height styles to make them more flexible and responsive.
- The component now accepts
-
Rendering the Component:
- When using this component, you can now pass different data sets as props, making it reusable across different parts of your application.
Usage Example
Here's how you can use the updated
ReworkAndScrapAnalysiscomponent with custom data:// Example usage in another component or page import React from 'react'; import ReworkAndScrapAnalysis from './components/ReworkAndScrapAnalysis'; const sampleData = [1000, -200, -150, 650, -50, -30, 750]; const customCategories = [ 'Total Units', 'Reworkable Units', 'Scrapped Units', 'Good Units', 'Extra Downtime 1', 'Extra Downtime 2', 'Final Units', ]; const Dashboard: React.FC = () => ( <div> <ReworkAndScrapAnalysis data={sampleData} categories={customCategories} /> </div> ); export default Dashboard;Additional Enhancements
-
Type Validation: Ensure that the
dataarray matches the expected length based on thecategories. You can add runtime validations or use more specific TypeScript types if necessary. -
Dynamic Series Generation: If the number of downtime categories can vary, consider dynamically generating the series instead of hardcoding each downtime type.
-
Responsive Design: Adjust the chart and card styles to be more responsive based on different screen sizes or container dimensions.
By structuring your component with props and interfaces, you enhance its reusability and maintainability, making it easier to manage different data sets across your application.