Backend:
export async function specificationValue(owner, start, end) {
let filter = wixData.filter().eq("_owner", owner).eq("specified", true).ge("_createdDate", start).le("_createdDate", end);
return await wixData.aggregate("projectproducts")
.filter(filter)
.sum("specificationValue")
.run()
.then((results) => {
if (results.items.length > 0) {
let specificationSumMonthCurrent = results.items[0].specificationValueSum;
let finalValue = specificationSumMonthCurrent; // Matthew: formats numbers to be separated by thousands
console.log(finalValue);
return (finalValue);
} else {
// handle case where no matching items found
let finalValue = 0; // Matthew: formats numbers to be separated by thousands
console.log(finalValue);
console.log("No specification value for this user");
return (finalValue);
}
})
.catch((error) => {
let errorMsg = error.message;
console.log(errorMsg);
let code = error.code;
console.log(code);
});
}
I’ve got this working, it returns 0 when it’s 0.
Then I’m running 12 functions like this on the page code to call the backend function ( specificationValue() ):
export async function month_current() {
let owner = $w('#recordId').text;
var date = new Date(); // full date today
var start = new Date(date.getFullYear(), date.getMonth(), 1);
var end = new Date(date.getFullYear(), date.getMonth() + 1, 0);
let month_number_current = date.getMonth(); // current month in a number 0 - 11
await specificationValue(owner, start, end).then(result => {
let month_current = result;
month_minus_1(owner, month_current, month_number_current);
})
.catch(error => {
console.log(error);
});
}
Once done all results are passed to a function to create an array & objects:
export async function createArrays(
owner,
month_current,
month_minus_1,
month_minus_2,
month_minus_3,
month_minus_4,
month_minus_5,
month_minus_6,
month_minus_7,
month_minus_8,
month_minus_9,
month_minus_10,
month_minus_11,
) {
let months = [
"January",
"February",
"March",
"April",
"May",
"June",
"July",
"August",
"September",
"October",
"November",
"December"
];
let currentMonth = new Date().getMonth();
let monthsB = months.splice((currentMonth + 1) % 12).reverse();
months = months.reverse();
months.push(monthsB);
months = months.flat();
let label = [
months[0],
months[1],
months[2],
months[3],
months[4],
months[5],
months[6],
months[7],
months[8],
months[9],
months[10],
months[11]
];
let data = [
month_current,
month_minus_1,
month_minus_2,
month_minus_3,
month_minus_4,
month_minus_5,
month_minus_6,
month_minus_7,
month_minus_8,
month_minus_9,
month_minus_10,
month_minus_11
];
let background_colour = [
"rgba(54, 162, 235, 0.2)",
"rgba(255, 206, 86, 0.2)",
"rgba(75, 192, 192, 0.2)",
"rgba(153, 102, 255, 0.2)",
"rgba(255, 159, 64, 0.2)",
"rgba(255, 99, 132, 0.2)",
"rgba(85, 162, 235, 0.2)",
"rgba(33, 206, 86, 0.2)",
"rgba(180, 192, 192, 0.2)",
"rgba(153, 102, 255, 0.2)",
"rgba(200, 159, 64, 0.2)",
"rgba(100, 99, 132, 0.2)"
];
let border_colour = [
"rgba(54, 162, 235, 1)",
"rgba(255, 206, 86, 1)",
"rgba(75, 192, 192, 1)",
"rgba(153, 102, 255, 1)",
"rgba(255, 159, 64, 1)",
"rgba(255, 99, 132, 1)",
"rgba(85, 162, 235, 1)",
"rgba(33, 206, 86, 1)",
"rgba(180, 192, 192, 1)",
"rgba(200, 102, 255, 1)",
"rgba(255, 159, 64, 1)",
"rgba(100, 99, 132, 1)"
];
let item = {
label: label,
data: data,
background_colour: background_colour,
border_colour: border_colour
};
console.log("item");
console.log(item);
await insertValues(item)
.then((popchart) => {
console.log("insert values result");
if (popchart === "success") {
$w('#specificationTimeline').show();
$w('#specificationTimeline').expand();
} else {
console.log("error populating chart");
}
});
}
Once done trying to get this info to a chart, following these instructions:
(I’ve got a sample chart working)
export async function insertValues(item) {
console.log("item");
console.log(item);
let chart = new ChartJSAPI($w('#specificationTimeline'));
chart.customization = chartCustomization;
const chartItems = item;
const labels = /* chartItems.map(item => */ Object.keys(chartItems.label); //);
const data = /* chartItems.map(item => */ Object.keys(Number(chartItems.data)); //););
const backgroundColor = /* chartItems.map(item => */ Object.keys(chartItems.backgroundColor); //););
const borderColor = /* chartItems.map(item => */ Object.keys(chartItems.borderColor); //););
chart.data = {
labels,
datasets: [{ data, backgroundColor, borderColor }]
}
let rendered = $w('#specificationTimeline').rendered;
if (rendered === true) {
return "success"
} else {
return "error"
}
}
So now the issue is not so much an if else, but getting this data onto a chart. @amandagm, any idea? Sorry for the deviation