Stop Google Charts from magnifying the view

I'm currently working on creating a column chart using Google Charts. The purpose of this chart is to display the number of pageviews for a specific page within the last 30 days. Below is the JavaScript code I used to create the chart (you can access the full, uncut fiddle here):

google.charts.load('current', {packages: ['corechart', 'bar']});
google.charts.setOnLoadCallback(drawBasic);

function drawBasic() {

      var data = new google.visualization.DataTable();
      data.addColumn('date', 'Day');
      data.addColumn('number', 'Visits');

      data.addRows([
        // Data rows extracted from my database (timestamp + hits)
        [new Date(1458691200 * 1000),null],
        [new Date(1458777600 * 1000),null],
        // Repeat process for each day, all with null value
        [new Date(1461283200 * 1000),2],
        [new Date(1461369600 * 1000),null]
      ]);

      var options = {
      chartArea: {
        width: '70%',
        height: '70%'
      },
      hAxis: {
        format: 'd',
        gridlines: {
          count: 15
        },
        title: 'Day'
      },
      vAxis: {
        baseline: 0,
        format: '#',
        gridlines: {
          count: -1
        },
        title: 'Views',
        viewWindowMode:'explicit',
        viewWindow: {
          max: 10,
          min: 0
        }
      }
    };

      var chart = new google.visualization.ColumnChart(
        document.getElementById('chart_div')
      );

      chart.draw(data, options);
    }

The current version of the chart works effectively and displays what I need. However, there is an issue where the graph is "zoomed in" on the only non-null data point:

As a result, it appears as if the 2 views on April 22 are actually representing the period from April 12 to April 22, which is incorrect.

Is there a way for me to prevent the chart from zooming in? Ideally, it should adjust itself based on the gridlines of the relevant period.

Answer №1

It doesn't seem to be related to a zoom issue.
It appears to be a bug or problem with the current version.
Version 43 displays the expected chart...

google.charts.load('43', {packages: ['corechart', 'bar']});
google.charts.setOnLoadCallback(drawBasic);

function drawBasic() {
  var data = new google.visualization.DataTable();
  data.addColumn('date', 'Day');
  data.addColumn('number', 'Visits');

  data.addRows([
    [new Date(1458691200 * 1000),null],
    [new Date(1458777600 * 1000),null],
    [new Date(1458864000 * 1000),null],
    [new Date(1458950400 * 1000),null],
    [new Date(1459036800 * 1000),null],
    [new Date(1459123200 * 1000),null],
    [new Date(1459209600 * 1000),null],
    [new Date(1459296000 * 1000),null],
    [new Date(1459382400 * 1000),null],
    [new Date(1459468800 * 1000),null],
    [new Date(1459555200 * 1000),null],
    [new Date(1459641600 * 1000),null],
    [new Date(1459728000 * 1000),null],
    [new Date(1459814400 * 1000),null],
    [new Date(1459900800 * 1000),null],
    [new Date(1459987200 * 1000),null],
    [new Date(1460073600 * 1000),null],
    [new Date(1460160000 * 1000),null],
    [new Date(1460246400 * 1000),null],
    [new Date(1460332800 * 1000),null],
    [new Date(1460419200 * 1000),null],
    [new Date(1460505600 * 1000),null],
    [new Date(1460592000 * 1000),null],
    [new Date(1460678400 * 1000),null],
    [new Date(1460764800 * 1000),null],
    [new Date(1460851200 * 1000),null],
    [new Date(1460937600 * 1000),null],
    [new Date(1461024000 * 1000),null],
    [new Date(1461110400 * 1000),null],
    [new Date(1461196800 * 1000),null],
    [new Date(1461283200 * 1000),2],
    [new Date(1461369600 * 1000),null]
  ]);

  var options = {
    chartArea: {
      width: '70%',
      height: '70%'
    },
    hAxis: {
      format: 'd',
      gridlines: {
        count: 15
      },
      title: 'Day'
    },
    vAxis: {
      baseline: 0,
      format: '#',
      gridlines: {
        count: -1
      },
      title: 'Views',
      viewWindowMode:'explicit',
      viewWindow: {
        max: 10,
        min: 0
      }
    }
  };

  var chart = new google.visualization.ColumnChart(
    document.getElementById('chart_div')
  );

  chart.draw(data, options);
}
<script src="https://www.gstatic.com/charts/loader.js"></script>
<div id="chart_div"></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

Closing a dropdown menu when opening another one

As a beginner in Vue.js, I am currently working on a CRUD project for my coursework. One issue I am facing is with the behavior of the dropdown menu. Can anyone provide assistance? https://i.sstatic.net/1MozuN3L.png I have specific requirements: The dr ...

Retrieve the values from the getJSON request and provide a list

Need to search through a vast JSON file for a specific match on my webpage. The file is jam-packed with various values and objects, structured like this: name: john doe, id: 5891, description: product, name: jane doe, id: 5892, description: product, and ...

