Utilizing Unix timestamps for x-values while displaying dates as x-labels in an ECharts line chart

I'm struggling with incorporating date-converted unix timestamps as values in my ECharts graph. The x-axis of my chart is meant to display the recording time for each buy or sell price, represented by respective lines. Everything functions properly when I use the raw timestamp (e.g. 1627353870000), but once I try to change the x-axis type to 'time', it transforms into a strange linear graph as shown below.

Data representation using timestamp

Incorrect display with 'time' type set

This snippet represents the key code block I am tackling:

var buy_price = {
    title: {
        text: 'Prices Comparison Chart'
    },
    tooltip: {
        trigger: 'axis'
    },
    legend: {
        data: ['Buy Price', 'Sell Price']
    },
    xAxis: {
        type: 'time', // problematic line (displays correctly without this)
        data: [1627353870000, 1627353810000, 1627353820000, 1627353830000]
    },
    yAxis: [{ min: 1,
             max: 100,
             type: 'value'
    }],
    series: [{
        name: 'Buy Price',
        type: 'line',
        symbol: 'none',
        data: [2, 4, 6, 8]
    },
    { 
        name: 'Sell Price',
        type: 'line',
        symbol: 'none',
        data: [1, 2, 3, 4]
    }]
};

I'm at a loss here; there doesn't seem to be adequate support available online for solving this issue within this library.

Answer №1

If you want to convert the dateValue into a dateString using "Date.parse()", and bind the dates to the prices, your code should resemble the following:

var option;

var data1 = [["2018-08-15T10:04:01.339Z",2],["2018-08-15T10:14:13.914Z",4],["2018-08-15T10:40:03.147Z",6],["2018-08-15T11:50:14.335Z",8]];

var data2 = [["2018-08-15T10:04:01.339Z",1],["2018-08-15T10:14:13.914Z",2],["2018-08-15T10:40:03.147Z",3],["2018-08-15T11:50:14.335Z",4]];

option = {
title: {
    text: 'Prices Comparison Chart'
},
tooltip: {
    trigger: 'axis'
},
legend: {
    data: ['Buy Price', 'Sell Price']
},
xAxis: {
    type:"time",
},
yAxis: {
         type: 'value'
},
series: [{
    name: 'Buy Price',
    type: 'line',
    symbol: 'none',
    data: data1
},
{
    name: 'Sell Price',
    type: 'line',
    symbol: 'none',
    data: data2
}
]
};

option && myChart.setOption(option);

Your graph would appear as shown in this image: Graph preview

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

IE and Firefox display different responses when encountering an empty XML document

When working with jQuery to read an XML file, I occasionally encounter the situation where the XML is empty. In this case, I anticipate that the error function (no_info) will be triggered because the file is not formatted as expected for the dataType. Int ...

Trouble with sending arguments to Express middleware

I'm currently working on developing a custom input validation middleware using Express. My aim is to create a middleware that takes 2 parameters in order to validate client input effectively. Despite referencing various sources, including the Express ...

What steps can I take to resolve the CLIENT_MISSING_INTENTS issue?

After diving into learning about discord.js, I've run into a bit of a roadblock. Despite trying to troubleshoot by searching online, I can't seem to resolve the issue. const Discord = require('discord.js'); // const Discord = require(&a ...

Retrieving custom data attributes from slides within a slick carousel

