c# - 在 MVC 5 Razor 中使用 ChartJs

标签 c# asp.net-mvc sql-server-2008 chart.js

我想提供帮助,我正在尝试使用 ChartJs 在图表中显示我的查询数据。如何返回 JSON 格式的数据?

FaturamentoIvel.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BiDAL.Entity.Graficos
{
    public class FaturamentoIvel
    {
        public string Operacao { get; set; }
        public string AnoMes { get; set; }
        public float ValorNF { get; set; }
        public bool TradeMarketing { get; set; }
    }
}

FaturamentoIvelDAL.cs

using BiDAL.Entity.Graficos;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BiDAL.Persistence
{
    public class FaturamentoIvelDAL : Conexao
    {
        public List<FaturamentoIvel> FindAllFaturamentoIvel()
        {
            try
            {
                OpenConnection();
                Cmd = new SqlCommand("SELECT Operacao, AnoMes, TradeMarketing, SUM(ValorNF) AS ValorTotal FROM dbo.FatoFaturamentoIVEL WHERE TradeMarketing = 0 GROUP BY Operacao, AnoMes, TradeMarketing ORDER BY SUM(ValorNF) DESC", Con);

                Dr = Cmd.ExecuteReader();
                List<FaturamentoIvel> lista = new List<FaturamentoIvel>();

                while (Dr.Read())
                {
                    FaturamentoIvel ft = new FaturamentoIvel();
                    ft.Operacao = Convert.ToString(Dr["Operacao"]);
                    ft.AnoMes = Convert.ToString(Dr["AnoMes"]);
                    ft.ValorNF = Convert.ToSingle(Dr["ValorNF"]);

                    lista.Add(ft);
                }
                return lista;
            }
            catch (Exception ex)
            {
                throw new Exception("Erro ao listar Faturamento: " + ex.Message);
            }
        }


    }
}

我的 Controller AdminController

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace BiFrontEnd.Controllers
{
    public class AdminController : Controller
    {
        // GET: Home
        public ActionResult Home()
        {
            return View();
        }


    }
}

我的观点

@{
    ViewBag.Title = "Home";
    Layout = "~/Views/Template/_Layout.cshtml";
}

<label>Gráfico Mês Atual</label>
<select id="ddlOperacao">
    <option>Venda</option>
    <option>Troca</option>
    <option>Bonificação</option>
    <option>Outras</option>
</select>
<button id="btnGerarGraficoMesAtual">Exibir</button>

最佳答案

这只是它应该如何工作的例子。 在你的 Controller 中制作类似这样的东西。

public ActionResult Chart()
{
   var Value1 = db.MYTABLE.Where(i => i.Value1);
   var Value2 = db.MYTABLE.Where(i => i.Value2);          
   var dataForChart = new[]
   {  
      new 
      {
         label = "Your Label1", 
         value = Value1.Count()
      },
      new 
      {
         label = "Your Label2", 
         value = Value2.Count()
      }                
   };

   var result = Json(dataForChart, JsonRequestBehavior.AllowGet);
   return result;

 }

您的 JavaScript 应如下所示:

$.get("/ControllerName/Chart", function (response) {            
  //Construct your ChartJS here.          
});

将它放在您的 View 中以调用它:

<canvas id="Chart" width="400" height="400"></canvas>

关于c# - 在 MVC 5 Razor 中使用 ChartJs,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36600870/

相关文章:

c# - 如何使用 xsl 拆分 html 文件?

c# - 通过在文本框中输入进行过滤

c# - 使用 C# 在字符串中查找文本

sql - 是否可以在 SQL Server 中使用 CLR 代码创建新的 T-SQL 运算符?

c# - 用java在C#中解密AES加密文件

c# - WebApplication 在 IIS Express 上运行良好,但在本地 IIS 上运行不正常

asp.net - 如何在 IIS 上限制/验证文件上传文件类型服务器端

JavaScript if else 在全日历上设置事件颜色的条件不起作用

sql - SSIS - 跳过丢失的文件

mysql - 带月份名称列的区间统计