Adjust the x-axis on the Vue.js bar chart

I'm currently working on a Vue.js Laravel application where I am trying to incorporate a bar chart using the ApexCharts module.

<apexchart ref="apexChart" :options="chartOptions" :series="chartData" type="bar"></apexchart>

Below is my code snippet:

<script>
import ApexCharts from 'apexcharts'
import axios from 'axios'

export default {
    data() {
        return {
            daywise_sales: [],
            chartData: [],
            mostSellingDay: '',
            leastSellingDay: '',
            chartOptions: {
                xaxis: {
                    categories: []
                },
                yaxis: {
                    title: {
                        text: "Sales"
                    }
                },
                chart: {
                    id: 'daywise_sales'
                },
                title: {
                    text: 'Day wise sales'
                }
            }
        }
    },
    mounted() {
        // Fetch day-wise sales data
        axios.get('/shopify-day-wise-sales')
        .then(response => {
            this.daywise_sales = response.data.day_totals;
            // Set X-axis labels
            this.chartOptions.xaxis.categories = Object.keys(this.daywise_sales)
            .map(date => {
              return new Date(date).toLocaleString('default', {weekday: 'long'});
            });
            this.chartData = [{data: Object.values(this.daywise_sales)}]; // Update chart data
            
            // Find the most and least selling days
            let mostSellingDay = '';
            let mostSellingDaySales = 0;
            let leastSellingDay = '';
            let leastSellingDaySales = Number.MAX_SAFE_INTEGER;
            for (let date in this.daywise_sales) {
                if (this.daywise_sales[date] > mostSellingDaySales) {
                    mostSellingDay = date;
                    mostSellingDaySales = this.daywise_sales[date];
                }
                if (this.daywise_sales[date] < leastSellingDaySales) {
                    leastSellingDay = date;
                    leastSellingDaySales = this.daywise_sales[date];
                }
            }
            this.mostSellingDay = new Date(mostSellingDay).toLocaleString('default', {weekday: 'long'});
            this.leastSellingDay = new Date(leastSellingDay).toLocaleString('default', {weekday: 'long'});
        })
        .catch(error => {
            console.log(error);
        });
    }
}
</script>

For response.data.day_totals, the backend returns an array as shown below:

array:8 [
  "2023-01-11" => 1
  "2023-01-09" => 1
  "2023-01-05" => 0
  "2023-01-06" => 0
  "2023-01-07" => 0
  "2023-01-08" => 0
  "2023-01-10" => 0
  "2023-01-12" => 0
]

The issue is that I need to display dates in short format (Sat, Sun, Mon, Tue...etc) instead of integers on the x-axis and represent the number of sales per day on the y-axis.

Here is a snapshot of my current chart:

https://i.stack.imgur.com/Uw6Kz.png

How can I resolve my x-axis labeling concern?

Answer №1

This particular section of documentation might be helpful for you:

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

issue with transparent html5

