score:1
You can specify the data
of your dataset
as objects with x/y
coordinates but only when using the time scale. Therefore your data would have to be defined as follows.
data: [
{ x: '1', y: 1 },
{ x: '2', y: 2 },
{ x: '3', y: 3 },
{ x: '5', y: 1 },
{ x: '6', y: 5 },
{ x: '7', y: 1 },
],
Together with this, you'll need to define the xAxis
as follows:
xAxes: [{
offset: true,
type: 'time',
time: {
parser: 's',
unit: 'second',
displayFormats: {
second: 's'
}
},
ticks: {
stepSize: 1
},
gridLines: {
offsetGridLines: true
},
}]
Please note that Chart.js uses Moment.js for the functionality of the time axis. Therefore you should use the bundled version of Chart.js that includes Moment.js in a single file.
Please have a look at the runnable code snippet below.
new Chart(document.getElementById("chart"), {
type: "bar",
data: {
datasets: [{
label: "My Dataset",
data: [
{ x: '1', y: 1 },
{ x: '2', y: 2 },
{ x: '3', y: 3 },
{ x: '5', y: 1 },
{ x: '6', y: 5 },
{ x: '7', y: 1 },
],
backgroundColor: ["rgba(255, 99, 132, 0.2)", "rgba(255, 159, 64, 0.2)", "rgba(255, 205, 86, 0.2)", "rgba(75, 192, 192, 0.2)", "rgba(54, 162, 235, 0.2)", "rgba(153, 102, 255, 0.2)"],
borderColor: ["rgb(255, 99, 132)", "rgb(255, 159, 64)", "rgb(255, 205, 86)", "rgb(75, 192, 192)", "rgb(54, 162, 235)", "rgb(153, 102, 255)"],
borderWidth: 1
}]
},
options: {
scales: {
yAxes: [{
ticks: {
beginAtZero: true,
stepSize: 1
}
}],
xAxes: [{
offset: true,
type: 'time',
time: {
parser: 's',
unit: 'second',
displayFormats: {
second: 's'
}
},
ticks: {
stepSize: 1
},
gridLines: {
offsetGridLines: true
},
}]
}
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.9.3/Chart.bundle.min.js"></script>
<canvas id="chart" height="80"></canvas>
score:1
uminder's answer is right, but the problem of using "seconds" as the time unit is that you cannot set values greater than 59 on the X axis. If you do, the chart will appear blank.
By just changing the time data to use years you will be able to set any value you want.
time: {
parser: 'Y',
unit: 'year',
displayFormats: {
year: 'Y'
}
}
Source: stackoverflow.com
Related Query
- Chart.js - Bar chart with linear cartesian (numerical) X axis?
- Chartjs 2 - Stacked bar and unstacked line on same chart with same y axis
- Chartjs 3.x - How to duplicate X axis on both sides of horizontal bar chart with only 1 dataset?
- chart js - bar chart with time scale on Y axis in Hours and Minutes
- Linear x axis for bar chart chartjs
- Issue with chartjs linear gradient for the mixed bar chart in ReactJS is not calculated for each individual Bars
- Chart.js bar chart with time on X axis and category on Y axis is not rendered
- ChartJS: Bar chart with axis ticks wider than categories
- ChartJS New Lines '\n' in X axis Labels or Displaying More Information Around Chart or Tooltip with ChartJS V2
- Horizontal stacked bar chart with chart.js
- Vertical stacked bar chart with chart.js
- Chart.js Mixed Bar and Line chart with different scales
- How do I draw a vertical line on a horizontal bar chart with ChartJS?
- How to display inline values in a stacked bar chart with Chart.js?
- Chart.js - Draw bar chart with multiple labels
- Show X axis on top and bottom in line chart rendered with Chart.js 2.4.0
- ChartJS bar chart with legend which corresponds to each bar
- Chart.js Dynamically Updating Chart with X Axis Time
- Obtain max value of y axis of line chart rendered with Chart.js
- Line chart with large number of labels on X axis
- angular-chart.js bar chart with 100% opacity color
- Chartjs - data format for bar chart with multi-level x-axes
- Fill Chart.js bar chart with diagonal stripes or other patterns
- How to hide the y axis and x axis line and label in my bar chart for chart.js
- Chart.js with dual axis on bar and line graph
- Stacked horizontal bar chart along total count with chart.js
- to increase space between x axis and first horizontal bar in chart js
- Stacked bar chart with rounded corner of bar using Chart.js
- ChartJS: create linear x-axis on bar chart
- Add buffer to Y axis with chart js
More Query from same tag
- ChartJs - Round borders on a doughnut chart with multiple datasets
- Line chart disable interpolation
- reset the zoom to the initial in chart.js?
- Remove/Hide (display:none) the graphical chart in small-mobile view - JS chart
- How do I create a legend for a bar chart?
- ChartJs, How can I get different color fills between my two datasets in a line graph?
- chartjs stepSize by 5
- Remove background color in chartjs line chart
- Highlight chart element when hovering over its corresponding legend Item
- Multiple canvas, reusable gradients
- Chart JS - Use time for xAxes
- How to make y axis only integer scaling in ChartJS?
- Drawing line chart in chart.js with json data
- ChartJS executes a function multiple times
- How to use chartjs-plugin-annotation in Angular
- Click event on stacked bar chart - ChartJs
- Where is the appropriate place to instantiate a javascript widget/chart/utility when using angular.js?
- How do I access Chart.js default settings? (& related -- how do I turn the tool tips on?)
- How to remove background color and color example from tooltip in Chart.js?
- Chartjs for Line chart, values on y-axis
- Type 'import("/@types/chart.js/index.d.ts")' is missing the following properties from type 'any[]': length, pop, push, concat, and 26 more
- Uncaught TypeError: [Array].filter is not a function
- chart.js: Chart in chart
- chart.js: set specific numbers in y-axis
- Is it possible to fill particular portion between given start & end angle in pie chart in chart.js?
- Getting chart.js to work with Django and Apache
- How to chart missing data in chartjs
- How to bind data from Controler to chartjs line chart to create it as dynamic?
- how can conditionally show/hide gridlines on the yAxis using Chart.js?
- Chart.js line from annotations plugin is not appearing