C# axis#0的数据列不能是字符串-谷歌图表类型

C# axis#0的数据列不能是字符串-谷歌图表类型,c#,javascript,asp.net-mvc,charts,google-visualization,C#,Javascript,Asp.net Mvc,Charts,Google Visualization,我试图在Google图表中显示一些数据,但出现以下错误: Data column(s) for axis #0 cannot be of type string.. 我在这个类中有两个属性: public class GAStatistics { public string Date { get; set; } public string Visitors { get; set; } } 我正在向此控制器传递这些属性的列表: public class GAStatistics

我试图在Google图表中显示一些数据,但出现以下错误:

Data column(s) for axis #0 cannot be of type string..
我在这个类中有两个属性:

public class GAStatistics
{
    public string Date { get; set; }
    public string Visitors { get; set; }
}
我正在向此控制器传递这些属性的列表:

public class GAStatisticsController : Controller
{
    
     //GET: /ShopStatistics/


    public ActionResult GetData()
    {
        return Json(CreateCompaniesList(), JsonRequestBehavior.AllowGet);
    }


    private IEnumerable<GAStatistics> CreateCompaniesList()
    {
        
        List<GAStatistics> ListGaVisitors = new List<GAStatistics>();

        foreach (var row in d.Rows)
        {

            GAStatistics GaVisits = new GAStatistics(row[0], row[1]);
            ListGaVisitors.Add(GaVisits);
        }
        

        return ListGaVisitors;
   
    }

}
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
    
    <script type="text/javascript">
    
        google.load("visualization", "1", { packages: ["corechart"] });
        google.load("visualization", "1", { packages: ["treemap"] });
        google.setOnLoadCallback(drawChart);
        function drawChart() {
            $.get('/GAStatistics/GetData', {},
                function (data) {
                    var tdata = new google.visualization.DataTable();
    
                    tdata.addColumn('string', 'Date');
                    tdata.addColumn('string', 'Visitors');
            
    
                    for (var i = 0; i < data.length; i++) {
                        //tdata.addRow([data[i].Year, data[i].Salary, data[i].Expense]);
                        tdata.addRow([data[i].Date, data[i].Visitors]);
                    }
    
    
    
                    var options = {
                        title: "Expenses, salary For the current year"
                    };
    
                    var chart1 = new google.visualization.AreaChart(document.getElementById('chart_div1'));
    
                    chart1.draw(tdata, options);

                });
    
    
        }
    </script>
公共类GAStatisticsController:控制器
{
//获取统计数据/
公共操作结果GetData()
{
返回Json(createCompanyList(),JsonRequestBehavior.AllowGet);
}
私有IEnumerable CreateCompaniesList()
{
List ListGaVisitors=新列表();
foreach(变量行在d.Rows中)
{
GAStatistics=新的GAStatistics(第[0]行、第[1]行);
listgagvisitors.Add(gavisitors);
}
回访旅客;
}
}
我将列表从控制器传递到该视图:

public class GAStatisticsController : Controller
{
    
     //GET: /ShopStatistics/


    public ActionResult GetData()
    {
        return Json(CreateCompaniesList(), JsonRequestBehavior.AllowGet);
    }


    private IEnumerable<GAStatistics> CreateCompaniesList()
    {
        
        List<GAStatistics> ListGaVisitors = new List<GAStatistics>();

        foreach (var row in d.Rows)
        {

            GAStatistics GaVisits = new GAStatistics(row[0], row[1]);
            ListGaVisitors.Add(GaVisits);
        }
        

        return ListGaVisitors;
   
    }

}
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
    
    <script type="text/javascript">
    
        google.load("visualization", "1", { packages: ["corechart"] });
        google.load("visualization", "1", { packages: ["treemap"] });
        google.setOnLoadCallback(drawChart);
        function drawChart() {
            $.get('/GAStatistics/GetData', {},
                function (data) {
                    var tdata = new google.visualization.DataTable();
    
                    tdata.addColumn('string', 'Date');
                    tdata.addColumn('string', 'Visitors');
            
    
                    for (var i = 0; i < data.length; i++) {
                        //tdata.addRow([data[i].Year, data[i].Salary, data[i].Expense]);
                        tdata.addRow([data[i].Date, data[i].Visitors]);
                    }
    
    
    
                    var options = {
                        title: "Expenses, salary For the current year"
                    };
    
                    var chart1 = new google.visualization.AreaChart(document.getElementById('chart_div1'));
    
                    chart1.draw(tdata, options);

                });
    
    
        }
    </script>

