Articles in this section
Category / Section

How to plot date time values on the vertical axis?

4 mins read

This article explains how to plot date time values on the vertical axis in the React Chart.

Customize the plotting of date time values on the vertical axis

You can plot date time values on the primary y axis(vertical axis) by using the axisLabelRender event.

In the following code example, the primary y axis values are customized with date time values.

Index.js

import { createRoot } from 'react-dom/client';
import * as React from 'react';
import { ChartComponent, SeriesCollectionDirective, SeriesDirective, Inject, LineSeries, DateTime} from '@syncfusion/ej2-react-charts';

export let data = [
   { x: new Date(2005, 0, 1), y: 1107194400000 },
   { x: new Date(2006, 0, 1), y: 1138730400000 },
   { x: new Date(2007, 0, 1), y: 1170268200000 },
   { x: new Date(2008, 0, 1), y: 1201804200000 }
];

const Line = () => {
const axisLabelRender = (args) => {
 if (args.axis.name === 'primaryYAxis') {
   const date = new Date(1970, 0, 1);
   date.setMilliseconds(args.value);
   const hours = date.getHours();
   const amOrPm = hours >= 12 ? 'PM' : 'AM';
   const formattedHours = hours % 12 || 12;
   const formattedDate = `${date.getDate()}/${
     date.getMonth() + 1
   }/${date.getFullYear()} ${formattedHours}:${date.getMinutes()}:${date.getSeconds()} ${amOrPm}`;
   args.text = formattedDate;
 }
};

return (
     <ChartComponent
       id="charts"
       primaryXAxis={{
         valueType: 'DateTime',
         edgeLabelPlacement: 'Shift',
         labelFormat: 'y'
       }}
       primaryYAxis={{
         rangePadding: 'None'
       }}
       axisLabelRender={axisLabelRender.bind(this)}
     >
       <Inject services={[LineSeries, DateTime]} />
       <SeriesCollectionDirective>
         <SeriesDirective
           dataSource={data}
           xName="x"
           yName="y"
           width={2}
           type="Line"
           marker={{
             visible: true,
             width: 7,
             height: 7,
             isFilled: true
           }}
         ></SeriesDirective>
       </SeriesCollectionDirective>
     </ChartComponent>
  );
};
export default Line;

const root = createRoot(document.getElementById('sample'));
root.render(<Line />);

The following screenshot portrays the results of the code snippet mentioned above.

Screenshot:

React chart vertical axis as datetime axis

View Sample in Stackblitz

Conclusion

I hope you enjoyed learning how to plot date time values on the vertical axis in the React Chart Component.

You can refer to our React Chart feature tour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications. You can also explore our React Chart example to understand how to create and manipulate data.

For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our other controls.

If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forums, support portal, or feedback portal. We are always happy to assist you!

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please  to leave a comment
Access denied
Access denied