The uneven distribution of x-axis divisions in Google Chart is causing undesirable results in JavaScript

I am currently working on a project involving the creation of a Column Chart using JavaScript and Google Chart. The chart is intended to display the number of tickets meeting SLA criteria per week. However, I encountered an issue where the x-axis values are not displaying as whole numbers but instead as divided numbers (refer to the image below). Is there a way for me to fix this?

https://i.sstatic.net/JuYmw.jpg

Below is the dataset being used to create the column chart:

[[1, 7, 4, 5, 0, 0, 1]
,[2, 12, 2, 9, 1, 1, 0]
,[3, 10, 0, 1, 1, 1, 0]
,[4, 4, 3, 0, 1, 0, 0]
,[5, 7, 5, 0, 0, 0, 0]
,[6, 6, 1, 0, 0, 0, 0]
,[7, 11, 4, 0, 0, 0, 0]
,[8, 11, 2, 0, 0, 0, 1]]

The following code snippet is used to plot the data onto the chart:

 console.info('Start ticketsSolvedPerWeek SLA')
    console.info(ds.Data)
    // (A)Define column headers
    var dataNew = new google.visualization.DataTable()
    dataNew.addColumn('number', 'Weeknumber');
    dataNew.addColumn('number', '00');
    dataNew.addColumn('number', '01');
    dataNew.addColumn('number', '02');
    dataNew.addColumn('number', '03');
    dataNew.addColumn('number', '04');
    dataNew.addColumn('number', '05');

    dataNew.addRows(ds.Data)

    //(3) Set graph options
    var options = {
        title: ds.title,
        hAxis: {
            title: 'Weeknumber'
        },
        vAxis: {
            title: 'Tickets'
        },
        trendlines: {
            0: {
                type: 'polynomial',
                degree: 3,
                visibleInLegend: true,
                pointSize: 20, 
                opacity: 0.1
            }
        },
        width: 750,
        height: 500,
    };

    //(4) Draw Graph
    var slaChart = new google.visualization.ColumnChart(
    document.getElementById('ticketsSLA'));
    slaChart.draw(dataNew, options);

Answer №1

Utilize the hAxis.ticks option to customize axis labels

This option requires an array of values that match the data table axis column

In this example, the
data table method getDistinctValues(columnIndex) is utilized to generate the array for ticks

google.charts.load('current', {
    packages: ['corechart']
}).then(function () {
  var dataNew = new google.visualization.DataTable()
  dataNew.addColumn('number', 'Weeknumber');
  dataNew.addColumn('number', '00');
  dataNew.addColumn('number', '01');
  dataNew.addColumn('number', '02');
  dataNew.addColumn('number', '03');
  dataNew.addColumn('number', '04');
  dataNew.addColumn('number', '05');

  dataNew.addRows([
    [1, 7, 4, 5, 0, 0, 1]
    ,[2, 12, 2, 9, 1, 1, 0]
    ,[3, 10, 0, 1, 1, 1, 0]
    ,[4, 4, 3, 0, 1, 0, 0]
    ,[5, 7, 5, 0, 0, 0, 0]
    ,[6, 6, 1, 0, 0, 0, 0]
    ,[7, 11, 4, 0, 0, 0, 0]
    ,[8, 11, 2, 0, 0, 0, 1]
  ]);

  var options = {
      title: 'title',
      hAxis: {
          ticks: dataNew.getDistinctValues(0),
          title: 'Weeknumber'
      },
      vAxis: {
          title: 'Tickets'
      },
      trendlines: {
          0: {
              type: 'polynomial',
              degree: 3,
              visibleInLegend: true,
              pointSize: 20,
              opacity: 0.1
          }
      },
      width: 750,
      height: 500,
  };

  var slaChart = new google.visualization.ColumnChart(
  document.getElementById('ticketsSLA'));
  slaChart.draw(dataNew, options);
});
<script src="https://www.gstatic.com/charts/loader.js"></script>
<div id="ticketsSLA"></div>

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

Values are being subtracted correctly without displaying any negative results

I'm working with the following code snippet: const my_transactions = [{amount: -100,currency: 'EUR'},{amount: -200,currency: 'EUR'},{amount: -400,currency: 'EUR'}]; let total = 0; my_transactions.forEach(el => total ...

The switchMap function is sending back a single item

I'm having an issue with switching the observable using the switchMap operator: return this.db.list(`UserPlaces/${this.authData.auth.auth.currentUser.uid}`, { query: { orderByChild: 'deleted', equalTo: false } }) .ma ...

Adjusting a variable based on when the phone is rotated using JavaScript

