Skip to content

Commit

Permalink
add table
Browse files Browse the repository at this point in the history
  • Loading branch information
alyzaraviandi committed May 17, 2024
1 parent 3525fed commit e48c6b5
Show file tree
Hide file tree
Showing 13 changed files with 132 additions and 1,883 deletions.
1,704 changes: 0 additions & 1,704 deletions code_context.txt

This file was deleted.

75 changes: 0 additions & 75 deletions get_code_context.sh

This file was deleted.

26 changes: 21 additions & 5 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,16 @@ <h2>Trend</h2>
<div class="trend">
<div class="trendDay">
<h3>Daily Transaction per Product Type</h3>
<canvas id="trendDay"></canvas>
<canvas id="trendDay" style="margin-bottom: 20px;"></canvas>
<h3>Leading Store for Each Product Type</h3>
<table id="storeTable">
<tr>
<th>Product Type</th>
<th>Store</th>
<th>Transactions</th>
<th>Revenue</th>
</tr>
</table>
</div>
<div class="transactionAndRevenue">
<div class="totalTransaction">
Expand All @@ -146,7 +155,7 @@ <h3>Total Revenue per Product Type</h3>
<canvas id="totalRevenue"></canvas>
</div>
</div>
</div>
</div>
<div class="distribution">
<h2>Distribution</h2>
<div class="distribution-wrapper">
Expand All @@ -167,8 +176,11 @@ <h3>Product Variety on Transaction</h3>
<div class="price-influence-purchasing-behaviour">
<h3>Product Price Influence on Purchasing Behaviour</h3>
<canvas id="price-influence-purchasing-behaviour"></canvas>
<div id="js-legend-purchasing-behaviour" class="chart-legend"></div>
</div>
<div
id="js-legend-purchasing-behaviour"
class="chart-legend"
></div>
</div>
</div>
</div>
</div>
Expand All @@ -181,7 +193,11 @@ <h3>Product Price Influence on Purchasing Behaviour</h3>
<script type="module" src="script/totalRevenue.js"></script>
<script type="module" src="script/productVarietyRevenue.js"></script>
<script type="module" src="script/productVarietyTransaction.js"></script>
<script type="module" src="script/priceInfluencePurchasingBehaviour.js"></script>
<script
type="module"
src="script/priceInfluencePurchasingBehaviour.js"
></script>
<script type ="module" src="script/leadingStore.js"></script>

<script src="https://unpkg.com/chart.js-plugin-labels-dv/dist/chartjs-plugin-labels.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/chartjs-plugin-datalabels@2"></script>
Expand Down
36 changes: 19 additions & 17 deletions script/filter.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
// filter.js
export function filterData(originalData) {
return originalData.filter((item) => {
if (!document.querySelector(`input[value="${item.dayOfWeek}"]`).checked) {
return false;
}
if (!document.querySelector(`input[value="${item.monthOfYear}"]`).checked) {
return false;
}
if (!document.querySelector(`input[value="${item.productCategory}"]`).checked) {
return false;
}
if (!document.querySelector(`input[value="${item.storeLocation}"]`).checked) {
return false;
}
return true;
return originalData.filter((item) => {
const dayOfWeekChecked = document.querySelector(`input[value="${item.dayOfWeek}"]`).checked;
const monthOfYearChecked = document.querySelector(`input[value="${item.monthOfYear}"]`).checked;
const productCategoryChecked = document.querySelector(`input[value="${item.productCategory}"]`).checked;
const storeLocationChecked = document.querySelector(`input[value="${item.storeLocation}"]`).checked;

return dayOfWeekChecked && monthOfYearChecked && productCategoryChecked && storeLocationChecked;
});
}

export function addCheckboxEventListeners(updateDataDisplay, originalData) {
document
.querySelectorAll('input[type="checkbox"]')
.forEach((checkbox) => {
checkbox.addEventListener("change", () => {
const filteredData = filterData(originalData);
updateDataDisplay(filteredData);
});
});
}

}
63 changes: 63 additions & 0 deletions script/leadingStore.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import { filterData, addCheckboxEventListeners } from './filter.js';

window.addEventListener('load', initialize);

function initialize() {
fetchData("data/trend_data.json")
.then((originalData) => {
const updateDataDisplay = createDataDisplayUpdater(originalData);
updateDataDisplay(originalData);
addCheckboxEventListeners(updateDataDisplay, originalData);
})
.catch((error) => console.error("Error:", error));
}

function fetchData(url) {
return fetch(url).then((response) => response.json());
}

function createDataDisplayUpdater(originalData) {
return function updateDataDisplay(data) {
const filteredData = filterData(data);
processAndDisplayData(filteredData);
};
}

function processAndDisplayData(data) {
const table = document.getElementById("storeTable");
table.innerHTML = ''; // Clear existing table rows

const groupedData = data.reduce((acc, curr) => {
const key = `${curr.productCategory}-${curr.productType}-${curr.storeLocation}`;
if (!acc[key]) {
acc[key] = {
productCategory: curr.productCategory,
productType: curr.productType,
storeLocation: curr.storeLocation,
totalQty: 0,
totalRevenue: 0,
};
}
acc[key].totalQty += curr.totalQty;
acc[key].totalRevenue += curr.totalRevenue;
return acc;
}, {});

const sortedData = Object.values(groupedData).sort((a, b) => {
if (a.productCategory < b.productCategory) return -1;
if (a.productCategory > b.productCategory) return 1;
if (a.productType < b.productType) return -1;
if (a.productType > b.productType) return 1;
return b.totalRevenue - a.totalRevenue;
});

sortedData.forEach((item) => {
const row = `<tr>
<td>${item.productType}</td>
<td>${item.storeLocation}</td>
<td>${item.totalQty}</td>
<td>${item.totalRevenue}</td>
</tr>`;
table.innerHTML += row;
});
}
13 changes: 2 additions & 11 deletions script/priceInfluencePurchasingBehaviour.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { filterData } from './filter.js';
import { filterData, addCheckboxEventListeners } from './filter.js';
import productColors from "../colors.js";

window.addEventListener('load', function() {
Expand Down Expand Up @@ -104,16 +104,7 @@ window.addEventListener('load', function() {
}

updateChart(originalData);

document
.querySelectorAll('input[type="checkbox"]')
.forEach((checkbox) => {
checkbox.addEventListener("change", () => {
const filteredData = filterData(originalData);

updateChart(filteredData);
});
});
addCheckboxEventListeners(updateChart, originalData);
})
.catch((error) => console.error("Error:", error));
});
12 changes: 2 additions & 10 deletions script/productVarietyRevenue.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { filterData } from './filter.js';
import { filterData, addCheckboxEventListeners } from './filter.js';
import productColors from "../colors.js";

