Different approach to including labels on scatter plot without relying on "chartjs-plugin-datalabels"

I need help adding labels to the pink scatter dots without affecting the green bars in the horizontal bar chart generated by ngchart. You can see an image of the chart here.

Whenever I try to include the following code:

import ChartDataLabels from "chartjs-plugin-datalabels";

I encounter these errors:

     Error: node_modules/chartjs-plugin-datalabels/types/context.d.ts:1:16 - error TS2724: Module '"../../@types/chart.js"' has no exported member 'ChartDataset'. Did you mean 'ChartDataSets'?

1 import {Chart, ChartDataset} from 'chart.js';
                 ~~~~~~~~~~~~
node_modules/chartjs-plugin-datalabels/types/index.d.ts:1:20 - error TS2305: Module '"../../@types/chart.js"' has no exported member 'Plugin'.

1 import {ChartType, Plugin} from 'chart.js';
                     ~~~~~~

Does anyone know of an alternative library I can use to add labels to the pink scatter dots or a solution to resolve these errors?

Thank you in advance!

UPDATE:

After @LeeLenalee's suggestion, I have installed version 1 of the library, but I'm not seeing any labels next to the dots. Can anyone help me understand why? Here is the code for my chart:

  public disproportionChartData = [
    {
      label: "Disproportionality",
      data: [
        { y: 80, x: 80 },
        { x: 40, y: 40 },
        { y: 0, x: 0 },
        { y: -40, x: -40 },
        { y: -80, x: -80 },
      ],
      borderColor: "pink",
      backgroundColor: "pink",
      pointRadius: 10,
      type: "scatter",
      xAxisID: "x2",
      yAxisID: "y2",
      datalabels: {
        display: true,
        align: "right",
        color: "black",
        formatter: function (value, context) {
          return context.chart.data.labels[context.dataIndex];
        },
      },
    },
    {
      type: "horizontalBar",
      label: "Population 1 %",
      data: [-10, -40, -80, -70, -20],
    },
    {
      type: "horizontalBar",
      label: "Population 2 %",
      data: [10, 20, 30, 50, 90],
    },
  ];
  public disproportionChartLabels = [
    "Type 1",
    "Type 2",
    "Type 3",
    "Type 4",
    "Type 5",
  ];
  public disproportionChartPlugins = [ChartDataLabels];
  public disproportionChartOptions = {
    responsive: true,
    legend: {
      position: "top",
    },
    title: {
      display: false,
    },
    tooltips: {
      callbacks: {
        title(items) {
          const ds2 = items.filter((element) => element.datasetIndex === 1);
          return ds2[0].label;
        },
      },
    },
    plugins: {
      datalabels: {
        display: false,
      },
    },
    scales: {
      xAxes: [
        {
          id: "x2",
          type: "linear",
          position: "top",
          ticks: {
            callback(value) {
              return Math.abs(value);
            },
            min: -100,
            max: 100,
          },
        },
      ],
      yAxes: [
        {
          stacked: true,
          position: "left",
          ticks: {
            callback: function (value, index, values) {
              return value < 0 ? -value : value;
            },
          },
        },
        {
          id: "y2",
          stacked: true,
          position: "right",
          ticks: {
            callback(value) {
              return value;
            },
            min: -100,
            max: 100,
          },
        },
      ],
    },
  };

Answer №1

It appears you are utilizing V2 of the datalabels plugin alongside V2 of chart.js, which is not compatible as it is designed for chart.js v3. To address this issue, consider switching to V1 of the datalabels plugin to ensure compatibility with your current version of chart.js.

Answer №2

In reference to the query Including Scatter Dots in Horizontal Stacked Bar Chart using Chart.js, it is crucial to utilize Datalabels version 1. Both the provided example code and codepen demonstration rely on Datalabels version 1. It appears that the plugin registration is missing (refer to the snippet code in the linked question).

