Google Charts,同时使用多个条形图和相关控件

发布于 2024-11-08 02:45:08 字数 6628 浏览 0 评论 0原文

我正在尝试使用具有相关控件的 Google Charts 制作条形图。我遇到的问题涉及以可用于我的任务的格式输入数据。

以下是我要使用的数据示例:

'Option1heading'、'Option2heading'、'Option3heading'、'val1'、'val2'、'val3'、'val4'、'val5'、'val6'

'Row1val1' , '行1val2', '行1val3', 1336060, 1538156, 1576579, 1600652, 1968113, 123345

'Row2val1', 'Row2val2', 'Row2val3', 400361, 366849, 440514, 434552, 393032, 234374

'Row3val1', 'Row3val2', 'Row3val3', 1001582, 1119450, 993360, 1004163, 979198, 578236

'Row4val1', 'Row4val2', 'Row4val3', 997974, 941795, 930593, 897127, 108087, 4893

此示例中的第一行包含我要在“Option1heading”、“Option2heading”和“Option3heading”上过滤的选项。实际上,这些可能类似于“国家”、“地区”、“州”。第二行往后包含数据,“Row1val1”、“Row1val2”、“Row1val3”是过滤器信息(例如“法国”、“北方”、“巴黎”)。

接下来,6 个数值将是该行的单独数据条。在此示例的图例中,这些值等于“val1”-“val6”(根据第一行)。实际上,这些可能是“人口”、“男性”、“女性”、“0-10 岁”等。

这是当前的代码。它“有点”有效,但工作不正常。这是否可能,任何人都可以指出我正确的方向来做到这一点吗?

<html>
<head>
    <!--Load the AJAX API-->
    <script type="text/javascript" src="https://www.google.com/jsapi"></script>
    <script src="http://code.jquery.com/jquery-latest.js"></script>
    <script src="jquery.dump.js"></script>

    <script type="text/javascript">

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

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

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


        var data = new google.visualization.DataTable();

        var raw_data = [['Option1', 'Option2', 'option3', 'val 1', 'val 2', 'val 3', 'val 4', 'val 5', 'val 6'],
                        ['Ford', 's', 'm', 1336060, 1538156, 1576579, 1600652, 1968113, 123345],
                        ['Citroen', 's', 'm', 400361, 366849, 440514, 434552, 393032, 234374],
                        ['BMW', 's', 'm', 1001582, 1119450, 993360, 1004163, 979198, 578236],
                        ['Toyota', 's', 'm', 997974, 941795, 930593, 897127, 108087, 4893]];

        var my_rows = ['Row1', 'Row2', 'Row3', 'Row4', 'Row5', 'Row6'];

        data.addColumn('string', 'Year');
        for (var i = 0; i  < raw_data.length; ++i) {
            data.addColumn('number', raw_data[i][0]);

        }

        data.addRows(my_rows.length);
        for (var j = 0; j < my_rows.length; ++j) {
            data.setValue(j, 0, my_rows[j].toString());
        }

        for (var i = 1; i  < raw_data.length; ++i) {
            for (var j = 3; j  < raw_data[i].length; ++j) {
                data.setValue(j-3, i+1, raw_data[i][j]);
            }
        }

        // Create a dashboard.
        var dashboard = new google.visualization.Dashboard(document.getElementById('dashboard_div'));

        var controlPicker1 = new google.visualization.ControlWrapper({
            'controlType': 'CategoryFilter',
            'containerId': 'control1',
            'options': {
            'filterColumnLabel': 'Ford',
            'ui': {
                'labelStacking': 'horizontal',
                'allowTyping': false,
                'allowMultiple': true
            }
            }
        });

        var controlPicker2 = new google.visualization.ControlWrapper({
            'controlType': 'CategoryFilter',
            'containerId': 'control2',
            'options': {
            'filterColumnLabel': 'Citroen',
            'ui': {
                'labelStacking': 'horizontal',
                'allowTyping': false,
                'allowMultiple': true
            }
            }
        });

        var controlPicker3 = new google.visualization.ControlWrapper({
            'controlType': 'CategoryFilter',
            'containerId': 'control3',
            'options': {
            'filterColumnLabel': 'BMW',
            'ui': {
                'labelStacking': 'horizontal',
                'allowTyping': false,
                'allowMultiple': true
            }
            }
        });

        var barChart = new google.visualization.ChartWrapper({
            'chartType': 'BarChart',
            'containerId': 'chart_div',
            'options': {
            'width': '100%',
            'height': '100%',
            'vAxis': {title: "Year"},
            'hAxis': {title: "Cups"},
            'fontSize': 14,
            'chartArea': {top: 0, right: 0, bottom: 0, height:'100%', width:'70%'}
            },
            // Configure the barchart to use columns 2 (City) and 3 (Population)
        });

        google.visualization.events.addListener(dashboard, 'ready', function() {
            // Dashboard redraw, have a look at how many rows the barChart is displaying
            var numRows = barChart.getDataTable().getNumberOfRows();
            var expectedHeight = (numRows * 60)+50;
            if (parseInt(barChart.getOption('height'), 10) != expectedHeight) {
                // Update the chart options and redraw just it
                Div("chart_div", expectedHeight);
                barChart.setOption('height', expectedHeight);
                barChart.draw();

            }

        });

        // Establish dependencies, declaring that 'filter' drives 'pieChart',
        // so that the pie chart will only display entries that are let through
        // given the chosen slider range.
        dashboard.bind(controlPicker1, controlPicker2);
        dashboard.bind(controlPicker2, controlPicker3);
        dashboard.bind(controlPicker3, barChart);

        // Draw the dashboard.
        dashboard.draw(data);
    }

    function Div(id,h) {

        var div=document.getElementById(id);
        h = (h) + "px";

        var w=parseInt(div.style.width);
        if($(this).width() >= 1200){
            w = 1200 + "px";
        }else{
            w = ($(this).width()-30) + "px";
        }

        $(div).height(h);
        $(div).width(w);
    }

    </script>
