Home Can't build chart with data from database
Reply: 1

Can't build chart with data from database

Adrian
1#
Adrian Published in 2017-09-13 21:51:09Z

I can't build chart with data from database. Tried like this, but it don't work

Controller:

public ActionResult Chart()
{
    return PartialView("_Chart");
}

In main view:

img src="@Url.Action("Chart")"/>

Partial view (_Chart.cshtml):

@using TestProject.Models;

@{
    DateTime[] xVal = new DateTime();
    Decimal[] yVal = new Decimal();

    TestProjectContext db = new TestProjectContext();
    var deals = from d in db.DealsDirectory
                select d;

    deals.ToList().ForEach(x => xVal.Add(x.Date));
    deals.ToList().ForEach(y => yVal.Add(y.Price));

    var Chart = new Chart(width: 600, height: 400)
        .AddTitle("Зависимость цены от времени")
        .AddSeries(
            name: "USD",
            xValue: xVal,
            yValues: yVal,
            chartType:"Line")
        .Write();
}
Shyju
2#
Shyju Reply to 2017-09-13 22:23:27Z

Your code has some basic syntax problem which will prevent your code to even properly compile!

The below snippet is not valid !

DateTime[] xVal = new DateTime();
Decimal[] yVal = new Decimal();

You can not assign a DateTime object to an Array/Collection of DateTimes

You can use implicit variable declaration along with your LINQ query expression.

This should work

@{      
    var db = new TestProjectContext();    

    var xVal = db.DealsDirectory.Select(g => g.Date).ToArray();
    var yVal = db.DealsDirectory.Select(g => g.Price).ToArray();

    var Chart = new Chart(width: 600, height: 400)
        .AddTitle("Зависимость цены от времени")
        .AddSeries(
            name: "USD",
            xValue: xVal,
            yValues: yVal,
            chartType:"Line")
        .Write();
}
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.34842 second(s) , Gzip On .

© 2016 Powered by cudou.com design MATCHINFO