简体   繁体   English

图表ASP.NET MVC5

[英]Charts asp.net MVC5

I want to implement a chart on view getting values from database 我想实现从数据库获取值的视图图表

Code: 码:

 @{
var db = Database.Open("aspnet-NewApp-20150923010220");
var data = db.Query("SELECT Name, Price FROM Product");
var myChart = new Chart(width: 600, height: 400)
    .AddTitle("Product Sales")
    .DataBindTable(dataSource: data, xField: "Name")
    .Write();
}

But when I try to use database.open and db.query it says me cannot resolve symbol, how can I resolve this? 但是,当我尝试使用database.opendb.query它说我无法解析符号,该如何解决呢?

If you require quick chart creation, use the ChartHelper 如果需要快速创建图表,请使用ChartHelper

Pass the data from your controller action as your model to the View . 将来自控制器操作的数据作为model传递到View

Something like: 就像是:

 public class Product
 {
    public string Name { get; set; }
    public string Price { get; set; }
 }

    [HttpGet]
    public ActionResult DrawChart()
    {
        var products = new List<Product>();

        string connectionString = @"Data Source=.;Initial Catalog=TestDb;Integrated Security=True";

        using (SqlConnection con = new SqlConnection(connectionString))
        {
            con.Open();

            using (SqlCommand command = new SqlCommand("SELECT Name, Price FROM Product", con))
            using (SqlDataReader reader = command.ExecuteReader())
            {
                while (reader.Read())
                {
                   products.Add(new Product{ Name = reader.GetString(0), Price = reader.GetString(1)});
                }
            }
        }

        return View(products);
    }

Customize the above as required. 根据需要自定义以上内容。 (there are alternate ways to get the data ofcourse) (还有其他获取课程数据的方法)

This is an example View : 这是一个示例View

@model IEnumerable<Product>

@{

var myChart = new Chart(width: 500, height: 300, theme: ChartTheme.Green)
    .AddTitle("Product Sales")
    .AddSeries("Default",
               xValue: Model, xField: "Name",
               yValues: Model, yFields: "Price")
    .Write("png");
}

For more control over your chart use: System.Windows.Forms.DataVisualization.Charting 要更好地控制图表,请使用: System.Windows.Forms.DataVisualization.Charting

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM