Timeline With vis.js Using ASP.NET MVC 5

Introduction

In this post, I will show you, how to create a timeline, based on vis.js plugin, using ASP.NET MVC5 and Entity Framework.

Prerequisites

As I said before, we are going to use timeline plugin in our MVC Application. For this, you must have Visual Studio 2015 (.NET Framework 4.5.2) and SQL Server.

SQL database part

Here, you find the scripts to create the database and table.

Create database

  1. USE [master]  
  2. GO  
  3.   
  4. /****** Object: Database [TimeLineDB] Script Date: 9/16/2016 10:02:47 AM ******/  
  5. CREATE DATABASE [TimeLineDB]  
  6. CONTAINMENT = NONE  
  7. ON PRIMARY   
  8. NAME = N'TimeLineDB', FILENAME = N'c:\Program Files (x86)\Microsoft SQL Server\MSSQL11.MSSQLSERVER\MSSQL\DATA\TimeLineDB.mdf' , SIZE = 3072KB , MAXSIZE = UNLIMITED, FILEGROWTH = 1024KB )  
  9. LOG ON   
  10. NAME = N'TimeLineDB_log', FILENAME = N'c:\Program Files (x86)\Microsoft SQL Server\MSSQL11.MSSQLSERVER\MSSQL\DATA\TimeLineDB_log.ldf' , SIZE = 1024KB , MAXSIZE = 2048GB , FILEGROWTH = 10%)  
  11. GO  
  12.   
  13. ALTER DATABASE [TimeLineDB] SET COMPATIBILITY_LEVEL = 110  
  14. GO  
  15.   
  16. IF (1 = FULLTEXTSERVICEPROPERTY('IsFullTextInstalled'))  
  17. begin  
  18. EXEC [TimeLineDB].[dbo].[sp_fulltext_database] @action = 'enable'  
  19. end  
  20. GO  
  21.   
  22. ALTER DATABASE [TimeLineDB] SET ANSI_NULL_DEFAULT OFF   
  23. GO  
  24.   
  25. ALTER DATABASE [TimeLineDB] SET ANSI_NULLS OFF   
  26. GO  
  27.   
  28. ALTER DATABASE [TimeLineDB] SET ANSI_PADDING OFF   
  29. GO  
  30.   
  31. ALTER DATABASE [TimeLineDB] SET ANSI_WARNINGS OFF   
  32. GO  
  33.   
  34. ALTER DATABASE [TimeLineDB] SET ARITHABORT OFF   
  35. GO  
  36.   
  37. ALTER DATABASE [TimeLineDB] SET AUTO_CLOSE OFF   
  38. GO  
  39.   
  40. ALTER DATABASE [TimeLineDB] SET AUTO_CREATE_STATISTICS ON   
  41. GO  
  42.   
  43. ALTER DATABASE [TimeLineDB] SET AUTO_SHRINK OFF   
  44. GO  
  45.   
  46. ALTER DATABASE [TimeLineDB] SET AUTO_UPDATE_STATISTICS ON   
  47. GO  
  48.   
  49. ALTER DATABASE [TimeLineDB] SET CURSOR_CLOSE_ON_COMMIT OFF   
  50. GO  
  51.   
  52. ALTER DATABASE [TimeLineDB] SET CURSOR_DEFAULT GLOBAL   
  53. GO  
  54.   
  55. ALTER DATABASE [TimeLineDB] SET CONCAT_NULL_YIELDS_NULL OFF   
  56. GO  
  57.   
  58. ALTER DATABASE [TimeLineDB] SET NUMERIC_ROUNDABORT OFF   
  59. GO  
  60.   
  61. ALTER DATABASE [TimeLineDB] SET QUOTED_IDENTIFIER OFF   
  62. GO  
  63.   
  64. ALTER DATABASE [TimeLineDB] SET RECURSIVE_TRIGGERS OFF   
  65. GO  
  66.   
  67. ALTER DATABASE [TimeLineDB] SET DISABLE_BROKER   
  68. GO  
  69.   
  70. ALTER DATABASE [TimeLineDB] SET AUTO_UPDATE_STATISTICS_ASYNC OFF   
  71. GO  
  72.   
  73. ALTER DATABASE [TimeLineDB] SET DATE_CORRELATION_OPTIMIZATION OFF   
  74. GO  
  75.   
  76. ALTER DATABASE [TimeLineDB] SET TRUSTWORTHY OFF   
  77. GO  
  78.   
  79. ALTER DATABASE [TimeLineDB] SET ALLOW_SNAPSHOT_ISOLATION OFF   
  80. GO  
  81.   
  82. ALTER DATABASE [TimeLineDB] SET PARAMETERIZATION SIMPLE   
  83. GO  
  84.   
  85. ALTER DATABASE [TimeLineDB] SET READ_COMMITTED_SNAPSHOT OFF   
  86. GO  
  87.   
  88. ALTER DATABASE [TimeLineDB] SET HONOR_BROKER_PRIORITY OFF   
  89. GO  
  90.   
  91. ALTER DATABASE [TimeLineDB] SET RECOVERY SIMPLE   
  92. GO  
  93.   
  94. ALTER DATABASE [TimeLineDB] SET MULTI_USER   
  95. GO  
  96.   
  97. ALTER DATABASE [TimeLineDB] SET PAGE_VERIFY CHECKSUM   
  98. GO  
  99.   
  100. ALTER DATABASE [TimeLineDB] SET DB_CHAINING OFF   
  101. GO  
  102.   
  103. ALTER DATABASE [TimeLineDB] SET FILESTREAM( NON_TRANSACTED_ACCESS = OFF )   
  104. GO  
  105.   
  106. ALTER DATABASE [TimeLineDB] SET TARGET_RECOVERY_TIME = 0 SECONDS   
  107. GO  
  108.   
  109. ALTER DATABASE [TimeLineDB] SET READ_WRITE   
  110. GO  
