1.0.2 â€Ē Published 5 months ago

@aniruddha1806/boxplot-chart v1.0.2

Weekly downloads
-
License
MIT
Repository
-
Last release
5 months ago

React Box Plot Chart

A powerful, customizable box plot chart component for React applications with TypeScript support. Perfect for visualizing statistical distributions, quartiles, outliers, and data spread across categories.

Installation

npm install @aniruddha1806/boxplot-chart

Features

  • 📊 Interactive box plot charts with statistical calculations
  • 📈 Automatic quartile, median, and outlier detection
  • ðŸŽĻ Customizable colors and styling for all chart elements
  • ðŸ“ą Responsive design with automatic scaling
  • ðŸ–ąïļ Interactive tooltips with detailed statistics
  • ðŸŽŊ Outlier visualization with toggle option
  • 📏 Customizable axis labels and titles
  • ðŸŽĻ CSS class support for advanced styling
  • 📝 TypeScript support with full type definitions
  • â™ŋ Accessibility features
  • ðŸŠķ Zero dependencies for chart rendering

Quick Start

Basic Box Plot

import BoxPlot from '@aniruddha1806/boxplot-chart';

function App() {
  const data = {
    'Group A': [12, 15, 18, 20, 22, 25, 28, 30, 35, 40],
    'Group B': [8, 12, 16, 18, 20, 24, 26, 28, 32, 38],
    'Group C': [10, 14, 17, 19, 21, 23, 27, 29, 33, 36]
  };

  return (
    <BoxPlot
      data={data}
      title="Sample Box Plot"
      xAxisLabel="Groups"
      yAxisLabel="Values"
      width="100%"
      height={400}
    />
  );
}

Props

Core Props

PropTypeDefaultDescription
dataDataset \| string{}Chart data as object or CSV string
widthnumber \| string"100%"Chart width
heightnumber \| string400Chart height
titlestring"Box Plot"Chart title
xAxisLabelstring""X-axis label
yAxisLabelstring""Y-axis label

Styling Props

PropTypeDefaultDescription
classNamestring""CSS class for container
boxClassNamestring""CSS class for box elements
medianClassNamestring""CSS class for median lines
whiskerClassNamestring""CSS class for whiskers
outlierClassNamestring""CSS class for outliers

Color Props

PropTypeDefaultDescription
colors.boxstring"#3b82f6"Box fill color
colors.medianstring"#1e40af"Median line color
colors.whiskerstring"#93c5fd"Whisker color
colors.outlierstring"#ef4444"Outlier point color

Feature Props

PropTypeDefaultDescription
showOutliersbooleantrueDisplay outlier points
showTooltipbooleantrueEnable interactive tooltips

Data Types

type DataPoint = number;
type Category = string;
type Dataset = Record<Category, DataPoint[]>;

interface BoxPlotStats {
  min: number;
  q1: number;
  median: number;
  q3: number;
  max: number;
  outliers: number[];
}

Examples

Basic Statistical Analysis

Simple box plot for comparing distributions:

import BoxPlot from '@aniruddha1806/boxplot-chart';

function StatisticalAnalysisExample() {
  const testScores = {
    'Math': [78, 82, 85, 88, 90, 92, 95, 98, 100, 85, 87, 89],
    'Science': [75, 80, 83, 86, 88, 91, 94, 96, 99, 82, 84, 87],
    'English': [80, 83, 86, 89, 91, 93, 96, 98, 100, 88, 90, 92],
    'History': [72, 76, 79, 82, 85, 88, 91, 94, 97, 80, 83, 86]
  };

  return (
    <div style={{ padding: '20px' }}>
      <h2>Student Test Scores by Subject</h2>
      <BoxPlot
        data={testScores}
        title="Test Score Distribution"
        xAxisLabel="Subjects"
        yAxisLabel="Score"
        width="100%"
        height={500}
        colors={{
          box: '#10b981',
          median: '#059669',
          whisker: '#6ee7b7',
          outlier: '#f59e0b'
        }}
      />
    </div>
  );
}

Custom Styled Box Plot

Apply custom styling with CSS classes:

import BoxPlot from '@aniruddha1806/boxplot-chart';
import './custom-boxplot.css'; // Your custom CSS

function CustomStyledExample() {
  const performanceData = {
    'Q1': [85, 88, 92, 95, 98, 90, 87, 93, 96, 89],
    'Q2': [88, 91, 95, 98, 101, 93, 90, 96, 99, 92],
    'Q3': [90, 93, 97, 100, 103, 95, 92, 98, 101, 94],
    'Q4': [92, 95, 99, 102, 105, 97, 94, 100, 103, 96]
  };

  return (
    <BoxPlot
      data={performanceData}
      title="Quarterly Performance Metrics"
      xAxisLabel="Quarters"
      yAxisLabel="Performance Score"
      className="custom-boxplot"
      boxClassName="custom-box"
      medianClassName="custom-median"
      whiskerClassName="custom-whisker"
      outlierClassName="custom-outlier"
      width="100%"
      height={450}
    />
  );
}

