简体   繁体   中英

Bubbles chart: how to avoid bubbles being cut off? google visualization

I'm using google visualization for bubble chart, data to x axis and Y axis is dynamic. I'm facing issue here is that bubbles get cut-off and there size is also not uniform.

using following options

options = {
                'title': 'Chart',
                'width': '100%',
                'height': 550,
                legend: {position: 'right'},
                vAxis: {
                        title: 'Score',
                    viewWindow: {
                        min: 0,
                        max: 5
                    },

                    baselineColor: {
                       color: '#4c78c6', 
                    },
                    sizeAxis : {minValue: 0,  maxSize: 15},
                    ticks: [1, 2, 3, 4, 5]
                },
                hAxis: {
                    title: 'Years',
                    baselineColor: {
                       color: '#4c78c6', 
                    }
                },
                sizeAxis : {minValue: 0,  maxSize: 15},
                bubble: {
                    textStyle: {
                        color: 'none',
                    }
                },
                tooltip: {
                    isHtml: true,
                },
                colors: colors,
                chartArea: { width: "30%", height: "50%" }
            };

EDIT data passed to

var rows = [
    ['ID','YEAR','SCORE', 'AVG1', 'AVG']
    ['Deka marc', 2.5, 5, '76-100%', 100]
    ['Max cala',  28.2,3.4,'76-100%', 77]
    ['shane root',4.2, 1, '0-25%', 0]
]
var data = google.visualization.arrayToDataTable(rows);

from above array I'm removing element 3 on hover as do not wish to show in tooltip. AVG1 column is for legend

getting o/p like this 在此处输入图片说明

use

var rangeX = data.getColumnRange(1);

to know the range of column and then use

hAxis: {
  viewWindow: { 
            min: rangeX.min-10,
            max: rangeX.max+10
            }
       },
}

do similarly for yAxis

https://jsfiddle.net/geniusunil/nt4ymrLe/4/

Add inside hAxis the viewWindow option. This is a sample code:

viewWindow: { 
            min: 0,
            max: 40
            }

You can change max according your biggest value in your dataset you want to show. I mean if is 30 (as in your example) you can set max: 40, or if is 75 you can set max equal to 85.

JSfiddle here .

to find the range of each axis dynamically, use data table method --> getColumnRange

then you can use the ticks option to increase the range.

var rangeX = data.getColumnRange(1);
var ticksX = [];
for (var i = (Math.floor(rangeX.min / 10) * 10); i <= (Math.ceil(rangeX.max / 10) * 10); i = i + 10) {
  ticksX.push(i);
}

var rangeY = data.getColumnRange(2);
var ticksY = [];
for (var i = Math.floor(rangeY.min) - 1; i <= Math.ceil(rangeY.max) + 1; i++) {
  ticksY.push(i);
}

to make the size of the bubble uniform, set minSize & maxSize to the same value.

sizeAxis : {minSize: 15,  maxSize: 15},

see following working snippet...

 google.charts.load('current', { packages: ['corechart'] }).then(function () { var rows = [ ['ID','YEAR','SCORE', 'AVG1', 'AVG'], ['Deka marc', 2.5, 5, '76-100%', 100], ['Max cala', 28.2,3.4,'76-100%', 77], ['shane root',4.2, 1, '0-25%', 0] ]; var data = google.visualization.arrayToDataTable(rows); var rangeX = data.getColumnRange(1); var ticksX = []; for (var i = (Math.floor(rangeX.min / 10) * 10); i <= (Math.ceil(rangeX.max / 10) * 10); i = i + 10) { ticksX.push(i); } var rangeY = data.getColumnRange(2); var ticksY = []; for (var i = Math.floor(rangeY.min) - 1; i <= Math.ceil(rangeY.max) + 1; i++) { ticksY.push(i); } var options = { title: 'Chart', width: '100%', height: 550, legend: {position: 'right'}, vAxis: { title: 'Score', baselineColor: { color: '#4c78c6', }, sizeAxis : {minSize: 15, maxSize: 15}, ticks: ticksY }, hAxis: { title: 'Years', baselineColor: { color: '#4c78c6', }, ticks: ticksX }, sizeAxis : {minSize: 10, maxSize: 10}, bubble: { textStyle: { color: 'none', } }, tooltip: { isHtml: true, }, //colors: colors, chartArea: { width: "30%", height: "50%" } }; var chart = new google.visualization.BubbleChart(document.getElementById('chart_div')); chart.draw(data, options); });
 <script src="https://www.gstatic.com/charts/loader.js"></script> <div id="chart_div"></div>

Add 10% of the difference between the max and min

vAxis: {
         viewWindow: { 
            min: rangeY.min - ((+rangeY.max - rangeY.min) * 10 / 100),
            max: rangeY.max + ((+rangeY.max - rangeY.min) * 10 / 100)
            }
       },

hAxis: {
        viewWindow: { 
            min: rangeX.min - ((+rangeX.max - rangeX.min) * 10 / 100),
            max: rangeX.max + ((+rangeX.max - rangeX.min) * 10 / 100)
            }
       },

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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