score:9
You can check the length of your labels (or data) arrays and add dummy non-renderable points to the left and right by using empty string labels and null value, like so
var chartData = {
labels: ['', "A", ''],
datasets: [
{
fillColor: "rgba(255, 52, 21, 0.2)",
pointColor: "#da3e2f",
strokeColor: "#da3e2f",
data: [null, 20, null]
},
{
fillColor: "rgba(52, 21, 255, 0.2)",
strokeColor: "#1C57A8",
pointColor: "#1C57A8",
data: [null, 30, null]
},
]
}
Fiddle - https://jsfiddle.net/pf24vg16/
score:0
Wanted to add to the above answer and say that I got a similar effect on a time series scatter plot using this:
if (values.length === 1) {
const arrCopy = Object.assign({}, values);
values.unshift({x: arrCopy[0].x - 86400000, y: null});
values.push({x: arrCopy[0].x + 2 * 86400000, y: null});
}
That only handles for a single point, however. To add in functionality for multiple points, I did the following:
const whether = (array) => {
const len = array.length;
let isSame = false;
for (let i = 1; i < len; i++) {
if (array[0].x - array[i].x >= 43200000) {
isSame = false;
break;
} else {
isSame = true;
}
}
return isSame;
}
if (values.length === 1 || whether(arr[0])) {
const arrCopy = Object.assign({}, values);
values.unshift({x: arrCopy[0].x - 86400000, y: null});
values.push({x: arrCopy[0].x + 2 * 86400000, y: null});
}
You might notice I'm just subtracting/adding a day in milliseconds into the x values. To be honest, I was just having the worst of times with moment.js and gave up haha. Hope this helps someone else!
Note: my code has a tolerance of 43200000, or 12 hours, on the time. You could use moment.js to compare days if you have better luck with it than I did tonight :)
score:0
For your specific problem, try to modify the options->scales->xAxes option like so:
options: {
title: {
display: true,
text: 'mytitle1'
},
scales: {
xAxes: [{
type: 'linear',
ticks: {
suggestedMin: 0,
suggestedMax: (11.12*2),
stepSize: 1 //interval between ticks
}
}],
More info at: Chart JS: Ignoring x values and putting point data on first available labels
score:22
Instead of hardcoding the labels and values with blank parameters, use the offset
property.
const options = {
scales: {
x: {
offset: true
}
}
}
Documentation: https://www.chartjs.org/docs/latest/axes/cartesian/linear.html#common-options-to-all-cartesian-axes
Source: stackoverflow.com
Related Query
- Chartjs linechart with only one point - how to center
- ChartJS - Line chart issue with only 1 point
- Chartjs 3.x - How to duplicate X axis on both sides of horizontal bar chart with only 1 dataset?
- Chartjs Radar chart - How to dynamically highlight one of the gridlines at a specific point
- Chartjs chart with only one axis
- How to Change the Y axis of an Primefaces 7.0 ChartJS Linechart with Java?
- chart.js - how to draw and manage line when only one label present in chart js Linechart
- How to display a progress over time in one horizontally stacked bar with chartjs
- ChartJS with React: Only one value showing on time chart
- Auto center ticks and values on graph when there's only one with chart.js
- how to change size of point in ChartJS
- ChartJs 2.0 How do I obtain point information being clicked upon?
- line chart with {x, y} point data displays only 2 values
- How to fix chart Legends width-height with overflow scroll in ChartJS
- How to display Line Chart dataset point labels with Chart.js?
- How to display value of only one datapoint in line chart
- How to change the color of legend in chartjs and be able to add one more legend?
- How to add an empty data point to a linechart on Chart.js?
- Draw two plots using chartjs over one another with transparency
- How to display only one label in a multi line char?
- How to customize Title position with ChartJS
- Chart.js LineChart how to show only part of dataset and support horizontal scrolling
- how to get React chartjs to resize back down with window
- How to work out Chartjs using TypeScript with Chartjs.definitelyTyped from github
- How to add ChartJS code in Html2Pdf to view image
- ChartJS - how to display line chart with single element as a line? (not as a dot)
- How to Change the Label Strike-Through with light gray on a ChartJS Doughnut?
- How to remove bars for those bars with zero value in Chartjs bar chart?
- How can I change color of only one column in Barchart using Chart.js
- ChartJS (React) Line Chart - How to show single tooltip with data and labels from 3 (multiple) dataset?
More Query from same tag
- React-chartjs options doesn't take effect
- Chart.js: Can't get a coordinate for value for x axis unless x axis has the exact same value
- Show label for every data point in line chart
- Chart.js barchart custom on hover method
- ChartJS, merge legends for multiple charts
- Change mysql x-axes date to day in chart.js
- Angular chart.js chart scale
- dump missing date in data for chartjs
- How to set max and min value for Y axis
- Chartjs displays numbers not time
- Chart.js shaded regions
- Chart.Js vers2 multiline to version 3
- ChartJS GridLine Dynamically
- iframes without sandbox attribute is a Vulnerability
- Ability to rotate y axis title in chart.js
- chart js draw line chart by points instead of by lines
- Chart JS Tooltip - place it in a fixed location outside the canvas
- chart.js bar chart datalabel vanishes on hovering over chart
- How to use Chart.js to draw mixed Financial / Candlestick and Bar Chart?
- ChartJS - Moving vertical line is display on top of tooltip
- text inside the bar - chart.js
- Chart.js add icon at the top of bar
- How to generate dynamic bar chart in laravel using chart.js
- Chart.js v3.60 - Add % symbol to the end of the tooltip label on a doughnut chart?
- Display values in Pareto chart using Chart.js 2.0.2
- Is there a way in chartjs to display different Boolean values with an offset in Y over a common timeline?
- Chart.JS TypeError: this.scale is undefined
- Cannot change the color of column chart according to its value
- Django chart.js multi axis line chart
- Chart JS Legend Styling