window.addEventListener('load', function() {
Expand Down Expand Up @@ -115,15 +115,7 @@ window.addEventListener('load', function() {

updateChart(originalData);

document
.querySelectorAll('input[type="checkbox"]')
.forEach((checkbox) => {
checkbox.addEventListener("change", () => {
const filteredData = filterData(originalData);

updateChart(filteredData);
});
});
addCheckboxEventListeners(updateChart, originalData);
})
.catch((error) => console.error("Error:", error));

Expand Down
12 changes: 2 additions & 10 deletions script/productVarietyTransaction.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { filterData } from './filter.js';
import { filterData, addCheckboxEventListeners } from './filter.js';
import productColors from "../colors.js";

window.addEventListener('load', function() {
Expand Down Expand Up @@ -115,15 +115,7 @@ window.addEventListener('load', function() {

updateChart(originalData);

document
.querySelectorAll('input[type="checkbox"]')
.forEach((checkbox) => {
checkbox.addEventListener("change", () => {
const filteredData = filterData(originalData);

updateChart(filteredData);
});
});
addCheckboxEventListeners(updateChart, originalData);
})
.catch((error) => console.error("Error:", error));
});
12 changes: 2 additions & 10 deletions script/totalRevenue.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { filterData } from './filter.js';
import { filterData, addCheckboxEventListeners } from './filter.js';
import productColors from "../colors.js";

window.addEventListener('load', function() {
Expand Down Expand Up @@ -98,15 +98,7 @@ window.addEventListener('load', function() {

updateChart(originalData);

document
.querySelectorAll('input[type="checkbox"]')
.forEach((checkbox) => {
checkbox.addEventListener("change", () => {
const filteredData = filterData(originalData);

updateChart(filteredData);
});
});
addCheckboxEventListeners(updateChart, originalData);
})
.catch((error) => console.error("Error:", error));
});
14 changes: 2 additions & 12 deletions script/totalTransaction.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { filterData } from './filter.js';
import { filterData, addCheckboxEventListeners } from './filter.js';
import productColors from "../colors.js";

window.addEventListener('load', function() {
Expand Down Expand Up @@ -99,17 +99,7 @@ window.addEventListener('load', function() {
// Initial chart creation
updateChart(originalData);

// Add event listener to each checkbox
document
.querySelectorAll('input[type="checkbox"]')
.forEach((checkbox) => {
checkbox.addEventListener("change", () => {
// Filter out the data that matches the unchecked property
const filteredData = filterData(originalData);

updateChart(filteredData);
});
});
addCheckboxEventListeners(updateChart, originalData);
})
.catch((error) => console.error("Error:", error));
});
14 changes: 2 additions & 12 deletions script/trendDay.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import productColors from "../colors.js";
import { filterData } from './filter.js';
import { filterData, addCheckboxEventListeners } from './filter.js';

window.addEventListener('load', function() {
const ctx = document.getElementById("trendDay").getContext("2d");
Expand Down Expand Up @@ -101,17 +101,7 @@ window.addEventListener('load', function() {
updateChart(originalData);

// Add event listener to each checkbox
document
.querySelectorAll('input[type="checkbox"]')
.forEach((checkbox) => {
checkbox.addEventListener("change", () => {
// Filter out the data that matches the unchecked property
const filteredData = filterData(originalData);

// Update the chart
updateChart(filteredData);
});
});
addCheckboxEventListeners(updateChart, originalData);
})
.catch((error) => console.error("Error:", error));

Expand Down
Loading

0 comments on commit e48c6b5

Please sign in to comment.