Adjust the cursor resolution to minutes on amCharts while maintaining the axis in days

Setting a custom date format for the dateAxis tooltip is necessary to display the hour, minute, and second.

If only one day with different times is set, it displays as expected with the proper time in the tooltip. However, when more than one day is set in the data (which is required), 12:00:00 always appears as the time, and moving the cursor only navigates between days, not individual points with different times.

While the scale in days is functioning correctly, how can the cursor be moved to each point in the data and show the corresponding time?

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

function amRangeAreaChart() {

am4core.ready(function () {

    am4core.useTheme(am4themes_animated);

    var chart = am4core.create("amLineChart", am4charts.XYChart);
    chart.hiddenState.properties.opacity = 0; // this creates initial fade-in

    var data = [];
    var open = 100;
    var close = 250;

    for (var i = 1; i < 30; i++) {
        open += Math.round((Math.random() < 0.5 ? 1 : -1) * Math.random() * 4);
        close = Math.round(open + Math.random() * 5 + i / 5 - (Math.random() < 0.5 ? 1 : -1) * Math.random() * 2);
        data.push({ date: new Date(2018, 1, i, 11, i, i), open: open, close: close });
    }

    chart.data = data;
    var dateAxis = chart.xAxes.push(new am4charts.DateAxis());
    var valueAxis = chart.yAxes.push(new am4charts.ValueAxis());
    valueAxis.tooltip.disabled = true;
    var series = chart.series.push(new am4charts.LineSeries());

    series.dataFields.dateX = "date";
    series.dataFields.openValueY = "open";
    series.dataFields.valueY = "close";

    dateAxis.tooltipDateFormat = "yyyy-MM-dd / hh:mm:ss";

    series.tooltipText = "temp: {openValueY.value}";
    series.sequencedInterpolation = true;
    series.fillOpacity = 0.3;
    series.defaultState.transitionDuration = 1000;
    series.tensionX = 0.8;

    var series2 = chart.series.push(new am4charts.LineSeries());
    series2.dataFields.dateX = "date";
    series2.dataFields.valueY = "open";
    series2.sequencedInterpolation = true;
    series2.defaultState.transitionDuration = 1500;
    series2.stroke = chart.colors.getIndex(6);
    series2.tensionX = 0.8;

    chart.cursor = new am4charts.XYCursor();
    chart.cursor.xAxis = dateAxis;
    chart.scrollbarX = new am4core.Scrollbar();

});

}

Answer №1

When dealing with dates in amCharts, it's advisable to set the baseInterval property of your dateAxis. The relevant documentation can be found here: DateAxis – amCharts 4 Documentation

If you have seconds as part of your data, make sure to explicitly instruct the library to display them.

A quick fix to this issue can be accomplished with just one line of code.

am4core.ready(function () {

    am4core.useTheme(am4themes_animated);

    var chart = am4core.create("amLineChart", am4charts.XYChart);
    chart.hiddenState.properties.opacity = 0; // initiates fade-in effect

    var data = [];
    var open = 100;
    var close = 250;

    for (var i = 1; i < 30; i++) {
        open += Math.round((Math.random() < 0.5 ? 1 : -1) * Math.random() * 4);
        close = Math.round(open + Math.random() * 5 + i / 5 - (Math.random() < 0.5 ? 1 : -1) * Math.random() * 2);
        data.push({ date: new Date(2018, 1, i, 11, i, i), open: open, close: close });
    }

    chart.data = data;
    var dateAxis = chart.xAxes.push(new am4charts.DateAxis());
    // ==================================================
    dateAxis.baseInterval = { timeUnit: "second", count: 1 }; // <--- ADJUST HERE
    // ==================================================
    var valueAxis = chart.yAxes.push(new am4charts.ValueAxis());
    valueAxis.tooltip.disabled = true;
    var series = chart.series.push(new am4charts.LineSeries());

    series.dataFields.dateX = "date";
    series.dataFields.openValueY = "open";
    series.dataFields.valueY = "close";

    dateAxis.tooltipDateFormat = "yyyy-MM-dd / hh:mm:ss";

    series.tooltipText = "temp: {openValueY.value}";
    series.sequencedInterpolation = true;
    series.fillOpacity = 0.3;
    series.defaultState.transitionDuration = 1000;
    series.tensionX = 0.8;

    var series2 = chart.series.push(new am4charts.LineSeries());
    series2.dataFields.dateX = "date";
    series2.dataFields.valueY = "open";
    series2.sequencedInterpolation = true;
    series2.defaultState.transitionDuration = 1500;
    series2.stroke = chart.colors.getIndex(6);
    series2.tensionX = 0.8;

    chart.cursor = new am4charts.XYCursor();
    chart.cursor.xAxis = dateAxis;
    chart.scrollbarX = new am4core.Scrollbar();

});
#amLineChart {
  width: 100%;
  height: 350px;
}
<script src="https://cdn.amcharts.com/lib/4/core.js"></script>
<script src="https://cdn.amcharts.com/lib/4/charts.js"></script>
<script src="https://cdn.amcharts.com/lib/4/themes/animated.js"></script>

<div id="amLineChart"></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

Design a webpage using material-ui components

I'm struggling to create a layout using material-ui for a child page. Can someone guide me on how to achieve this design? https://i.sstatic.net/TwYR5.png ...