const ctx = document.getElementById("myChart");
Chart.plugins.register(ChartDataLabels); // --> It seems like this line is not included
const myChart = new Chart(ctx, {

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

Efficiently process and handle the responses from Promise.all for every API call, then save the retrieved data

Currently, I am passing three API calls to Promise.all. Each API call requires a separate error handler and data storage in its own corresponding object. If I pass test4 to Promise.all, how can I automatically generate its own error and store the data in ...

Issue with NullInjectorError: Failure to provide a custom component - Attempting to add to providers without success

I keep encountering errors during my test attempts... Despite looking into similar issues, I am still facing problems even after adding my custom LoginComponent to app.module.ts providers. It is already included in the imports section. app.module.ts @Ng ...

What is the best way to make the current tab stand out?

I have implemented a TabHeader component to create a dynamic Tab Menu that displays table contents based on months. The loop runs from the current month back to January, and the content is updated dynamically through an API call triggered by changes in the ...

Collision Detection in Kendo UI Menu

When working with TypeScript, it ensures that the parameters are of the correct type. For Kendo's menu, in order to disable the popupCollision property, you need to set it to false. However, this property actually accepts a string as its value, so if ...

I need guidance on retrieving items from the useRouter() hook when utilizing Next.js with the App Router approach

This code snippet demonstrates how to use Pages Router in React import { useRouter } from "next/router"; import React from "react"; function ShowroomListings({}) { const router = useRouter(); const { listingId } = router.query as ...

problem encountered while attempting to drag and drop list elements on a web application utilizing dhtmlx 5.0 and wijmo grid

My web application utilizes Dhtmlx 5.0, Wijmo grid, and Typescript. One feature of the app is a dialog box that displays a list of items which can be rearranged using drag and drop functionality. This feature works without any issues on Windows PCs but enc ...

Executing the routing component prior to any other tasks

Having an issue where the ProductsService is fetching data from the server and storing it in an Array. The ProductsComponent serves as the parent component, while the ProductsListComponent and ProductListItemsComponent are its children components. The flow ...

Is there a way to maintain formdata between components in Angular?

With only the tools of @Input, @Output, routing, and activatedRoute at my disposal, I set out to create a dynamic application. In this project, there are two crucial components: addbook and showbook. The addbook component features a form with a submit bu ...

Error: Unable to load chunk.js in Angular 7

After upgrading to Angular 7, I've been diving into the world of Lazy loaded modules. However, despite my efforts, I can't seem to find #chunk.js anywhere in the network tab when I click on components within the lazy loaded module. Even when Con ...

Guide on deploying multiple applications under a single URL with S3 on Amazon Web Services

I am working on deploying multiple Angular and React apps on AWS through S3. Currently, one of my Angular apps is successfully running when accessing mysite.com. It functions properly as intended. I am looking for a way to load different apps based on th ...

Firebase Angular encountering issues with AngularFirestoreModule

I have encountered a challenge with Firebase authentication in my Angular applications. Due to updated read and write rules that require auth!=null, I successfully implemented Firebase authentication in one of my apps using Angular 13. Now, I am trying to ...

The mat-paginator fails to display the page information

Material paginator is not displaying the page information correctly. In the official documentation, it shows the page info as 'Page 1 of 20', but when I run their code locally or on Stackblitz, the output is different. Instead, it shows the num ...

Obtain the current time for a specific user

I'm currently struggling with obtaining the accurate 'trusted' user time, in order to prevent any cheating through manipulation of their computer's clock. Whether I utilize a basic date object, moment timezone, or even Google timezone ...

Unidentified Angular NgRX action

After building my Angular application using NgRX, I integrated actions, reducers, services, and effects. However, when trying to access the action in the component code, I encountered the following error: Error: Property 'GetEmployees' does no ...

Ways to create a unified component from two similar but distinct components by assigning a value

Upon reviewing my menu setup, I realized that the submenus in my project are essentially the same component with only a single index differentiating them. In order to simplify the structure, I am looking to consolidate these components into one. HTML < ...

Having trouble retrieving information from combineLatest in Angular?

I'm having some trouble with fetching files to include in the post logs. It seems that the data is not being passed down the chain correctly when I attempt to use the pipe function after combining the latest data. This code snippet is part of a data r ...

The GIPHY API object returns no results

Utilizing Angular 2 to fetch data from the GIPHY API. export class ListaGifsComponent { gifs : Object[] = []; urlBase = "http://api.giphy.com/v1/gifs/search?q="; termoPesquisado = "ryan+gosling"; key = "O8RhkTXfiSPmSCHosPAnhO70pdnHUiWn"; ...

Tips on sending a list via @Input using Angular5

I was seeking guidance on how to pass a list through an input. In my parent component, I have the following: list: Hero[] = [{Id: 2, Name: "Sll"}, {Id: 3, Name: "Sldsadasd"}] and in the HTML: <app-add-hero list="{{list}}" hero={{hero}}></app-ad ...

Nest JS is currently experiencing difficulties with extending multiple classes to include columns from other entities

Currently, I am immersed in a new project that requires me to enhance my entity class by integrating common columns from another class called BASEMODEL. import { Index, PrimaryGeneratedColumn } from "typeorm"; export class BaseModel { @Prima ...

Ways to make a component gradually appear and disappear in an Angular application

I have developed a task management application using Angular and I wanted to implement a fading effect for each task when it is created and before it is deleted. Despite successfully applying the fade in effect at the todo-item component level, I encounter ...