Learn the art of animating "basic jQuery filtering" exclusively with CSS

Does anyone have suggestions on how to animate elements when filtered by JavaScript? The code I've tried so far doesn't seem to be working. Here's what I currently have: http://jsfiddle.net/ejkim2000/J7TF4/ $("#ourHolder").css("animation", ...

The image source is visible in Vue.js, but unfortunately, my picture is not showing up

Below is the code and script that I have: <template> <div class="tasks_container"> <div class="tasks_content"> <h1>Tasks</h1> <ul class="tasks_list"> ...

Error message: "Unable to find a windows instance" encountered while conducting tests on Paho MQTT Client using mocha and typescript

After spending countless days searching online, I have yet to find any resources on testing the Paho MQTT Client. My approach so far has been somewhat naive, as shown below: import { suite, test, slow, timeout, skip, only } from 'mocha-typescript&apo ...

Quote unexpectedly sent by a bot - Unknown Identifier

Encountering a strange error message that reads like this: SyntaxError: Unexpected identifier at createScript (vm.js:80:10) at Object.runInThisContext (vm.js:139:10) at Module._compile (module.js:616:28) at Object.Module._extensions..js (m ...

Displaying dropdown options based on the previous selection made by the user

Can I link the data in my second dropdown to the selection made in the first dropdown? I tried a similar solution from stackoverflow without success. You can find the reference here. The code works when directly copied and pasted but not within my system. ...

What steps should I take to insert a divider in a dropdown menu?

I am attempting to enhance my dropdown menu by incorporating a separator using the following code: <li class='divider'></li> Below is my HTML code with the separator included: <ul class="dropdown-menu dropdown-menu-right" id="ul ...

JQuery requests functioning flawlessly on one system while encountering issues on other systems

I've encountered an issue with the code on my admin page. It used to work perfectly fine on my system, but now it seems to have stopped functioning. My client urgently needs to update this page, however, when I attempt to run it, the JQuery requests a ...

Unable to display Bootstrap Glyphicons within a div container

I've created the following code snippet: function acceptButtonClick(th) { var id = $(th).attr('data-id'); $(th).parent().parent().find('div.Declined').attr('class', "Approved"); $(th).parent().parent().find( ...

Using AngularJS with Spring MVC and @RequestParam

As a newcomer to AngularJS, I have a Spring controller that retrieves an object from the database based on its id. I want to pass this id using @RequestParam in AngularJS. However, when attempting to do so, I encountered the following error message: "Error ...

Distinguishing SharePoint React Webpart from Office: A Comparison

My goal is to develop a client-side Webpart in React for SharePoint Online that utilizes OfficeJs to initiate a new email message on the user's Outlook desktop. However, I'm encountering a persistent "Office is not defined" error no matter what ...

Angular JS throwing error: 'ngMessages' not initialized

Here is the code snippet I am working with: // LoginCtrl.js angular.module('homeon', [ 'ngMessages' ]).controller('loginCtrl', function($rootScope, $scope, $state, $ionicLoading, dbservices, server) { //------------ ...

Walls that collide in three.js

I am currently developing a game using three.js and despite being new to this field, I have extensively studied collision documentation. To handle collisions between my boat (inside a cube) and the islands (contained in cubes), I implemented raycasting. He ...

increasing the size of an array in react javascript

componentWillMount(){ this.adjustOrder(); } componentDidMount(){} adjustOrder(){ var reorderedArray = []; this.state.reservation1.length = 9; for (var i = 0; i < this.state.reservation1.length; i++) { if(this.state.reservation1[i]){ r ...

Numerous demands causing replacements of variable values

I am new to working with nodejs and I have encountered a situation where, upon hitting a specific endpoint, I need to perform two separate post calls. The first post call returns a URL that is required for the second call. Below is a simplified module I cr ...

How can I identify the main text of a specific <MenuItem/> component in ReactJS Material-UI?

I've been working with the Material-UI Dropdown Menu component and I'm trying to figure out how to console log the primaryText of the selected <MenuItem/>. Can anyone provide guidance on how to achieve this? ...

Prevent encountering the error message "Cannot read property of undefined" while mapping data

When I retrieve data from the Google Books API, I need to transform it into a more manageable array for further processing. To achieve this, I am using the following code snippet: data.items.map(function(book) { return { googleId : book.id, image : book ...

Guide on leveraging socket post connection event in socket.io

Apologies for any language barriers. I am facing an issue with my app.js server. I have implemented sockets and utilize the join event within the 'connection' event. A function inside the 'connection' event requires a socket parameter, ...

What is the best way to invoke a Rest API within a Vue component?

As a newcomer to VueJS, my goal is to create a basic page featuring a pie chart displaying some data. Currently, I have successfully displayed the chart using example data. However, I now wish to populate the chart with data fetched from an API call on my ...