Sunday 31 May 2015

Chart.js with ASP.net MVC 5

Here I have add some dummy data in database for daily transactions details















Add Method in your Controller to get data from Database and pass it to view in JSON formate


public JsonResult GetData()
        {
            DataTable dtTable = new DataTable();
            List<string> lstMonth = new List<string>();
            List<string> lstExpences = new List<string>();
            List<string> lstIncome = new List<string>();

            string strQuery = "sp_GetBarChartData";

            SqlConnection con = new SqlConnection
            (ConfigurationManager.ConnectionStrings["eXpenceMgrContext"].ToString());

            SqlCommand cmd = new SqlCommand(strQuery, con);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("TYPE", "Expences");
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            da.Fill(dtTable);
            foreach (DataRow row in dtTable.Rows)
            {
                if (row["TransactionType"].ToString() == "Expences")
                {
                    lstExpences.Add(row["Amount"].ToString());
                }
                lstMonth.Add(row["Month"].ToString());
            }

            cmd.Parameters.RemoveAt(0);
            dtTable.Clear();
            cmd.Parameters.AddWithValue("TYPE", "Income");
            da.Fill(dtTable);
            foreach (DataRow row in dtTable.Rows)
            {
                if (row["TransactionType"].ToString() == "Income")
                {
                    lstIncome.Add(row["Amount"].ToString());
                }
            }

            var BarChart = new
            {
                Month = lstMonth,
                Expences = lstExpences,
                Income = lstIncome,
            };

            return Json(BarChart, JsonRequestBehavior.AllowGet);
        }


Now Create View and copy given code in <script></script>























And provide json data to the Chart.js Method

function BarChart(data) {
        var barChartData = {
            labels: data.Month,
            datasets: [
                {
                    fillColor: "rgba(220,220,220,0.5)",
                    strokeColor: "rgba(220,220,220,0.8)",
                    highlightFill: "rgba(220,220,220,0.75)",
                    highlightStroke: "rgba(220,220,220,1)",
                    data: data.Expences
                },
                {
                    fillColor: "rgba(151,187,205,0.5)",
                    strokeColor: "rgba(151,187,205,0.8)",
                    highlightFill: "rgba(151,187,205,0.75)",
                    highlightStroke: "rgba(151,187,205,1)",
                    data: data.Income
                }
            ]

        }
        var ctx = document.getElementById("dvbarchart").getContext("2d");
        window.myBar = new Chart(ctx).Bar(barChartData, {
            responsive: true
        });

    }

Finally to bind the Chart in view you need to add canvas tag in your View

<div style="width:550px;">
    <canvas id="dvbarchart"></canvas>
</div>

and  here is the final output







Show Multiple Web Grid on 1 View with MVC 5


Add controller method and provide multiple <List> to the view

    public ActionResult Index()
        {
            dynamic mymodel = new ExpandoObject();

            mymodel.ExpenceTypes = db.ExpenceTypes.ToList();

            mymodel.TransactionTypes = db.TransactionTypes.ToList();

            mymodel.TransactionSubTypes = db.TransactionSubTypes.ToList();

            return View(mymodel);

        }

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Create view for given Controller

Add following code in View

Index View -
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@model dynamic

@{
    ViewBag.Title = "Index";
    Layout = "~/Views/Sharedd/_MasterPage.cshtml";

    var gridExpenceTypes = new WebGrid(source: Model.ExpenceTypes, canPage: true, rowsPerPage: 10, canSort: true);
    gridExpenceTypes.Pager(WebGridPagerModes.All);

    var gridTransactionType = new WebGrid(source: Model.TransactionTypes, canPage: true, rowsPerPage: 10, canSort: true);
    gridTransactionType.Pager(WebGridPagerModes.All);

    var gridTransactionSubType = new WebGrid(source: Model.TransactionSubTypes, canPage: true, rowsPerPage: 10, canSort: true);
    gridTransactionSubType.Pager(WebGridPagerModes.All);
}

<h2>Setting</h2>
@Html.ActionLink("Create Expence Type", "CreateExpenceType", null, new { id = "lnkCreate" })

