1
0
mirror of https://github.com/elisspace/FxLifeSheet.git synced 2026-08-29 15:44:00 +00:00

Add 2-step graph printing

This commit is contained in:
Felix Krause
2019-05-28 12:01:50 +02:00
parent 6351e0f9c0
commit 732f1d5cf7
2 changed files with 96 additions and 52 deletions

View File

@@ -42,14 +42,16 @@ function getButtonText(number) {
}
return emojiNumber + " " + currentlyAskedQuestionObject.buttons[number];
}
function printGraph(key, ctx, numberOfRecentValuesToPrint, additionalValue) {
function printGraph(key, ctx, numberOfRecentValuesToPrint, additionalValue, skipImage) {
// additionalValue is the value that isn't part of the sheet yet
// as it was *just* entered by the user
var loadingMessageID = null;
ctx.reply("Loading history...").then(function (_a) {
var message_id = _a.message_id;
loadingMessageID = message_id;
});
if (numberOfRecentValuesToPrint > 0) {
ctx.reply("Loading history...").then(function (_a) {
var message_id = _a.message_id;
loadingMessageID = message_id;
});
}
rawDataSheet.getRows({
offset: 0,
limit: 100,
@@ -72,7 +74,7 @@ function printGraph(key, ctx, numberOfRecentValuesToPrint, additionalValue) {
var value = Number(rows[i].value);
allValues.unshift(value);
allTimes.unshift(time.format("MM-DD"));
if (i < numberOfRecentValuesToPrint) {
if (i < numberOfRecentValuesToPrint - 1) {
rawText.unshift(time.format("YYYY-MM-DD") + ": " + value.toFixed(2));
}
if (value < minimum) {
@@ -85,24 +87,31 @@ function printGraph(key, ctx, numberOfRecentValuesToPrint, additionalValue) {
if (additionalValue) {
allValues.push(additionalValue);
allTimes.push(moment());
rawText.push(moment().format("YYYY-MM-DD") +
": " +
Number(additionalValue).toFixed(2));
}
// Print the raw values
ctx.telegram.editMessageText(ctx.update.message.chat.id, loadingMessageID, null, rawText.join("\n") + "\nMinimum: " + minimum + "\nMaximum: " + maximum);
minimum -= 2;
maximum += 2;
if (numberOfRecentValuesToPrint > 0 && loadingMessageID) {
ctx.telegram.editMessageText(ctx.update.message.chat.id, loadingMessageID, null, rawText.join("\n") + "\nMinimum: " + minimum + "\nMaximum: " + maximum);
}
// Generate the graph
var url = "https://chart.googleapis.com/chart?cht=lc&chd=t:" +
allValues.join(",") +
"&chs=800x350&chl=" +
allTimes.join("%7C") +
"&chf=bg,s,e0e0e0&chco=000000,0000FF&chma=30,30,30,30&chds=" +
minimum +
"," +
maximum;
console.log(url);
ctx.replyWithPhoto({
url: url
});
if (!skipImage) {
minimum -= 2;
maximum += 2;
var url = "https://chart.googleapis.com/chart?cht=lc&chd=t:" +
allValues.join(",") +
"&chs=800x350&chl=" +
allTimes.join("%7C") +
"&chf=bg,s,e0e0e0&chco=000000,0000FF&chma=30,30,30,30&chds=" +
minimum +
"," +
maximum;
console.log(url);
ctx.replyWithPhoto({
url: url
});
}
});
}
function triggerNextQuestionFromQueue(ctx) {
@@ -172,6 +181,12 @@ function triggerNextQuestionFromQueue(ctx) {
var message_id = _a.message_id;
currentlyAskedQuestionMessageId = message_id;
});
if (currentlyAskedQuestionObject.type == "number" ||
currentlyAskedQuestionObject.type == "range" ||
currentlyAskedQuestionObject.type == "boolean") {
// To show the graph before, as it takes a while to load
printGraph(currentlyAskedQuestionObject.key, ctx, 0, null, false);
}
}
function insertNewValue(parsedUserValue, ctx, key, type) {
console.log("Inserting value '" + parsedUserValue + "' for key " + key);
@@ -278,7 +293,7 @@ function parseUserInput(ctx, text) {
currentlyAskedQuestionObject.type == "range" ||
currentlyAskedQuestionObject.type == "boolean") {
// To show potential streaks and the history
printGraph(currentlyAskedQuestionObject.key, ctx, 3, parsedUserValue);
printGraph(currentlyAskedQuestionObject.key, ctx, 5, parsedUserValue, true);
}
console.log("Got a new value: " +
parsedUserValue +
@@ -409,7 +424,7 @@ function initBot() {
}
var key = ctx.match[1];
console.log("User wants to graph a specific value " + key);
printGraph(key, ctx, 5, null);
printGraph(key, ctx, 100, null, false);
});
bot.on("location", function (ctx) {
if (ctx.update.message.from.username != process.env.TELEGRAM_USER_ID) {

View File

@@ -51,13 +51,23 @@ function getButtonText(number) {
return emojiNumber + " " + currentlyAskedQuestionObject.buttons[number];
}
function printGraph(key, ctx, numberOfRecentValuesToPrint, additionalValue) {
function printGraph(
key,
ctx,
numberOfRecentValuesToPrint,
additionalValue,
skipImage
) {
// additionalValue is the value that isn't part of the sheet yet
// as it was *just* entered by the user
let loadingMessageID = null;
ctx.reply("Loading history...").then(({ message_id }) => {
loadingMessageID = message_id;
});
if (numberOfRecentValuesToPrint > 0) {
ctx.reply("Loading history...").then(({ message_id }) => {
loadingMessageID = message_id;
});
}
rawDataSheet.getRows(
{
offset: 0,
@@ -84,7 +94,7 @@ function printGraph(key, ctx, numberOfRecentValuesToPrint, additionalValue) {
allValues.unshift(value);
allTimes.unshift(time.format("MM-DD"));
if (i < numberOfRecentValuesToPrint) {
if (i < numberOfRecentValuesToPrint - 1) {
rawText.unshift(time.format("YYYY-MM-DD") + ": " + value.toFixed(2));
}
@@ -99,33 +109,43 @@ function printGraph(key, ctx, numberOfRecentValuesToPrint, additionalValue) {
if (additionalValue) {
allValues.push(additionalValue);
allTimes.push(moment());
rawText.push(
moment().format("YYYY-MM-DD") +
": " +
Number(additionalValue).toFixed(2)
);
}
// Print the raw values
ctx.telegram.editMessageText(
ctx.update.message.chat.id,
loadingMessageID,
null,
rawText.join("\n") + "\nMinimum: " + minimum + "\nMaximum: " + maximum
);
minimum -= 2;
maximum += 2;
if (numberOfRecentValuesToPrint > 0 && loadingMessageID) {
ctx.telegram.editMessageText(
ctx.update.message.chat.id,
loadingMessageID,
null,
rawText.join("\n") + "\nMinimum: " + minimum + "\nMaximum: " + maximum
);
}
// Generate the graph
let url =
"https://chart.googleapis.com/chart?cht=lc&chd=t:" +
allValues.join(",") +
"&chs=800x350&chl=" +
allTimes.join("%7C") +
"&chf=bg,s,e0e0e0&chco=000000,0000FF&chma=30,30,30,30&chds=" +
minimum +
"," +
maximum;
console.log(url);
ctx.replyWithPhoto({
url: url
});
if (!skipImage) {
minimum -= 2;
maximum += 2;
let url =
"https://chart.googleapis.com/chart?cht=lc&chd=t:" +
allValues.join(",") +
"&chs=800x350&chl=" +
allTimes.join("%7C") +
"&chf=bg,s,e0e0e0&chco=000000,0000FF&chma=30,30,30,30&chds=" +
minimum +
"," +
maximum;
console.log(url);
ctx.replyWithPhoto({
url: url
});
}
}
);
}
@@ -202,6 +222,15 @@ function triggerNextQuestionFromQueue(ctx) {
ctx.reply(question, keyboard).then(({ message_id }) => {
currentlyAskedQuestionMessageId = message_id;
});
if (
currentlyAskedQuestionObject.type == "number" ||
currentlyAskedQuestionObject.type == "range" ||
currentlyAskedQuestionObject.type == "boolean"
) {
// To show the graph before, as it takes a while to load
printGraph(currentlyAskedQuestionObject.key, ctx, 0, null, false);
}
}
function insertNewValue(parsedUserValue, ctx, key, type) {
@@ -330,7 +359,7 @@ function parseUserInput(ctx, text = null) {
currentlyAskedQuestionObject.type == "boolean"
) {
// To show potential streaks and the history
printGraph(currentlyAskedQuestionObject.key, ctx, 3, parsedUserValue);
printGraph(currentlyAskedQuestionObject.key, ctx, 5, parsedUserValue, true);
}
console.log(
@@ -507,7 +536,7 @@ function initBot() {
let key = ctx.match[1];
console.log("User wants to graph a specific value " + key);
printGraph(key, ctx, 100, null);
printGraph(key, ctx, 100, null, false);
});
bot.on("location", ctx => {