Skip to content

a simple charting library for rendering charts as images.

License

Notifications You must be signed in to change notification settings

phamtung1/SimpleImageCharts

Repository files navigation

SimpleImageCharts

This is a simple charting library for rendering charts as images using GDI+.

Install

.NET Standard 2.0

Usage

After creating a chart instance, you just need to call:

var image = chart.CreateImage();

Available Charts

  1. Pie Chart
  2. Donut Chart
  3. Bar Chart
  4. Stacked Bar Chart
  5. Double Axis Bar Chart
  6. 100% Stacked Bar Chart
  7. Column Chart
  8. 100% Stacked Column Chart
  9. Radar Chart
  10. Single Range Bar Chart
  11. Semi Circle Gauge Chart

1. Pie Chart

Create sample data:

private static PieEntry[] CreatePieEntries()
{
    var rand = new Random();
    var entries = new PieEntry[10];
    for (int i = 0; i < entries.Length; i++)
    {
        entries[i] = new PieEntry
        {
            Value = (float)rand.Next(10, 40) / 10,
            Color = Color.FromArgb(rand.Next(0, 200), rand.Next(0, 200), rand.Next(0, 200)),
            Label = "Data " + i
        };
    }

    return entries;
}

Create chart:

PieEntry[] entries = CreatePieEntries();

var chart = new PieChart
{
    Width = 300,
    Height = 600,
    Entries = entries
};

2. Donut Chart

PieEntry[] entries = CreatePieEntries();

var chart = new PieChart
{
    Width = 300,
    Height = 600,
    Entries = entries,
    IsDonut = true // default: false
};

3. Bar Chart

var chart = new BarChart
{
    Legend = new LegendModel
    {
        Margin = new PointF(0, 40),
        VerticalAlign = VerticalAlign.Bottom,
        HorizontalAlign = HorizontalAlign.Center
    },
    ChartGridModel = new ChartGridModel
    {
        LineColor = Color.LightGreen
    },
    SubTitle = new SubTitleModel { Text = "AAAAAAA" },
    Size = size,
    Categories = new[] { "Product A", "Product B", "Product C" },
    DataSet = new[]
    {
        new DataSeries
        {
            Label = "LightBlue",
            Color = Color.LightBlue,
            Data = new[] { -5f, 10f, 15f },
        },
        new DataSeries
        {
            Label = "LightCoral",
            Color = Color.LightCoral,
            Data = new[] { 1f, -2f, 3f },
        }
        ,
        new DataSeries
        {
            Label = "LightGreen",
            Color = Color.LightGreen,
            Data = new[] { 5f, 20f, -13f },
        }
    }
};

4. Stacked Bar Chart

var chart = new BarChart
{
    Legend = new LegendModel
    {
        Margin = new PointF(0, 50),
        VerticalAlign = VerticalAlign.Bottom
    },
    ChartGridModel = new ChartGridModel
    {
        LineColor = Color.LightGreen
    },
    BarSetting = new BarSettingModel
    {
        IsStacked = true,
        FormatValue = "{0:0;0}",
    },
    SubTitle = new SubTitleModel { Text = "Some random text" },
    Size = size,
    FormatAxisValue = "{0:0;0}", // force positive values
    Categories = new[] { "Product A", "Product B", "Product C", "Product A", "Product B", "Product C", "Product A", "Product B", "Product C" },
    DataSet = new[]
    {
        new DataSeries
        {
            Label = "Yesterday",
            Color = Color.LightBlue,
            Data = new[] { -5f, -10f, -1f , -5f, -10f, -1f , -5f, -10f, -1f },
        },
        new DataSeries
        {
            Label = "Today",
            Color = Color.LightCoral,
            Data = new[] { 10f, 20f, 5f, 10f, 20f, 5f, 10f, 20f, 5f },
        }
    }
};

5. Double Axis Bar Chart

var chart = new DoubleAxisBarChart
{
    FormatBarValue = "{0}%",
    Size = size,
    Categories = new[] { "Product A", "Product B", "Product C", "Product D", "Product E", "Product F" },
    FirstDataSet = new DoubleAxisBarSeries
    {
        Label = "Income",
        Color = Color.LightBlue,
        Data = new[] { 5f, 10f, 5f, 1f, 12f, 7f },
    },
    SecondDataSet = new DoubleAxisBarSeries
    {
        Label = "Outcome",
        Color = Color.LightCoral,
        Data = new[] { 15f, 10f, 15f, 8f, 2f, 14f },
    }
};

6. 100% Stacked Bar Chart

var chart = new StackedBar100Chart
{
    Legend = new LegendModel
    {
        Margin = new PointF(0, 40),
        VerticalAlign = VerticalAlign.Bottom,
        HorizontalAlign = HorizontalAlign.Center
    },
    SubTitle = new SubTitleModel { Text = "AAAAAAA" },
    Size = size,
    Categories = new[] { "Product A", "Product B", "Product C" },
    DataSet = new[]
    {
        new DataSeries
        {
            Label = "LightBlue",
            Color = Color.LightBlue,
            Data = new[] { 25f, 13f, 3f },
        },
        new DataSeries
        {
            Label = "LightCoral",
            Color = Color.LightCoral,
            Data = new[] { 25f, 3f, 33f },
        }
        ,
        new DataSeries
        {
            Label = "LightGreen",
            Color = Color.LightGreen,
            Data = new[] { 5f, 3f, 3f },
        },
        new DataSeries
        {
            Label = "Red",
            Color = Color.Red,
            Data = new[] { 25f, 13f, 3f },
        }
    }
};

7. Column Chart

Single Dataset:

