score:5
you can achieve this using chart.js plugins. they let you handle specific events triggered during the chart creation (beforeinit
, afterupdate
, afterdraw
...) and are also easy to implement :
chart.pluginservice.register({
afterupdate: function(chart) {
// this will be triggered after every update of the chart
}
});
now you just have to loop through your dataset data model (the property used to draw charts) and add the offset you want :
chart.pluginservice.register({
afterupdate: function(chart) {
// we get the dataset and set the offset here
var dataset = chart.config.data.datasets[0];
var offset = 20;
// for every data in the dataset ...
for (var i = 0; i < dataset._meta[0].data.length; i++) {
// we get the model linked to this data
var model = dataset._meta[0].data[i]._model;
// and add the offset to the `x` property
model.x += offset;
// .. and also to these two properties
// to make the bezier curve fits the new graph
model.controlpointnextx += offset;
model.controlpointpreviousx += offset;
}
}
});
you can see your example working on this jsfiddle and here is its result :
score:2
leading on from the answer given by 'tektiv' i needed a similar solution but one that works for responsive charts.
so instead of using fixed measurements for the given offset shown in tektiv's plugin, we first count the number of objects in the dataset array. we then divide the chart.width by the number of objects in the array to give us equal segments, then in order to define the half way point between each grid line, we divide that sum by a factor of 2.
note 1: you could also replace the factor of 2 to a variable so the user could chose the portion of offset needed.
note 2: i've placed the plugin code within the chart script given i don't want this as a global affect by registering a global plugin.
note 3: this is second re-edit of my solution given the plugin code i partially copied from the answer given by 'tektiv' above was only firing successfully for the first time, but then when re-loading a new instance of the chart i experienced some null errors on the dataset._meta (worth also seeing answer here on this particular topic as this helped me fix and finalize my answer: dataset._meta[0].dataset is null in chartjs code example:
<script>
var mychart;
function drawchart() {
var ctx = document.getelementbyid('mychart').getcontext('2d');
ctx.innerhtml = '';
if (mychart != null) {
mychart.destroy();
}
var datasetarray = [5, 10.5, 18.2, 33.9, 121.2, 184.9, 179.9, 196.1, 158.3, 166.3, 66.4, 20.6, null];
mychart = new chart(ctx, {
type: 'line',
data: {
labels: ["jan", "feb", "mar", "apr", "may", "jun", "jul", "aug", "sep", "oct", "nov", "dec", ""],
datasets: [{
data: datasetarray,
borderwidth: 2,
bordercolor: "#f37029",
pointbordercolor: "#f37029",
pointbackgroundcolor: "#f37029",
pointhitradius: 10,
spangaps: false,
}]
},
plugins: [{
afterupdate: function (chart, options) {
//..
var dataset = chart.config.data.datasets[0];
// get the number of objects in the dataset array.
var nodatapoints = datasetarray.length;
//alert(nodatapoints); // testing only, you'll notice that this
// alert would fire each time the responsive chart is resized.
var xoffset = (chart.width / nodatapoints) / 2;
for (var i = 0; i < dataset.data.length; i++) {
for (var key in dataset._meta) {
var model = dataset._meta[key].data[i]._model;
model.x += xoffset;
model.controlpointnextx += xoffset;
model.controlpointpreviousx += xoffset;
}
}
}
}],
options: {
scales: {
xaxes: [{
gridlines: {
offsetgridlines: false,
display: true,
},
ticks: {
fontsize: 8,
maxrotation: 0
}
}],
yaxes: [{
gridlines: {
display: true
},
ticks: {
beginatzero: true,
}
}]
},
legend: {
display: false
},
responsive: true,
maintainaspectratio: true
}
});
}
</script>
first screenshot below shows the responsive chart stretched to a wide screen view:
second screenshot shows the responsive chart resized to a smaller and more conventional window size:
score:10
check out - http://www.chartjs.org/docs/latest/axes/cartesian/ .
in chapter "common configuration",there is a boolean attribute offset
. default value is false
(except in case of bar
chart)
if true, extra space is added to the both edges and the axis is scaled to fit into the chart area. this is set to true in the bar chart by default.
so you can just set it to true, and it should work.
Source: stackoverflow.com
Related Query
- How to add an offset to a dataset in Chart js
- How to add new dataset in line chart of chart.js by iterating over dictionary in JavaScript?
- How to add text inside the doughnut chart using Chart.js?
- Chart.js - How to set a line chart dataset as disabled on load
- How to add an on click event to my Line chart using Chart.js
- How to display Line Chart dataset point labels with Chart.js?
- Add DataSet Bar Chart - Chart.JS
- How to add second Y-axis for Bar and Line chart in Chart.js?
- How to add text in centre of the doughnut chart using Chart.js?
- How to add datas to chart js from javascript array itself?
- How to add panning to chart in chartjs?
- how to add a title to my ng2-charts bar chart
- Chart.JS: How to add Data to a specific dataset
- How to dynamically set ChartJs line chart width based on dataset size?
- How to add comma in this chart js
- How to add a dataset toggle to Chart.js?
- How to add gradient background to Line Chart [vue-chart.js]
- How can I add some text in the middle of a half doughnut chart in Chart.JS?
- How to add ChartJS code in Html2Pdf to view image
- How to draw a needle on a custom donut chart and add datapoints to the needle?
- How to add images to chart labels with vue-chartjs?
- How to add an image to a slice of a donut chart in chart.js?
- How to add background image in bar chart column in Chart.js?
- How to add label in chart.js for polar chart area
- How to add title inside doughnut chart in Angular Chart?
- ChartJS version 3 how to add percentage to pie chart tooltip
- Using chart js version 3, how to add custom data to external tooltip?
- How to add area with break on line chart with fill color
- How do I add time sourced from an external source as an X axis to a ChartJS graph?
- how to add FAHRENHEIT symbol in chart js donut chart
More Query from same tag
- How to hide radar chart index labels (chart.js)
- How to make pie chart from array values?
- In Chart.js and django, add data by clicking the button
- ng2 chart multiple chart update
- How to add a second set of labels to a Chart.js doughnut chart?
- Chart.js - Multiple Doughnut Charts on same Canvas
- Drawing a doughnut chart with columns inside to represent hourly stats in chart.js
- nodejs express jade mongodb project: how to import include chart.js
- how to modify horizontal bar chart using chart js
- ChartJS custom legend doughnut separate legend from chart area
- Meteor with Chart js cannot read property ‘getContext’ of null only AFTER new document in collection?
- How to generate color code dynamically by swapping 2 characters within a string using PHP
- How to Reverse Chart.js Label Order?
- Chartjs - Add backgroundColor for labels radar chart
- Chart.js line doesn't fit in
- Chart.js not installing with NPM
- How to grouping Label In Char JS
- Stepped line with null values in chart.js
- Bar chart does not appear with Chart.js
- Chart.js - Hover labels to display data for all data points on x-axis
- Chart.JS removing data stuck in loop
- ChartJS: Highlight dataset in a stacked bar chart when hovering over the legend?
- Resize chart.js canvas for printing
- Is it possible to print a chart with vue-chartjs?
- Chartjs v2.8 showAllTooltips does not exist
- Chart JS dynamic chart
- Chart.js multiTooltip labels
- Draw a mathematical function in chartjs
- react-chartjs: difference between `scales.x`/`xAxes`/`xAxes[]`
- how to display chart.js legend with different bar color