Replies: 16 comments
-
This is as designed at the moment to allow for creating scatter charts |
Beta Was this translation helpful? Give feedback.
-
Hey :) I didn't see much point of creating a js fiddle for this ... I think you get it anyway. Is there anyway I could modify this designed behaviour by overriding some of the functions ? Thank you for your help! If not possible it would be a great feature. ------ more explanation if needed ------ The green graph shows the "wrong info" It suggest that on Jun 6 there was some units yet there was nothing. I have to submit 0 as a value for every single date in the graph to get the red line showing (which I have just drawn in Photoshop). This is a pain :) because that is not how naturally comes out of mysql for example. If you do something like If you have no data for a one particular day that date will not be in the result. So I have to somehow bodge 0 values to display on the graph ... |
Beta Was this translation helpful? Give feedback.
-
@turigeza could you post what your data looks like? I think I have an idea of how to make this work. If you specify your data like the following it might work the way you want data: {
datasets: [{
data: [{
x: x value,
y: y value
}, {
x:,
y:
}]
}]
} |
Beta Was this translation helpful? Give feedback.
-
You were right i should have started with a js fiddle :) The data looks just like you did there ... Here is the link for an example: I would like the red line to behave like the green one. Aka default to 0 on the date 2016-02-03 without me having to supply the data for that date. |
Beta Was this translation helpful? Give feedback.
-
That's going to take some work to support. it might be easier to simply add the 0s into the data. |
Beta Was this translation helpful? Give feedback.
-
Thank you for your help ... I will modify the data so it contains default values for each date now. I was hoping there might an already existing option or some way of tapping into a function which gets called on every x value but after that I realised you are possibly drawing the points in the order I give you the data and not in order of x. It is worth mentioning in this case I have to recreate the same logic as the time scale on the xAxis does aka generate unique dates for every day. Anyway I go and do it :)) Thank you again taking you time :) |
Beta Was this translation helpful? Give feedback.
-
Yup, the points are drawn in the order given to allow for charts that loop back. It is annoying to have to do all the date work. I will keep this issue open for any future work we may do on it |
Beta Was this translation helpful? Give feedback.
-
I have exactly the same problem. I think this is a really common problem for time-based charts. May it be clicks/sells/.. which are stored/aggregated in a database like |
Beta Was this translation helpful? Give feedback.
-
I have written a workaround plugin: var fillScatteredTimeScaleDataPlugin = {
beforeUpdate: function(c) {
var timeAxis = c.options.scales.xAxes[0].time;
if (!timeAxis || !timeAxis.fillGapsWithZero) return;
for (var i=0;i<c.data.datasets.length;i++){
var set = c.data.datasets[i];
var min, max, hash = {};
for (var j=0;j<set.data.length;j++){
var val = moment(set.data[j].x, timeAxis.parser);
if (!min || min.diff(val)>0)
min = val;
if (!max || max.diff(val)<0)
max = val;
hash[set.data[j].x] = 1;
}
for (var val = min; max.diff(val)>0; val.add(1, timeAxis.minUnit)){
var d = val.format(timeAxis.parser);
if (!hash[d])
set.data.push({x:d, y:0});
}
set.data.sort(function(a,b){
return a.x < b.x?-1:1;
});
}
}
}
Chart.pluginService.register(fillScatteredTimeScaleDataPlugin); Usage:
|
Beta Was this translation helpful? Give feedback.
-
Wow that was fast : ) I thought I might post the solution I had but there is no point now : D |
Beta Was this translation helpful? Give feedback.
-
@cyberbeat , I'm trying to use your plugin, but on page load getting an error that moment is not defined. This is with Chart.bundle.js v2.6.0. Any advice? |
Beta Was this translation helpful? Give feedback.
-
I don't use the bundle, but include chartjs and moment separatly. |
Beta Was this translation helpful? Give feedback.
-
Yes, I had to include moment separately. Seems like your plugin must only work for an older version of Chart.js, no? It's not OK for multiple datasets, and there is something screwy going on with the timezone or something, but I currently have this:
|
Beta Was this translation helpful? Give feedback.
-
Any updates on this? I have the same problem. |
Beta Was this translation helpful? Give feedback.
-
+1 would like this feature |
Beta Was this translation helpful? Give feedback.
-
I ended up doing this before showing the data. private normaliseSerie(dataSet: ChartData['serie']): ChartData['serie'] {
let normalisedSerie: ChartData['serie'] = [];
for (let index = 0; index < dataSet.length; index++) {
const dataPoint = dataSet[index];
normalisedSerie.push(dataPoint);
if (index < dataSet.length - 1) {
const currentDate = dataPoint.day;
const nextDate = dataSet[index + 1].day;
const differenceInDays = Math.round(
(nextDate.getTime() - currentDate.getTime()) / (1000 * 3600 * 24),
);
if (differenceInDays > 1) {
for (let i = 1; i < differenceInDays; i++) {
const missingDate = new Date(currentDate.getTime() + i * (1000 * 3600 * 24));
normalisedSerie.push({ day: missingDate, value: 0 });
}
}
}
} where the data types are: export interface DataPoint {
day: Date;
value: number;
}
export interface ChartData {
name: string;
serie: DataPoint[];
axis: Axis;
} |
Beta Was this translation helpful? Give feedback.
-
My apologies for asking it here. I did ask on stackoverflow but no luck. Any help would be greatly appreciated.
http://stackoverflow.com/questions/39547512/default-0-value-for-missing-data
I would like missing Y values to default to 0 without me explicitly submitting the value as 0.
On the screenshot the data what is in the table was plotted onto the chart.
It is displayed in green. The red is what I would like to achieve. Notice how I have only got data for 7 days.
Is there any way that I can achieve this? In someway I would have expected this to be the default behaviour ... so it might just be an option I can not find in the docs.
The code is similar to this one:
Beta Was this translation helpful? Give feedback.
All reactions