0

I have read a lot of articles and questions here at stackoverflow, but most of them are about using DataTable. However, as I understood there is NO DataTable in asp.net CORE. I'm still pretty new to this and have no idea what alternative to use instead of DataTable. The Google Charts works perfectly with the hard coded values, but how to approach it using actual data from my database is where my understanding ends.

Can someone please provide me with a detailed approach on how to do this so that I can understand it better? I'll greatly appreciate it and thanks in advance!

I used information and code samples from here:

But, most likely, I got confused and mixed suggestions from different answers. In the comments of chart_script.js there is that hardcode that works pretty well.

If there is any additional information required please let me know and I will try to add it in to the question.

Model:

namespace MealProject.Models
{
    public partial class MealQuantity
    {
        public int MealQuantityId { get; set; }
        public int InitialQuantity { get; set; }
        public int RemainingQuantity { get; set; }
    }
}

Controller:

namespace MealProject.Controllers
{
    [Authorize]
    public class MainController : Controller
    {
        private DBContext _db;

        public MainController(DBContext context)
        {
            _db = context;
            if (User != null && AccountController.AdminConnect(_db, User.Claims.FirstOrDefault().Type, User.Claims.FirstOrDefault().Value).Result) { }
        }


[Route("/Chart")]
public IActionResult Chart()
{
    return View();
}

public IActionResult GetChart()
        {
            return Json(_db.MealQuantity
                .Select(p =>new {p.MealQuantityId, p.InitialQuantity, p.RemainingQuantity }));
        }

View:

@* Maybe we should try System.Windows.Forms.DataVisualization.Charting ?
    or https://dotnethighcharts.codeplex.com/
    or https://gionkunz.github.io/chartist-js/index.html
*@
<strong>Hope this works</strong>

<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
<script type="text/javascript" src="~/scripts/chart_script.js"></script>
<div id="chart_div"></div>

DBContext:

namespace MealProject.Models
{
    public partial class DBContext : DbContext
    {
        public DBContext(DbContextOptions<DBContext> options) : base(options) { }
        public virtual DbSet<MealQuantity> MealQuantity { get; set; }
        public virtual DbSet<Users> Users { get; set; }

    }
}

chart_script.js:

google.charts.load('current', { packages: ['corechart', 'line'] });
google.charts.setOnLoadCallback(drawLineColors);
function drawLineColors() {

    var data = new google.visualization.DataTable();

    data.addColumn('number', '@Model.MealQuantityId');
    data.addColumn('number', '@Model.InitialQuantity');
    data.addColumn('number', '@Model.RemainingQuantity');

    $.getJSON("@Url.Action('GetChart')", null, function (chartData) {
        $.each(chartData, function (i, item) {
            data.addRow(item.MealQuantity, item.InitialQuantity, item.RemainingQuantity);
        });


        var options = {
            title: 'Chart',
            hAxis: { title: '@Model.Chart', titleTextStyle: { color: 'red' } }
        };

        var chart = new google.visualization.ColumnChart(document.getElementById('chart_div'));
        chart.draw(data, options);
    });
}
5
  • Unclear what your asking. What problems are you having? Your GetChart() method just needs to return the data in the form expected by google charts. Commented Jul 24, 2016 at 23:07
  • I'm asking why is not it working. When I open localhost:4411/Chart I get nothing except: "Hope this works" . Tha means view is loaded, but javascript code ignored, right?As you can see I tried to follow instructions from stackoverflow.com/questions/32524745/… But it does not work. I suppose it is because new asp.net core have no DataTable support. So, what should I do? Commented Jul 25, 2016 at 9:37
  • But what data to you want to add, and how does that relate to the data returned by the GetChart() method, which returns what appears to be 3 int values (you have not even shown your model so we cannot guess). In any case, it certainly does not contain objects which contain properties Year, Sale and Purchase Commented Jul 25, 2016 at 9:43
  • Oh, @StephenMuecke , yes, sorry for misprinting, I mean item.MealQuantity, item.InitialQuantity, item.RemainingQuantity - have just corrected it in question. Still does not work Commented Jul 25, 2016 at 10:25
  • It still does not match - your controller method has MealQuantityId but the ajax has MealQuantity. Debug your code and test the output of console.log(item.MealQuantity); and console.log(item.InitialQuantity); etc. In anycase, you should be initially testing it by hard-coding in some values. Commented Jul 25, 2016 at 10:28

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.