Arif Sam Arif Sam - 1 year ago 93
ASP.NET (C#) Question

x axis for bar graph only shows 1 data from database

So i just started learning MVC, and Im learning to produce graph.
Im trying to pull data from SQL server and place it in X and Y axis.
My problem is, on the y axis, it populate till the end of my db but my x axis only shows 1 data from db.
Or am i doing everything wrong?
Do help. TQVM.

controller code:

public ActionResult MyBarGraph()
DataTable data = DB.GetTable("SELECT * FROM tblproduct");
Chart c = new Chart(width: 800, height: 200);
foreach(DataRow item in data.Rows)
chartType: "column",
xValue: new[] {item["productname"]},
yValues: new[] { item["quantity"] });

return null;

view code:

<h2>Bar Graph Example</h2>
<img src="@Url.Action("MyBarGraph")" alt="SimpleChart" />

image :

only Apple is shown


new problem

Answer Source

You should use this syntax instead:

            var myChart = new Chart(width: 600, height: 400)
            .AddTitle("Product Chart")
            name: "Products",
            xValue: data.AsDataView(), xField: "productname",
            yValues: data.AsDataView(), yFields: "quantity")
            .Save("~/Images/Chart01.png", "png");

enter image description here

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download