load(“可视化”、“1”、{packages:[“corechart”]});
load(“可视化”、“1”、{packages:[“treemap”]});
setOnLoadCallback(drawChart);
函数绘图图(){
$.get('/GAStatistics/GetData',{},
功能(数据){
var tdata=new google.visualization.DataTable();
tdata.addColumn('string','Date');
tdata.addColumn('string','Visitors');
对于(变量i=0;i

有什么想法吗?

我已经测试了您的示例,发现主要问题是第二列
访问者
应该是数字而不是字符串

我已经替换了
tdata.addColumn('number','Visitors')中的列类型行,并在循环内添加了
parseInt

tdata.addColumn('date', 'Date');
tdata.addColumn('number', 'Visitors');

// sample data: var data = [{ Date: "20140124", Visitors: "873" }, { Date: "20140125", Visitors: "875" }];
for (var i = 0; i < data.length; i++) {
    var dateStr = data[i].Date.substr(0, 4) + "-" + data[i].Date.substr(4,2) + "-" + data[i].Date.substr(6,2);
    tdata.addRow([new Date(dateStr), parseInt(data[i].Visitors)]);
}
tdata.addColumn('date','date');
tdata.addColumn('number','Visitors');
//样本数据:var数据=[{日期:“20140124”,访问者:“873”},{日期:“20140125”,访问者:“875”}];
对于(变量i=0;i

另外,第一列
Date
可能仍然是
字符串
,但我将其类型替换为
Date
,并添加了
新日期
转换。

您应该将C#datetime字符串转换为javascript日期类型。它应该类似于
tdata.addRow([新日期(2014,1,1))…
,但是你不应该使用
新日期
你应该使用你自己的解析函数。也许可以通过将C#datetime转换为时间戳数字,就像这里解释的那样:
数据
看起来像什么?谢谢vorrtex。所以有必要在javascript中将字符串值转换为日期值?我实际上在编写时遵循了一个指南这家伙在一个列表中用Year属性写了这样的东西-Year=new DateTime(2014,3,1)。ToString(“yyy/mm/dd”)是Galiant-我不知道,这就是我所有的,在网上找到了一个例子。我不知道你的
字符串日期如何转换。但是如果你的日期字符串看起来像“2011-10-20”或“1999/01/31”,或者“1990年1月1日”它将很容易被javascript解析,例如
newdate(“2011-10-20”)
。将调试断点放在某个地方,看看
Date
属性有什么值。噢,Ookej。“Date”是C#class GAStatistics中的字符串属性,这个类还包含一个名为Visitors的字符串属性。Date的格式如下”2014-01-24“访客只包含像“873”这样的数字。很好!一周左右的时间里,我一直在努力解决这个问题。我现在在柱状图中得到了访客数量,但日期根本没有显示出来,但是,它说”NaN NaN和一个值。但是,如果将日期更改为数字并使用pareInt,则值将正确显示如下:她的使用“日期”得到的结果-折线图:imageshack.com/i/nmq2typ柱状图:imageshack.com/i/nr4ziop再次感谢!@Kristoferandersson工具提示“20140126”似乎是日期“2014-01-26”如果没有连字符,这就是它无法工作的原因。可以通过添加这些连字符并使用类似于
str.substr(0,4)+“-”+str.substr(4,2)+“-”+str.substr(6,2)的代码来修复此问题
。这种方式应该更好,因为工具提示和标签将显示更多的逻辑日期值,而不是数字。@Kristoferandersson我在回答中更新了代码,您可以试试。如果不起作用,您可以在此网站上发布一个新问题。您好Vortex,很抱歉再次挖掘这个问题,但我想知道数字(0,4)、(4,2)和(4,2)是什么(62)表示。如果您能完全理解此代码,将非常高兴。谢谢you@KristofferAndersson我使用了内置的javascript函数
substr
,您可以在这里了解它,例如,参数(0,4)表示从索引0(str[0])开始,并使用4个字符,
“abcdef”。substr(0,4)//abcd
,参数(4,2)-从str[4]开始并使用2个字符
“abcdef”。substr(4,2)//ef
。您可以使用Chrome开发工具(按F12),打开控制台选项卡并测试任何javascript代码。