var categories = new[] { "A Long Product Name", "Product B", "Product C", "Another Long Name D", "Product E" };
var rand = new Random();
var datasets = new ColumnSeries[1];
var colors = new Color[categories.Length];
for (int i = 0; i < colors.Length; i++)
{
    colors[i] = Color.FromArgb(rand.Next(0, 256), rand.Next(0, 256), rand.Next(0, 256));
}

for (int i = 0; i < datasets.Length; i++)
{
    var data = new float[categories.Length];
    for (int j = 0; j < categories.Length; j++)
    {
        data[j] = 100 - (j * 10);
    }

    datasets[i] = new ColumnSeries
    {
        Colors = colors,
        Data = data
    };
}

var chart = new ColumnChart
{
    Padding = new Padding(60, 50, 30, 150),
    YAxisMinText = "Min",
    YAxisMaxText = "Max",
    ColumnValuesVisible = false,
    IsOneHundredPercentChart = true,
    ColumnSize = 50,
    Size = size,
    Categories = categories,
    DataSets = datasets,
};

Multiple Datasets:

var categories = new[] { "Product A", "Product B", "Product C", "Product D", "Product E" };
var rand = new Random();
var datasets = new ColumnSeries[3];
for (int i = 0; i < datasets.Length; i++)
{
    var data = new float[categories.Length];
    for (int j = 0; j < categories.Length; j++)
    {
        data[j] = rand.Next(30) - 10;
    }

    var dataset = new ColumnSeries
    {
        Color = Color.FromArgb(rand.Next(0, 256), rand.Next(0, 256), rand.Next(0, 256)),
        Data = data
    };
    datasets[i] = dataset;
}

datasets[0].OffsetX = 10;
var chart = new ColumnChart
{
    ColumnSize = 30,
    Size = size,
    Categories = categories,
    DataSets = datasets
};

8. 100% Stacked Column Chart

var chart = new StackedColumn100Chart
{
    Legend = new LegendModel
    {
        Margin = new PointF(0, 40),
        VerticalAlign = VerticalAlign.Bottom,
        HorizontalAlign = HorizontalAlign.Center
    },
    Padding = new Padding(80, 20, 20, 120),
    BarSetting = new BarSettingModel
    {
        Size = 70,
    },
    SubTitle = new SubTitleModel { Text = "AAAAAAA" },
    Size = size,
    Categories = new[] { "Product A", "Product B", "Product C" },
    DataSet = new[]
    {
        new DataSeries
        {
            Label = "LightBlue",
            Color = Color.LightBlue,
            Data = new[] { 25f, 3f, 3f },
        },
        new DataSeries
        {
            Label = "LightCoral",
            Color = Color.LightCoral,
            Data = new[] { 25f, 3f, 2f },
        }
        ,
        new DataSeries
        {
            Label = "LightGreen",
            Color = Color.LightGreen,
            Data = new[] { 50f, 3f, 5f },
        }
        ,
        new DataSeries
        {
            Label = "Red",
            Color = Color.Red,
            Data = new[] { 50f, 3f, 5f },
        }
    }
};

9. Radar Chart

var categories = new[] { "Eating", "Sleeping", "Doing Nothing", "Playing", "Relaxing", "Watching" };
var chart = new RadarChart
{
    //   MaxDataValue = 100,
    StepSize = 10,
    Size = size,
    Categories = categories,
    DataSets = new[]
    {
        new RadarChartSeries
        {
            Label = "My Life",
            Color = Color.LightCoral,
            Data = GenerateRandomArray(random, categories.Length, 1, 50),
        },
        new RadarChartSeries
        {
            Label = "My Wife Life",
            Color = Color.LightBlue,
            Data = GenerateRandomArray(random, categories.Length, 1, 100),
        }
    }
};

10. Single Range Bar Chart

const float MinValue = 10;
const float MaxValue = 15;
var values = new[] { 11, 12, 15 };
var rand = new Random();
var entries = new SingleRangeBarEntry[values.Length];

for (int i = 0; i < entries.Length; i++)
{
    entries[i] = new SingleRangeBarEntry
    {
        Value = values[i],
        Color = Color.FromArgb(rand.Next(0, 200), rand.Next(0, 200), rand.Next(0, 200)),
        Label = "Data " + i
    };
}

var chart = new SingleRangeBarChart
{
    MinValue = MinValue,
    MaxValue = MaxValue,
    Size = size,
    Entries = entries,
    LeftLabel = "Min \nvalue = 10",
    CenterLabel = "Center = ?",
    RightLabel = "Max \nvalue = 15",
    Font = new SlimFont("Arial", 12),
    TextColor = Color.Black
};

11. Semi Circle Gauge Chart

var chart = new SemiCircleGaugeChart
{
    Legend = new LegendModel
    {
        Margin = new PointF(40, 80),
        VerticalAlign = VerticalAlign.Bottom,
        HorizontalAlign = HorizontalAlign.Center
    },
    MaxValue = 8,
    Size = size,
    DataItems = new[]
    {
        new DataItem { Color = Color.Brown, Value = 0.5f, Label = "Brown" },
        new DataItem { Color = Color.LightCoral, Value = 2, Label = "LightCoral" },
        new DataItem { Color = Color.LightGreen, Value = 4.5f, Label = "LightGreen" },
        new DataItem { Color = Color.LightBlue, Value = 5, Label = "LightBlue" },
        new DataItem { Color = Color.Yellow, Value = 6, Label = "Yellow" },
        new DataItem { Color = Color.LightPink, Value = 7, Label = "LightPink" },
        new DataItem { Color = Color.Aqua, Value = 8, Label = "Aqua" },
    }
};

License

MIT

About

a simple charting library for rendering charts as images.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages