@@ -3,13 +3,23 @@ import * as d3 from "d3";
3
3
4
4
const HorizontalBarChart = ( ) => {
5
5
const [ data , setData ] = useState ( [ ] ) ;
6
+ const [ topicsData , setTopicsData ] = useState ( [ ] ) ;
6
7
7
8
useEffect ( ( ) => {
8
9
const fetchData = async ( ) => {
9
10
try {
11
+ // fetch post data
10
12
const response = await fetch ( "/data/consolidated_posts.json" ) ;
11
13
const json = await response . json ( ) ;
12
14
15
+ // fetch topics data
16
+ const responseTopics = await fetch (
17
+ "/data/topic-modeling/results/topics_NMF_15.json"
18
+ ) ;
19
+ const topicsJson = await responseTopics . json ( ) ;
20
+ console . log ( "bubble fetched topics: " , topicsJson ) ;
21
+ setTopicsData ( topicsJson ) ;
22
+
13
23
// Parse and aggregate data
14
24
const parsedData = Object . entries ( json )
15
25
. map ( ( [ , post ] ) => ( {
@@ -31,10 +41,17 @@ const HorizontalBarChart = () => {
31
41
) ;
32
42
33
43
setData (
34
- aggregatedData . map ( ( [ topic , stats ] ) => ( {
35
- topic,
36
- ...stats ,
37
- } ) )
44
+ aggregatedData . map ( ( [ topic , stats ] ) => {
45
+ // find corresponding topic color from topicsJson
46
+ const topicData = topicsJson . find ( ( t ) => t . label === topic ) ;
47
+ const color = topicData ? topicData . color : "#000000" ; // Default to black if no match
48
+
49
+ return {
50
+ topic,
51
+ ...stats ,
52
+ color, // Add color to each aggregated data entry
53
+ } ;
54
+ } )
38
55
) ;
39
56
} catch ( error ) {
40
57
console . error ( "Error fetching data:" , error ) ;
@@ -45,6 +62,7 @@ const HorizontalBarChart = () => {
45
62
} , [ ] ) ;
46
63
47
64
const createBubbleChart = ( chartData , containerId ) => {
65
+ console . log ( "data structure in bubble" , data ) ;
48
66
d3 . select ( `#${ containerId } ` ) . select ( "svg" ) . remove ( ) ;
49
67
50
68
const margin = { top : 60 , right : 300 , bottom : 60 , left : 100 } ;
@@ -155,7 +173,8 @@ const HorizontalBarChart = () => {
155
173
. attr ( "cx" , ( d ) => xScale ( d . numPosts ) ) // X-axis is number of posts
156
174
. attr ( "cy" , ( d ) => yScale ( d . avgLength ) ) // Y-axis is average word length
157
175
. attr ( "r" , ( d ) => sizeScale ( d . avgSentiment ) ) // Bubble size is sentiment
158
- . attr ( "fill" , ( d ) => colorScale ( d . topic ) ) // Color represents topic
176
+ // .attr("fill", (d) => colorScale(d.topic)) // Color represents topic
177
+ . attr ( "fill" , ( d ) => d . color )
159
178
. attr ( "opacity" , 0.5 )
160
179
. on ( "mouseover" , ( event , d ) => {
161
180
tooltip . transition ( ) . duration ( 200 ) . style ( "opacity" , 1 ) ;
@@ -218,7 +237,7 @@ const HorizontalBarChart = () => {
218
237
. attr ( "cx" , 10 )
219
238
. attr ( "cy" , 20 + i * 20 )
220
239
. attr ( "r" , 5 )
221
- . attr ( "fill" , colorScale ( d . topic ) ) ;
240
+ . attr ( "fill" , d . color ) ;
222
241
223
242
legend
224
243
. append ( "text" )
0 commit comments