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
- angular-chart.js, how to change colours labels
- How to position chart details next to a chart responsive for multiple devices?
- Python list does not work in django with ChartJS
- UndescoreJS - Convert Object Properties Into Arrays
- Issues getting JSON string for chart.js data
- Styling problems with Chart.Js
- chart.js zeroLineColor does not work as expected
- Show data dynamically in line chart - ChartJS
- How to make Chart.js with dynamic months on x-axis
- ChartJS - Injecting data from the server-side
- Chart.js not showing all labels on pie chart
- How to create a variable for chart js datalabel plugin formatter
- How do you make a progressive line chart with time as the X axis?
- chart.js v2.0.0 redrawing the chart
- Chart JS - Make chart scrollable if too much data
- Chart.js with plotting durations against dates
- ng2-charts access base chart object
- Trying to display different types of charts using select/option tag
- Display two datasets from an array using chart.js in node.js
- Chart.js Update start date with button selector
- Updating chart.js based on selections from dropdown
- ChartJS using AJAX call to retrieve data isn't rendering visualization
- How I create a pie chart based on COUNT(php + mysql)?
- Chartjs splitting double digit numbers
- Create data in data with Chartjs
- ChartJS not showing from MySQL
- How to remove transparency from images downloaded in Chart.js
- How to make 2 bar in a certain section of chart
- Chartjs not rendering chart and no error thrown
- How to set a full length background color for each bar in chartjs bar