diff --git a/index.php b/index.php
index 3361058..47cc84c 100644
--- a/index.php
+++ b/index.php
@@ -301,7 +301,8 @@
analyze_complaints: "Analysis of customer complaints shows that 65% of delivery time issues are concentrated in the West region, primarily with one shipping carrier. I recommend diversifying our shipping partners in that region.",
generate_seller_dashboard: "Weekly Seller Performance Dashboard: \n**Top Performers:** SellerA (99% positive feedback), SellerB (fastest shipping), SellerC (most new products). \n**Needs Attention:** SellerX (rating dropped to 85%), SellerY (2 open disputes).",
summarize_refunds: "Monthly Refund Summary: A total of 152 refunds were processed this month. Top reasons: 1. Item arrived damaged (45%), 2. Item not as described (30%), 3. Accidental order (15%). There is a 20% increase in 'damaged item' refunds for electronics.",
- generate_sales_chart: 'Here is the sales report for the last 6 months:
'
+ generate_sales_chart: 'Here is the sales report for the last 6 months:
',
+ generate_pie_chart: 'Here is a pie chart based on your request:
'
};
const taskMap = {};
@@ -326,29 +327,49 @@
sidebarQuickActions.addEventListener('click', handleQuickActionClick);
}
- function renderChart() {
- // Use a MutationObserver to wait for the canvas to be added to the DOM
+ function renderChart(type = 'bar', canvasId = 'salesChart', data = {}) {
const observer = new MutationObserver((mutationsList, observer) => {
for(const mutation of mutationsList) {
if (mutation.type === 'childList') {
- const chartCanvas = document.getElementById('salesChart');
+ const chartCanvas = document.getElementById(canvasId);
if (chartCanvas) {
const ctx = chartCanvas.getContext('2d');
- new Chart(ctx, {
- type: 'bar',
- data: {
- labels: ['January', 'February', 'March', 'April', 'May', 'June'],
- datasets: [{
- label: 'Sales',
- data: [12, 19, 3, 5, 2, 3],
- backgroundColor: 'rgba(75, 192, 192, 0.2)',
- borderColor: 'rgba(75, 192, 192, 1)',
- borderWidth: 1
- }]
- },
- options: { scales: { y: { beginAtZero: true } } }
- });
- observer.disconnect(); // Stop observing once the chart is rendered
+ let chartConfig;
+ if (type === 'pie') {
+ chartConfig = {
+ type: 'pie',
+ data: {
+ labels: data.labels || ['Red', 'Blue', 'Yellow'],
+ datasets: [{
+ label: 'My First Dataset',
+ data: data.values || [300, 50, 100],
+ backgroundColor: [
+ 'rgb(255, 99, 132)',
+ 'rgb(54, 162, 235)',
+ 'rgb(255, 205, 86)'
+ ],
+ hoverOffset: 4
+ }]
+ }
+ };
+ } else { // default to bar
+ chartConfig = {
+ type: 'bar',
+ data: {
+ labels: data.labels || ['January', 'February', 'March', 'April', 'May', 'June'],
+ datasets: [{
+ label: 'Sales',
+ data: data.values || [12, 19, 3, 5, 2, 3],
+ backgroundColor: 'rgba(75, 192, 192, 0.2)',
+ borderColor: 'rgba(75, 192, 192, 1)',
+ borderWidth: 1
+ }]
+ },
+ options: { scales: { y: { beginAtZero: true } } }
+ };
+ }
+ new Chart(ctx, chartConfig);
+ observer.disconnect();
return;
}
}
@@ -373,19 +394,37 @@
function processMessage(text) {
const trimmedText = text.trim();
+ const lowerCaseText = trimmedText.toLowerCase();
if (!trimmedText) return;
activateSidebar();
addMessage(trimmedText, 'user');
userInput.value = '';
- const task = taskMap[trimmedText] || Object.keys(taskMap).find(key => key.includes(trimmedText));
+ let task = taskMap[trimmedText] || Object.keys(taskMap).find(key => key.includes(trimmedText));
+ let response = assistantResponses[task];
+ let chartType = null;
+ let chartData = {};
+
+ if (!task) {
+ if (lowerCaseText.includes('pie chart')) {
+ chartType = 'pie';
+ task = 'generate_pie_chart';
+ response = assistantResponses[task];
+ } else if (lowerCaseText.includes('chart') || lowerCaseText.includes('graph') || lowerCaseText.includes('report')) {
+ chartType = 'bar';
+ task = 'generate_sales_chart';
+ response = assistantResponses[task];
+ }
+ }
setTimeout(() => {
- const response = assistantResponses[task] || "I'm a demo assistant. My capabilities are limited to the quick actions for now. Please select one of the options or upload a file.";
+ response = response || "I'm a demo assistant. My capabilities are limited to the quick actions for now. Please select one of the options or upload a file.";
- if (task === 'generate_sales_chart') {
- renderChart();
+ if (task === 'generate_sales_chart' || chartType === 'bar') {
+ renderChart('bar', 'salesChart', chartData);
+ } else if (task === 'generate_pie_chart' || chartType === 'pie') {
+ renderChart('pie', 'pieChart', chartData);
}
addMessage(response, 'assistant');