Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

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

1 Answer

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>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...