Skip to content

Instantly share code, notes, and snippets.

@skeeto
Created May 10, 2012 01:29
Show Gist options
  • Save skeeto/2650299 to your computer and use it in GitHub Desktop.
Save skeeto/2650299 to your computer and use it in GitHub Desktop.
JFreeChart demo
build
dist
cache.properties
TAGS
<?xml version="1.0"?>
<project name="" default="jar" xmlns:ivy="antlib:org.apache.ivy.ant">
<!-- Project-specific configuration -->
<property name="artifactId" value="chart-demo"/>
<property name="description" value="JFreeChart demo."/>
<property name="version" value="0.1"/>
<property name="package.main" value="chart"/>
<property name="main.class" value="${package.main}.Launcher"/>
<property name="base.name" value="${artifactId}-${version}"/>
<!-- Standard Directory Layout -->
<property name="src.dir" value="src"/>
<property name="test.dir" value="test"/>
<property name="build.dir" value="build"/>
<property name="dist.dir" value="dist"/>
<!-- Targets -->
<target name="resolve" description="Retrieve dependencies with Ivy.">
<ivy:resolve log="download-only"/>
<ivy:cachepath conf="build" pathid="build.classpath"/>
<ivy:cachepath conf="default" pathid="runtime.classpath"/>
<ivy:cachefileset conf="default" setid="runtime.fileset"/>
<ivy:cachepath conf="test" pathid="test.classpath"/>
<ivy:cachepath conf="analysis" pathid="analysis.classpath"/>
</target>
<target name="compile" depends="resolve" description="Compile all sources.">
<mkdir dir="${dist.dir}"/>
<mkdir dir="${build.dir}/classes"/>
<javac srcdir="${src.dir}" destdir="${build.dir}/classes"
optimize="on" debug="on" deprecation="on" includeantruntime="no">
<compilerarg value="-Xlint"/>
<classpath refid="build.classpath"/>
</javac>
<copy todir="${build.dir}/classes">
<fileset dir="${src.dir}" excludes="**/*.java"/>
</copy>
</target>
<target name="jar" depends="compile"
description="Generate the jarfile distributable.">
<jar destfile="${dist.dir}/${base.name}.jar"
basedir="${build.dir}/classes">
<manifest>
<attribute name="Main-Class" value="${main.class}"/>
</manifest>
</jar>
<jar destfile="${dist.dir}/${base.name}-all.jar"
basedir="${build.dir}/classes">
<zipgroupfileset refid="runtime.fileset"/>
<manifest>
<attribute name="Main-Class" value="${main.class}"/>
</manifest>
</jar>
</target>
<target name="run" depends="compile" description="Run the application.">
<java classname="${main.class}" classpath="${build.dir}/classes" fork="yes">
<classpath refid="runtime.classpath"/>
</java>
</target>
<target name="clean" description="Delete all generated files.">
<delete dir="${build.dir}"/>
<delete dir="${dist.dir}"/>
<delete file="cache.properties"/>
</target>
<target name="javadoc" depends="resolve,compile"
description="Generate documentation.">
<taskdef classname="lombok.delombok.ant.DelombokTask" name="delombok"
classpathref="build.classpath"/>
<delombok verbose="true" to="${build.dir}/src" from="${src.dir}">
<classpath refid="build.classpath"/>
</delombok>
<javadoc destdir="${dist.dir}/javadoc"
link="http://download.oracle.com/javase/6/docs/api/"
sourcepath="${build.dir}/src"
Doctitle="${ant.project.name} ${version}"
Windowtitle="${ant.project.name} ${version}">
<classpath refid="build.classpath"/>
</javadoc>
</target>
<target name="check" depends="resolve" description="Run Checkstyle.">
<taskdef resource="checkstyletask.properties"
classpathref="analysis.classpath"/>
<checkstyle config="checkstyle.xml">
<fileset dir="${src.dir}" includes="**/*.java"/>
<classpath path="${build.dir}/classes"/>
</checkstyle>
</target>
<!-- Unit testing (JUnit) -->
<target name="test-compile" depends="compile">
<mkdir dir="${build.dir}/test"/>
<javac srcdir="${test.dir}" destdir="${build.dir}/test"
optimize="on" debug="on" deprecation="on"
classpath="${build.dir}/classes" includeantruntime="no">
<compilerarg value="-Xlint"/>
<classpath refid="build.classpath"/>
<classpath refid="test.classpath"/>
</javac>
<copy todir="${build.dir}/test">
<fileset dir="${test.dir}" excludes="**/*.java"/>
</copy>
</target>
<target name="test" depends="test-compile" description="Run the unit tests.">
<junit fork="yes">
<classpath>
<pathelement path="${build.dir}/classes"/>
<pathelement path="${build.dir}/test"/>
<path refid="test.classpath"/>
</classpath>
<batchtest>
<formatter type="brief" usefile="false"/>
<fileset dir="${build.dir}/test"/>
</batchtest>
</junit>
</target>
</project>
<?xml version="1.0"?>
<!DOCTYPE module PUBLIC
"-//Puppy Crawl//DTD Check Configuration 1.2//EN"
"http://www.puppycrawl.com/dtds/configuration_1_2.dtd">
<!--
Checkstyle configuration that checks the sun coding conventions from:
- the Java Language Specification at
http://java.sun.com/docs/books/jls/second_edition/html/index.html
- the Sun Code Conventions at http://java.sun.com/docs/codeconv/
- the Javadoc guidelines at
http://java.sun.com/j2se/javadoc/writingdoccomments/index.html
- the JDK Api documentation http://java.sun.com/j2se/docs/api/index.html
- some best practices
Checkstyle is very configurable. Be sure to read the documentation at
http://checkstyle.sf.net (or in your downloaded distribution).
Most Checks are configurable, be sure to consult the documentation.
To completely disable a check, just comment it out or delete it from the file.
Finally, it is worth reading the documentation.
-->
<module name="Checker">
<!--
If you set the basedir property below, then all reported file
names will be relative to the specified directory. See
http://checkstyle.sourceforge.net/5.x/config.html#Checker
<property name="basedir" value="${basedir}"/>
-->
<!-- Checks that a package-info.java file exists for each package. -->
<!-- See http://checkstyle.sf.net/config_javadoc.html#JavadocPackage -->
<module name="JavadocPackage"/>
<!-- Checks whether files end with a new line. -->
<!-- See http://checkstyle.sf.net/config_misc.html#NewlineAtEndOfFile -->
<module name="NewlineAtEndOfFile"/>
<!-- Checks that property files contain the same keys. -->
<!-- See http://checkstyle.sf.net/config_misc.html#Translation -->
<module name="Translation"/>
<!-- Checks for Size Violations. -->
<!-- See http://checkstyle.sf.net/config_sizes.html -->
<module name="FileLength"/>
<!-- Checks for whitespace -->
<!-- See http://checkstyle.sf.net/config_whitespace.html -->
<module name="FileTabCharacter"/>
<!-- Miscellaneous other checks. -->
<!-- See http://checkstyle.sf.net/config_misc.html -->
<module name="RegexpSingleline">
<property name="format" value="\s+$"/>
<property name="minimum" value="0"/>
<property name="maximum" value="0"/>
<property name="message" value="Line has trailing spaces."/>
</module>
<module name="TreeWalker">
<!-- Checks for Javadoc comments. -->
<!-- See http://checkstyle.sf.net/config_javadoc.html -->
<module name="JavadocMethod"/>
<module name="JavadocType"/>
<module name="JavadocStyle"/>
<!-- Checks for Naming Conventions. -->
<!-- See http://checkstyle.sf.net/config_naming.html -->
<module name="ConstantName"/>
<module name="LocalFinalVariableName"/>
<module name="LocalVariableName"/>
<module name="MemberName"/>
<module name="MethodName"/>
<module name="PackageName"/>
<module name="ParameterName"/>
<module name="StaticVariableName"/>
<module name="TypeName"/>
<!-- Checks for Headers -->
<!-- See http://checkstyle.sf.net/config_header.html -->
<!-- <module name="Header"> -->
<!-- The follow property value demonstrates the ability -->
<!-- to have access to ANT properties. In this case it uses -->
<!-- the ${basedir} property to allow Checkstyle to be run -->
<!-- from any directory within a project. See property -->
<!-- expansion, -->
<!-- http://checkstyle.sf.net/config.html#properties -->
<!-- <property -->
<!-- name="headerFile" -->
<!-- value="${basedir}/java.header"/> -->
<!-- </module> -->
<!-- Following interprets the header file as regular expressions. -->
<!-- <module name="RegexpHeader"/> -->
<!-- Checks for imports -->
<!-- See http://checkstyle.sf.net/config_import.html -->
<module name="AvoidStarImport"/>
<module name="IllegalImport"/> <!-- defaults to sun.* packages -->
<module name="RedundantImport"/>
<module name="UnusedImports"/>
<!-- Checks for Size Violations. -->
<!-- See http://checkstyle.sf.net/config_sizes.html -->
<module name="LineLength"/>
<module name="MethodLength"/>
<module name="ParameterNumber"/>
<!-- Checks for whitespace -->
<!-- See http://checkstyle.sf.net/config_whitespace.html -->
<module name="EmptyForIteratorPad"/>
<module name="GenericWhitespace"/>
<module name="MethodParamPad"/>
<module name="NoWhitespaceAfter"/>
<module name="NoWhitespaceBefore"/>
<module name="OperatorWrap"/>
<module name="ParenPad"/>
<module name="TypecastParenPad"/>
<module name="WhitespaceAfter"/>
<module name="WhitespaceAround"/>
<!-- Modifier Checks -->
<!-- See http://checkstyle.sf.net/config_modifiers.html -->
<module name="ModifierOrder"/>
<module name="RedundantModifier"/>
<!-- Checks for blocks. You know, those {}'s -->
<!-- See http://checkstyle.sf.net/config_blocks.html -->
<module name="AvoidNestedBlocks"/>
<module name="EmptyBlock"/>
<module name="LeftCurly"/>
<module name="NeedBraces"/>
<module name="RightCurly"/>
<!-- Checks for common coding problems -->
<!-- See http://checkstyle.sf.net/config_coding.html -->
<module name="AvoidInlineConditionals"/>
<module name="DoubleCheckedLocking"/> <!-- MY FAVOURITE -->
<module name="EmptyStatement"/>
<module name="EqualsHashCode"/>
<module name="IllegalInstantiation"/>
<module name="InnerAssignment"/>
<module name="MissingSwitchDefault"/>
<module name="RedundantThrows"/>
<module name="SimplifyBooleanExpression"/>
<module name="SimplifyBooleanReturn"/>
<!-- Checks for class design -->
<!-- See http://checkstyle.sf.net/config_design.html -->
<module name="DesignForExtension"/>
<module name="FinalClass"/>
<module name="HideUtilityClassConstructor"/>
<module name="InterfaceIsType"/>
<module name="VisibilityModifier"/>
<!-- Miscellaneous other checks. -->
<!-- See http://checkstyle.sf.net/config_misc.html -->
<module name="ArrayTypeStyle"/>
<module name="FinalParameters"/>
<module name="TodoComment"/>
<module name="UpperEll"/>
</module>
</module>
<?xml version="1.0"?>
<ivy-module version="2.0">
<info organisation="com.nullprogram" module=""/>
<configurations>
<conf name="default"/>
<conf name="build" extends="default" visibility="private"/>
<conf name="test" extends="build" visibility="private"/>
<conf name="analysis" extends="build" visibility="private"/>
</configurations>
<dependencies>
<!-- Run -->
<dependency org="org.jfree" name="jfreechart" rev="1.0.14"
conf="default->default"/>
<!-- Build -->
<dependency org="org.projectlombok" name="lombok" rev="0.10.4"
conf="build->default"/>
<!-- Unit Test -->
<dependency org="junit" name="junit" rev="4.10"
conf="test->default"/>
<!-- Static Analysis -->
<dependency org="com.puppycrawl.tools" name="checkstyle" rev="5.5"
conf="analysis->default"/>
</dependencies>
</ivy-module>
package chart;
import java.awt.Dimension;
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JSlider;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import lombok.val;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.xy.XYDataset;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;
import static org.jfree.chart.ChartFactory.createXYLineChart;
/**
* The main class.
*/
public final class Launcher {
/** Size of the plot. */
private static final Dimension SIZE = new Dimension(480, 320);
/**
* Hidden constructor.
*/
private Launcher() {
}
/**
* The main method.
* @param args command line arguments.
*/
public static void main(final String[] args) {
/* Set up data. */
final XYSeries data = new XYSeries(0d);
JSlider slider = new JSlider(1, 600);
double scale = 100;
PlotFunction func = new XZSin();
for (double x = -30; x <= 30; x += 0.1) {
data.add(new PlotPoint(slider, scale, x, func));
}
/* Set up plot. */
XYDataset dataset = new XYSeriesCollection(data);
val chart = createXYLineChart(null, "X", "Y", dataset,
PlotOrientation.VERTICAL,
false, false, false);
/* Set up GUI. */
JFrame frame = new JFrame("Chart demo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
val layout = new BoxLayout(frame.getContentPane(), BoxLayout.Y_AXIS);
frame.setLayout(layout);
ChartPanel panel = new ChartPanel(chart);
panel.setPreferredSize(SIZE);
frame.add(panel);
frame.add(slider);
frame.pack();
frame.setVisible(true);
slider.addChangeListener(new ChangeListener() {
public void stateChanged(final ChangeEvent e) {
data.fireSeriesChanged();
}
});
}
}
/**
* Just testing out JFreeChart.
*/
package chart;
package chart;
/**
* A function that can be plotted in the demo.
*/
public interface PlotFunction {
/**
* Compute the Y value for the given X and Z.
* @param x the X value
* @param z the X value
* @return the y value
*/
double getY(double x, double z);
}
package chart;
import javax.swing.JSlider;
import org.jfree.data.xy.XYDataItem;
/**
* A single point of the plot that updates based on a JSlider.
*/
@SuppressWarnings("serial")
public final class PlotPoint extends XYDataItem {
private final JSlider slider;
private final double scale;
private final PlotFunction func;
/**
* Create a new plot point for a Z JSlider.
* @param slider the Z component
* @param scale scale of the ticks on the slider
* @param x the X-value of this point
* @param func the function for calculating Y
*/
public PlotPoint(final JSlider slider, final double scale,
final double x, final PlotFunction func) {
super(x, 0);
this.slider = slider;
this.scale = scale;
this.func = func;
}
@Override
public double getYValue() {
return func.getY(getXValue(), slider.getValue() / scale);
}
@Override
public Number getY() {
return getYValue();
}
}
This is free and unencumbered software released into the public domain.
Anyone is free to copy, modify, publish, use, compile, sell, or
distribute this software, either in source code form or as a compiled
binary, for any purpose, commercial or non-commercial, and by any
means.
In jurisdictions that recognize copyright laws, the author or authors
of this software dedicate any and all copyright interest in the
software to the public domain. We make this dedication for the benefit
of the public at large and to the detriment of our heirs and
successors. We intend this dedication to be an overt act of
relinquishment in perpetuity of all present and future rights to this
software under copyright law.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
For more information, please refer to <http://unlicense.org/>
package chart;
/**
* For the function "y = sin(x * z) * x^2".
*/
public final class XZSin implements PlotFunction {
@Override
public double getY(final double x, final double z) {
return Math.sin(x * z) * x * x;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment