MDLSoft.TinyPieChart
1.0.4
dotnet add package MDLSoft.TinyPieChart --version 1.0.4
NuGet\Install-Package MDLSoft.TinyPieChart -Version 1.0.4
<PackageReference Include="MDLSoft.TinyPieChart" Version="1.0.4" />
<PackageVersion Include="MDLSoft.TinyPieChart" Version="1.0.4" />
<PackageReference Include="MDLSoft.TinyPieChart" />
paket add MDLSoft.TinyPieChart --version 1.0.4
#r "nuget: MDLSoft.TinyPieChart, 1.0.4"
#:package MDLSoft.TinyPieChart@1.0.4
#addin nuget:?package=MDLSoft.TinyPieChart&version=1.0.4
#tool nuget:?package=MDLSoft.TinyPieChart&version=1.0.4
MDLSoft.TinyPieChart
A lightweight, dependency-free C# library for creating pie charts and exporting them to JPG/PNG format.
Features
- No external dependencies - Uses only System.Drawing.Common
- .NET Standard 2.0 support - Compatible with .NET Framework 4.8+ and .NET Core
- Simple API - Easy to configure and customize
- Custom colors - Set specific colors for each slice or use auto-generated defaults
- Legend support - Display slice labels and percentages
- Configurable slice labels - Show labels directly on pie slices
- Summary box - Display total, item count, and average values with configurable position
- Percentages display - Show percentage values directly on pie slices
- Multiple export formats - Export to JPG or PNG
- Cross-platform - Works on Windows, Linux, and macOS
Installation
- Add the MDLSoft.TinyPieChart project to your solution
- Reference it in your project file or via
dotnet add reference
Quick Start
using MDLSoft.TinyPieChart;
using System.Drawing;
// Create a pie chart
var chart = new PieChart { Title = "Sales by Product" };
chart.AddSlice("Product A", 350);
chart.AddSlice("Product B", 280);
chart.ExportToJpg("chart.jpg");
Advanced Usage
Custom Colors
var chart = new PieChart { Title = "Market Share" };
// Add slices with specific colors
chart.AddSlice("Company A", 1500, Color.FromArgb(255, 99, 132));
chart.AddSlice("Company B", 1200, Color.FromArgb(54, 162, 235));
chart.AddSlice("Company C", 800, Color.FromArgb(75, 192, 75));
chart.AddSlice("Others", 500, Color.FromArgb(255, 206, 86));
chart.ExportToJpg("market.jpg");
Advanced Usage
Custom Colors
var chart = new PieChart { Title = "Market Share" };
// Add slices with specific colors
chart.AddSlice("Company A", 1500, Color.FromArgb(255, 99, 132));
chart.AddSlice("Company B", 1200, Color.FromArgb(54, 162, 235));
chart.AddSlice("Company C", 800, Color.FromArgb(75, 192, 75));
chart.AddSlice("Others", 500, Color.FromArgb(255, 206, 86));
chart.ExportToJpg("market.jpg");
Display Labels on Slices
var chart = new PieChart
{
Title = "Expense Categories",
ShowLabels = true, // Display slice labels directly on pie
ShowPercentages = true, // Also show percentages
ShowLegend = false // Hide legend since labels are on slices
};
chart.AddSlice("Housing", 1200, Color.FromArgb(255, 107, 107));
chart.AddSlice("Food", 400, Color.FromArgb(74, 144, 226));
chart.AddSlice("Transport", 300, Color.FromArgb(56, 142, 60));
chart.ExportToJpg("expenses.jpg");
Add a Summary Box with Legend and Percentages
var chart = new PieChart
{
Title = "Website Traffic",
ShowSummaryBox = true,
SummaryBoxPosition = SummaryBoxPosition.TopRight // Corner position
};
chart.AddSlice("Organic Search", 3500);
chart.AddSlice("Direct", 2100);
chart.AddSlice("Referral", 1800);
chart.AddSlice("Social Media", 1200);
chart.ExportToJpg("traffic.jpg");
The summary box displays:
- Color box - Colored square matching the slice
- Label - Name of the slice
- Percentage - Percentage of total value
Full-Width Summary Box (Top Center)
var chart = new PieChart
{
Title = "Department Budget",
ShowLabels = true,
ShowLegend = false,
ShowSummaryBox = true,
SummaryBoxPosition = SummaryBoxPosition.TopCenter // Spans full width
};
chart.AddSlice("Engineering", 5000);
chart.AddSlice("Sales", 3500);
chart.AddSlice("Marketing", 2500);
chart.AddSlice("Operations", 2000);
chart.ExportToJpg("budget.jpg");
Full-Width Summary Box (Bottom Center)
var chart = new PieChart
{
Title = "Product Sales",
ShowLabels = false,
ShowLegend = false,
ShowSummaryBox = true,
SummaryBoxPosition = SummaryBoxPosition.BottomCenter // Spans full width at bottom
};
chart.AddSlice("Product A", 4200);
chart.AddSlice("Product B", 3800);
chart.AddSlice("Product C", 2900);
chart.AddSlice("Product D", 2100);
chart.AddSlice("Product E", 1800);
chart.ExportToJpg("sales.jpg");
API Reference
PieChart Class
Properties
Title(string) - The title of the pie chart (default: "Pie Chart")Width(int) - Width in pixels (default: 800, minimum: 300)Height(int) - Height in pixels (default: 600, minimum: 300)ShowLegend(bool) - Display legend with labels and percentages (default: true)ShowPercentages(bool) - Display percentage values on slices (default: true)ShowLabels(bool) - Display slice labels directly on pie slices (default: false)ShowSummaryBox(bool) - Display summary box with totals and averages (default: false)SummaryBoxPosition(SummaryBoxPosition) - Position of the summary box (default: TopRight)SliceCount(int) - Read-only property returning the number of slices
Methods
AddSlice(string label, float value, Color? color = null)- Add a single sliceAddSlices(params PieSlice[] slices)- Add multiple slices at onceClear()- Remove all slicesExportToJpg(string filePath)- Export chart to JPG fileExportToPng(string filePath)- Export chart to PNG file
SummaryBoxPosition Enum
TopLeft- Summary box in top-left cornerTopRight- Summary box in top-right corner (default)BottomLeft- Summary box in bottom-left cornerBottomRight- Summary box in bottom-right cornerTopCenter- Full-width summary box at topBottomCenter- Full-width summary box at bottom
PieSlice Class
Constructor
PieSlice(string label, float value, Color? color = null)
label- The label/name for the slicevalue- The numeric value (must be positive)color- Optional color; if not provided, a default color will be assigned
Properties
Label(string) - The label for this sliceValue(float) - The numeric value of the sliceColor(Color?) - The color of the slice (nullable)
Color Palette
If colors are not explicitly provided, the library uses this default palette (cycles for more than 10 slices):
- Red (#FF6384)
- Blue (#36A2EB)
- Green (#4BC04B)
- Yellow (#FFCE56)
- Purple (#9966FF)
- Orange (#FF9F40)
- Grey (#C7C7C7)
- Indigo (#5366FF)
- Magenta (#FF63FF)
- Cyan (#00C8C8)
Configuration Options
Display Options
| Option | Default | Purpose |
|---|---|---|
| ShowLegend | true | Show legend with labels and percentages |
| ShowPercentages | true | Show percentage on each slice |
| ShowLabels | false | Show slice labels directly on pie |
| ShowSummaryBox | false | Show summary statistics box |
Summary Box Positions
- TopLeft - Upper-left corner with vertical legend items
- TopRight - Upper-right corner with vertical legend items (default)
- BottomLeft - Lower-left corner with vertical legend items
- BottomRight - Lower-right corner with vertical legend items
- TopCenter - Top spanning full width with horizontal legend items
- BottomCenter - Bottom spanning full width with horizontal legend items
The summary box displays each slice with:
- Color box matching the slice color
- Slice label text
- Percentage of total value
Requirements
- .NET Standard 2.0 or higher
- .NET Framework 4.8+ or .NET Core 2.0+
- System.Drawing.Common (included as dependency)
Error Handling
The library validates input:
- PieSlice value must be positive - Throws
ArgumentExceptionif value ⇐ 0 - Cannot export empty chart - Throws
InvalidOperationExceptionif no slices are added - Minimum chart dimensions - Width and Height are automatically adjusted to minimum 300px
Examples
The library includes 12 sample demonstrations:
- Simple pie chart with default colors
- Custom colors
- Using PieSlice objects
- Budget allocation
- PNG export format
- Labels displayed on slices
- Summary box (top-right corner)
- Summary box with labels (bottom-left corner)
- Summary box at top center (full width)
- Summary box at bottom center (full width)
- Corner summary box with legend items
- Full-width summary at bottom with many items
Run the sample with:
cd MDLSoft.TinyPieChart.Sample
dotnet run
All examples generate JPG/PNG files demonstrating:
- Various positioning options (corners and full-width)
- Legend display with color boxes and percentages
- Different combinations of labels, percentages, and legend
- Chart dimensions and positioning
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
License
This project is licensed under the MIT License - see the LICENSE file for details.
Support
If you encounter any issues or have questions, please open an issue on GitHub.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
| .NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
| .NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
| .NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
| MonoAndroid | monoandroid was computed. |
| MonoMac | monomac was computed. |
| MonoTouch | monotouch was computed. |
| Tizen | tizen40 was computed. tizen60 was computed. |
| Xamarin.iOS | xamarinios was computed. |
| Xamarin.Mac | xamarinmac was computed. |
| Xamarin.TVOS | xamarintvos was computed. |
| Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.0
- System.Drawing.Common (>= 10.0.9)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.