1
votes

I am building a scatter graph in angular 4 using ng2-google-charts from https://www.npmjs.com/package/ng2-google-charts

So actually i think this is a wrapper of google chart service The graph looks fine with few values. However , i try with a graph with lots of value ( around 100). The graph become like thisscatter graph with multiple values

As you see, the two axises is too small to display all the range for all of the values. ( cannot see any value on the x-axis)

Is there anyway to solve this problem ??

Refer to this docs : https://developers.google.com/chart/interactive/docs/gallery/scatterchart#configuration-options

Attempt: i have try to set the width and height of the chart manually using configuration and this works

chartArea:{width:"80%"},
      height: 500,
      width: 1350

But is there anyway that the chart will auto define a size to fit all the values beautifully

1

1 Answers

0
votes

try using option --> chartArea

you can stretch the chart's area to the edges of the container,
then leave room on top, left, bottom, & right for titles and labels...

// set chart area size
chartArea: {
  height: '100%',
  width: '100%',
  top: 48,        // leave room on top edge (adjust accordingly)
  left: 48,       // leave room on left edge
  right: 16,      // leave room on right edge
  bottom: 48      // leave room on bottom edge
},

// set overall chart size
height: '100%',
width: '100%',

in this case, looks like the chart is narrow in height,
you may also need to reduce the font size on the axis labels...

// adjust font size on each axis
hAxis: {
  textStyle: {
    fontSize: 9
  }
},
vAxis: {
  textStyle: {
    fontSize: 9
  }
},

see following working snippet...

google.charts.load('current', {
  packages: ['corechart']
}).then(function () {
  var statsData = new google.visualization.DataTable();
  statsData.addColumn('number', 'x');
  statsData.addColumn('number', 'y');
  statsData.addRows([
    [0, 0],   [1, 10],  [2, 23],  [3, 17],  [4, 18],  [5, 9],
    [6, 11],  [7, 27],  [8, 33],  [9, 40],  [10, 32], [11, 35],
    [12, 30], [13, 40], [14, 42], [15, 47], [16, 44], [17, 48],
    [18, 52], [19, 54], [20, 42], [21, 55], [22, 56], [23, 57],
    [24, 60], [25, 50], [26, 52], [27, 51], [28, 49], [29, 53],
    [30, 55], [31, 60], [32, 61], [33, 59], [34, 62], [35, 65],
    [36, 62], [37, 58], [38, 55], [39, 61], [40, 64], [41, 65],
    [42, 63], [43, 66], [44, 67], [45, 69], [46, 69], [47, 70],
    [48, 72], [49, 68], [50, 66], [51, 65], [52, 67], [53, 70],
    [54, 71], [55, 72], [56, 73], [57, 75], [58, 70], [59, 68],
    [60, 64], [61, 60], [62, 65], [63, 67], [64, 68], [65, 69],
    [66, 70], [67, 72], [68, 75], [69, 80]
  ]);

  var options = {
    // set chart area size
    chartArea: {
      height: '100%',
      width: '100%',
      top: 32,        // leave room on top edge (adjust accordingly)
      left: 24,       // leave room on left edge
      right: 12,      // leave room on right edge
      bottom: 24      // leave room on bottom edge
    },

    // set overall chart size
    height: '100%',
    width: '100%',

    // adjust font size on each axis
    hAxis: {
      textStyle: {
        fontSize: 9
      }
    },
    vAxis: {
      textStyle: {
        fontSize: 9
      }
    },

    legend: {
      position: 'top'
    }
  };

  var chart = new google.visualization.ScatterChart(document.getElementById('chart_div'));

  drawChart();
  window.addEventListener('resize', drawChart, false);
  function drawChart() {
    chart.draw(statsData, options);
  }
});
html, body {
  height: 100%;
  margin: 0px 0px 0px 0px;
  overflow: hidden;
  padding: 0px 0px 0px 0px;
}

.chart {
  height: 100%;
}
<script src="https://www.gstatic.com/charts/loader.js"></script>
<div class="chart" id="chart_div"></div>