Rendering FusionCharts in ASP.NET MVC Framework using Razor View

Razor is an ASP.NET programming syntax used to create dynamic web pages with the C# or Visual Basic .NET programming languages. For creating web applications, the ASP.NET MVC framework provides an alternative to the ASP.NET Web Forms pattern.

This tutorial showcases how you can render FusionCharts in ASP.NET MVC framework based application using Razor View. Let’s get started with the requirements and the steps for rendering FusionCharts.

System requirements

To start rendering charts in ASP.NET MVC framework using razor view, make sure that you have the following components downloaded in the local machine:

Rendering FusionCharts in ASP.NET MVC Framework

Rendering charts using FusionCharts in ASP.NET MVC Framework can be achieved easily with the help of FusionCharts ASP.NET wrapper. Follow the steps given below to render charts in ASP.NET MVC framework:

Step 1

Create a new project in ASP.NET application (click File->New->Project) and choose MVC from installed templates as shown in the image below:

A directory will be created with structure as shown below:

Step 2

Include the fusioncharts.js and fusioncharts.charts.js libraries within script folder as shown in the image below:

Step 3

Insert Index.cshtml to Home folder under Views, as shown in the image below:

Once done remove all the codes written in the file and include js files of FusionCharts in it. Include the following code in the <div> created:

Step 4

Add FusionCharts.dll (as a reference) into the project to use FusionCharts ASP.NET wrapper. To add FusionCharts.dll as a reference:

  • Right click on References->Select Add References. Refer to the image below:

Step 5

From Reference Manager dialogue box, search for FusionCharts.dll in your local machine and add it into this box. Refer to the image below:

Step 6

Insert HomeController.cs in controllers folder and include FusionCharts namespace stated below:

  • using FusionCharts.Charts;

Step 7

Create a method named createChart and define the return type as string. createChart will return the string fetched after calling Render function as shown below:

Step 8

In HomeController.cs, search for index method. In that define an attribute and initialize it with the return value of the defined createChart method.

Step 9

Now, you are ready to render FusionCharts in your MVC framework. Press ctrl + f5 to run and check the output of the chart as shown in image below:

If you find any difficulty in rendering the chart or you see any error in your code, click here to download the complete source code of the sample project we have created for this tutorial.

Was There a Problem Rendering Charts?

In case something went wrong and you are unable to see the chart, check for the following:

  • The chart ID should be unique for all charts rendered on the same page. Otherwise, it will result in a JavaScript error.
  • If the chart does not show up at all, check if the fusioncharts.js and fusioncharts wrapper FusionCharts.java was loaded. Also, check if the path to fusioncharts.js and FusionCharts.java file is correct, and whether the file exists in that location.
  • Marcus June 21, 2017, 10:27 am

    I need an example for using database. Well good article

Leave a Comment