Add horizontal bar visualization example

This commit is contained in:
Lee moon soo 2016-05-14 20:17:08 -07:00
parent 52255511bb
commit b974eb14d7
6 changed files with 290 additions and 0 deletions

View file

@ -36,6 +36,7 @@
<modules>
<module>zeppelin-example-clock</module>
<module>zeppelin-example-horizontalbar</module>
</modules>
<build>

View file

@ -0,0 +1,113 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Licensed to the Apache Software Foundation (ASF) under one or more
~ contributor license agreements. See the NOTICE file distributed with
~ this work for additional information regarding copyright ownership.
~ The ASF licenses this file to You under the Apache License, Version 2.0
~ (the "License"); you may not use this file except in compliance with
~ the License. You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License.
-->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<artifactId>zeppelin-examples</artifactId>
<groupId>org.apache.zeppelin</groupId>
<version>0.6.0-incubating-SNAPSHOT</version>
<relativePath>..</relativePath>
</parent>
<groupId>org.apache.zeppelin</groupId>
<artifactId>zeppelin-example-horizontalbar</artifactId>
<packaging>jar</packaging>
<version>0.6.0-incubating-SNAPSHOT</version>
<name>Zeppelin: Example application - Horizontal Bar chart</name>
<url>http://zeppelin.incubator.apache.org</url>
<dependencies>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>zeppelin-interpreter</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.7</version>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<configuration>
<filesets>
<fileset>
<directory>${project.basedir}/../../helium</directory>
<includes>
<include>${project.artifactId}.json</include>
</includes>
</fileset>
</filesets>
</configuration>
</plugin>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>2.7</version>
<executions>
<execution>
<phase>generate-resources</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${project.basedir}/../../helium/</outputDirectory>
<resources>
<resource>
<directory>${project.basedir}</directory>
<includes>
<include>${project.artifactId}.json</include>
</includes>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>

View file

@ -0,0 +1,85 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.zeppelin.example.app.horizontalbar;
import org.apache.commons.io.IOUtils;
import org.apache.zeppelin.helium.Application;
import org.apache.zeppelin.helium.ApplicationContext;
import org.apache.zeppelin.helium.ApplicationException;
import org.apache.zeppelin.interpreter.InterpreterResult;
import org.apache.zeppelin.interpreter.dev.ZeppelinApplicationDevServer;
import org.apache.zeppelin.resource.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;
/**
* Basic example application.
* TableData for input
*/
public class HorizontalBar extends Application {
private final Logger logger = LoggerFactory.getLogger(HorizontalBar.class);
InterpreterResult result;
public HorizontalBar(ApplicationContext context) {
super(context);
}
@Override
public void run(ResourceSet resources) throws ApplicationException, IOException {
// Get data from resource args
result = (InterpreterResult) resources.get(0).get();
// create element
println(String.format(
"<div id=\"horizontalbar_%s\" style=\"height:400px\"><svg></svg></div>",
context().getApplicationInstanceId()));
// write js
printResourceAsJavascript("example/app/horizontalbar/horizontalbar.js");
}
@Override
public void unload() throws ApplicationException {
}
/**
* Development mode
*/
public static void main(String[] args) throws Exception {
LocalResourcePool pool = new LocalResourcePool("dev");
InputStream ins = ClassLoader.getSystemResourceAsStream(
"example/app/horizontalbar/horizontalbar_mockdata.txt");
InterpreterResult result = new InterpreterResult(
InterpreterResult.Code.SUCCESS,
InterpreterResult.Type.TABLE,
IOUtils.toString(ins));
pool.put(WellKnownResourceName.ZeppelinTableResult.name(), result);
ZeppelinApplicationDevServer devServer = new ZeppelinApplicationDevServer(
HorizontalBar.class.getName(),
pool.getAll());
devServer.start();
devServer.join();
}
}

View file

@ -0,0 +1,56 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
var data = [];
_.forEach($z.result.columnNames, function(col, series) {
if (series == 0) return;
var values = _.map($z.result.rows, function(row) {
return {
label: row[0],
value : parseFloat(row[series])
}
});
data.push({
key : col.name,
values : values
})
});
nv.addGraph(function() {
var chart = nv.models.multiBarHorizontalChart()
.x(function(d) { return d.label })
.y(function(d) { return d.value })
.margin({top: 30, right: 20, bottom: 50, left: 175})
.showValues(true) //Show bar value next to each bar.
.tooltips(true) //Show tooltips on hover.
.showControls(true); //Allow user to switch between "Grouped" and "Stacked" mode.
chart.yAxis
.tickFormat(d3.format(',.2f'));
d3.select('#horizontalbar_' + $z.id + ' svg')
.datum(data)
.call(chart);
nv.utils.windowResize(chart.update);
return chart;
});

View file

@ -0,0 +1,10 @@
Label Series1 Series2
GroupA -1.8746444827653 25.307646510375
GroupB -8.0961543492239 16.756779544553
GroupC -0.57072943117674 18.451534877007
GroupD -2.4174010336624 8.6142352811805
GroupE -0.72009071426284 7.8082472075876
GroupF -0.77154485523777 5.259101026956
GroupG -0.90152097798131 0.30947953487127
GroupH -0.91445417330854 0
GroupI -0.055746319141851 0

View file

@ -0,0 +1,25 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
{
type : "APPLICATION",
name : "zeppelin.horizontalbar",
description : "Horizontal Bar chart (example)",
artifact : "zeppelin-examples/zeppelin-example-horizontalbar/target/zeppelin-example-horizontalbar-0.6.0-incubating-SNAPSHOT.jar",
className : "org.apache.zeppelin.example.app.horizontalbar.HorizontalBar",
resources : [[":org.apache.zeppelin.interpreter.InterpreterResult"]],
icon : '<i class="fa fa-bar-chart" style="-webkit-transform: rotate(90deg) scaleX(-1);-moz-transform: rotate(90deg) scaleX(-1);-ms-transform: rotate(90deg) scaleX(-1);"></i>'
}