ldavis2019/ldavis.js
// the circle argument should be the appropriate circle element
the circle argument should be the appropriate circle element
the circle argument should be the appropriate circle element
function topic_on(circle) {
            if (circle == null) return null;
            var old_term_clicked_id = termID + vis_state.term_clicked;
            var topic_clicked_id = topicID + vis_state.topic_clicked;
            if (vis_state.term_clicked != "" && circle.id != topic_clicked_id) {
                var oldterm = document.getElementById(old_term_clicked_id);
                if (oldterm != null) {
                    oldterm.style.textDecoration = null;  
                }
            }
            var d = circle.__data__
            var Freq = Math.round(d.Freq * 10) / 10,
            topics = d.topics;
            circle.style.opacity = highlight_opacity;
            circle.style.fill = color2;
            var text = d3.select(".bubble-tool");
            text.remove();
            d3.select("#bar-freqs")
		.append("text")
		.attr("x", barwidth/2)
		.attr("y", -30)
		.attr("class", "bubble-tool") 
		.style("text-anchor", "middle")
		.style("font-size", "16px")
		.text("Top-" + R + " Most Relevant Terms for Topic " + topics + " (" + Freq + "% of tokens)");
            var dat2 = lamData.filter(function(d) {
                return d.Category == "Topic" + topics
            });
            for (var i = 0; i < dat2.length; i++) {
                dat2[i].relevance = lambda.current * dat2[i].logprob +
                    (1 - lambda.current) * dat2[i].loglift;
            }
            dat2.sort(fancysort("relevance"));
            var dat3 = dat2.slice(0, R);
            var y = d3.scale.ordinal()
                .domain(dat3.map(function(d) {
                    return d.Term;
                }))
                .rangeRoundBands([0, barheight], 0.15);
            var x = d3.scale.linear()
                .domain([0, d3.max(dat3, function(d) {
                    return d.Total;
                })])
                .range([0, barwidth])
                .nice();
            d3.selectAll(".overlay").remove();
            d3.selectAll(".bar-totals")
                .data(dat3)
                .attr("x", 0)
                .attr("y", function(d) {
                    return y(d.Term);
                })
                .attr("height", y.rangeBand())
                .attr("width", function(d) {
                    return x(d.Total);
                })
                .style("fill", color1)
                .attr("opacity", 0.4);
            d3.selectAll(".terms")
                .data(dat3)
                .attr("x", -5)
                .attr("y", function(d) {
                    return y(d.Term) + 12;
                })
                .attr("id", function(d) {
                    return (termID + d.Term)
                })
                .style("text-anchor", "end") 
                .text(function(d) {
                    return d.Term;
                });
            d3.select("#bar-freqs").selectAll(".overlay")
                .data(dat3)
                .enter()
                .append("rect")
                .attr("class", "overlay")
                .attr("x", 0)
                .attr("y", function(d) {
                    return y(d.Term);
                })
                .attr("height", y.rangeBand())
                .attr("width", function(d) {
                    return x(d.Freq);
                })
                .style("fill", color2)
                .attr("opacity", 0.8);
            var xAxis = d3.svg.axis().scale(x)
                .orient("top")
                .tickSize(-barheight)
                .tickSubdivide(true)
                .ticks(6);
            d3.selectAll(".xaxis")
                .call(xAxis);
        }
function topic_on ( circle ) { if ( circle == null ) return null ; var old_term_clicked_id = termID + vis_state . term_clicked ; var topic_clicked_id = topicID + vis_state . topic_clicked ; if ( vis_state . term_clicked != "" && circle . id != topic_clicked_id ) { var oldterm = document . getElementById ( old_term_clicked_id ) ; if ( oldterm != null ) { oldterm . style . textDecoration = null ; } } var d = circle . __data__ var Freq = Math . round ( d . Freq * 10 ) / 10 , topics = d . topics ; circle . style . opacity = highlight_opacity ; circle . style . fill = color2 ; var text = d3 . select ( ".bubble-tool" ) ; text . remove ( ) ; d3 . select ( "#bar-freqs" ) . append ( "text" ) . attr ( "x" , barwidth / 2 ) . attr ( "y" , - 30 ) . attr ( "class" , "bubble-tool" ) . style ( "text-anchor" , "middle" ) . style ( "font-size" , "16px" ) . text ( "Top-" + R + " Most Relevant Terms for Topic " + topics + " (" + Freq + "% of tokens)" ) ; var dat2 = lamData . filter ( function ( d ) { return d . Category == "Topic" + topics } ) ; for ( var i = 0 ; i < dat2 . length ; i ++ ) { dat2 [ i ] . relevance = lambda . current * dat2 [ i ] . logprob + ( 1 - lambda . current ) * dat2 [ i ] . loglift ; } dat2 . sort ( fancysort ( "relevance" ) ) ; var dat3 = dat2 . slice ( 0 , R ) ; var y = d3 . scale . ordinal ( ) . domain ( dat3 . map ( function ( d ) { return d . Term ; } ) ) . rangeRoundBands ( [ 0 , barheight ] , 0.15 ) ; var x = d3 . scale . linear ( ) . domain ( [ 0 , d3 . max ( dat3 , function ( d ) { return d . Total ; } ) ] ) . range ( [ 0 , barwidth ] ) . nice ( ) ; d3 . selectAll ( ".overlay" ) . remove ( ) ; d3 . selectAll ( ".bar-totals" ) . data ( dat3 ) . attr ( "x" , 0 ) . attr ( "y" , function ( d ) { return y ( d . Term ) ; } ) . attr ( "height" , y . rangeBand ( ) ) . attr ( "width" , function ( d ) { return x ( d . Total ) ; } ) . style ( "fill" , color1 ) . attr ( "opacity" , 0.4 ) ; d3 . selectAll ( ".terms" ) . data ( dat3 ) . attr ( "x" , - 5 ) . attr ( "y" , function ( d ) { return y ( d . Term ) + 12 ; } ) . attr ( "id" , function ( d ) { return ( termID + d . Term ) } ) . style ( "text-anchor" , "end" ) . text ( function ( d ) { return d . Term ; } ) ; d3 . select ( "#bar-freqs" ) . selectAll ( ".overlay" ) . data ( dat3 ) . enter ( ) . append ( "rect" ) . attr ( "class" , "overlay" ) . attr ( "x" , 0 ) . attr ( "y" , function ( d ) { return y ( d . Term ) ; } ) . attr ( "height" , y . rangeBand ( ) ) . attr ( "width" , function ( d ) { return x ( d . Freq ) ; } ) . style ( "fill" , color2 ) . attr ( "opacity" , 0.8 ) ; var xAxis = d3 . svg . axis ( ) . scale ( x ) . orient ( "top" ) . tickSize ( - barheight ) . tickSubdivide ( true ) . ticks ( 6 ) ; d3 . selectAll ( ".xaxis" ) . call ( xAxis ) ; }
the circle argument should be the appropriate circle element
the circle argument should be the appropriate circle element
// whenever the topic changes we have to remove the underline style // from any clicked term // grab data bound to this element // change opacity and fill of the selected circle // Remove 'old' bar chart title // append text with info relevant to topic of interest //  set class so we can remove it when highlight_off is called   // grab the bar-chart data for this topic only: // define relevance: // sort by relevance: // truncate to the top R tokens: // scale the bars to the top R terms: // remove the red bars if there are any: // Change Total Frequency bars // Change word labels // right align text - use 'middle' for center alignment // Create red bars (drawn over the gray ones) to signify the frequency under the selected topic // adapted from http://bl.ocks.org/mbostock/1166403 // redraw x-axis //.attr("class", "xaxis")