Skip to content

Commit

Permalink
refactor (lda chart): cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
AlasDiablo committed Nov 28, 2023
1 parent d8b7275 commit f682605
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 46 deletions.
70 changes: 36 additions & 34 deletions src/app/js/formats/vega-lite/component/lda-chart/LdaChart.js
Original file line number Diff line number Diff line change
@@ -1,46 +1,48 @@
import { CustomActionVegaLite } from '../vega-lite-component';
import { VEGA_LITE_DATA_INJECT_TYPE_A } from '../../../chartsUtils';
import PropTypes from 'prop-types';
import React from 'react';
import React, { useMemo } from 'react';

const LdaChart = ({ data, title, colors }) => {
const spec = {
$schema: 'https://vega.github.io/schema/vega-lite/v5.json',
config: { legend: { disable: true } },
title: title,
encoding: {
y: { field: 'word', type: 'nominal', sort: null },
x: { field: 'word_weight', type: 'quantitative' },
},
layer: [
{
mark: 'bar',
encoding: {
color: {
field: 'word_weight',
scale: { range: colors.split(' ') },
},
},
const spec = useMemo(() => {
return {
$schema: 'https://vega.github.io/schema/vega-lite/v5.json',
config: { legend: { disable: true } },
title: title,
encoding: {
y: { field: 'word', type: 'nominal', sort: null },
x: { field: 'word_weight', type: 'quantitative' },
},
{
mark: {
type: 'text',
align: 'left',
baseline: 'middle',
dx: 3,
layer: [
{
mark: 'bar',
encoding: {
color: {
field: 'word_weight',
scale: { range: colors.split(' ') },
},
},
},
encoding: {
text: {
field: 'word_weight',
type: 'quantitative',
format: '.2f',
{
mark: {
type: 'text',
align: 'left',
baseline: 'middle',
dx: 3,
},
encoding: {
text: {
field: 'word_weight',
type: 'quantitative',
format: '.2f',
},
},
},
},
],
width: 'container',
height: { step: 20 },
};
],
width: 'container',
height: { step: 20 },
};
}, [data, title, colors]);
return (
<CustomActionVegaLite
spec={spec}
Expand Down
26 changes: 14 additions & 12 deletions src/app/js/formats/vega-lite/component/lda-chart/LdaChartView.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,24 +37,26 @@ const LdaChartView = props => {
*/
const topicsValues = rawValue.value.topics;
Object.entries(topicsValues).forEach(entry => {
const key = entry[0];
const topic = entry[0];
/**
* @type {{word: string, word_weight: string}[]}
*/
let current = entry[1].words;
const previous = values.get(key);
let currentWords = entry[1].words;
const previousWords = values.get(topic);

if (previous) {
current = current.map(v => {
const pre = previous.find(preV => preV.word === v.word);
if (previousWords) {
currentWords = currentWords.map(word => {
const preWord = previousWords.find(
preV => preV.word === word.word,
);
return {
word: v.word,
word: word.word,
word_weight:
parseFloat(v.word_weight) +
parseFloat(pre.word_weight),
parseFloat(word.word_weight) +
parseFloat(preWord.word_weight),
};
});
current.sort((a, b) => {
currentWords.sort((a, b) => {
if (a.word_weight > b.word_weight) {
return -1;
}
Expand All @@ -65,15 +67,15 @@ const LdaChartView = props => {
});
}

values.set(key, current);
values.set(topic, currentWords);
});
}

return {
values: Object.fromEntries(values),
topics,
};
}, [props]);
}, [props.data]);

return (
<div style={{ margin: '12px' }}>
Expand Down

0 comments on commit f682605

Please sign in to comment.