Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
118 views
in Technique[技术] by (71.8m points)

javascript - Resizing google visualization with ChartEditor

is it possible to set the width and height of a chart when using the google visualization ChartEditor?

I require the chart to be 100% the width of the page and then set the height in pixels, currently when I set the width and height in the options they are being ignored.

the code that I have worked on so far is below:

<script type="text/javascript">

        google.load("visualization", "1", { packages: ["corechart", "controls", "charteditor"] });

        google.setOnLoadCallback(loadEditor);
        var chartEditor = null;

        window.onresize = function() {
            loadEditor();
        };

        function loadEditor() {

            var data = google.visualization.arrayToDataTable([@Html.Raw(@ViewBag.ChartData)]);

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

            var rangeSlider = new google.visualization.ControlWrapper({
                'controlType': 'CategoryFilter',
                'containerId': 'filter_div',
                'options': {
                    'filterColumnLabel': 'VAR1',
                    'ui': {
                        'label': 'Years'
                    }
                }
            });
            var wrapper = new google.visualization.ChartWrapper({
                chartType: 'ColumnChart',
                containerId: 'chart_div',
                dataTable: data
            });
            chartEditor = new google.visualization.ChartEditor();
            google.visualization.events.addListener(chartEditor, 'ok', redrawChart);
            chartEditor.openDialog(wrapper, {});
            dashboard.bind(rangeSlider, wrapper);
            dashboard.draw(data);
        }

        function redrawChart() {
            chartEditor.getChartWrapper().draw(document.getElementById('chart_div'));

        }
    </script>

the chart also incorporates a CategoryFilter linked with a dashboard is required to work with the chart displayed.

Currently when the chart is rendered it is fairly small.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Something like this should work, I switched out the data and modified the slider to get the snippet to work.

        google.load("visualization", "1", { packages: ["corechart", "controls", "charteditor"] });

        google.setOnLoadCallback(loadEditor);

        window.addEventListener('resize', redrawChart, false);

        var chartEditor;
        var data;
        var dashboard;
        var rangeSlider;
        var wrapper;

        function loadEditor() {
            data = google.visualization.arrayToDataTable([
              ['Element', 'Density'],
              ['Copper', 8.94],
              ['Silver', 10.49],
              ['Gold', 19.30],
              ['Platinum', 21.45]
            ]);

            dashboard = new google.visualization.Dashboard(document.getElementById('dashboard_div'));

            rangeSlider = new google.visualization.ControlWrapper({
                'controlType': 'CategoryFilter',
                'containerId': 'filter_div',
                'options': {
                    'filterColumnLabel': 'Density',
                    'ui': {
                        'label': 'Density'
                    }
                }
            });

            wrapper = new google.visualization.ChartWrapper({
                chartType: 'ColumnChart',
                containerId: 'chart_div',
                dataTable: data
            });

            chartEditor = new google.visualization.ChartEditor();
            google.visualization.events.addListener(chartEditor, 'ok', drawChart);
            chartEditor.openDialog(wrapper, {});
        }

        function drawChart() {
            wrapper = chartEditor.getChartWrapper();
            redrawChart();
        }

        function redrawChart() {
            var height;
            var width;

            height = '200px';
            width = Math.min(document.documentElement.clientWidth, window.innerWidth || 0) + 'px';

            wrapper.setOption('height', height);
            wrapper.setOption('width', width);

            dashboard.bind(rangeSlider, wrapper);
            dashboard.draw(data);
        }
<script src="https://www.google.com/jsapi"></script>

<div id="dashboard_div">
  <div id="filter_div"></div>
  <div id="chart_div"></div>
</div>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...