How to Create Interactive Dashboards with Excel Charts in C#
Tags: webdev, devops, csharp, tutorial
Published: 2024-05-31 13:07:50

---
canonical_url: https://developer.mescius.com/blogs/how-to-create-interactive-dashboards-with-excel-charts-in-c-sharp
description: Learn how to create interactive dashboards with Excel charts in C#. See more from Document Solutions today.
---
**What You Will Need**
- Visual Studio
- DsExcel NuGet

**Controls Referenced**
- [Document Solutions for Excel](https://developer.mescius.com/document-solutions/dot-net-excel-api/docs/online/overview.html)

**Tutorial Concept**

C# Interactive Dashboards - Using a C# .NET Excel API and chart features, users can build interactive dashboards for their desktop applications.

---
Are you looking to make smarter decisions with your Excel data? Use charts to transform complex information into clear insights!

Identifying trends using raw data alone is very difficult. Without visual aids, it’s tough to spot patterns and key insights, which leads to slower and less accurate decision-making. Charts play a crucial role in creating dashboards or business reports by transforming complex data into clear visual insights. They help you easily identify trends, compare metrics, and see patterns in the data.

With our [Document Solutions for Excel](https://developer.mescius.com/document-solutions/dot-net-excel-api "https://developer.mescius.com/document-solutions/dot-net-excel-api") (DsExcel) API, you can effortlessly create various Excel charts and customize different parts to suit your needs. In this blog, we showcase how charts can be used with sales data to create a performance dashboard using the DsExcel API.  The Excel dashboard includes different charts to display various metrics such as sales by each representative, quantity sold of each product, sales distribution by product, and sales trends over time.

Let’s break down the creation of the dashboard into the following simple steps:

*   [Setup a Project with DsExcel Dependency](#Setup)
*   [Add Data to the Worksheet](#Add)
*   [Create Charts for Dashboard](#Create)

## <a id="Setup"></a>Setup a Project with DsExcel Dependency

Let's begin by setting up a new .NET 8 Console App that includes the DsExcel dependency by following these steps:

1.  Open Visual Studio and select **File **| **New **| **Project **to create a new **Console App**.
2.  Right-click on the project in Solution Explorer and choose **Manage NuGet Packages…** from the context menu.  

![Manage NuGet Packages](//cdn.mescius.io/umb/media/z5onabpq/manage-nuget-packages.png?rmode=max&width=703&height=362)  

3.Search for ** [**Ds.Documents.Excel**](https://www.nuget.org/packages/DS.Documents.Excel "https://www.nuget.org/packages/DS.Documents.Excel") in the NuGet Package Manager and click on Install**.  

![Install](//cdn.mescius.io/umb/media/xynhltqy/install.png?rmode=max&width=704&height=431)

Now that we've successfully set up the project, it is time to create a new [Workbook](https://developer.mescius.com/document-solutions/dot-net-excel-api/docs/online/ManageWorkbook.html "https://developer.mescius.com/document-solutions/dot-net-excel-api/docs/online/ManageWorkbook.html") object to develop our report. The DsExcel code to initialize the new Workbook is below:

```
    // Create a new Workbook Object
    Workbook workbook = new Workbook();
    //Access the first sheet
    IWorksheet worksheet = workbook.Worksheets[0];
```

Next, we will add the sales data to our worksheet from the JSON file.

## <a id="Add"></a>Add Data to the Worksheet

In this step, we will add the sales data to our worksheet for which we will create charts to analyze the data. We have the sales data in the JSON file, so let’s [deserialize](https://learn.microsoft.com/en-us/dotnet/standard/serialization/system-text-json/deserialization "https://learn.microsoft.com/en-us/dotnet/standard/serialization/system-text-json/deserialization") and convert it into an object array.

The DsExcel code to extract the data and assign it to the worksheet is as follows:

```
    public class SalesData
    {
      public string Date { get; set; }
      public string Region { get; set; }
      public string ProductCategory { get; set; }
      public string ProductName { get; set; }
      public int Sales { get; set; }
      public int QuantitySold { get; set; }
      public string CustomerSegment { get; set; }
      public string SalesRep { get; set; }
      public string SalesChannel { get; set; }
    }

    //Load JSON data into string
    string jsonString = File.ReadAllText("Data.json");
    JArray? jsonObject = JArray.Parse(jsonString);
    // Parse the data into a C# array
    SalesData[]? salesData = jsonObject?.ToObject<SalesData[]?>();
    //Create Normal Object array to assign it to Workbook
    object[,] objectSalesDataArray = new object[jsonObject!.Count + 1, typeof(SalesData).GetProperties().Length];
    int colIndex = 0;
    int rowIndex = 0;
    foreach (var property in typeof(SalesData).GetProperties())
    {
    objectSalesDataArray[rowIndex, colIndex] = property.Name;
    colIndex++;
    }
    rowIndex++;
    //Iterate sales data array to get the values
    foreach (var data in salesData!)
    {
    colIndex = 0;
    foreach (var property in typeof(SalesData).GetProperties())
    {
    objectSalesDataArray[rowIndex, colIndex] = property.GetValue(data)!;
    colIndex++;
    }
    rowIndex++;
    }
    //Assign object array to range
    worksheet.Range["A1:I11"].Value = objectSalesDataArray;
```

After assigning the data and applying some formatting, the worksheet looks like this:

![Data](//cdn.mescius.io/umb/media/boul1s2s/data.png?rmode=max&width=777&height=214)

## <a id="Create"></a>Create Charts for Dashboard

After setting up the workbook with data, it is time to create charts to show and analyze the different metrics we initially discussed. Let’s follow the steps below to add a bar chart to the sheet showing the sales made by each sales representative. Then, we will perform various modifications to the chart.

### Create a Chart

**1:** To create the chart, use the [AddChart](https://developer.mescius.com/document-solutions/dot-net-excel-api/docs/online/DS.Documents.Excel~GrapeCity.Documents.Excel.Drawing.IShapes~AddChart(ChartType,IRange).html "https://developer.mescius.com/document-solutions/dot-net-excel-api/docs/online/DS.Documents.Excel~GrapeCity.Documents.Excel.Drawing.IShapes~AddChart(ChartType,IRange).html") method of the [Shapes](https://developer.mescius.com/document-solutions/dot-net-excel-api/docs/online/DS.Documents.Excel~GrapeCity.Documents.Excel.Drawing.IShapes.html "https://developer.mescius.com/document-solutions/dot-net-excel-api/docs/online/DS.Documents.Excel~GrapeCity.Documents.Excel.Drawing.IShapes.html") collection of the sheet and pass the ChartType and target range to plot the chart using the code below:

```
    //Add BarClustered Chart to create sales by representative chart
    GrapeCity.Documents.Excel.Drawing.IShape representativeSalesChart = worksheet.Shapes.AddChart(ChartType.BarClustered, worksheet.Range["K2"]);

```
**2:** To add the data source for the chart, we will use the [SetSourceData](https://developer.mescius.com/document-solutions/dot-net-excel-api/docs/online/DS.Documents.Excel~GrapeCity.Documents.Excel.Drawing.IChart~SetSourceData(IRange,RowCol).html "https://developer.mescius.com/document-solutions/dot-net-excel-api/docs/online/DS.Documents.Excel~GrapeCity.Documents.Excel.Drawing.IChart~SetSourceData(IRange,RowCol).html") method of the [Chart](https://developer.mescius.com/document-solutions/dot-net-excel-api/docs/online/DS.Documents.Excel~GrapeCity.Documents.Excel.Drawing.IChart.html "https://developer.mescius.com/document-solutions/dot-net-excel-api/docs/online/DS.Documents.Excel~GrapeCity.Documents.Excel.Drawing.IChart.html") class that we can access via the Chart property of our [IShape](https://developer.mescius.com/document-solutions/dot-net-excel-api/docs/online/DS.Documents.Excel~GrapeCity.Documents.Excel.Drawing.IShape_members.html "https://developer.mescius.com/document-solutions/dot-net-excel-api/docs/online/DS.Documents.Excel~GrapeCity.Documents.Excel.Drawing.IShape_members.html") object. The DsExcel code implementing this is as follows:

```
    //Set Data Source for chart
    representativeSalesChart.Chart.SetSourceData(worksheet.Range["E1: E11"], RowCol.Columns);
```

**3:** To set the sales representative’s name in the category axis with their associated values, let’s set the [CategoryNames](https://developer.mescius.com/document-solutions/dot-net-excel-api/docs/online/DS.Documents.Excel~GrapeCity.Documents.Excel.Drawing.IAxis~CategoryNames.html "https://developer.mescius.com/document-solutions/dot-net-excel-api/docs/online/DS.Documents.Excel~GrapeCity.Documents.Excel.Drawing.IAxis~CategoryNames.html") property of the Category axes. This property takes the string array, so we fetch this data from the _SalesRepresentative_ column using the following code:

    
```
representativeSalesChart.Chart.Axes.Item(AxisType.Category).CategoryNames = Enumerable.Range(1, 10).Select(i => worksheet.Range["H1: H11"][i, 0].Value.ToString()).ToArray();
```

**4:** To set the chart’s size and location, use the Height, Width, Top, and Left properties of the IShape object as below:

```
    //Set Chart Size and Position
    representativeSalesChart.Height = 230;
    representativeSalesChart.Width = 500;
    representativeSalesChart.Left = 450;
    representativeSalesChart.Top = 10;
```
### Format a Chart

**1**: To set and format the chart title, use the [ChartTitle](https://developer.mescius.com/document-solutions/dot-net-excel-api/docs/online/DS.Documents.Excel~GrapeCity.Documents.Excel.Drawing.IChart~ChartTitle.html "https://developer.mescius.com/document-solutions/dot-net-excel-api/docs/online/DS.Documents.Excel~GrapeCity.Documents.Excel.Drawing.IChart~ChartTitle.html") class and customize its font and color using the following code:

```
    //Set and Format Chart Title
    representativeSalesChart.Chart.ChartTitle.Text = "Sales by Representative";
    representativeSalesChart.Chart.ChartTitle.Font.Bold = true;
    representativeSalesChart.Chart.ChartTitle.Font.Size = 24;
    representativeSalesChart.Chart.ChartTitle.TextFrame.TextRange.Paragraphs[0].Font.Color.RGB = Color.FromArgb(141, 180, 226);
```

**2:** To customize the chart’s border, use the [ChartArea](https://developer.mescius.com/document-solutions/dot-net-excel-api/docs/online/DS.Documents.Excel~GrapeCity.Documents.Excel.Drawing.IChart~ChartArea.html "https://developer.mescius.com/document-solutions/dot-net-excel-api/docs/online/DS.Documents.Excel~GrapeCity.Documents.Excel.Drawing.IChart~ChartArea.html") class to set the border color and weight, as well as round its corners.

```
    //Format Chart Area Border
    representativeSalesChart.Chart.ChartArea.RoundedCorners = true;
    representativeSalesChart.Chart.ChartArea.Format.Line.Color.RGB = Color.Black;
    representativeSalesChart.Chart.ChartArea.Format.Line.Weight = 2;

```
**3:** To format any data point, you can access it from the Points collection using the Points property of your series. The DsExcel code to format the third data point is as follows:

```
    //Customize Particular Data Point in the Chart
    representativeSalesChart.Chart.SeriesCollection[0].Points[2].Format.Fill.Color.RGB = Color.Green;
    representativeSalesChart.Chart.SeriesCollection[0].Points[2].Format.Line.Weight = 1.5;
```

After performing the steps above, the chart will appear as below:

![Chart](//cdn.mescius.io/umb/media/mr4lyrvf/onechart.png?rmode=max&width=787&height=147)

In the same way, you can add the chart for other mentioned metrics. Check out the [attached sample](https://cdn.mescius.io/umb/media/i2fnvcge/dsexcelchartdemo.zip) to see how they’re implemented!

The final dashboard will appear as shown below after adding all the charts:

![Dashboard](//cdn.mescius.io/umb/media/204nwv0f/dashboard.png?rmode=max&width=785&height=259)

## Conclusion

In this blog post, we demonstrated how to create a sales performance dashboard using [DsExcel charts](https://developer.mescius.com/document-solutions/dot-net-excel-api/docs/online/UseChart.html "https://developer.mescius.com/document-solutions/dot-net-excel-api/docs/online/UseChart.html"). You can also leverage additional features of Excel like Pivot, Slicer, and Conditional Formatting to create more advanced reports using the DsExcel API.

For more details, please refer to the documentation and demos linked below:

*   [Product Demo .NET](https://developer.mescius.com/document-solutions/dot-net-excel-api/demos "https://developer.mescius.com/document-solutions/dot-net-excel-api/demos")
*   [Documentation .NET](https://developer.mescius.com/document-solutions/dot-net-excel-api/docs/online/overview.html "https://developer.mescius.com/document-solutions/dot-net-excel-api/docs/online/overview.html")
*   [Product Demo JAVA](https://developer.mescius.com/document-solutions/java-excel-api/demos "https://developer.mescius.com/document-solutions/java-excel-api/demos")
*   [Documentation JAVA](https://developer.mescius.com/document-solutions/java-excel-api/docs/online/overview.html "https://developer.mescius.com/document-solutions/java-excel-api/docs/online/overview.html")