Chartjs: Invalid Scale Configuration for Scale: xAxes

I get a strange error in ChartJS and have no idea how to fix it, due to the error I can't change anything about the scales.

My Code (Sensor.tsx):

import { useParams } from "solid-app-router"
import { createSignal, createResource, onMount, createMemo, createEffect } from "solid-js";
import Chart from "";

const numToSenor = {
    1: "temperature",
    2: "air_humidity",
    3: "soil_humidity",
    4: "photoresistor",
    5: "microphone",
    6: "ble",
    7: "gas",
    8: "co2",
    9: "batter_voltage",
}

let myGraph;
let chart

export const Sensor = () => {
    const params = useParams();
    const [sensortType, setSensortType] = createSignal(1);
    const [sensorData, setSensorData] = createSignal([]);
    const fetchURL = createMemo(() => `()]}`)
    
    createEffect(() => {
        fetch(`()]}`)
        .then(raw => raw.json())
        .then(data => setSensorData(data));

    })
    onMount(() => {
        let canvas = myGraph.getContext("2d")
        chart = new Chart(canvas, {
         type: 'line',
         data: {
            datasets: [{ 
                  data: sensorData(),
                  label: "Temperature",
                  borderColor: "#3e95cd",
                  fill: false
            }]
         },
         options: {
            responsive: true,
            legend: {
               display: false
            },
            tooltips: {
               mode: "index",
               intersect: false,
            },
            hover: {
               mode: "nearest",
               intersect: true
            },
            scales: {
                xAxes: [{
                  type: "time",
                  ticks: {
                    autoSkip: true,
                    maxTicksLimit: 20,
                    },
                    display: true,
                    scaleLabel: {
                        display: true,
                        labelString: "Point"
                    }
                }],
                yAxes: [{
                    display: true,
                    scaleLabel: {
                        display: true,
                        labelString: "Value"
                    }
                }]
            }
         }
      })
    })
    createEffect(() => {
        chart.data = {
            datasets: [{ 
                  data: sensorData(),
                  label: "Temperature",
                  borderColor: "#3e95cd",
                  fill: false
            }]
         }
         chart.update();
    })

    

    return (
        <>
        <div class='w-full wp-2 overflow-y-auto h-screen' >
            {/* Header */}
            <div  class=" bg-slate-50 duration-200 m-6 p-2 border-b-3 border-light rounded-md flex ">
                SensorID: {params.id}
            </div>
            
            {/* Data Graph Picker */}
            <div class="flex mx-6 mt-12">
                <div onClick={() => setSensortType(1)} class={`${sensortType() == 1 ? "border-slate-400" : "border-light"} bg-slate-50 duration-200 mr-4 p-2 border-b-3  rounded-md flex hover:bg-slate-200`}>
                    Temperature
                </div>
                <div onClick={() => setSensortType(2)} class={`${sensortType() == 2 ? "border-slate-400" : "border-light"} bg-slate-50  duration-200 mr-4 p-2 border-b-3  rounded-md flex hover:bg-slate-200`}>
                    Air Humidity
                </div>
                <div onClick={() => setSensortType(3)} class={`${sensortType() == 3 ? "border-slate-400" : "border-light"} bg-slate-50  duration-200 mr-4 p-2 border-b-3  rounded-md flex hover:bg-slate-200`}>
                    Soil Humidity
                </div>
                <div onClick={() => setSensortType(4)} class={`${sensortType() == 4 ? "border-slate-400" : "border-light"} bg-slate-50  duration-200 mr-4 p-2 border-b-3  rounded-md flex hover:bg-slate-200`}>
                    Brightness
                </div>
                <div onClick={() => setSensortType(5)} class={`${sensortType() == 5 ? "border-slate-400" : "border-light"} bg-slate-50  duration-200 mr-4 p-2 border-b-3  rounded-md flex hover:bg-slate-200`}>
                    Loudness
                </div>
                <div onClick={() => setSensortType(6)} class={`${sensortType() == 6 ? "border-slate-400" : "border-light"} bg-slate-50  duration-200 mr-4 p-2 border-b-3  rounded-md flex hover:bg-slate-200`}>
                    BLE
                </div>
                <div onClick={() => setSensortType(7)} class={`${sensortType() == 7 ? "border-slate-400" : "border-light"} bg-slate-50  duration-200 mr-4 p-2 border-b-3  rounded-md flex hover:bg-slate-200`}>
                    Gas
                </div>
                <div onClick={() => setSensortType(8)} class={`${sensortType() == 8 ? "border-slate-400" : "border-light"} bg-slate-50  duration-200 mr-4 p-2 border-b-3  rounded-md flex hover:bg-slate-200`}>
                     CO2
                </div>
                <div onClick={() => setSensortType(9)} class={`${sensortType() == 9 ? "border-slate-400" : "border-light"} bg-slate-50  duration-200 mr-4 p-2 border-b-3  rounded-md flex hover:bg-slate-200`}>
                    Battery Voltage
                </div>
            </div>

            {/* Data Graph */}
            <div class=" block bg-slate-50 duration-200 m-6 mt-2 p-2 border-b-3 border-light rounded-md ">
                {numToSenor[sensortType()]}
                <canvas ref={myGraph}></canvas>
            </div>
        </div>
        </>
    )
}

I use SolidJS so don't wonder about that part and vite as the build tool.

Here are the error codes in the console,as you can see none of the axis parameters were apllied (for example no autoSkip on the x-axis labels)

What am I doing wrong?

1

2 Answers

You are using V3 of chart.js while using V2 syntax for all of your options.

For example, the tooltip and legend should be configured in the plugin's namespace.

For the error you are getting, in v3 all scales are their own object in the scales object so no more separate arrays for the x and y axes.

For all changes please read the migration guide:

3.x Migration Guide

1

You need, from version 3 onwards, to remove 'Axes' from the name and also remove the brackets as below:

  options: {
       scales: {
            x: {
              type: "time",
              ticks: {
                autoSkip: true,
                maxTicksLimit: 20,
                },
                display: true,
                scaleLabel: {
                    display: true,
                    labelString: "Point"
                }
            },
            y: {
                display: true,
                scaleLabel: {
                    display: true,
                    labelString: "Value"
                }
            }
       }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Marcus Vance

Marcus Vance

Cybersecurity & Digital Privacy Researcher

Marcus Vance is a cybersecurity auditor and technology writer dedicated to educating the public about online safety, data privacy regulations, enterprise security, and emerging cyber threats.

Share this article
Twitter Facebook Pinterest