I've tried searching online and researching how to accomplish this task, but so far I haven't been successful. I've created a variable named 'width'. My goal is to have JavaScript check the screen width: if it's smaller than 6 ...

Have I potentially compromised my project by executing the command `npm audit fix --force`?

While working on a React project using Vite, everything was going smoothly. I decided to incorporate some charts and came across the recharts package, which I found quite appealing. So, I added it to my project using the command npm i recharts. After runn ...

How can one hand over a file to the http.write method in Node?

When I attempt to print a file using the res.write() method, I encounter an error: TypeError: First argument must be a string or Buffer This is my code snippet: var fs = require("fs"); var http = require("http"); http.createServer(function (req, res){ ...

Guidelines for creating a dynamic filter in Prisma js

I am looking to create a dynamic filter based on user input from the frontend. On mapping the data, I found that the object results appear like this: { id: '2', name: 'yuhu' } The keys 'id' and 'name' need to be dyn ...

Tips for accessing the value of a DOM node during the first render

I am attempting to extract a value from an input textbox during the initial rendering process using useRef, but I am encountering the following error: " ref is not a prop. Trying to access it will result in undefined being returned. If you need to ac ...

Is it possible to determine if a style property has been altered

I am looking to identify changes in CSS properties without needing the actual values. I only require the specific style property that has been altered. For example, I need the style property to be stored in a variable, as shown below: document.getElementB ...

A step-by-step guide on incorporating universal CSRF tokens using JQuery AJAX

As part of my development work, I am in the process of creating jquery code that communicates with the server through ajax to input data into a database based on specific request parameters. My main concern at this point is the vulnerability to CSRF attac ...

The checkbox system is designed so that if all child checkboxes are chosen, the parent checkbox will automatically become selected as well

view image description hereLet's create a checkbox structure with some specific conditions: When the parent checkbox is selected, all child checkboxes should automatically get selected. If all child checkboxes are unselected, then the parent checkbo ...

Retrieving the Image of an Amazon Product using ASIN

My goal is to locate the image URLs for Amazon products using only their ASIN. For instance: This link pertains to a product with the ASIN B07NVVQL66 in the United States. Clicking on it will display an image of the product http://images.amazon.com/imag ...

Issue encountered: "require" is not recognized when attempting to access my local JSON file in Vue.js

I am venturing into the world of vuejs... I attempted to retrieve data from my JSON file stored locally, but the decision on which specific JSON file's data to fetch is dynamic. I keep encountering an error stating 'require' is not define ...

Having trouble with Angular2's Maximum Call Stack Exceeded error when trying to display images?

I am facing an issue in my Angular2 application where I am trying to display an image in Uint8Array format. The problem arises when the image size exceeds ~300Kb, resulting in a 'Maximum Call Stack Exceeded' error. Currently, I have been able to ...

Retrieving a Date object from a JSON response

Hey there, I have a situation where I'm trying to convert a JSON response into a date object in order to display it in a different format. Here's the JSON response I'm working with: responseData: [{"id":10,"createdDate":"1 Sep, 2014 12:48:5 ...

changing a variable in javascript

Is there a way to successfully update the "storage" variable set in uploadify? I have a function called set_path that is designed to modify the variable by combining it with other values whenever specific content is selected. $(document).ready(function () ...

Determine whether an array contains any unique characters

I am attempting to determine if a password string includes any special characters. I am trying to achieve this using the code below: const passwordArr = "A1b2c3d4e5!@#".split(""); const specialChar = "~!@#$%^&*_-+=`|(){}[]:;& ...

Problem with AngularJS function execution - require a delay in executing the second function

I am developing a small utility in AngularJS to manage the shopping cart for users. Here is the code snippet from my cart-service.js file: var myStoreCartService = angular.module("myStoreCartService", []); myStoreCartService.factory('Cart', fu ...

Troubleshooting: Unable to trigger click event on Vuejs component

Working with Vuejs and Vuikit components, I have set up the following structure: <template> <div class="uk-scope"> <vk-modal :show="isShow" v-if="config"> <vk-modal-close @click="alert('hello!')" large& ...

We'll show you the steps to properly organize a set of radio buttons created dynamically within a 'controlgroup' using jQuery Mobile

I am facing an issue with grouping dynamically generated radio buttons into a jQuery Mobile control group. I generate the radio buttons and append them to a div, but they are displayed separately even though the wrapping div contains a 'controlgroup&a ...

Can arrays be passed as function parameters in CrossRider without the need to convert them into objects first?

Our team is currently utilizing CrossRider to develop an extension specifically for Internet Explorer. We have a crucial object that is downloaded from , but we are facing an issue where arrays within this object are getting converted into objects with int ...