简体   繁体   English

谷歌图表加载多列图表的问题

[英]Google Charts Problem with loading multi column chart

I am attempting to create a multi column chart with google visualization, I am using two querys for this but it's not working.我正在尝试使用谷歌可视化创建一个多列图表,我为此使用了两个查询,但它不起作用。

I get the following error in the console:我在控制台中收到以下错误:

Error: Row given with size different than 3 (the number of columns in the table).

Here is my querys:这是我的查询:

Query1:查询 1:

 #region Total Hours Per Month sick

        var querythpmsick = (from r in db.SickLeaveRequestForms
                            where r.EmployeeID == id
                            group r by r.MonthOfHoliday into g
                            select new { Value = g.Key, Count1 = g.Sum(h => h.SickLeaveTaken) }
                           ).OrderBy(e => e.Value);


        var resultthpmsick = querythpmsick.ToList();

        var datachartthpmsick = new object[resultthpmsick.Count];
        int Q = 0;
        foreach (var i in resultthpmsick)
        {
            datachartthpmsick[Q] = new object[] { i.Value.ToString(), i.Count1 };
            Q++;
        }
        string datathpmsick = JsonConvert.SerializeObject(datachartthpmsick, Formatting.None);
        ViewBag.datajthpmsick = new HtmlString(datathpmsick);

        #endregion

Query 2:查询 2:

  #region Total Hours Per Month
        var querythpmpro = (from r in db.HolidayRequestForms
                            where r.EmployeeID == id
                            group r by r.MonthOfHoliday into g
                            select new { Value = g.Key, Count = g.Sum(h => h.HoursTaken) }
                           ).OrderBy(e => e.Value);


        var resultthpmpro = querythpmpro.ToList();

        var datachartthpmpro = new object[resultthpmpro.Count];
        int S = 0;
        foreach (var i in resultthpmpro)
        {
            datachartthpmpro[S] = new object[] { i.Value.ToString(), i.Count };
            S++;
        }
        string datathpmpro = JsonConvert.SerializeObject(datachartthpmpro, Formatting.None);
        ViewBag.datajthpmpro = new HtmlString(datathpmpro);

        #endregion

And Finally my Javascript:最后是我的 Javascript:

 <script>
var datathpmpro = '@ViewBag.datajthpmpro';
var datassthpmpro = JSON.parse(datathpmpro);

var datathpmsick = '@ViewBag.datajthpmsick';
var datassthpmsick = JSON.parse(datathpmsick);

 <script type="text/javascript">

// Load the Visualization API and the corechart package.
google.charts.load('current', { 'packages': ['corechart'] });

// Set a callback to run when the Google Visualization API is loaded.
google.charts.setOnLoadCallback(drawChartA);

// Callback that creates and populates a data table,
// instantiates the pie chart, passes in the data and
// draws it.
function drawChartA() {

    // Create the data table.

    var data = new google.visualization.DataTable();
    data.addColumn('string', 'Value');
    data.addColumn('number', 'Count');
    data.addColumn('number', 'Count1');
    data.addRows([[datassthpmpro], [datassthpmsick]]);


    // Set chart options
    var options = {
        'title': 'Holiday Hours Taken Per Month',
        'width': 600,
        'height': 350,
        'hAxis': {title: 'Month Number'},
        'vAxis': {title: 'Holiday Hours Taken'},
        'is3D': true,
        'legend': 'none'
    };

    // Instantiate and draw our chart, passing in some options.
    var chart = new google.visualization.ColumnChart(document.getElementById('chartTHPMpro_div'));
    chart.draw(data, options);
}

I'd like it so I can compare the two querys on the same chart.我喜欢它,这样我就可以在同一个图表上比较两个查询。 Is there a work around for this error?是否有解决此错误的方法? Does it have to be two different querys in order to work?是否必须是两个不同的查询才能工作?

each of your queries has two columns,您的每个查询都有两列,
but the google data table has three columns,但是谷歌数据表有三列,
so that's not going to work.所以这是行不通的。

you'll need to create two separate data tables,您需要创建两个单独的数据表,
then use the join() method to combine them into one.然后使用join()方法将它们合二为一。

var data1 = new google.visualization.DataTable();
data1.addColumn('string', 'Value');
data1.addColumn('number', 'Count');
data1.addRows([datassthpmpro]);

var data2 = new google.visualization.DataTable();
data2.addColumn('string', 'Value');
data2.addColumn('number', 'Count1');
data2.addRows([datassthpmsick]);

var joinedData = google.visualization.data.join(data1, data2, 'full', [[0, 0]], [1], [1]);

then use the joined data table to draw the chart...然后使用连接的数据表绘制图表...

chart.draw(joinedData, options);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM