Angular 4 scatter chart with multiple data points using Google charts

I'm currently developing a scatter graph in Angular 4 using ng2-google-charts from https://www.npmjs.com/package/ng2-google-charts

It seems like this is essentially a wrapper for Google Chart Service. The graph looks great with a few values, but when I try to plot a larger dataset (around 100 values), the graph ends up looking like this https://i.sstatic.net/5Gzfn.png

As you can see, the axes are too small to display the full range of values, making it impossible to see any value on the x-axis.

Is there a solution to this issue?

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

Attempt: I've tried manually setting the width and height of the chart using configuration, which did work:

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

However, is there a way for the chart to automatically adjust its size to fit all values beautifully?

Answer №1

Here is a helpful tip: utilize the option chartArea to customize the chart's area.

You can expand the chart's area to fill the container, while leaving space on all sides for titles and labels.

// adjust chart area dimensions
chartArea: {
  height: '100%',
  width: '100%',
  top: 48,        // provide space at the top (adjust as needed)
  left: 48,       // provide space on the left side
  right: 16,      // provide space on the right side
  bottom: 48      // provide space at the bottom
},

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

In this scenario, it appears that the chart is vertically narrow, so you may need to decrease the font size of the axis labels.

// modify font size for each axis
hAxis: {
  textStyle: {
    fontSize: 9
  }
},
vAxis: {
  textStyle: {
    fontSize: 9
  }
},

Take a look at the following working example:

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 = {
    // adjust chart area dimensions
    chartArea: {
      height: '100%',
      width: '100%',
      top: 32,        // provide space at the top (adjust as needed)
      left: 24,       // provide space on the left side
      right: 12,      // provide space on the right side
      bottom: 24      // provide space at the bottom
    },

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

    // modify font size for 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;
  overflow: hidden;
  padding: 0px;
}

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

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Is there an alternative course of action since determining if Observable is empty is not feasible?

I am diving into Angular 11 and exploring the world of Observables and Subjects as a beginner. Within my application, I have a mat-autocomplete component that organizes its results into categories. One of these categories is dedicated to articles, and I&a ...

What is the most effective approach to handling dependencies for an AngularJs 1.x web application?

Right now, I have a bunch of script elements pointing to cdn/local files, which isn't ideal. I'm considering declaring all necessary packages using npm/yarn and serving cdn files with self-hosted fallback. Is this a good idea? Would it be bette ...

What could be causing the invalid_client error in response to this POST request I am making?

I am currently trying to establish a connection with an external API that has specific specifications: Host: name.of.host Content-Type: application/x-www-form-urlencoded Cache-Control: no-cache Through the utilization of the request module from npm, I am ...

Tips for validating the request body prior to sending in Angular unit testing

I'm experiencing some difficulties creating a unit test. Despite reading numerous articles, none have proven to be helpful in simplifying the process. The specific function I need to test returns an Observable. My main objective is to ensure that myB ...

Vue textarea not accepting null values

My current setup includes the following dependencies: - "vue": "3.2.26", - "vee-validate": "4.5.6", - "typescript": "4.5.4" While working on a textarea field in vue3, I encountered an issue Here's a snippet with vee-validate integration import { Fie ...

Angular 14 is experiencing issues with NgRx Store failing to properly recognize the payload

The issue lies in TypeScript not recognizing action.payload.index as a valid property. I am unsure how to resolve this problem and make the 'index' visible in my project. shopping-list.actions.ts import {Action} from "@ngrx/store"; im ...

Tips for displaying dynamic data using innerHTML

Recently, I set out to implement an infinite scroll feature in Angular 2 using JavaScript code (even though it may seem a bit unusual to use JavaScript for this purpose, it's actually working perfectly). Initially, I was able to create an infinitely s ...

This TypeScript error occurs when the props are not compatible and cannot be assigned to

Hello fellow Internet dwellers! I am a novice in the world of coding and TypeScript, seeking some assistance here. I am attempting to extract an array of objects from props, transform it into a new object with specific information, and then return it - ho ...

How can I position ports on the right side of a graph element in JointJS?

I'm in the process of developing an Angular application that allows users to dynamically create graphs. Currently, I am working on a function that adds ports to vertices and I want the user to be able to select the position of the port (right, left, t ...

Is it possible to use data retrieved from an API in Angular Url Matcher to determine if the matcher should be considered true or not based on the API response?

The following question was posed during a micro-learning video session focused on Custom Angular URL Matchers in the Spartacus Storefront Is it possible to extract prefix data from an API response and determine whether a matcher is true based on that dat ...

No routes found for URL segment 'master-system/update-system'

I seem to be missing something crucial while attempting to set up authorization for child routes. Specifically, I am trying to restrict access based on user roles - Admin and notAdmin. I have implemented an authentication check in the login.component.ts fi ...

Encountering a problem while attempting to incorporate SQLite into a Node.js environment

I've encountered issues while attempting to import SQLite into node. Here is my import statement: import * as sqlite from './sqlite'; But unfortunately, I am receiving the following error message: node:internal/process/esm_loader:74 int ...

The dilemma between installing Electron or installing Electron-Builder: which one

When it comes to installing Electron for an Electron app with React, the method can vary depending on the tutorial. Some tutorials use electron-builder while others do not, but there is little explanation as to why. First: npx create-react-app app cd app ...

Encountering an issue when trying to download a PDF from an Angular 6 frontend using a Spring Boot API - receiving an error related to

When I directly call the Spring Boot API in the browser, it successfully creates and downloads a PDF report. However, when I try to make the same GET request from Angular 6, I encounter the following error: Here is the code snippet for the Spring Boot (Ja ...

Sending emails using Angular 4 and PHP

Having some trouble sending email from the PHP send function. It's sending an email, but not populating it with any data - it's coming through as null. This is my Send Service in Angular. The message here contains all the data from my form: ...

Ensuring the Presence of a Legitimate Instance in NestJS

I have been working on validating my request with the Product entity DTO. Everything seems to be in order, except for the 'From' and 'To' fields. The validation works correctly for the Customer and Type fields, but when incorrect data i ...

Cloud9 encounters NPM update issue with error message 'npm log' module cannot be found

Hey there! This morning I kicked off a brand new project in Cloud9, and encountered an update prompt when running "npm init" which I complied with. After what seemed like a successful installation, I proceeded to run "npm install" for some packages, only ...

How can I access the object that created an interval from inside an interval callback function?

I am facing a challenge with JavaScript as a beginner. I am working on a React App and trying to add a timer feature to it. I have successfully created the timer functionality, but the issue lies in the timer callback (similar to this query: setInterval in ...

Differences between Pipe and Tap when working with ngxsWhen working with

While experimenting with pipe and subscribe methods, I encountered an issue. When using pipe with tap, nothing gets logged in the console. However, when I switch to using subscribe, it works perfectly. Can you help me figure out what I'm doing wrong? ...

Angular Material calendar tool customization for designated input

Is it possible to individually control the format of input for a datepicker without affecting the format for the entire module? <input matInput [matDatepicker]="dp" [formControl]="date" [format]="'DD/MM/YYYY'"> <-- Can this be done? < ...