0

我必须根据项目列表制作图表。为此,我使用 dataview 图表和 ASP。网。

问题是我的图表是空的,但我有数据。

数据:

xmlns:xsd="http://www.w3.org/2001/XMLSchema">
- <Expense>
  <Type>Achat pièce</Type> 
  <Total>173.50</Total> 
  </Expense>
  </Root>

用于构建字符的代码:

DataSet dataSet = new DataSet();
DataTable dataTable = new DataTable("Table");
dataTable.Columns.Add("Total", typeof(string));
dataTable.Columns.Add("Type", typeof(string));
dataSet.Tables.Add(dataTable);

//GetData & fill dataTable
            var listOfExpense = Helper.JsonHelper.FromJson<ExpensePie>(resp.Content);
            foreach (var expense in listOfExpense.ListOfExpense)
            {
                dataTable.Rows.Add(expense.Total, expense.Type);
            }


//Fill data view with dataset
var dataView = new DataView(dataSet.Tables["Table"]);

//Build the chart
var myChart = new Chart(width: 600, height: 400, theme: ChartTheme.Blue)
   .AddSeries("Default", chartType: "bar",
   xValue:dataView, xField:"Type",
   yValues: dataView, yFields: "Total")
   .GetBytes("png");
return File(myChart, "image/png");
4

1 回答 1

1

您解析 XML 的方式一定有问题。您似乎正在使用一些自定义 FromJson 方法和一些 XmlDocument 来填充您的数据集,但我猜您的数据集不包含任何值,因为以下工作非常好:

public ActionResult Index()
{
    DataSet dataSet = new DataSet();
    DataTable dataTable = new DataTable("Table");
    dataTable.Columns.Add("Total", typeof(double));
    dataTable.Columns.Add("Type", typeof(string));
    dataSet.Tables.Add(dataTable);

    dataTable.Rows.Add(173.5, "Achat pièce");
    dataTable.Rows.Add(145.7, "Something else");

    //Fill data view with dataset
    var dataView = new DataView(dataSet.Tables["Table"]);

    //Build the chart
    var myChart = new Chart(width: 600, height: 400, theme: ChartTheme.Blue)
        .AddSeries("Default", chartType: "bar",
        xValue: dataView, xField: "Type",
        yValues: dataView, yFields: "Total")
        .GetBytes("png");
    return File(myChart, "image/png");
}

结果:

在此处输入图像描述

于 2012-05-29T08:14:34.790 回答