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

An issue with Nuxt.js causing body parameters to not be passed successfully while using this.$http.post

I've encountered an issue where using the @nuxt/http this.$http.post and this.$http.patch methods is causing problems with parsing body parameters during posting. Strangely, it used to work perfectly fine before, leaving me unsure of where to even beg ...

Modules failing to load in the System JS framework

Encountering a puzzling issue with System JS while experimenting with Angular 2. Initially, everything runs smoothly, but at random times, System JS struggles to locate modules... An error message pops up: GET http://localhost:9000/angular2/platform/bro ...

Chrome is having trouble loading basic JavaScript

Here's the JavaScript code I have placed inside the head tag of my HTML: <script type="text/javascript"> function over1() { var img1 = document.getElementById("1").src; document.getElementById("big").src = img1; } function out() { ...

jQuery sidebar with a fixed position

Is there a way to implement a sidebar menu feature using jQuery that reappears on the left as the user scrolls down the page? You can see an example sidebar menu here. ...

Include toggle attribute in v-b-popover for touch functionalities

Currently, I am implementing v-b-popover on my website to handle both touch and mouse events. To achieve this, I have set the tag of v-b-popover as 'hover'. I am wondering if there is a way for me to toggle the popover (show and hide) for both to ...

Laravel and PHP provide the flexibility to have one model with numerous polymorphic relations

Within my web application, users have the capability to store extracted data in a table named field_results. However, this particular model requires referencing two polymorphic relationships: FieldResults.php: // A field result can morph into either ...

Vuetify's <v-text-field> feature automatically clears the input after selecting a result from Google Maps autocomplete

A dilemma I'm facing is with a page that has a <v-text-field> containing GoogleMaps autocomplete. The problem arises when Vuetify clears the input once an address is selected by the user. I have discovered that this complication is connected to ...

Tips for showing various images from a server using ng-repeat

Is it possible to display different images from a server using AngularJS? I have ng-repeat set up for posts and for each post, I want to retrieve its avatar. My approach is to call a function getImage(post.author.id) to fetch the corresponding avatar. $ ...

Using jQuery, target the specific elements that have certain data attributes assigned to them

Is there a way to target elements with a specific data attribute and a unique class using jQuery? For instance, I want to select these elements: <div data-roomid="55" data-status="NoData"></div> <div data-roomid="55" data-status="Open"> ...

What is the best way to transmit data from Ajax to a Laravel controller? Why am I encountering a 422 (Unprocessable Content) error when the form keys are not being sent via Ajax

While working on CRUD operations using Laravel, Ajax, and Blade forms in a modal, I encountered an issue where the create method works fine but the update method doesn't. Even though both methods have similar functionalities, I believe the problem lie ...

Issue with Java Script inheritance in google.maps.OverlayView not functioning as expected

UPDATE: After spending another day working on this, I believe I have identified the issue, although it is not yet confirmed. Once I have verified the solution, I will update this post with an answer. My current understanding is that the problem arises fro ...

Strategies for positioning identical Highcharts series next to one another

I am currently utilizing highcharts to create column charts. My column chart consists of multiple series, structured like this: Here is the code I am working with: $(function () { var chart; $(document).ready(function() { ...

In React, CSS @media queries are specifically designed to function only within the Device Mode of Developer Tools

As indicated by the title, I have designed a responsive web app using the React framework along with various @media queries. When I resize the page in Developer Tools' Device Mode, the queries work perfectly fine and everything functions as expected. ...

The key you entered in local storage was not defined and the value associated with

Having an issue with my HTML signup form where the key is showing as undefined (email should be the key) and the value displays as [object Object]. Any help in resolving this problem would be greatly appreciated. Thank you. <!DOCTYPE html> <html& ...

AngularJS: The blend of bo-bind, bindonce, and the translate filter

I am currently working with angular 1.2.25, angular-translate 2.0.1, angular-translate-loader-static-files 2.0.0, and angular-bindonce 0.3.1. My goal is to translate a static key using bindonce. Here is the code snippet I have: <div bindonce> < ...

Shifting a div element around the webpage and letting it fall into place if it intersects with another div using plain JavaScript

Check out this jsFiddle link. Upon opening it, you will encounter a moveable div. However, I am looking to enhance this functionality by allowing the div to disappear if moved to the 'trash' area. Essentially, when you place the moveable div in t ...

Learn how to easily incorporate a drop-down list into the material-UI Search component within the navbar to enhance the search results

I integrated a Material UI search bar into my React app's navbar following the instructions from the official documentation on MUI. However, the article does not provide any guidance on how to add a dropdown list when selecting the search input field. ...

Issue with HTTP headers not being effective in $.AJAX request

Regardless of the method I attempt to use for setting headers in an AJAX call, and no matter which header I set, every time there is a header present, the AJAX call is aborted and does not go through. However, if I try to make the call without setting any ...

Dealing with a passed EJS variable in string form

When working with passed data in ejs, I usually handle it like this and it works perfectly: let parsed_json = JSON.parse('<%-JSON.stringify(passed_data)%>'); However, I encountered a problem when trying to dynamically pass a string variabl ...

Tips for integrating CSS with Material UI TableContainer

I have utilized Material UI Grid to display data in a chart. However, the appearance of the chart does not match my expectations. Instead of resembling the desired "dense table," it looks different: Actual Look of the Chart Below is the code snippet I ha ...