Chart.js does not support applying custom configurations to the x-axis

Resolved on update 26-04-2021 (see below)

I have encountered difficulties with defining the x-axis on Charts.js version 3.1.0.

Time is formatted in ISO standard (javascript so): '2021-04-12T01:51:40' Chart configuration options.scaled[scaledId] for the x-axis can only be an array (contrary to what is described in the documentation). It seems that defining an array does not apply the custom x-axis configuration.

I attempted to define the x-axis options.scaled[x] as an object but it results in the following error:

Uncaught Error: This method is not implemented: either no adapter can be found or an incomplete integration was provided.
Uncaught TypeError: Cannot read property 'left' of undefined

Has anyone else experienced this issue and/or have any ideas about it?


Sample code:

<script type='text/javascript' src='https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="c9aaa1a8bbbde7a3ba89fae7f8e7f9">[email protected]</a>/dist/chart.min.js'></script>

<script>
var config = {
    type: 'line',
    data: {
        datasets: [
            {
                label: 'RENAULT TWINGO',
                borderColor: 'rgb(255, 99, 132)',
                data: [
                    {x: '2021-04-11T01:51:40', y: 132},
                    {x: '2021-04-12T03:57:03', y: 102}
                ],
            },
            {
                label: 'CITROEN C3',
                borderColor: 'rgb(54, 162, 235)',
                data: [
                    {x: '2021-04-11T01:51:40', y: 123},
                    {x: '2021-04-12T03:57:03', y: 143}
                ],
            },
        ],
    },
    options: {
        plugins: {
        title: {
            display: true,
            text: 'Prix des voitures',
        }
        },
        responsive: true,
        scales: {
            x: [{
                axis: 'x',
                type: 'timeseries',
                title: {
                    display: true,
                    text: 'Temps',
                },
                ticks: {
                  source: 'data'
                },
                time: {
                    // parser: 'YYYY-MM-DDTHH:mm:ss',
                    unit: 'day',
                    displayFormats: {
                        'day': 'DD/MM'
                    },
                },
            }],
            y: {
                axis: 'y',
                type: 'linear',
                title: {
                    display: true,
                    text: 'Prix',
                },
                beginAtZero: true,
            },
        }
    }
}

var ctx = document.getElementById('myChart');
var myChart = new Chart(ctx, config);
</script>

and its result: https://i.sstatic.net/DyUaH.png

The expected outcome should be the date formatted as DD/MM (e.g. 11/04 and 12/04).


Updated on 26-04-2021

A library for date formatting AND the associated adapter were missing.

<script src="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="fd909290989389bdcfd3cfcad3cd">[email protected]</a>"></script>
<script src="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="e7848f8695938d94ca86838697938295ca8a888a828993a7d7c9d6c9d6">[email protected]</a>"></script>

As a result, assigning objects for the x-axis now works correctly.

scales: {
    x: {
        axis: 'x',
        type: 'time',
        time: {
            parser: 'YYYY-MM-DDTHH:mm:ss',
            unit: 'minute',
            displayFormats: {
                'minute': 'DD-MM HH:mm'
            },
        },
    }
}

Answer №1

To properly configure X, you must remove the square array brackets and format it as x: {} instead of X:[{}]

UPDATE: With the latest version of chart.js (v3), moment is no longer included by default, meaning date adapters are also not shipped. In order to utilize the timescale feature, it is necessary to add a date adapter following the instructions provided here: https://www.chartjs.org/docs/latest/axes/cartesian/time.html#date-adapters

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

Resolving Errors in React Webpack Production Mode

I am currently working on setting up a webpack configuration for a React application, and I need the production version of React in my build. My development environment is Windows. After researching online, I found that many suggest using the following co ...

Steps for accessing an iframe link with a VPN

How can I embed a German link on my website that is only accessible to the German public? When trying to access it from another country, I receive a "This show is not available in your country for legal reasons" message. However, if I use a VPN in that c ...

Choosing a specific category to display in a list format with a load more button for easier navigation

Things were supposed to be straightforward, but unexpected behaviors are popping up all over the place! I have a structured list like this XHTML <ul class = "query-list"> <li class="query"> something </li> <li class="query" ...

React Native buttons are displaying at a fixed width and not adjusting as expected