How can I trigger an API call 30 seconds after my React Native app switches to the background?

Seeking assistance with my React Native app: I am trying to make an API call 30 seconds after the app goes into the background. I am using AppState from react-native to detect when the app goes into the background, but I'm not receiving any callback a ...

The renderValue property is malfunctioning in the Material-UI native Select component

Whenever I utilize the native prop in the MUI Select component, the renderValue prop fails to function properly. Additionally, if I attempt to assign a custom value to the value prop, it does not display. Take a look at the code snippet below: const [selec ...

Unable to retrieve the value of a selected table cell from dynamically generated HTML tables

I've gone through a plethora of Google and Stack Overflow examples To showcase my problem, I have set up a fiddle. Issue: "Upon clicking on a person's name, I want to retrieve their name from the first column." While I managed to create a click ...

Troubleshooting issue: Utilizing $(this) with Vue.js in jQuery method is not functioning as

Initially, I understand that combining jQuery with Vue is not advisable. Nevertheless, I am attempting to modify an element after a click event but encountering issues with $(this). methods: { openSMS() { $(this).hide(); // <-- facing difficulty ...

Retrieving Specific Node Value in Array From XML Document

In my JavaScript function, I have a variable named "result". The value of the variable "result" is an XML. I need to create an array containing only the values of the opportunityid (highlighted in the image). How can I extract the particular node value ...

What is the appropriate command for "building" a fresh NPM project?

I'm currently working on a JS website with Three.js. I kicked off my project like this: $ npm init $ npm install three Can anyone guide me on where to place my code utilizing Three.js, and which npm command should I use to "compile" my script for dep ...

Angular ng-bind is incorrectly displaying the value as 'object' instead of the intended value

I am working on an Angular app where I am retrieving data from a service in the following manner: angular.module('jsonService', ['ngResource']) .factory('MyService',function($http) { return { getItems: ...

retrieve parameters from Express using an intercepting middleware

I am seeking a way to capture a req.params item in an interception layer: Imagine having an instance of an express server application like this: const app = express(); along with an interception layer: app.use((req, res, next) => { console.log(req ...

Is it possible for JQuery to recognize the addition of new classes in real-time, such as a newly posted comment?

My micropost/commenting system currently refreshes the page every time a new micropost or comment is submitted, but I want to update it to add posts without refreshing the entire page using AJAX. I have started implementing AJAX on my form with the follow ...

Learn the process of updating an Array with a list of values based on checkbox selection and dynamic filtering using AngularJS

$scope.selectObjectType = function () { $scope.selected = []; // reset previous selection $scope.model.allItemsSelected = true; // all are selected by default unless... // If any object type is not checked, then uncheck the "allItemsSelected" ...

Struggling to enable Google Cast functionality on Apache Cordova: Unhandled error - chrome is not recognized

Struggling to integrate Google Cast with Apache Cordova, I'm facing challenges due to outdated guides and plugins. Despite finding a recently updated plugin three months ago, I keep encountering this error: Uncaught ReferenceError: chrome is not defi ...

What steps do I need to take to ensure that my angular application can be displayed properly on Internet Explorer

I am facing an issue with my application not rendering in ie8. I have added all the necessary shim and shiv scripts, but it still doesn't work. The strange thing is that my application runs perfectly on every other browser version above ie9. Any help ...

The text color remains the same even after the method has returned a value

I have a vuex query that returns a list of books. I want to display each book with a specific color based on its status. I tried using a method to determine the color name for each book and bind it to the v-icon, but it's not working as expected - no ...

Perform a function dynamically once a specific textfield is filled and continuously while filling another textfield

Imagine we have two text fields - one for a first name and one for a surname. As I type in the surname field after already filling out the first name, a function called sug() should provide suggestions or perform some action each time I add another letter ...

Designing a Custom Wordpress Extension and Integrating External Scripts

As I dive into the world of WordPress plugin development, I'm seeking guidance from the codex to enhance my skills. Currently, I have a basic plugin that loads a javascript file from a CDN and is supposed to display tooltips. However, I'm facing ...

Tips for setting a default value in a Multi Select component with reactjs and Material UI

Is it possible to set a default value on a Multiple selection (CHIP) using reactjs and material ui? Despite searching extensively online, I have not been able to find any relevant documentation addressing this issue. import * as React from 'react&apos ...

Guide on creating a Sequelize query to retrieve all tasks linked to a specific user_id

I'm currently developing my first to-do list application using node express ejs and sequelize with sqlite Below is my sqlite.js file which contains the database schemas: const Sequelize = require("sequelize"); const sequelize = new Sequelize({ dia ...

Retrieving Progress Updates from a PHP Script Using XMLHttpRequest

I am currently using JavaScript to execute an XMLHttpRequest to a PHP script that returns data. My goal is to display a progress bar for the user instead of a spinning circle, indicating the progress of fetching and receiving the data. While I understand t ...

Exploring the Depths of NodeJS X-Ray Web-Scraper: Uncovering Hidden Gems within Sub Pages

Currently, I am attempting to scrape content using the node.js x-ray scraping framework. While I have successfully retrieved data from a single page, I am struggling with navigating through links and extracting content from subpages simultaneously. Althou ...