</head>

    <style>
        #chart_div { width: 1200px; height: 30000px; }
    </style>

<body>
    <!--Div that will hold the dashboard-->
    <div id="dashboard_div">
    <!--Divs that will hold each control and visualization-->
    <div id="control1"></div>
    <div id="control2"></div>
    <div id="control3"></div>
    <div id="chart_div"></div>
    </div>
</body>
</html>

非常感谢您提供的任何帮助。

I'm trying to make a bar chart using Google Charts with dependent controls. The problem I am having concerns inputting the data in a format that is usable for my task.

Here is an example of the data I want to use:

'Option1heading', 'Option2heading', 'Option3heading', 'val1', 'val2', 'val3', 'val4', 'val5', 'val6'

'Row1val1', 'Row1val2', 'Row1val3', 1336060, 1538156, 1576579, 1600652, 1968113, 123345

'Row2val1', 'Row2val2', 'Row2val3', 400361, 366849, 440514, 434552, 393032, 234374

'Row3val1', 'Row3val2', 'Row3val3', 1001582, 1119450, 993360, 1004163, 979198, 578236

'Row4val1', 'Row4val2', 'Row4val3', 997974, 941795, 930593, 897127, 108087, 4893

The first row in this example contains the options that I want to filter on 'Option1heading', 'Option2heading' and 'Option3heading'. In reality these might be something like 'country', 'region', 'state'. The second row onwards then contains the data, 'Row1val1', 'Row1val2', 'Row1val3' being the filter information (e.g. 'France', 'North', 'Paris').

Following that, the 6 numeric values would be individual bars of data for that row. In the legend for this example these would equal 'val1' - 'val6' (as per the first row). In reality these might be things like 'population', 'Male', 'female', '0-10 years' etc.

Here is the code as it currently stands. It 'kind of' works but is not working correctly. Is this even possible and can anyone point me in the right direction in order to do it?