Within a row, I have two buttons with a padding of 20 between them, and they are only taking up the necessary width for the button text. The width remains constant! Here is the code for the buttons: <View style={styles.buttonContainer}> &l ...

Utilize the JSON response upon successful completion of an AJAX request

I'm currently working with this code snippet: function openIzmeni(value) { $.ajax({ url: "getRzaizmenu.php", type: "POST", async: true, data: { vrednostid:value}, //your form data to post goes here as ...

Troubleshooting issues with filtering two MongoDB arrays in ES6 and finding a solution

I have a scenario where I am requesting two arrays of objectIDs from MongoDB, and then attempting to identify the differences between the two arrays. In addition, I am passing these arrays through express middleware using res.locals. Below is the code sn ...

Utilizing web components in React by solely relying on a CDN for integration

I have a client who has provided us with a vast library of UI elements that they want incorporated into our project. These elements are stored in javascript and css files on a CDN, and unfortunately I do not have access to the source code. All I have at my ...

Angular.js: The $setDirty() method on a form does not automatically affect its child form elements

Currently, I am working on a form validation project using Angular.js. A specific challenge that I am facing is setting the dirty state on a child element of a form in an isolated scope within a directive. Does anyone know how to achieve this and set the i ...

Dotted JavaScript property within an object

Having trouble sorting an array of objects using the lodash orderBy function because it doesn't work when the iteratees contain a dot in the middle. For a better understanding of the issue, check out this plunker. plunker ar users = [ { 'us ...

I am attempting to create a skybox, yet it seems like I am overlooking a crucial element

Currently, I am attempting to create a skybox but have encountered difficulties with various tutorials. Initially, I tried to use an array approach to pass parameters for the material based on a previous example, but it seems that the method has been updat ...

The click event that is dynamically added is triggered multiple times

On clicking a button, I am dynamically adding a row to an HTML table. The row contains a cell with a list item (li) element which has a click event assigned to it. However, when I click on the list item, the event fires multiple times and I'm unsure w ...

Enhanced jQuery implementation for hiding elements

I encountered a peculiar issue where jQuery's .is(':hidden') function wrongly returned true for an element that visibly displayed content. You can see the problem demonstrated in this fiddle. The :hidden pseudo checks both offsetWidth and o ...

JS/JQuery: Retrieve value from dropdown list or input field

I'm looking for a way for my user to choose a value from a drop-down menu, but if they can't find the right option there, I want them to be able to input a custom value. I've figured out a workaround by deactivating the drop-down and activa ...

Is there a way to store session variables in Angular without the need to make an API call?

I am currently working with a backend in PHP Laravel 5.4, and I am looking for a way to access my session variables in my Angular/Ionic project similar to how I do it in my Blade files using $_SESSION['variable_name']. So far, I have not discove ...

Ways to achieve outcomes from functions employing concatMap within rxjs?

When calling two functions, I make use of fn1 and fn2. To execute them one after the other, I utilize concatMap. I choose not to use exhaustMap and switchMap as they can result in nested "callback-hell". exhaustMap(() => fn1().pipe( swit ...

Ways to retrieve the value from a button when it is clicked

I am facing an issue where I have buttons that are generated dynamically depending on the number of phone numbers available in the database. For example, if there are 3 phone numbers in the database, there will be three buttons displayed. The aim is that w ...

Sort the elements within the *ngFor loop according to the category upon clicking the button in Angular

Currently, I have a collection of items that I am iterating through using *ngFor. Above this list, there are category buttons available as shown in the HTML snippet below. My goal is to enable filtering of the list based on the category of the button click ...

Steps to retrieve the central coordinates of the displayed region on Google Maps with the Google Maps JavaScript API v3

Is there a way to retrieve the coordinates for the center of the current area being viewed on Google Maps using JavaScript and the Google Maps JavaScript API v3? Any help would be greatly appreciated. Thank you! ...

The ideal scenarios for employing functional setState

Lately, I've been delving into the world of React, immersing myself in tutorials and explanations on how to manipulate elements in different ways. One aspect that has caught my interest is the setState function, which allows you to update or override ...

What is the process for displaying a hidden div using a button?

I'm running into a problem with my project. Here is the code I've been using to hide my div element : @keyframes hideAnimation { to { visibility: hidden; } } To trigger the animation and hide the div after 6 seconds, I have implemented ...