Skip to content

Commit

Permalink
chore: add renderchart util in gitignore
Browse files Browse the repository at this point in the history
  • Loading branch information
stephanoshadjipetrou committed Dec 11, 2023
1 parent 5ffcfbe commit 7fe5c4c
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 71 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ typings/
# Cache used by TypeScript's incremental build
*.tsbuildinfo

src/utils/renderChart/rawgraphs-charts

# DS_Store files
.DS_Store

140 changes: 69 additions & 71 deletions src/utils/renderChart/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import fs from "fs";
import _ from "lodash";
import fs from 'fs';
import _ from 'lodash';

// execute renderChartData with passed arguments 1 2 and 3
import { chart as rawChart } from "@rawgraphs/rawgraphs-core";
import {chart as rawChart} from '@rawgraphs/rawgraphs-core';
import {
alluvialdiagram,
arcdiagram,
Expand Down Expand Up @@ -40,7 +40,7 @@ import {
violinplot,
voronoidiagram,
voronoitreemap,
} from "./rawgraphs-charts/lib/index.cjs.js";
} from './rawgraphs-charts/lib/index.cjs.js';

// consts
const charts = {
Expand Down Expand Up @@ -84,31 +84,28 @@ const charts = {
};

// utils
function getDatasetFilterOptions(
dataset,
onlyKeys,
) {
function getDatasetFilterOptions(dataset, onlyKeys) {
const filterOptions = [];
if (!dataset || dataset.length === 0) {
return filterOptions;
}
const itemKeys = _.filter(Object.keys(dataset[0]), (key) => {
const itemKeys = _.filter(Object.keys(dataset[0]), key => {
return (
key !== "id" &&
!key.toLowerCase().includes("amount") &&
!key.toLowerCase().includes("date") &&
!key.toLowerCase().includes("number") &&
!key.toLowerCase().includes("title")
key !== 'id' &&
!key.toLowerCase().includes('amount') &&
!key.toLowerCase().includes('date') &&
!key.toLowerCase().includes('number') &&
!key.toLowerCase().includes('title')
);
});

if (onlyKeys) return itemKeys;

itemKeys.forEach((key) => {
itemKeys.forEach(key => {
const options = _.filter(
Object.keys(_.groupBy(dataset, key)),
(optionKey) =>
optionKey !== "undefined" && optionKey !== "null" && optionKey !== ""
optionKey =>
optionKey !== 'undefined' && optionKey !== 'null' && optionKey !== '',
);
const name = key;

Expand All @@ -117,12 +114,12 @@ function getDatasetFilterOptions(
name,
enabled: true,
options: _.orderBy(
_.uniq(options).map((o) => ({
_.uniq(options).map(o => ({
label: o,
value: o,
})),
"label",
"asc"
'label',
'asc',
),
});
}
Expand All @@ -131,43 +128,34 @@ function getDatasetFilterOptions(
return filterOptions;
}

function filterData(
parsedDataset,
appliedFilters
) {
function filterData(parsedDataset, appliedFilters) {
// Get the filter keys
const filterKeys = Object.keys(appliedFilters || {});
if (filterKeys.length === 0) return parsedDataset; // can't be 0, but safety return

// Filter 'data' based on 'appliedFilters' using the specified 'filterKeys'
const filteredData = _.filter(parsedDataset, (item) => {
const filteredData = _.filter(parsedDataset, item => {
// Check if all conditions hold for each 'filterKey'
return filterKeys.every((filterKey) =>
appliedFilters[filterKey]?.includes(item[filterKey]?.toString())
return filterKeys.every(filterKey =>
appliedFilters[filterKey]?.includes(item[filterKey]?.toString()),
);
});

return filteredData;
}

function renderChart(
item,
parsed,
id,
itemAppliedFilters,
vizType
) {
function renderChart(item, parsed, id, itemAppliedFilters, vizType) {
const chart = charts[vizType];
let title = "";
let subtitle = "";
let description = "";
let title = '';
let subtitle = '';
let description = '';

if (vizType === "bigNumber") {
if (vizType === 'bigNumber') {
// remove title, subtitle, description from item.mapping
title = item.mapping.title;
subtitle = item.mapping.subtitle;
description = item.mapping.description;
item.mapping = { "value": item.mapping.value };
item.mapping = {value: item.mapping.value};
}

const viz = rawChart(chart, {
Expand All @@ -178,30 +166,27 @@ function renderChart(
});
let vizData = viz._getVizData();

if (vizType === "bigNumber") {
if (vizType === 'bigNumber') {
// remove title, subtitle, description from item.mapping
vizData = {
"title": title ?? "tmp",
"value": vizData.value,
"subtitle": subtitle ?? "tmp",
"description": description ?? "tmp",
title: title ?? 'tmp',
value: vizData.value,
subtitle: subtitle ?? 'tmp',
description: description ?? 'tmp',
};
}

let tabItem = {
renderedContent: "",
renderedContent: '',
appliedFilters: itemAppliedFilters || item.appliedFilters,
filterOptionGroups: getDatasetFilterOptions(
parsed.dataset
),
enabledFilterOptionGroups:
item.enabledFilterOptionGroups,
filterOptionGroups: getDatasetFilterOptions(parsed.dataset),
enabledFilterOptionGroups: item.enabledFilterOptionGroups,
dataTypes: parsed.dataTypes,
mappedData: vizData,
dimensions: chart.dimensions,
ssr: false,
};
if (id !== "new") {
if (id !== 'new') {
tabItem = {
...tabItem,
mapping: item.mapping,
Expand All @@ -215,9 +200,9 @@ function renderChart(

export async function renderChartData(id, body, chartData) {
let internalData;
if (id === "new" || body.rows) {
if (id === 'new' || body.rows) {
if (!body.rows || body.rows.length === 0) {
return { "error": "no rows"};
return {error: 'no rows'};
}
internalData = body.rows;
} else {
Expand All @@ -230,41 +215,54 @@ export async function renderChartData(id, body, chartData) {
const item = internalData[0][0];
let parsed = null;
try {
const filePath = process.env.PARSED_DATA_FILES_PATH || `./src/parsed-data-files/`;
const parsedData = fs.readFileSync(`${filePath}${item.datasetId}.json`)
const filePath =
process.env.PARSED_DATA_FILES_PATH || `./src/parsed-data-files/`;
const parsedData = fs.readFileSync(`${filePath}${item.datasetId}.json`);
parsed = JSON.parse(parsedData.toString());
} catch (error) {
console.log("Error reading parsed data file", error)
console.log('Error reading parsed data file');
}
// Check if there are either filters in the item.appliedFilters or in the body.previewAppliedFilters
const itemAppliedFilters = _.get(body, `previewAppliedFilters[0][0]`, null);
// If there are filters, filter the data
if (!_.isEmpty(item.appliedFilters) || itemAppliedFilters) {
parsed.dataset = filterData(parsed.dataset, itemAppliedFilters || item.appliedFilters);
parsed.dataset = filterData(
parsed.dataset,
itemAppliedFilters || item.appliedFilters,
);
}

// render the chart
const renderedChart = renderChart(item, parsed, id, itemAppliedFilters, item.vizType);
const renderedChart = renderChart(
item,
parsed,
id,
itemAppliedFilters,
item.vizType,
);
// Return the rendered chart item
// json stringify and save to ./rendered.json
fs.writeFileSync(`${__dirname}/rendering/${id}_rendered.json`, JSON.stringify(renderedChart))
console.log("Success...")
};
fs.writeFileSync(
`${__dirname}/rendering/${id}_rendered.json`,
JSON.stringify(renderedChart),
);
console.log('Success...');
}

try {
// if argv2 is undefined, return error
if (process.argv[2] === undefined) {
console.error("No id provided");
console.error('No id provided');
} else {
// read the first argument as id
const id = process.argv[2] // 2 because 0 is node and 1 is this file
// read the data from ./source_data.json as json
const data = fs.readFileSync(`${__dirname}/rendering/${id}.json`)
const parsedData = JSON.parse(data);
const body = parsedData.body
const chartData = parsedData.chartData
renderChartData(id, body, chartData)
// read the first argument as id
const id = process.argv[2]; // 2 because 0 is node and 1 is this file
// read the data from ./source_data.json as json
const data = fs.readFileSync(`${__dirname}/rendering/${id}.json`);
const parsedData = JSON.parse(data);
const body = parsedData.body;
const chartData = parsedData.chartData;
renderChartData(id, body, chartData);
}
} catch (error) {
console.error("Something went wrong...\n")
console.error('Something went wrong...\n');
}

0 comments on commit 7fe5c4c

Please sign in to comment.