在ASP.NET MVC 4中使用Flot.js可视化SQL CE数据的一般方法? [英] General approach to visualize SQL CE data using Flot.js in ASP.NET MVC 4?

查看:60
本文介绍了在ASP.NET MVC 4中使用Flot.js可视化SQL CE数据的一般方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对SQL Server Flot.js还是很陌生,通常可以使用数据库.我正在ASP.NET MVC 4(使用VS 2012)中的一个项目中,该项目的Microsoft SQL Server Compact 4.0数据库中填充了数据,我想使用flot.js直观地显示此数据.

I'm very new to SQL Server, Flot.js, working with databases in general. I'm working on a project in ASP.NET MVC 4 (using VS 2012) which has a Microsoft SQL Server Compact 4.0 database filled with data, and I would like to visually display this data using flot.js.

我无法找到有关操作数据的常规步骤的任何信息.从SQL CE数据库中的DB条目开始,以JSON文件(或CSV-flot.js可以用作输入的任何内容)结束,这是Web开发人员可以使用ASP.NET的最常用方法.MVC 4?

I haven't been able to find any information on the general steps one would take to manipulate the data. Starting with the DB entries in the SQL CE database, and ending with a JSON file (or a CSV - anything flot.js could use as input), what would be the most common approach that a web developer could tak, using ASP.NET MVC 4?

推荐答案

您好,您的问题范围很广,因此如果答案在某些地方含糊不清,我深表歉意.由于您已有数据库,因此首先使用实体框架数据库将数据库映射到有意义的上下文,然后可以在您的代码中对其进行操作.创建edmx模型后,即可在控制器中使用它来处理数据:

Hi your question is quite broad so I apologise if answer is quite vague in places. As you have an existing database it would make sense to use Entity Framework Database First to map your database to a meaningful context which can then be manipulated in your code. Once you have created an edmx model you can then use it in your controllers to manipulate data:

public class YourController : Controller
{
    private DatabaseEntities db = new DatabaseEntities();

    //.... Your controller actions

首先使用数据库,您可以对现有数据库中的模型进行反向工程.该模型存储在EDMX文件中(扩展名为.edmx),并且可以在实体框架设计器中进行查看和编辑.您在应用程序中与之交互的类是从EDMX文件自动生成的.

Database First allows you to reverse engineer a model from an existing database. The model is stored in an EDMX file (.edmx extension) and can be viewed and edited in the Entity Framework Designer. The classes that you interact with in your application are automatically generated from the EDMX file.

Flot采取以下格式的数据:

Flot takes data in the following format:

[[1,2],[3,4],[5,5]]  // x, y coordinates
[[1,"a"],[2,"b"],[3,"c"]] // Categories

有关更多详细信息,请参见 Flot文档.因此,使用Json不能直接与flot一起使用.您将必须创建一个控制器操作,以正确的格式返回所需的数据:

See the Flot documentation for further details. So using Json will not work with flot directly. You will have to create a controller action that returns the data you require in the correct format:

public string GetData()
{
    var query = db.Table.Where(... // linq query for desired data

    var builder = new StringBuilder();
    builder.Append("[");
    foreach (var item in query)
        builder.AppendFormat("[{0}, {1}], ", item.x, item.y);
    var result = builder.ToString();

    return result;
}

现在在客户端,您可以通过jQuery进行调用以获取数据并绘制图表:

Now on the client side you can make a call from jQuery to get the data and draw the chart:

$(function () {

    $.getJSON("../controller/action", function (data) {

        $.plot("#placeholder", [data], {
                // your chart

这只是一种实现方法,但认为使用MVC是一种好方法.希望这会给您一个很好的概述,并且您应该从中获得足够的信息,至少可以开始使用.

Just one way of doing it, but think its a good way using MVC. Hopefully this will give you a good overview and you should have enough info from this to get started at least.

这篇关于在ASP.NET MVC 4中使用Flot.js可视化SQL CE数据的一般方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