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

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

Elementary component placed in a single line

Creating a text dropdown menu using the following code: import { Autocomplete, TextField } from '@mui/material' import React, { useState } from 'react' const options = [ 'Never', 'Every Minute', 'Every 2 ...

Angular - The filter method cannot be used on Observables

I have a database with users who need to complete unique homework sessions. Each homework session has its own identifier, name, date, and other details. After fetching all the user's homework from the database, it is stored in a private variable call ...

Getting the route parameter in Angular 2 is easy with the stable router

Currently working with the latest stable Angular 2 RC version. Unfortunately, the documentation for the new router component has yet to be completed. Struggling to retrieve a parameter from a navigated page. Defined routes: @Routes([ {path: '/resu ...

Tips for successfully passing a closure as a parameter in a constructor

I encountered an issue while working with a third-party library where I needed to register my own control. The problem arose when I tried to add another dependency to the control and struggled with passing a closure as a parameter to fulfill the required c ...

Comparing Angular 5 with --aot and Angular 5 with --aot=false configuration settings

I am having an issue with my Angular application using Angular 5.0.0 and rxjs ^5.5.2. When I run the command ng serve --aot=false, everything works fine. However, when I use ng serve --aot, I encounter the following error: core.js:1350 ERROR Error: Uncaug ...

Troubleshooting Angular and ASP.NET Core MVC: Addressing the "Uncaught SyntaxError: Unexpected token '<'" issue with index file references post deployment

My application is built using ASP.NET Core MVC and an Angular UI framework. Everything runs smoothly in the IIS Express Development Environment, but when switching to the IIS Express Production environment or deploying to an IIS host, I encounter issues wi ...

Is the callback for a request always invoked?

When using the npm module request, I often encounter situations where some requests are not called back, leading to various issues. This has raised a question in my mind - is it expected for the request function to always callback? For instance, if my req ...

What's the best way to insert values into data binding within a Typescript/ Angular mat Table?

Objective: Create a dynamic table using JSON data <mat-table class="mat-elevation-z8" *ngIf="carrierRates" [dataSource]="carrierRates"> <ng-container *ngFor="let columnName of columnsList" matColumn ...

Encountering an installation issue with Angular 2 data table

Is there a solution for resolving unmet peer dependencies during the installation of Angular 2 data table? Here is the issue I am encountering while trying to install it. ...

The assignment to 'total' is prohibited as it is either a constant or a property that is read-only within the get total() function

After running the command ng build --prod in my project, I encountered the following error message: "Cannot assign to 'total' because it is a constant or read-only property for function get total(){}" The function causing the issue is: get to ...

Created computed getter and setter for Vue models (also known as "props") using the syntax of Vue Class Component

After exploring the limitations of v-model in Vue 2.x in this Stack Overflow thread, I discovered a way to connect parent and child components using v-model. The solution proposed is as follows: --- ParentTemplate: <Child v-model="formData"> ...

Navigating the interface types between Angular, Firebase, and Typescript can be tricky, especially when working with the `firebase.firestore.FieldValue`

I am working on an interface that utilizes Firestore timestamps for date settings. export interface Album{ album_name: string, album_date: firebase.firestore.FieldValue; } Adding a new item functions perfectly: this.album ...

Steps for synchronously making multiple http calls at various levels in Angular 2 using RxJS

Currently, I am facing a challenge in Angular 2 where I need to make multiple HTTP calls synchronously at different levels using RxJS. Specifically, I have a resolver service that handles 5 API calls - a, b, c, d, and e. The order of execution I require i ...

Is it possible to export all types/interfaces from .d.ts files within multiple folders using index.ts in a React TypeScript project?

In my current React project, I am managing multiple configuration folders: -config -api/ |index.ts |types.d.ts -routes/ |index.ts |types.d.ts ... For example, in the api/index.ts file, I can import necessary types using import {SomeTyp ...

It appears that Typescript mistakenly interprets a class or type as a value, indicating that "'Classname' is being referred to as a value but used as a type here."

I want to pass an Object of a React.Component as "this" to a Child React.Component in the following way: component 1 file: class Comp1 extends React.Component<...,...> { ... render() { return (<Comp2 comp1={this}/> ...

When retrieving an npm module from GitHub, the process includes the download of the ".git" folder

After forking an npm package and making updates to suit my requirements, I modified the dependency in packages.json to link to my GitHub repository. Everything seemed to work fine until npm installed the module along with the git folder (.git). Consequentl ...

Mapped TypeScript type requiring scalar properties and allowing optional objects

I am in need of a TypeScript generic type that has the capability to alter another type so that all scalar properties (such as strings, numbers, booleans, etc.) remain mandatory, while object types become optional. For instance, given the User type below, ...

Issue with TypeScript not detecting exported Firebase Cloud Functions

Dealing With Firebase Cloud Functions Organization I am managing a large number of Firebase Cloud Functions, and in order to keep the code well-structured, I have divided them into separate files for each function category (such as userFunctions, adminFun ...

Utilizing External Libraries Added Through <script> Tags in React

My goal is to develop a Facebook Instant HTML5 application in React. Following their Quick Start guide, Facebook requires the installation of their SDK using a script tag: <script src="https://connect.facebook.net/en_US/fbinstant.6.3.js"></scrip ...

What is the proper way to invoke the correct store 'collection' using ngrx-store?

I'm currently working on a sample app to learn ngrx and I have two different sets of data - one for counters and the other for URLs. Each store is displayed correctly in their respective components, and I can also increment & decrement the counter usi ...