score:6
Accepted answer
Since you specified the x/y
coordinates of your data set, you have to set your xAxes
scale type as time
in your chart options.
var options = {
scales: {
xAxes: [
{
type: "time"
}
]
}
};
See official docs for all possible configurations or check the options used in the working example below.
Important: You have to change the date format in your data set to something like 'YYYY-MM-DD'
otherwise moment
would throw this warning.
Deprecation warning: value provided is not in a recognized RFC2822 or ISO format. moment construction falls back to js Date(), which is not reliable across all browsers and versions.
var data = {
labels: [
"2018-10-29",
"2018-10-30",
"2018-11-02",
"2018-11-14",
"2018-11-15",
"2018-11-19",
"2018-11-20",
"2018-11-28"
],
datasets: [{
pointRadius: 0,
label: "Positive",
lineTension: 0,
data: [{
x: "2018-11-15",
y: 18636
}],
borderWidth: 1,
backgroundColor: "rgba(0, 255, 0, 0.5)"
},
{
pointRadius: 0,
label: "Negative",
lineTension: 0,
data: [{
x: "2018-10-29",
y: -20480
},
{
x: "2018-10-30",
y: -284
},
{
x: "2018-11-02",
y: -1625
},
{
x: "2018-11-14",
y: -6622
},
{
x: "2018-11-15",
y: -12991
},
{
x: "2018-11-19",
y: -1645
},
{
x: "2018-11-20",
y: -1230
},
{
x: "2018-11-28",
y: -39612
}
],
borderWidth: 1,
backgroundColor: "rgba(255, 0, 0, 0.5)"
}
]
};
var options = {
scales: {
xAxes: [{
type: "time",
distribution: 'series',
time: {
displayFormats: {
day: 'D MMM, YY'
}
},
ticks: {
source: "labels"
},
gridLines: {
display: false
}
}]
}
};
var ctx = document.getElementById("myChart").getContext("2d");
var myBarChart = new Chart(ctx, {
type: "bar",
data: data,
options: options
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.23.0/moment.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.7.3/Chart.min.js"></script>
<canvas id="myChart" width="300" height="100"></canvas>
Source: stackoverflow.com
Related Query
- Chartjs 2.7.3: Set Y data at the correct X position axis
- How to set the chartjs bar graph scale to the highest value in the result data
- How to set the gap between data items in a chartjs chart
- How to set min max for x axis depend on data with Chartjs and Spring Boot?
- Using number/text input field to set the data values in ChartJs stops the chart from being displayed
- How to set ChartJS Y axis title?
- Chartjs change the specific label color in x axis in callback function
- Map event position to y axis value in chartjs line chart
- Changing the Y axis unit in Chartjs
- React ChartJS 2 : Get data on clicking the chart
- Custom data position on the doughnut chart in chart.js
- Chartjs: Is it possible to hide the data labels on the axis but show up on the graph on hover?
- How do I add time sourced from an external source as an X axis to a ChartJS graph?
- chartjs - how to set the order in which the different charts are displayed
- Ng2-charts + How to customize the position of X axis labels?
- ChartJS align axis label to the top
- How can I load multiple Chartjs charts with different data on the same page?
- Completely hide the Y-axis on chartjs when data is hidden
- Render chartjs and export image to data not in the DOM
- ChartJS Horizontal bars barely visible with 50 records on the Y axis
- How to get the database data into ChartJS using codeigniter
- How to reuse a Chartjs Chart component in with different Data and get past the **Canvas is already in use** error?
- How to set y axis to the max dataset value in Chartjs?
- ChartJS set styling on the last point only
- ChartJS x axis title not visible when axis position is centered
- Dynamically pass the JSON data in chartjs
- I can't change the legend position in Laravel Charts & ChartJS
- How to set ChartJS Axis label with PrimeFaces
- How to take a set of Date objects and display the frequency based on time of day using ChartJS
- How to show tooltip value of all data falling on the same axis in chart js?
More Query from same tag
- How to make ChartJS not cut off tooltips?
- Chart.js - style legend: smaller circles
- Chart JS change pointBackgroundColor based on data value
- Chartjs dynamic chart return both x & y axis undefined
- Bootstrap 3 tabs & Chart.js - charts not loading on tabs
- Add images inside bar chart in chart.js
- How can I show chartjs datalabels only last bar?
- Chartjs Data via json request not populating
- Chart.js pie chart overflowing div
- Render labels only when the data is available for a particular label
- How to create vertical arbitrary line in financial chart?
- Creating mathematical charts using chart.js jQuery
- ERROR TypeError: Cannot read property 'nativeElement' of undefined in Ionic 5 and chart.js
- How to Update beginAtZero in Chart.js
- ChartJS - legends and tooltips options
- Statistics page - Load every stats <div> one by one
- How to 1. pass two datasets and 2.have permanent label on charts in Chartjs
- Display values outside of pie chart in chartjs
- Why is Chart.js forEach is undefined
- ChartJS 3.7 data not dynamically updating with data streaming into CSV
- Apply color only to lower part of Chart.js line chart
- Having issues with running npm start after installing chartjs
- setting QuickCharts color
- Chartjs-plugin-zoom plugin does not change x axis labels
- Chart.js dataset options externally with functionality
- Two pie charts in one flexbox container
- Chart.js Polar Area Chart is not scale right
- Difficulty in using php json_encode data received in javascript to display graph using chart.js library
- Does ChartJS supports Jquery 3.4.1
- How can I add user inputted values to my chart.js line graph?