The ng2-chart linechart will only display the first value that is added

My line chart is experiencing a strange issue where only the first value added after the initial values will display properly. Subsequent values are getting cut off for some reason.

https://i.sstatic.net/pjW9y.png

Although I have checked out a similar issue on Stack Overflow and ensured that I am using labels, the problem still persists.

I have created a version of the issue on Plunker. Here is a snippet of my code:

Template:

<div style="display: block">
        <canvas baseChart
                [options]="chartOptions"
                [data]="chartData"
                [labels]="chartLabels"
                [chartType]="chartType"></canvas>
    </div>

    <button (click)="addRandomValue()">Add Random value</button>
    

Component:

export class ChartComponent implements OnInit {
        public chartType: string;

        public chartData: number[];
        public chartLabels: string[];

        public chartOptions: any = {
            responsive: true,
            maintainAspectRatio: false
        };

        ngOnInit(): void {
            this.chartType = 'line';
            this.chartLabels = [ '1', '2', '3' ];
            this.chartData = [ 0, 10, 20 ];
        }

        addRandomValue(): void {
            this.chartData.push(Math.random() * 10 + 10);
            this.chartLabels.push(this.chartData.length + '');

            // weird workaround for refreshing data, works fine
            // for all other chart types I tried it on
            this.chartData = this.chartData.slice();
            this.chartLabels = this.chartLabels.slice();
        }
    }
    

Is there a known solution for this issue, or is it possibly a bug?

Answer №1

To solve the issue, simply remove the slice of the labels.

So, your addRandomValue() function should be modified as follows:

addRandomValue(): void {
    this.chartData.push(Math.random() * 10 + 10);
    this.chartLabels.push(this.chartData.length + '');

    // a strange workaround to refresh data, but it works well
    // with all other types of charts I tested
    this.chartData = this.chartData.slice();
}

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

Having trouble importing a CSS file into a Vue.js single file component?

I am attempting to include 2 CSS files within the style tag of my Vue.js single-file component. <style > @import '../assets/styles/jquery-ui.js.css'; @import '../assets/styles/jquery-ui.css'; </style> However, I am ...

The pagination in React using React Query will only trigger a re-render when the window is in

Currently, I am utilizing React-Query with React and have encountered an issue with pagination. The component only renders when the window gains focus. This behavior is demonstrated in the video link below, https://i.sstatic.net/hIkFp.gif The video showc ...

Guide on creating elements dynamically with the ngModel attribute in Ionic

I am currently working on dynamically creating an ion-input element when the user clicks on the "+" sign. My goal is to then insert all the input values into an array. While I have successfully created the inputs, I am facing an issue with assigning the [( ...

Ensuring Code Execution Order in NODE.JS

I've encountered an issue with my code that involves parsing a pcap file, storing the data in an array data = [], and then writing it to a JSON file: var fs = require("fs"); var pcapp = require('pcap-parser'); var hex = ""; var data = []; v ...

Is it possible to create a button in one tab that links directly to a specific tab within a tabGroup in a different tab's window?

Within my tab group, there are 5 tabs including the "Home" screen. I want to allow users to easily navigate back to the home screen by clicking a button within any tab's window. The current approach I've tried involves creating a new window ever ...

Tips for addressing Navbar collision with body content or other pages using CSS

I have successfully created a navigation bar using CSS and JS. The image below illustrates the example of my navigation bar: https://i.sstatic.net/2l4gj.png The issue I am facing is that when I select "MY ACCOUNT," it displays some content. However, upon ...

After refreshing the page, the eventBus in Vue.js fails to emit the property

Is there a way to pass a property from one component to another, even if they are siblings, after the page has been refreshed? I want this to happen automatically on page load. To achieve this, I am using an eventBus: EventBus.$emit('generatedSum&apo ...

Updating the quantity of a Stripe subscription in Node.js: A Step-by-Step Guide

I'm having trouble modifying the quantity of items in an existing Stripe subscription. Whenever I attempt to update it, I encounter the following error: "error": { "message": "Invalid array", "param": &q ...

Using Vuex: Bypassing Action and triggering Mutation directly within Component

When working with a vue.js app and utilizing vuex as the state management store, one may need to update a property's value in vuex. This can be achieved through two methods: One can dispatch an action method, which will then commit a mutation to a ...

Issue with Angular 2 AOT Compilation when trying to access formArray

This is the formGroup I created this.createOrderForm = this.fb.group({ items: this.fb.array([]) }); To add an item on button click addItem() { const control = <FormArray>this.createOrderForm.controls['items']; const a ...

Utilizing the Google Maps JavaScript API in npm to generate route directions

After not working with Javascript for a couple of years, I decided to try loading the Google Maps Javascript API npm package in order to display a map and draw directions between two points (Lille and Biarritz, passing through Paris and Bordeaux). Unfortun ...

What is the best way to synchronize the heights of two div containers when zooming in the browser causes one container to expand, ensuring they both remain aligned at the bottom

Greetings! I am currently working on designing a webpage that consists of two parts: a left section with buttons and a right section with a table view. Everything looks perfect until I zoom in my browser to 150% or more, causing the content in the table to ...

There seems to be an issue with transitioning the React.js page

I'm currently working on managing the page with react-hook, react-router-dom, and redux. The login functionality has been implemented, and I've written the code to redirect to the main page upon successful login. To achieve this, I utilized hi ...

Retrieve the Axios response without interruption, even in the event of an exception, when making API calls with multiple files

Currently, as a beginner diving into Vue.js, I am exploring the world of frontend development by integrating Vue with my Laravel backend. To streamline my API calls and make them more organized for different models, I decided to create a separate file name ...

The tooltip is obscured by the table row

I'm having trouble with a simple CSS fix and can't seem to figure out why. The z-index and overflow visibility properties are not working as expected. I just need 'In Progress' to be displayed above everything else. Some things I' ...

Unsuccessful CORS on whateverorigin.org with YQL

Despite trying all three methods, I keep getting an error regarding cross domain access denied. Previously, I had successfully used YQL in different parts of my applications, but now it seems to have stopped working as well. JSFIDDLE <script> $(fun ...

Halting execution: Trying to use the keyword 'import' which is not allowed here

0. April 2023: error cannot be reproduced anymore see here The error is no longer replicable due to bug fixes in react-scripts 5.0.1. 1 Even though the error is gone, the question and my self-answer still seem relevant to Angular users and others as we ...

Tips for correctly decorating constructors in TypeScript

When a class is wrapped with a decorator, the superclasses lose access to that classes' properties. But why does this happen? I've got some code that demonstrates the issue: First, a decorator is created which replaces the constructor of a cla ...

The parent element of a 3D div is causing issues with hovering and clicking on the child elements

In my scenario, the parent div is transformed in 3D with rotation, causing it to move to the backside. The issue arises with the child div containing a button that becomes unclickable due to the parent div position. Setting backface-visibility to hidden al ...

Updated the application.js file by removing a function following a layout change

I'm currently working on a project using Rails version 6.0.3.1. In this app, the layout changes are dependent on the method being called, as shown in the code snippet below: def index @groups = Group.all render layout: 'application' end ...