<html>
<head>
    <!--Load the AJAX API-->
    <script type="text/javascript" src="https://www.google.com/jsapi"></script>
    <script src="http://code.jquery.com/jquery-latest.js"></script>
    <script src="jquery.dump.js"></script>

    <script type="text/javascript">

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

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

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


        var data = new google.visualization.DataTable();

        var raw_data = [['Option1', 'Option2', 'option3', 'val 1', 'val 2', 'val 3', 'val 4', 'val 5', 'val 6'],
                        ['Ford', 's', 'm', 1336060, 1538156, 1576579, 1600652, 1968113, 123345],
                        ['Citroen', 's', 'm', 400361, 366849, 440514, 434552, 393032, 234374],
                        ['BMW', 's', 'm', 1001582, 1119450, 993360, 1004163, 979198, 578236],
                        ['Toyota', 's', 'm', 997974, 941795, 930593, 897127, 108087, 4893]];

        var my_rows = ['Row1', 'Row2', 'Row3', 'Row4', 'Row5', 'Row6'];

        data.addColumn('string', 'Year');
        for (var i = 0; i  < raw_data.length; ++i) {
            data.addColumn('number', raw_data[i][0]);

        }

        data.addRows(my_rows.length);
        for (var j = 0; j < my_rows.length; ++j) {
            data.setValue(j, 0, my_rows[j].toString());
        }

        for (var i = 1; i  < raw_data.length; ++i) {
            for (var j = 3; j  < raw_data[i].length; ++j) {
                data.setValue(j-3, i+1, raw_data[i][j]);
            }
        }

        // Create a dashboard.
        var dashboard = new google.visualization.Dashboard(document.getElementById('dashboard_div'));

        var controlPicker1 = new google.visualization.ControlWrapper({
            'controlType': 'CategoryFilter',
            'containerId': 'control1',
            'options': {
            'filterColumnLabel': 'Ford',
            'ui': {
                'labelStacking': 'horizontal',
                'allowTyping': false,
                'allowMultiple': true
            }
            }
        });

        var controlPicker2 = new google.visualization.ControlWrapper({
            'controlType': 'CategoryFilter',
            'containerId': 'control2',
            'options': {
            'filterColumnLabel': 'Citroen',
            'ui': {
                'labelStacking': 'horizontal',
                'allowTyping': false,
                'allowMultiple': true
            }
            }
        });

        var controlPicker3 = new google.visualization.ControlWrapper({
            'controlType': 'CategoryFilter',
            'containerId': 'control3',
            'options': {
            'filterColumnLabel': 'BMW',
            'ui': {
                'labelStacking': 'horizontal',
                'allowTyping': false,
                'allowMultiple': true
            }
            }
        });

        var barChart = new google.visualization.ChartWrapper({
            'chartType': 'BarChart',
            'containerId': 'chart_div',
            'options': {
            'width': '100%',
            'height': '100%',
            'vAxis': {title: "Year"},
            'hAxis': {title: "Cups"},
            'fontSize': 14,
            'chartArea': {top: 0, right: 0, bottom: 0, height:'100%', width:'70%'}
            },
            // Configure the barchart to use columns 2 (City) and 3 (Population)
        });

        google.visualization.events.addListener(dashboard, 'ready', function() {
            // Dashboard redraw, have a look at how many rows the barChart is displaying
            var numRows = barChart.getDataTable().getNumberOfRows();
            var expectedHeight = (numRows * 60)+50;
            if (parseInt(barChart.getOption('height'), 10) != expectedHeight) {
                // Update the chart options and redraw just it
                Div("chart_div", expectedHeight);
                barChart.setOption('height', expectedHeight);
                barChart.draw();

            }

        });

        // Establish dependencies, declaring that 'filter' drives 'pieChart',
        // so that the pie chart will only display entries that are let through
        // given the chosen slider range.
        dashboard.bind(controlPicker1, controlPicker2);
        dashboard.bind(controlPicker2, controlPicker3);
        dashboard.bind(controlPicker3, barChart);

        // Draw the dashboard.
        dashboard.draw(data);
    }

    function Div(id,h) {

        var div=document.getElementById(id);
        h = (h) + "px";

        var w=parseInt(div.style.width);
        if($(this).width() >= 1200){
            w = 1200 + "px";
        }else{
            w = ($(this).width()-30) + "px";
        }

        $(div).height(h);
        $(div).width(w);
    }

    </script>
</head>

    <style>
        #chart_div { width: 1200px; height: 30000px; }
    </style>

<body>
    <!--Div that will hold the dashboard-->
    <div id="dashboard_div">
    <!--Divs that will hold each control and visualization-->
    <div id="control1"></div>
    <div id="control2"></div>
    <div id="control3"></div>
    <div id="chart_div"></div>
    </div>
</body>
</html>

Many thanks in advance for any help you can provide.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夜吻♂芭芘 2024-11-15 02:45:08

JSON 数据结构应该是这样的:

{
    "cols": [ {"id":"Col1", "label":"", "type":"date"} ],
    "rows":
    [
        { "c": [ {"v":"a"}, {"v":"Date(2010,10,6)"} ] },
        { "c": [ {"v":"b"}, {"v":"Date(2010,10,7)"} ] }
    ]
}

中有更多信息google.visualization.DataTable 参考文档。

The JSON data structure should be something like this:

{
    "cols": [ {"id":"Col1", "label":"", "type":"date"} ],
    "rows":
    [
        { "c": [ {"v":"a"}, {"v":"Date(2010,10,6)"} ] },
        { "c": [ {"v":"b"}, {"v":"Date(2010,10,7)"} ] }
    ]
}

There's more info in the google.visualization.DataTable reference docs.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文