<div id="content">
    @gridExpenceTypes.GetHtml(
tableStyle: "webgrid-table",
headerStyle: "webgrid-header",
footerStyle: "webgrid-footer",
alternatingRowStyle: "webgrid-alternating-row",
rowStyle: "webgrid-row-style",
                columns: gridExpenceTypes.Columns(
            //here i will add column for serial no
            gridExpenceTypes.Column(header: "Serial No", format: @<text><div>@(item.WebGrid.Rows.IndexOf(item) + 1)</div></text>),
           //gridExpenceTypes.Column(columnName: "ExpenceTypeID", header: "TransactionSubType ID"),
            gridExpenceTypes.Column(columnName: "ExpenceTypeID", header: "TransactionType"),
            gridExpenceTypes.Column(columnName: "ExpenceType1", header: "Expence Type"),
            gridExpenceTypes.Column(header: "Edit", format: @<text>@Html.ActionLink("Edit", "Edit", new { id = item.ExpenceTypeID }, new { @class = "lnkEdit" })</text>),
            gridExpenceTypes.Column(header: "Details", format: @<text>@Html.ActionLink("Details", "Details", new { id = item.ExpenceTypeID }, new { @class = "lnkDetail" })</text>),
            gridExpenceTypes.Column(header: "Delete?", format: @<text>@Html.ActionLink("Delete", "DeleteTransactionSubType", new { id = item.ExpenceTypeID }, new { @class = "lnkDelete" })</text>)
                                            ))

</div>
<br />
<hr />



@Html.ActionLink("Create Tranction Type", "CreateTransactionType", null, new { id = "lnkCreate" })
<br />
<div id="content">
    @gridTransactionType.GetHtml(
    tableStyle: "webgrid-table",
    headerStyle: "webgrid-header",
    footerStyle: "webgrid-footer",
    alternatingRowStyle: "webgrid-alternating-row",
    rowStyle: "webgrid-row-style",
            columns: gridTransactionType.Columns(
            //here i will add column for serial no
            gridTransactionType.Column(header: "Serial No", format: @<text><div>@(item.WebGrid.Rows.IndexOf(item) + 1)</div></text>),
            gridTransactionType.Column(columnName: "TransactionType1", header: "Transaction Type"),
            gridTransactionType.Column(header: "Edit", format: @<text>@Html.ActionLink("Edit", "Edit", new { id = item.TransactionTypeID }, new { @class = "lnkEdit" })</text>),
            gridTransactionType.Column(header: "Details", format: @<text>@Html.ActionLink("Details", "Details", new { id = item.TransactionTypeID }, new { @class = "lnkDetail" })</text>),
            gridTransactionType.Column(header: "Delete?", format: @<text>@Html.ActionLink("Delete", "DeleteConfirmed", new { id = item.TransactionTypeID }, new { @class = "lnkDelete" })</text>)
                        ))

</div>
<br />
<hr />
@Html.ActionLink("Create Tranction Sub Type", "TransactionSubType", null, new { id = "lnkCreate" })
<br />

<div id="content">
    @gridTransactionSubType.GetHtml(
tableStyle: "webgrid-table",
headerStyle: "webgrid-header",
footerStyle: "webgrid-footer",
alternatingRowStyle: "webgrid-alternating-row",
rowStyle: "webgrid-row-style",
        columns: gridTransactionType.Columns(
            //here i will add column for serial no
        gridTransactionSubType.Column(header: "Serial No", format: @<text><div>@(item.WebGrid.Rows.IndexOf(item) + 1)</div></text>),
        gridTransactionSubType.Column(columnName: "TransactionSubTypeID", header: "TransactionSubType ID"),
        gridTransactionSubType.Column(columnName: "TransactionTypeID", header: "TransactionType"),
        gridTransactionSubType.Column(columnName: "TransactionSubType1", header: "Transaction Sub Type"),
        gridTransactionSubType.Column(header: "Edit", format: @<text>@Html.ActionLink("Edit", "Edit", new { id = item.TransactionSubTypeID }, new { @class = "lnkEdit" })</text>),
        gridTransactionSubType.Column(header: "Details", format: @<text>@Html.ActionLink("Details", "Details", new { id = item.TransactionSubTypeID }, new { @class = "lnkDetail" })</text>),
        gridTransactionSubType.Column(header: "Delete?", format: @<text>@Html.ActionLink("Delete", "DeleteTransactionSubType", new { id = item.TransactionSubTypeID }, new { @class = "lnkDelete" })</text>)
                            ))

</div>
<br />
<hr />

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Output :: --