score:1
You should define your x-axis as a time cartesian axis and provide the data as individual points, where each element has a t
and an y
property (also take a look at this answer). Using Array.map()
, this can be done as follows:
data['Julian dates']
.map((d, i) => ({ t: julianIntToDate(d), y: data['ndvi values'][i] }))
Note that I use the
julianIntToDate
function provided by Stephane BOISSEAU in this answer to convert the Julian dates into regular dates.
Please take a look at the runnable code below and see how it works. While this is plain JavaScript, it should be easy to make it also work with react-chartjs-2
.
const data = {
'ndvi values': [0.1158, 0.1975, 0.1913, 0.2137, 0.1603, 0.13, 0.246, 0.249, 0.1955, 0.124, 0.1854, 0.2721, 0.2095, 0.1357, 0.2444],
'Julian dates': [2458208.0, 2458386.0, 2458476.0, 2458566.0, 2458653.0, 2458746.0, 2458836.0, 2458921.0, 2459026.0, 2459111.0, 2458391.0, 2458476.0, 2458566.0, 2458746.0, 2458836.0]
};
// convert a Julian number to a Gregorian Date (S.Boisseau / BubblingApp.com / 2014)
function julianIntToDate(n) {
var a = n + 32044;
var b = Math.floor(((4*a) + 3)/146097);
var c = a - Math.floor((146097*b)/4);
var d = Math.floor(((4*c) + 3)/1461);
var e = c - Math.floor((1461 * d)/4);
var f = Math.floor(((5*e) + 2)/153);
var D = e + 1 - Math.floor(((153*f) + 2)/5);
var M = f + 3 - 12 - Math.round(f/10);
var Y = (100*b) + d - 4800 + Math.floor(f/10);
return new Date(Y,M,D);
}
new Chart('myChart', {
type: 'scatter',
data: {
datasets: [{
label: 'ndvi values',
data: data['Julian dates'].map((d, i) => ({ t: julianIntToDate(d), y: data['ndvi values'][i] })),
borderColor: 'blue',
}]
},
options: {
scales: {
yAxes: [{
ticks: {
beginAtZero: true
}
}],
xAxes: [{
type: 'time',
time: {
unit: 'month',
displayFormats: {
month: 'YYYY/M'
},
tooltipFormat: 'YYYY/M/D'
}
}]
}
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.9.4/Chart.bundle.min.js"></script>
<canvas id="myChart" height="90"></canvas>
score:0
Try this library: Chart.Scatter/
It is an add-on to chart.js that may help you.
Source: stackoverflow.com
Related Query
- scattered graph with xAxes of date react-chratjs-2
- Graph streaming real-time data with react and chartjs
- load a graph with chart.js and react
- Chart.js - Plot line graph with X , Y coordinates
- Chart.js Bar graph with percentage values
- Using Charts.js with react
- Chartjs v2 xAxes label overlap with scaleLabel
- Line graph with linear timescale in Chart.js
- Chart.js with React
- Create a rounded bar graph with Angular and chartJS
- how to get React chartjs to resize back down with window
- Chart.js with dual axis on bar and line graph
- Chartjs line graph dataset with offset
- Chart.js - Plot line graph with X , Y coordinates and connected by line
- Maintain Y axis scale in graph with scroll in the X axis when the dataset grows bigger. Aspect-ratio problem
- Chartjs drill down issue with multiple Y-Axis Bar/Line graph
- Charts.js graph with multiple Y Axes
- Creating a Normal Distribution graph with Chart.js
- Dynamically change type with react-chartjs-2 React
- how to use chart.js with react Hook?
- Update Chart JS with date range selector
- Deleting and recreating an element with React
- React chart : prevent chart's canvas scaling with height and width
- chartjs-plugin-zoom not working with my React project
- How to Create Chart.JS Bar Graph with Min, Max & Average
- Chart.js xAxes Date Labels are altered based on screen width
- Chart.js - Vertical crosshair (vertical annotation that moves with mouse) in line graph
- Draw asynchronous information on a graph with VueJS
- Laravel PDF generation with Graph and send it with Email
- how to draw line graph with line animation from left to right using chartjs?
More Query from same tag
- Multiple Graphs on one page
- setting QuickCharts color
- Error when adding data on top of each bar
- Chartjs annotations-plugin: Can I add multiple vertical lines to identically named x-axis labels?
- How to read and plot json with Chart.js
- How to make rectangle in chart.js
- Edit tooltips in ChartJS
- chartjs - top and bottom padding of a chart area
- Stacked Horizontal Bars not in align Chart JS
- Unable to load Chart.js data from getJSON / PHP?
- line charts is higher that the dataset in Chart.JS
- How to force animation on chartjs?
- chartjs xaxis ticks with round values from shifted input data
- Increase point radius when tootip shown for certain data on Charts.js
- Add an image as background in ChartJS chart area [Not to canvas]
- custom label x-axis in chart.js
- Can't resolve 'chart.js/auto'
- ChartJS/High Charts Radar chart - Different radial axis labels for each category that appear on hover
- Ng2-charts Time xAxes : unwanted high zoom on click
- ChartJS and Flask, how to pass data?
- how to display last x float number value in chartjs
- ChartJS doughnut chart not displaying colours
- How to pass my array of objects into a graph with Chart.js
- Cannot initialize a pie chart with Chart.js
- I search additive options on chartsjs
- chart.js: set specific numbers in y-axis
- Rotate left legend chartjs
- Creating dynamic arrays in Angular2
- chartjs - Not taking the height in reponsive chart
- Vue3 Chart.js not rendering