This is a migrated thread and some comments may be shown as answers.

SQL Server / Kendo UI

1 Answer 185 Views
Charts
This is a migrated thread and some comments may be shown as answers.
Allan
Top achievements
Rank 1
Allan asked on 27 Jan 2013, 09:23 PM

I have a small application that returns SQL Server data to a cshtml page and populates a chart.

But, the code I used does not seem like the best solution and I was wondering if you could take a look at my code and give me an idea of a better way?

My code is below.

Model:

public class LoginHistory
{
    public Int32 LoginHistoryID { set; get; }
    public Decimal LoginDuration { set; get; }
    public string LoginApp { set; get; }
    public DateTime LoginDateTime { set; get; }
    public string LoginUser { set; get; }
    public string LoginLocation { set; get; }
    public string LoginEnvironment { set; get; }
}

CSHTML:

@model IEnumerable<AllanTest01.Models.LoginHistory>
 
@{
    Layout = null;
}
 
<!DOCTYPE html>
 
<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Index</title>
 
    <!-- Kendo References -->
    <link rel="stylesheet" href="~/Content/kendo.common.min.css">
    <link rel="stylesheet" href="~/Content/kendo.default.min.css">
    <link href="~/Content/kendo.dataviz.min.css" rel="stylesheet" type="text/css" />
    <script src="~/Scripts/jquery.min.js"></script>
    <script src="~/Scripts/kendo.all.min.js"></script>
    <script src="~/Scripts/kendo.aspnetmvc.min.js"></script>
 
</head>
<body>
    <div>
 
 
 
         
        Login History Charts 01
 
        <br /><br />
 
        filters_here
        <br />
        Date Range: @(Html.Kendo().DateTimePicker().Name("StartDateTime")) - @(Html.Kendo().DateTimePicker().Name("EndDateTime"))
 
        <br /><br />
 
        <table style="width:100%;">
            <tr>
                    @* loop thru the apps *@
                    @foreach (var item in Model)
                    {
                         
                            <td>
                                        @(Html.Kendo().Chart()
                                            .Name(item.LoginApp.ToString().Replace(" ", "_"))
                                            .Title(item.LoginApp)
                                            .Legend(legend => legend
                                                .Position(ChartLegendPosition.Bottom)
                                            )
                                            .Series(series =>
                                            {
                                                series.Column(new double[] { 15.7, 16.7, 20, 23.5, 26.6 }).Name("World");
                                                series.Column(new double[] { 67.96, 68.93, 75, 74, 78 }).Name("United States");
                                            })
                                            .CategoryAxis(axis => axis
                                                .Categories("2005", "2006", "2007", "2008", "2009")
                                            )
                                            .ValueAxis(axis => axis
                                                .Numeric().Labels(labels => labels.Format("{0}%"))
                                            )
                                            .Tooltip(tooltip => tooltip
                                                .Visible(true)
                                                .Format("{0}%")
                                            )
                                            .HtmlAttributes(new { style = "width:300px;" })
                                        )
                            </td>
                    }
            </tr>
        </table>
 
 
 
 
 
 
 
 
 
 
        @foreach (var item in Model)
{
   <p>@item.LoginHistoryID | @item.LoginDuration | @item.LoginApp | @item.LoginDateTime | @item.LoginUser | @item.LoginLocation</p>
}
    </div>
</body>
</html>

HomeController.cs:

using AllanTest01.Models;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.Mvc;
 
namespace AllanTest01.Controllers
{
    public class HomeController : Controller
    {
        //
        // GET: /Home/
 
        public ActionResult Index()
        {
            var items = GetLoginHistory();
 
            var newItems = (from p in items
                            select new LoginHistory
                            {
                                LoginHistoryID = p.Field<Int32>("LoginHistoryID"),
                                LoginDuration = p.Field<Decimal>("LoginDuration"),
                                LoginApp = p.Field<String>("LoginApp"),
                                LoginDateTime = p.Field<DateTime>("LoginDateTime"),
                                LoginUser = p.Field<String>("LoginUser"),
                                LoginLocation = p.Field<String>("LoginLocation")
                            }).ToList();
 
            return View(newItems);
        }
 
        private List<DataRow> GetLoginHistory()
        {
            List<DataRow> list = null;
            string srtQry = @"Select LoginHistoryID, LoginDuration, LoginApp, LoginDateTime, LoginUser, LoginLocation, LoginEnvironment
                              From LoginHistories";
            string connString = "Data Source=.\\sqlexpress;Initial Catalog=AllanTest;Integrated Security=True";
 
            using (SqlConnection conn = new SqlConnection(connString))
            {
                using (SqlCommand objCommand = new SqlCommand(srtQry, conn))
                {
                    objCommand.CommandType = CommandType.Text;
                    DataTable dt = new DataTable();
                    SqlDataAdapter adp = new SqlDataAdapter(objCommand);
                    conn.Open();
                    adp.Fill(dt);
                    if (dt != null)
                    {
                        list = dt.AsEnumerable().ToList();
                    }
                }
            }
            return list;
        }
 
    }
}

1 Answer, 1 is accepted

Sort by
0
Accepted
Iliana Dyankova
Telerik team
answered on 29 Jan 2013, 12:06 PM
Hi Allan

I am not quite sure if I understand you correctly, because in the provided code snippet your chart uses inline data. Generally speaking Kendo UI Chart can visualize series bound to both local and remote data through the DataSource (take a look at this help topic and the online demos). In order to assist you best and provide concrete recommendations I would like to ask you to provide more detailed information about the exact functionality you are trying to implement. 

Regards,
Iliana Nikolova
the Telerik team
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
Tags
Charts
Asked by
Allan
Top achievements
Rank 1
Answers by
Iliana Dyankova
Telerik team
Share this question
or