Last active
February 8, 2023 08:11
-
-
Save nishnik/b374ede5903bf850a922ef48f58df0af to your computer and use it in GitHub Desktop.
Create charts from the table data in screener.in
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==UserScript== | |
// @name Screener Charts | |
// @namespace http://tampermonkey.net/ | |
// @version 0.1 | |
// @description Create Charts for Screener | |
// @author You | |
// @match https://www.screener.in/company/* | |
// @icon https://www.google.com/s2/favicons?sz=64&domain=screener.in | |
// @grant none | |
// ==/UserScript== | |
(function() { | |
'use strict'; | |
// Get all the table elements | |
var tables = document.getElementsByClassName("data-table responsive-text-nowrap") | |
var tableArr = []; | |
var tableLab = []; // 0th column value | |
var tableLabels = []; // inefficient but works | |
var tableCount = 0; | |
for (var table_index = 0; table_index < tables.length; table_index++) | |
{ | |
var table = tables[table_index] // Get the current table | |
table.querySelectorAll('tr').forEach((row, j) => { // Add a new column to the table | |
const cell = document.createElement(j ? "td" : "th") | |
row.appendChild(cell) | |
}); | |
//loop all rows and form data array | |
var labels = [] | |
for (var j = 1; j < (table.rows[0].cells.length - 1); j++) { // Get the headers | |
labels.push(table.rows[0].cells[j].innerHTML.trim()) | |
} | |
for (var i = 1; i < table.rows.length; i++ ) { // For the rows i.e 1 to length of rows. 0 is the header | |
var rowValues = []; | |
// Iterating columns. Last column is created by us so skipping that. 0th column has text, not the value, so skipping that. | |
for (var j = 1; j < (table.rows[i].cells.length - 1); j++) { | |
rowValues.push(table.rows[i].cells[j].innerHTML.trim().replace(",","").replace("%","")) | |
} | |
tableArr.push(rowValues); | |
tableLab.push(table.rows[i].cells[0].textContent.trim()) | |
tableLabels.push(labels) // inefficient but works | |
var canvas = document.createElement("canvas"); | |
canvas.setAttribute("id", "myChart"+tableCount); | |
tableCount += 1 | |
table.rows[i].cells[table.rows[i].cells.length - 1].appendChild(canvas); | |
} | |
} | |
tableArr.forEach(function(e,i){ | |
var chartID = "myChart"+ (i) | |
var ctx = document.getElementById(chartID).getContext('2d'); | |
var myChart = new Chart(ctx, { | |
type: 'bar', | |
data: { | |
labels: tableLabels[i], | |
datasets: [{ | |
label: tableLab[i], | |
data: e, | |
backgroundColor: [ | |
'rgba(96, 60, 20, 0.3)' | |
], | |
borderColor: [ | |
'rgba(96, 60, 20, 0.3)' | |
], | |
borderWidth: 1 | |
}] | |
}, | |
type: 'line' | |
}); | |
}) | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Copy paste this content in TamperMonkey "Create a New Script"