After the recent Slick carousel update to version 1.4, there have been changes in how data attributes are accessed from the current slide. The previous method that was working fine is as follows: onAfterChange: function(slide, index) { $('.projec ...

Are the fetch functions within getStaticProps and getServerSideProps equivalent to the fetch API native to web browsers?

I've been working with Next.js for some time now and I'm questioning the fetch API used in both getStaticProps and getServerSideProps. Below is my understanding of these functions: getStaticProps runs during build time and ISR getServerSidePro ...

Which specific html container or element can be found on the mymsn pages?

When accessing mymsn, users have the ability to personalize the content and layout of their webpage. I am curious about what type of container is being utilized for this customization - does it involve an html element, or perhaps javascript, or something e ...

Issue encountered while deploying Next.js application on vercel using the replaceAll function

Encountering an error during deployment of a next.js app to Vercel, although local builds are functioning normally. The issue seems to be related to the [replaceAll][1] function The error message received is as follows: Error occurred prerendering page &q ...

The JSON data response is not being properly displayed on the div element

I am having trouble with the success function in my ajax call. The data is processed correctly in the view and the ajax call works fine, but for some reason, the data is not getting appended to the div. Here is my jQuery: $(document).ready(function() { ...

Deploying a static website using Node.JS without relying on any frameworks

I am currently working on deploying static web pages, which include HTML, CSS, and JS files, onto Node.js without utilizing any frameworks such as Express. I started by placing all the necessary webpage files into a public folder and then called the index. ...

What is the solution for handling nested promises in Node.js?

I am currently using the node-fetch module for making API calls. I have a function that handles all the API requests and returns both the status code and the response body. The issue I'm facing is that when returning an object with the response data, ...

When using React.js Material UI's CardActionArea with a flex display, the children elements may not automatically use the full width as expected

Getting straight to the point - I recently experimented with changing the display style property from block to flex, along with setting flexDirection: 'column' for the CardActionArea component. The main goal was to ensure that CardContent maintai ...

Efficiently handling multiple responses in Express.js per single request

Currently exploring Node.js and working on a bot utilizing Dialogflow. I am aiming to establish a straightforward process: Step 1: Dialogflow sends a POST request with parameter param1 Step 2: My application replies with a waiting message (e.g., "your re ...

What is the best way to populate a dropdown menu by matching keys from an array within an ng-repeat

My JSON structure looks like this: 101 : "List": [ { "Name": "Pink" }, { "Name": "Black" } ] 102 : "List": [ { "Name": "Red" }, { "Name": "Yellow" } ] $sco ...

Docusaurus font loading specifically optimized for body text, excluding headings

I've added the following CSS code to my Docusaurus custom stylesheet: @import url("https://fonts.googleapis.com/css2?family=Montserrat:ital,wght@0,400;0,500;0,600;0,700;1,400;1,500;1,600;1,700&display=swap"); :root { --ifm-color- ...

Tips on preserving type safety after compiling TypeScript to JavaScript

TS code : function myFunction(value:number) { console.log(value); } JS code, post-compilation: function myFunction(value) { console.log(value); } Are there methods to uphold type safety even after the conversion from TypeScript to JavaScript? ...

Italian calendar conversion for the react-multi-date-picker library

I recently integrated the react-multi-date-picker into my project, utilizing the multiple mode feature. However, I encountered an issue when trying to display the Italian calendar based on the language setting. Despite using locale="it", the calendar was n ...

Struggling with passing parameters through a route and displaying them in the Reddit app using React?

I'm currently working on a project that involves displaying threads from various subreddits when a user clicks on a list item using routes and react. However, I've encountered some issues with getting the information to display correctly. Below i ...

Adjust the language code in a URL using JavaScript or Node.js

Within my common header file, there is a navbar that includes a multilanguage dropdown menu. The issue I am facing is that when I select a language from the dropdown, the page translates correctly. However, when I navigate to other pages, the selected lang ...

Change the background of the play/stop icon with a jquery toggle

There is a list of video links with play icons as backgrounds in front of them. When a user clicks on a link, the video will start playing in a player located to the left of the links. The clicked link's background icon changes to a 'stop' i ...

Blend the power of Dynamic classes with data binders in Vue.js

Recently, I've been working on a v-for loop in HTML that looks like this: <ul v-for="(item, index) in openweathermap.list"> <li>{{item.dt_txt}}</li> <li>{{item.weather[0].description}}</li> <li>{{item.w ...