Here is the code snippet I am struggling with: function clear(){ context2D.clearRect(0, 0, canvas.width, canvas.height); } function drawCharacterRight(){ clear(); context2D.setTransform(1, 0.30, 1, -0.30, 10, 380);//having issues here c ...

Utilize a single WebAssembly instance within two separate Web Workers

After compiling a wasm file from golang (version 1.3.5), I noticed that certain functions using goroutines are not supported. When these functions are called, they run in the current thread and slow down my worker significantly. To address this issue, I w ...

Utilizing Javascript to initiate an AJAX call to the server

I am creating an application similar to JSbin / JS fiddle. My goal is to update my database by making an ajax request to the server when the user clicks on the save code button and submits the values entered in the textarea. However, I seem to be encount ...

Passing large arrays of data between pages in PHP

I'm facing a challenge where I need to pass large arrays of data between pages. Here's the situation: Users input their Gmail login details in a form, which is then sent to an AJAX page for authentication and contact retrieval. If the login fail ...

The bootstrap datepicker does not display the date range on the calendar

I tried to incorporate a bootstrap datepicker date-range in the code snippet below, but I am encountering an issue where the selected date range is not displaying on the calendar. <!DOCTYPE html> <html> <head> <link rel="stylesheet" ...

Problem with displaying images and videos in a lightbox gallery

Currently, I am encountering an issue with the lightbox feature on my website. When trying to play a video, there seems to be a layer (div tag) blocking it, preventing me from playing or stopping the video. This problem occurs when clicking on an image fir ...

Angular 2: trigger a function upon the element becoming visible on the screen

How can I efficiently trigger a function in Angular 2 when an element becomes visible on the screen while maintaining good performance? Here's the scenario: I have a loop, and I want to execute a controller function when a specific element comes into ...

The issue of calling the child window function from the parent window upon clicking does not seem to be functioning properly on Safari and Chrome

I'm attempting to invoke the function of a child window from the parent window when a click event occurs. Strangely, this code works in Firefox but not in Safari or Chrome. Here is the code snippet I am using: var iframeElem = document.getElementById( ...

"VS Code's word wrap feature is beneficial for wrapping long lines of text and code, preventing them from breaking and ensuring they are

text not aligning properly and causing unnecessary line breaks insert image here I attempted to toggle the word wrap feature, installed the Rewrap plugin, and played around with vscode settings ...

Generating a USA map with DataMaps in d3jsonData

I'm trying to create a basic US map using the DataMaps package and d3 library. Here's what I have attempted so far: <!DOCTYPE html> <html> <head> <title> TEST </title> <script src="https://d3js.org/d3.v5.js"> ...

Passing properties from the parent component to the child component in Vue3JS using TypeScript

Today marks my inaugural experience with VueJS, as we delve into a class project utilizing TypeScript. The task at hand is to transfer the attributes of the tabsData variable from the parent component (the view) to the child (the view component). Allow me ...

Troubleshooting the Confirm Form Resubmission problem on my website

Hello everyone! I'm working on a website and facing an issue where refreshing the page triggers a confirm form resubmission prompt. Could someone please advise me on how to resolve this? Thank you in advance! ...

Verify the occurrence of an element within an array inside of another array

Here is the scenario: const arr1 = [{id: 1},{id: 2}] const arr2 = [{id: 1},{id: 4},{id: 3}] I need to determine if elements in arr2 are present in arr1 or vice versa. This comparison needs to be done for each element in the array. The expected output sho ...

Preserving the selected options in a dynamically populated dropdown list after submitting a form using php

I am attempting to preserve form values even after submitting the form. document.getElementById('start_date').value = "<?php echo $_POST['start_date'];?>"; document.getElementById('end_date').value = "<?php echo $_P ...

Run a Javascript function two seconds after initiating

My goal is to implement a delay in JavaScript using either setInterval or setTimeout, but I am facing an issue where the primary element is getting removed. Code that works fine without Javascript delay: const selectAllWithAttributeAdStatus = document. ...

Clicking an element to uncover more information

Currently, I am working on solving the second question within this series of problems. The task involves creating a functionality where clicking on a legislator's name displays additional information about them. You can view my progress so far by visi ...

Comparison of Vue 3 Composition API - watchEffect and watch

I've recently been delving into the world of Vue Composition API and have found myself pondering about the distinctions between watchEffect and watch. The documentation implies that watch functions similarly to the Vue 2 watch, leading me to speculate ...

The requested external js scripts could not be found and resulted in a net::ERR_ABORTED 404 error

import express, { Express, Request, Response } from 'express'; const path = require("path"); import dotenv from 'dotenv'; dotenv.config(); const PORT = process.env.PORT || 5000; const app = express(); app.use(express.static(path.join ...

Issues with Angular 2 loading properly on Internet Explorer 11

We are currently running an Asp.net MVC 5 web application integrated with Angular 2. The application functions smoothly on Chrome, Firefox, and Edge browsers, but encounters loading issues on IE 11, displaying the error illustrated in the image below: ht ...

Is there a way to arrange an array based on the product or quotient of two values?

I'm working with an array of posts, each containing data on 'views' and 'likes', along with the user IDs associated with those likes. My goal is to sort this array based on the like rate. However, my current approach seems to be i ...