javascript 轴 #0 的数据列不能是字符串类型 - Google 图表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22413879/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 23:06:08  来源:igfitidea点击:

Data column(s) for axis #0 cannot be of type string - Google Charts

c#javascriptasp.net-mvcchartsgoogle-visualization

提问by WhoAmI

I'm trying to display some data in Google Charts but get this error:

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

Data column(s) for axis #0 cannot be of type string..

I have two properties from this class:

我有这个类的两个属性:

public class GAStatistics
{
    public string Date { get; set; }
    public string Visitors { get; set; }
}

I'm passing this a list of these properties from this controller:

我正在从这个控制器传递这些属性的列表:

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;

    }

}

To this view of wich i pass the list from the controller:

我从控制器传递列表到这个视图:

<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>

Any idea?

任何的想法?

采纳答案by vortexwolf

I have tested your example and found out that the main issue is because the second column Visitorsshould be a number instead of a string.

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

I have replaced the column type in the tdata.addColumn('number', 'Visitors');line and added parseIntinside the loop:

我已经替换了行中的列类型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)]);
}

Also the first column Datecould have remained a string, but I replaced its type to be dateand added new Dateconversion as well.

第一列Date也可以保留为string,但我将其类型替换为,date并添加了new Date转换。