Skip to content

Commit 2a615fd

Browse files
committed
Try ChartJS instead
1 parent 97ae29c commit 2a615fd

File tree

3 files changed

+853
-408
lines changed

3 files changed

+853
-408
lines changed

app/server.js

+49-27
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {
88
getCategoriesArray,
99
addCategory,
1010
} from "./db.js";
11-
import QuickChart from "quickchart-js";
11+
import { ChartJSNodeCanvas } from "chartjs-node-canvas"; // Import Chart.js
1212

1313
const { App, SocketModeReceiver } = pkg;
1414
dotenv.config();
@@ -20,6 +20,14 @@ const app = new App({
2020
}),
2121
});
2222

23+
// Set up the ChartJSNodeCanvas instance
24+
const chartWidth = 800;
25+
const chartHeight = 400;
26+
const chartJSNodeCanvas = new ChartJSNodeCanvas({
27+
width: chartWidth,
28+
height: chartHeight,
29+
});
30+
2331
app.command("/addcategory", async ({ command, ack, respond }) => {
2432
await ack(); // Acknowledge the command
2533

@@ -95,7 +103,6 @@ app.event("message", async ({ event, client }) => {
95103
}
96104
});
97105

98-
99106
// Listen for when the user clicks the dropdown and fetch categories
100107
app.options(/category_select-.*/, async ({ options, ack }) => {
101108
try {
@@ -133,7 +140,7 @@ app.options(/category_select-.*/, async ({ options, ack }) => {
133140
});
134141

135142
// Listen for the interaction from the dropdown menu
136-
app.action(/category_select-.*/, async ({ body, ack, say }) => {
143+
app.action(/category_select-.*/, async ({ body, ack }) => {
137144
// Acknowledge the action
138145
await ack();
139146
console.log("body", body);
@@ -156,7 +163,6 @@ app.command("/inc_stats", async ({ ack, body, client }) => {
156163
const dbResponse = await getIncs(numberOfDays);
157164

158165
const totalIncs = dbResponse.rows.length;
159-
160166

161167
const chartUrlByWeek = await generateIncByWeekChart(numberOfDays);
162168
const chartUrlByCategory = await generateIncByCategoryChart(numberOfDays);
@@ -184,7 +190,7 @@ app.command("/inc_stats", async ({ ack, body, client }) => {
184190
{
185191
type: "image",
186192
image_url: chartUrlByCategory,
187-
alt_text: "Incidents per Week",
193+
alt_text: "Incidents per Category",
188194
},
189195
{
190196
type: "image",
@@ -213,9 +219,8 @@ async function generateIncByWeekChart(numberOfDays) {
213219
);
214220
const data = incNumberByWeek.rows.map((row) => parseInt(row.count, 10));
215221

216-
// Generate the bar chart URL using QuickChart
217-
const chart = new QuickChart();
218-
chart.setConfig({
222+
// Create the chart configuration
223+
const chartConfig = {
219224
type: "line",
220225
data: {
221226
labels: labels,
@@ -236,11 +241,21 @@ async function generateIncByWeekChart(numberOfDays) {
236241
},
237242
},
238243
},
244+
};
245+
246+
247+
// Render the chart and get the image as JPEG
248+
const imageBuffer = await chartJSNodeCanvas.renderToBuffer(chartConfig, {
249+
format: 'jpeg', // Change format to JPEG
250+
width: 400, // Set width
251+
height: 200, // Set height
252+
quality: 0.5, // Set quality (0 to 1)
239253
});
240-
// Increase chart size
241-
chart.setWidth(400);
242-
chart.setHeight(200);
243-
return chart.getShortUrl();
254+
// Convert the image buffer to a base64 string
255+
const imageBase64 = imageBuffer.toString('base64');
256+
console.log("imageBase64", imageBase64.length);
257+
// Return the URL for Slack (using a data URL)
258+
return `data:image/png;base64,${imageBase64}`;
244259
}
245260

246261
async function generateIncByCategoryChart(numberOfDays) {
@@ -250,9 +265,8 @@ async function generateIncByCategoryChart(numberOfDays) {
250265
const labels = sortedCategories.map((row) => row.category);
251266
const data = sortedCategories.map((row) => parseInt(row.count, 10));
252267

253-
// Generate the bar chart URL using QuickChart
254-
const chart = new QuickChart();
255-
chart.setConfig({
268+
// Create the chart configuration
269+
const chartConfig = {
256270
type: "bar",
257271
data: {
258272
labels: labels,
@@ -268,23 +282,31 @@ async function generateIncByCategoryChart(numberOfDays) {
268282
},
269283
options: {
270284
scales: {
271-
xAxes: {
285+
x: {
272286
ticks: {
273-
max: labels.length, // Limit the number of labels to show
287+
autoSkip: false, // Show all labels
288+
maxTicksLimit: labels.length,
274289
},
275290
},
276-
yAxes: {
277-
ticks: {
278-
beginAtZero: true, // Start the y-axis at 0
279-
stepSize: 1, // Set the step size to 1
280-
}
291+
y: {
292+
beginAtZero: true,
293+
min: 0,
294+
stepSize: 1,
281295
},
282296
},
283297
},
298+
};
299+
300+
// Render the chart and get the image as JPEG
301+
const imageBuffer = await chartJSNodeCanvas.renderToBuffer(chartConfig, {
302+
format: 'jpeg', // Change format to JPEG
303+
width: 400, // Set width
304+
height: 200, // Set height
305+
quality: 0.5, // Set quality (0 to 1)
284306
});
285-
286-
// Increase chart size
287-
chart.setWidth(400);
288-
chart.setHeight(200);
289-
return chart.getShortUrl();
307+
// Convert the image buffer to a base64 string
308+
const imageBase64 = imageBuffer.toString('base64');
309+
console.log("imageBase64", imageBase64.length);
310+
// Return the URL for Slack (using a data URL)
311+
return `data:image/png;base64,${imageBase64}`;
290312
}

0 commit comments

Comments
 (0)