org.eobjects.analyzer.beans.valuedist.ValueDistributionHtmlFragment.scala Maven / Gradle / Ivy
package org.eobjects.analyzer.beans.valuedist
import scala.collection.JavaConversions._
import org.eobjects.analyzer.result.html.BodyElement
import org.eobjects.analyzer.result.html.DrillToDetailsBodyElement
import org.eobjects.analyzer.result.html.HeadElement
import org.eobjects.analyzer.result.html.HtmlFragment
import org.eobjects.analyzer.result.html.HtmlRenderingContext
import org.eobjects.analyzer.result.html.SimpleHtmlFragment
import org.eobjects.analyzer.result.renderer.RendererFactory
import org.eobjects.analyzer.result.GroupedValueCountingAnalyzerResult
import org.eobjects.analyzer.result.ListResult
import org.eobjects.analyzer.result.ValueCountingAnalyzerResult
import org.eobjects.analyzer.util.LabelUtils
import org.eobjects.analyzer.result.ListResult
import org.eobjects.analyzer.result.ListResult
import org.eobjects.analyzer.result.SingleValueFrequency
import org.eobjects.analyzer.result.ValueFrequency
class ValueDistributionHtmlFragment(result: ValueCountingAnalyzerResult, rendererFactory: RendererFactory) extends HtmlFragment {
val frag = new SimpleHtmlFragment();
override def initialize(context: HtmlRenderingContext) {
frag.addHeadElement(ValueDistributionReusableScriptHeadElement)
val html =
{
if (result.isInstanceOf[GroupedValueCountingAnalyzerResult]) {
val groupedResult = result.asInstanceOf[GroupedValueCountingAnalyzerResult];
groupedResult.getGroupResults().map(r => {
renderResult(r, context, true)
})
} else {
renderResult(result, context, false);
}
}
;
frag.addBodyElement(html.toString())
}
override def getHeadElements(): java.util.List[HeadElement] = {
return frag.getHeadElements();
}
override def getBodyElements(): java.util.List[BodyElement] = {
return frag.getBodyElements();
}
def renderResult(result: ValueCountingAnalyzerResult, context: HtmlRenderingContext, group: Boolean): scala.xml.Node = {
val chartElementId: String = context.createElementId();
val valueCounts = result.getReducedValueFrequencies(32);
frag.addHeadElement(new ValueDistributionChartScriptHeadElement(result, valueCounts, chartElementId));
val numBars = valueCounts.size();
val barHeight = if (numBars < 20) 40 else if (numBars < 30) 30 else 20
val height = numBars * barHeight;
val style = "height: " + height + "px;"
return
{
if (group && result.getName() != null) {
Group: { result.getName() }
}
}
{
}
{
if (!valueCounts.isEmpty()) {
{
valueCounts.iterator().map(valueFreq => {
{ valueFreq.getName() } { getCount(result, valueFreq, context) }
})
}
}
}
Total count { result.getTotalCount() }
{
if (result.getDistinctCount() != null) {
Distinct count { result.getDistinctCount() }
}
}
;
}
def getCount(result: ValueCountingAnalyzerResult, valueFreq: ValueFrequency, context: HtmlRenderingContext): scala.xml.Node = {
val count = valueFreq.getCount();
if (count == 0) {
return { count };
}
if (valueFreq.isComposite()) {
if (LabelUtils.UNIQUE_LABEL.equals(valueFreq.getName())) {
val uniqueValues = result.getUniqueValues()
if (uniqueValues != null && !uniqueValues.isEmpty()) {
val elementId = context.createElementId();
val listResult = new ListResult(uniqueValues.toList);
val bodyElement = new DrillToDetailsBodyElement(elementId, rendererFactory, listResult);
frag.addBodyElement(bodyElement);
val invocation = bodyElement.toJavaScriptInvocation()
return { count }
}
}
return { count };
}
var value = valueFreq.getValue();
val annotatedRowsResult = result.getAnnotatedRowsForValue(value);
if (annotatedRowsResult == null || annotatedRowsResult.getAnnotatedRowCount() == 0) {
return { count };
}
val elementId = context.createElementId();
val bodyElement = new DrillToDetailsBodyElement(elementId, rendererFactory, annotatedRowsResult);
frag.addBodyElement(bodyElement);
val invocation = bodyElement.toJavaScriptInvocation()
return { count }
}
}