Skip to content

Commit fef6190

Browse files
committed
Try ChartJS instead
1 parent 97ae29c commit fef6190

File tree

3 files changed

+851
-408
lines changed

3 files changed

+851
-408
lines changed

app/server.js

+47-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,20 @@ 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.8, // 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+
// Return the URL for Slack (using a data URL)
257+
return `data:image/png;base64,${imageBase64}`;
244258
}
245259

246260
async function generateIncByCategoryChart(numberOfDays) {
@@ -250,9 +264,8 @@ async function generateIncByCategoryChart(numberOfDays) {
250264
const labels = sortedCategories.map((row) => row.category);
251265
const data = sortedCategories.map((row) => parseInt(row.count, 10));
252266

253-
// Generate the bar chart URL using QuickChart
254-
const chart = new QuickChart();
255-
chart.setConfig({
267+
// Create the chart configuration
268+
const chartConfig = {
256269
type: "bar",
257270
data: {
258271
labels: labels,
@@ -268,23 +281,30 @@ async function generateIncByCategoryChart(numberOfDays) {
268281
},
269282
options: {
270283
scales: {
271-
xAxes: {
284+
x: {
272285
ticks: {
273-
max: labels.length, // Limit the number of labels to show
286+
autoSkip: false, // Show all labels
287+
maxTicksLimit: labels.length,
274288
},
275289
},
276-
yAxes: {
277-
ticks: {
278-
beginAtZero: true, // Start the y-axis at 0
279-
stepSize: 1, // Set the step size to 1
280-
}
290+
y: {
291+
beginAtZero: true,
292+
min: 0,
293+
stepSize: 1,
281294
},
282295
},
283296
},
297+
};
298+
299+
// Render the chart and get the image as JPEG
300+
const imageBuffer = await chartJSNodeCanvas.renderToBuffer(chartConfig, {
301+
format: 'jpeg', // Change format to JPEG
302+
width: 400, // Set width
303+
height: 200, // Set height
304+
quality: 0.8, // Set quality (0 to 1)
284305
});
285-
286-
// Increase chart size
287-
chart.setWidth(400);
288-
chart.setHeight(200);
289-
return chart.getShortUrl();
306+
// Convert the image buffer to a base64 string
307+
const imageBase64 = imageBuffer.toString('base64');
308+
// Return the URL for Slack (using a data URL)
309+
return `data:image/png;base64,${imageBase64}`;
290310
}

0 commit comments

Comments
 (0)