CSS file (custom-boxplot.css):

.custom-boxplot {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  border-radius: 12px;
  box-shadow: 0 8px 32px rgba(0,0,0,0.1);
}

.custom-boxplot .chart-title {
  color: white;
  font-size: 20px;
  font-weight: bold;
}

.custom-boxplot .axis-label {
  fill: white;
  font-weight: bold;
}

.custom-boxplot .tick text {
  fill: white;
}

.custom-box {
  stroke-width: 2;
  filter: drop-shadow(0 2px 4px rgba(0,0,0,0.2));
}

.custom-median {
  stroke-width: 3;
}

.custom-whisker {
  stroke-width: 2;
  stroke-dasharray: 5,5;
}

.custom-outlier {
  stroke: white;
  stroke-width: 2;
  filter: drop-shadow(0 1px 2px rgba(0,0,0,0.3));
}

.custom-outlier:hover {
  r: 6;
  transition: r 0.2s ease;
}

TypeScript Usage

The component provides full TypeScript support:

import BoxPlot, { BoxPlotProps, BoxPlotStats, Dataset } from '@aniruddha1806/boxplot-chart';
import { useState, useEffect } from 'react';

interface AnalyticsData {
  categories: string[];
  values: number[][];
}

interface ChartConfig {
  title: string;
  colors: {
    box: string;
    median: string;
    whisker: string;
    outlier: string;
  };
}

const AnalyticsChart: React.FC = () => {
  const [data, setData] = useState<Dataset>({});
  const [stats, setStats] = useState<Record<string, BoxPlotStats>>({});

  const calculateStats = (dataset: Dataset): Record<string, BoxPlotStats> => {
    const result: Record<string, BoxPlotStats> = {};
    
    Object.entries(dataset).forEach(([category, values]) => {
      const sorted = [...values].sort((a, b) => a - b);
      const q1 = sorted[Math.floor(sorted.length * 0.25)];
      const median = sorted[Math.floor(sorted.length * 0.5)];
      const q3 = sorted[Math.floor(sorted.length * 0.75)];
      const iqr = q3 - q1;
      
      const outliers = sorted.filter(v => 
        v < q1 - 1.5 * iqr || v > q3 + 1.5 * iqr
      );
      
      result[category] = {
        min: Math.min(...sorted),
        q1,
        median,
        q3,
        max: Math.max(...sorted),
        outliers
      };
    });
    
    return result;
  };

  useEffect(() => {
    // Fetch or generate data
    const sampleData: Dataset = {
      'Category A': [10, 15, 20, 25, 30],
      'Category B': [12, 18, 22, 28, 32],
      'Category C': [8, 14, 19, 24, 29]
    };
    
    setData(sampleData);
    setStats(calculateStats(sampleData));
  }, []);

  const chartConfig: ChartConfig = {
    title: 'Statistical Analysis',
    colors: {
      box: '#3b82f6',
      median: '#1e40af',
      whisker: '#93c5fd',
      outlier: '#ef4444'
    }
  };

  const boxPlotProps: BoxPlotProps = {
    data,
    title: chartConfig.title,
    colors: chartConfig.colors,
    width: '100%',
    height: 400,
    showTooltip: true,
    showOutliers: true,
    xAxisLabel: 'Categories',
    yAxisLabel: 'Values'
  };

  return (
    <div>
      <BoxPlot {...boxPlotProps} />
      <div>
        <h3>Statistics Summary:</h3>
        {Object.entries(stats).map(([category, stat]) => (
          <div key={category}>
            <h4>{category}</h4>
            <p>Median: {stat.median}, IQR: {stat.q3 - stat.q1}</p>
          </div>
        ))}
      </div>
    </div>
  );
};

Statistical Calculations

The component automatically calculates:

Quartiles

  • Q1 (25th percentile): First quartile
  • Q2 (50th percentile): Median
  • Q3 (75th percentile): Third quartile

Outlier Detection

  • Lower bound: Q1 - 1.5 × IQR
  • Upper bound: Q3 + 1.5 × IQR
  • Outliers: Values outside these bounds

Whiskers

  • Lower whisker: Minimum non-outlier value
  • Upper whisker: Maximum non-outlier value
1.0.2

5 months ago

1.0.1

6 months ago

1.0.0

6 months ago