Create table
  1. USE [TimeLineDB]  
  2. GO  
  3.   
  4. /****** Object: Table [dbo].[TimeLine_tbt] Script Date: 9/16/2016 10:03:21 AM ******/  
  5. SET ANSI_NULLS ON  
  6. GO  
  7.   
  8. SET QUOTED_IDENTIFIER ON  
  9. GO  
  10.   
  11. SET ANSI_PADDING ON  
  12. GO  
  13.   
  14. CREATE TABLE [dbo].[TimeLine_tbt](  
  15. [id] [intNOT NULL,  
  16. [content] [varchar](50) NULL,  
  17. [start] [datetime] NULL,  
  18. CONSTRAINT [PK_TimeLine_tbt] PRIMARY KEY CLUSTERED   
  19. (  
  20. [id] ASC  
  21. )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ONON [PRIMARY]  
  22. ON [PRIMARY]  
  23.   
  24. GO  SET ANSI_PADDING OFF
    GO
After creating the table, you can add some records, as shown below for demo-




Create your MVC Application

First of all, open Visual Studio and select file, click new project and a new dialog will pop up with the name New Project, select ASP.NET Web Application (.NET Framework), name your project and click OK button.



Now, new dialog will pop up to select the template. We are going to choose MVC template and click OK button.



After creating our project, we are going to add ADO.NET Entity Data Model.

Adding ADO.NET Entity Data Model

For this, right click on the project name, click Add > Add New Item. A dialog box will pop up. Inside Visual C#, select data, followed by ADO.NET Entity Data Model, enter the name for your Dbcontext model as DbContextTimeline and finally click Add.



At this stage, we are going to choose EF Designer from database, as given below-



In the snapshot given below, we need to select your Server name, then via dropdown list in connect to a database section, you should choose your database name and finally click OK button.





After clicking Next button, the dialog Entity Data Model wizard will pop up to choose object, which we want to use. In this example, we are going to chose TimeLine_tbt table and click Finish button. Finally, we see EDMX model generates TimeLine_tbt class.



Create a controller

Now, we are going to create a controller. Right click on the controllers folder > Add > Controller> selecting MVC 5 Controller – Empty > click Add.



Enter Controller name (‘TimelineController’).



TimelineController.cs
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.Mvc;  
  6.   
  7. namespace TimeLineApp.Controllers  
  8. {  
  9.     public class TimelineController : Controller  
  10.     {  
  11.         //DbContext  
  12.         private TimeLineDBEntities1 context = new TimeLineDBEntities1();  
  13.   
  14.   
  15.         // GET: Timeline  
  16.         public ActionResult Index()  
  17.         {  
  18.             return View();  
  19.         }  
  20.   
  21.         public JsonResult GetEventsList()  
  22.         {  
  23.             var data = context.TimeLine_tbt.ToList();  
  24.   
  25.             return Json(data, JsonRequestBehavior.AllowGet);  
  26.         }  
  27.   
  28.     }  
  29. }  
Here, I’m creating GetEventsList() action, which will select all the data from TimeLine_tbt table and return result as JSON.

Adding View

In timeline controller, just right click on Index() action, select Add View and dialog will pop up, write a name for your view, finally click Add.



Note- Don’t forget to download the following libraries from vis.js.
  1. <!-- CSS -->  
  2. t;script src="~/Scripts/vis.min.js"></script>  
  3.   <!-- JS -->  
  4. t;link href="~/Content/vis.min.css" rel="stylesheet" />  
Index cshtml
  1. @{  
  2.     ViewBag.Title = "Timeline demo";  
  3. }  
  4.   
  5. <h2>Timeline | ASP.NET MVC 5</h2>  
  6.   
  7. @section scripts{  
  8.   
  9.   <!-- CSS -->  
  10. <script src="~/Scripts/vis.min.js"></script>  
  11.     <!-- JS -->  
  12. <link href="~/Content/vis.min.css" rel="stylesheet" />  
  13.   
  14.     <script type="text/javascript">  
  15.   
  16.     $(document).ready(function(){  
  17.   
  18.   
  19.         $.ajax({  
  20.             type: "GET",  
  21.             url: "GetEventsList",  
  22.             contentType: "application/json; charset=utf-8",  
  23.             dataType: "json",  
  24.             success: ChartVis,  
  25.             error: OnError  
  26.   
  27.             });  
  28.   
  29.   
  30.         function ChartVis(response)  
  31.         {  
  32.             // DOM element where the Timeline will be attached  
  33.             var container = document.getElementById('visualization');  
  34.   
  35.             // Create a DataSet (allows two way data-binding)  
  36.             var items = new vis.DataSet(response);  
  37.   
  38.             // Configuration for the Timeline  
  39.             var options = {};  
  40.   
  41.             // Create a Timeline  
  42.             var timeline = new vis.Timeline(container, items, options);  
  43.         }  
  44.         function OnError(response) {  
  45.             alert("Error !");  
  46.         }  
  47.   
  48.   
  49.     })  
  50.   
  51.     </script>  
  52.   
  53. }  
  54.   
  55.   
  56. <div id="visualization"></div>  
Output

 

Up Next
    Ebook Download
    View all
    Learn
    View all