簡體   English   中英

圖表未顯示所有數據

[英]Chart Not Showing All Data

所以我剛剛開始學習MVC,而我正在學習生成圖形。 我試圖從SQL Server中提取數據並將其放置在X和Y軸上。 我的問題是,在y軸上,該數據一直填充到數據庫的末尾,但我的x軸僅顯示1個來自數據庫的數據。 還是我做錯了一切? 幫忙 TQVM。

控制器代碼:

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

        return null;
    }

查看代碼:

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

圖片 :

僅顯示蘋果

更新的圖像:

新問題

您添加了一堆系列,每個系列只有一個列。

填充數據的正確方法應該是:

    c.Series.Add("srs");
    foreach(DataRow item in data.Rows)
    {
        C.Series["srs"].Points.Add(item["productname"], item["quantity"]);
    }

這樣,您將為每個數據行獲得一列。

您應該改用以下語法:

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

在此處輸入圖片說明


編輯:為了自定義標簽,您需要將Theme文件傳遞給圖表,如下所示:

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

MyTheme.xml文件添加到項目的根文件夾中,內容如下:

<?xml version="1.0" encoding="utf-8" ?>
<Chart>
  <ChartAreas>
    <ChartArea Name="Default">
      <AxisX IsLabelAutoFit="false">
        <LabelStyle Angle="-90" Interval="1"></LabelStyle>
      </AxisX>
    </ChartArea>
  </ChartAreas>
</Chart>

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM