Skip to content
Snippets Groups Projects
Commit b789cd3a authored by Tim Molter's avatar Tim Molter
Browse files

new Chart Demo app, demos all charts in one GUI app.

parent 60ee0ac1
No related branches found
No related tags found
No related merge requests found
Showing
with 403 additions and 39 deletions
/**
* Copyright (c) 2011-2013 Xeiam LLC.
*
* Licensed 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 com.xeiam.xchart.demo;
import java.awt.Dimension;
import java.awt.GridLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTree;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreeSelectionModel;
import com.xeiam.xchart.XChartPanel;
import com.xeiam.xchart.demo.charts.Example10;
import com.xeiam.xchart.demo.charts.Example11;
import com.xeiam.xchart.demo.charts.Example2;
import com.xeiam.xchart.demo.charts.Example3;
import com.xeiam.xchart.demo.charts.Example5;
import com.xeiam.xchart.demo.charts.Example6;
import com.xeiam.xchart.demo.charts.Example7;
import com.xeiam.xchart.demo.charts.Example8;
import com.xeiam.xchart.demo.charts.Example9;
/**
* Class containing all XChart example charts
*
* @author timmolter
*/
public class ChartDemo extends JPanel implements TreeSelectionListener {
/** The main split frame */
private JSplitPane splitPane;
/** The tree */
private JTree tree;
/** The panel for chart */
private JPanel chartPanel;
/**
* Constructor
*/
public ChartDemo() {
super(new GridLayout(1, 0));
// Create the nodes.
DefaultMutableTreeNode top = new DefaultMutableTreeNode("XChart Example Charts");
createNodes(top);
// Create a tree that allows one selection at a time.
tree = new JTree(top);
tree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
// Listen for when the selection changes.
tree.addTreeSelectionListener(this);
// Create the scroll pane and add the tree to it.
JScrollPane treeView = new JScrollPane(tree);
// Create Chart Panel
chartPanel = new XChartPanel(new Example10().getChart());
// Add the scroll panes to a split pane.
splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
splitPane.setTopComponent(treeView);
splitPane.setBottomComponent(chartPanel);
Dimension minimumSize = new Dimension(100, 125);
treeView.setMinimumSize(minimumSize);
splitPane.setDividerLocation(100);
splitPane.setPreferredSize(new Dimension(800, 800));
// Add the split pane to this panel.
add(splitPane);
}
@Override
public void valueChanged(TreeSelectionEvent e) {
DefaultMutableTreeNode node = (DefaultMutableTreeNode) tree.getLastSelectedPathComponent();
if (node == null) {
return;
}
Object nodeInfo = node.getUserObject();
// tree leaf
if (node.isLeaf()) {
ChartInfo chartInfo = (ChartInfo) nodeInfo;
// displayURL(chartInfo.bookURL);
chartPanel = new XChartPanel(chartInfo.getExampleChart());
splitPane.setBottomComponent(chartPanel);
}
// tree branch node
else {
// do nothing
}
}
private void createNodes(DefaultMutableTreeNode top) {
// categories
DefaultMutableTreeNode category = null;
// leaves
DefaultMutableTreeNode chart = null;
// First category
category = new DefaultMutableTreeNode("Line Charts");
top.add(category);
chart = new DefaultMutableTreeNode(new ChartInfo("Example10", new Example10().getChart()));
category.add(chart);
chart = new DefaultMutableTreeNode(new ChartInfo("Example11", new Example11().getChart()));
category.add(chart);
chart = new DefaultMutableTreeNode(new ChartInfo("Example2", new Example2().getChart()));
category.add(chart);
chart = new DefaultMutableTreeNode(new ChartInfo("Example3", new Example3().getChart()));
category.add(chart);
chart = new DefaultMutableTreeNode(new ChartInfo("Example5", new Example5().getChart()));
category.add(chart);
chart = new DefaultMutableTreeNode(new ChartInfo("Example6", new Example6().getChart()));
category.add(chart);
// Second category
category = new DefaultMutableTreeNode("Other Charts");
top.add(category);
chart = new DefaultMutableTreeNode(new ChartInfo("Example7", new Example7().getChart()));
category.add(chart);
chart = new DefaultMutableTreeNode(new ChartInfo("Example8", new Example8().getChart()));
category.add(chart);
chart = new DefaultMutableTreeNode(new ChartInfo("Example9", new Example9().getChart()));
category.add(chart);
}
/**
* Create the GUI and show it. For thread safety, this method should be invoked from the event dispatch thread.
*/
private static void createAndShowGUI() {
// Create and set up the window.
JFrame frame = new JFrame("XChart Demo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Add content to the window.
frame.add(new ChartDemo());
// Display the window.
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
// Schedule a job for the event dispatch thread:
// creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
createAndShowGUI();
}
});
}
}
/**
* Copyright (C) 2013 Xeiam LLC http://xeiam.com
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
* of the Software, and to permit persons to whom the Software is furnished to do
* so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* 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 OR COPYRIGHT HOLDERS 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.
*/
package com.xeiam.xchart.demo;
import com.xeiam.xchart.Chart;
/**
* @author timmolter
*/
public final class ChartInfo {
private final String exampleChartName;
private final Chart exampleChart;
/**
* Constructor
*
* @param exampleChartName
* @param exampleChart
*/
public ChartInfo(String exampleChartName, Chart exampleChart) {
this.exampleChartName = exampleChartName;
this.exampleChart = exampleChart;
}
public String getExampleChartName() {
return exampleChartName;
}
public Chart getExampleChart() {
return exampleChart;
}
@Override
public String toString() {
return this.exampleChartName;
}
}
......@@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.xeiam.xchart.example;
package com.xeiam.xchart.demo.charts;
import java.util.Arrays;
import java.util.Collection;
......@@ -28,10 +28,18 @@ import com.xeiam.xchart.SwingWrapper;
*
* @author timmolter
*/
public class Example10 {
public class Example10 implements ExampleChart {
public static void main(String[] args) {
ExampleChart exampleChart = new Example10();
Chart chart = exampleChart.getChart();
new SwingWrapper(chart).displayChart();
}
@Override
public Chart getChart() {
// data
Number[] xDataArray = new Number[] { 0.0, 0.03139525976465669, 0.06266661678215213, 0.0936906572928623, 0.1243449435824274, 0.1545084971874737, 0.184062276342339, 0.21288964578253636,
0.24087683705085766, 0.26791339748949833, 0.29389262614623657, 0.3187119948743448, 0.34227355296434425, 0.3644843137107057, 0.38525662138789457, 0.4045084971874737, 0.42216396275100754,
......@@ -73,6 +81,6 @@ public class Example10 {
Series series1 = chart.addSeries("data", xData, yData);
series1.setMarker(SeriesMarker.NONE);
new SwingWrapper(chart).displayChart();
return chart;
}
}
......@@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.xeiam.xchart.example;
package com.xeiam.xchart.demo.charts;
import java.util.Arrays;
import java.util.Collection;
......@@ -28,10 +28,18 @@ import com.xeiam.xchart.SwingWrapper;
*
* @author timmolter
*/
public class Example11 {
public class Example11 implements ExampleChart {
public static void main(String[] args) {
ExampleChart exampleChart = new Example11();
Chart chart = exampleChart.getChart();
new SwingWrapper(chart).displayChart();
}
@Override
public Chart getChart() {
// data
Number[] xDataArray = new Number[] { 0.0, 2.0E-6, 4.0E-6, 6.0E-6, 8.0E-6, 9.999999999999999E-6, 1.1999999999999999E-5, 1.3999999999999998E-5, 1.6E-5, 1.8E-5, 2.0E-5, 2.2000000000000003E-5,
2.4000000000000004E-5, 2.6000000000000005E-5, 2.8000000000000006E-5, 3.0000000000000008E-5, 3.2000000000000005E-5, 3.4000000000000007E-5, 3.600000000000001E-5, 3.800000000000001E-5,
......@@ -88,6 +96,6 @@ public class Example11 {
Series series1 = chart.addSeries("data", xData, yData);
series1.setMarker(SeriesMarker.NONE);
new SwingWrapper(chart).displayChart();
return chart;
}
}
......@@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.xeiam.xchart.example;
package com.xeiam.xchart.demo.charts;
import java.util.ArrayList;
import java.util.Collection;
......@@ -30,10 +30,18 @@ import com.xeiam.xchart.SwingWrapper;
*
* @author timmolter
*/
public class Example2 {
public class Example2 implements ExampleChart {
public static void main(String[] args) {
ExampleChart exampleChart = new Example10();
Chart chart = exampleChart.getChart();
new SwingWrapper(chart).displayChart();
}
@Override
public Chart getChart() {
// generates sine data
int size = 30;
Collection<Number> xData1 = new ArrayList<Number>();
......@@ -45,7 +53,7 @@ public class Example2 {
}
// Create Chart
Chart chart = new Chart(440, 300);
Chart chart = new Chart(800, 600);
// Customize Chart
chart.setTitleVisible(false);
......@@ -58,7 +66,7 @@ public class Example2 {
series1.setMarkerColor(SeriesColor.GREEN);
series1.setMarker(SeriesMarker.SQUARE);
new SwingWrapper(chart).displayChart();
return chart;
}
}
......@@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.xeiam.xchart.example;
package com.xeiam.xchart.demo.charts;
import java.util.ArrayList;
import java.util.Collection;
......@@ -26,12 +26,20 @@ import com.xeiam.xchart.SwingWrapper;
*
* @author timmolter
*/
public class Example3 {
public class Example3 implements ExampleChart {
public static void main(String[] args) {
ExampleChart exampleChart = new Example10();
Chart chart = exampleChart.getChart();
new SwingWrapper(chart).displayChart();
}
@Override
public Chart getChart() {
// Create Chart
Chart chart = new Chart(700, 500);
Chart chart = new Chart(800, 600);
for (int i = 1; i <= 14; i++) {
......@@ -53,7 +61,6 @@ public class Example3 {
chart.addSeries(seriesName, xData, yData);
}
// JFrame testFrame = new TestFrame(chart);
new SwingWrapper(chart).displayChart();
return chart;
}
}
......@@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.xeiam.xchart.example;
package com.xeiam.xchart.demo.charts;
import com.xeiam.xchart.Chart;
import com.xeiam.xchart.SwingWrapper;
......@@ -23,12 +23,20 @@ import com.xeiam.xchart.SwingWrapper;
*
* @author timmolter
*/
public class Example5 {
public class Example5 implements ExampleChart {
public static void main(String[] args) {
ExampleChart exampleChart = new Example10();
Chart chart = exampleChart.getChart();
new SwingWrapper(chart).displayChart();
}
@Override
public Chart getChart() {
// Create Chart
Chart chart = new Chart(700, 500);
Chart chart = new Chart(800, 600);
// Customize Chart
chart.setTitle("Sample Chart");
......@@ -38,7 +46,7 @@ public class Example5 {
chart.addSeries("vertical", new double[] { 1, 1 }, new double[] { -10, 10 });
chart.addSeries("horizontal", new double[] { -10, 10 }, new double[] { 0, 0 });
new SwingWrapper(chart).displayChart();
return chart;
}
}
......@@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.xeiam.xchart.example;
package com.xeiam.xchart.demo.charts;
import com.xeiam.xchart.Chart;
import com.xeiam.xchart.SwingWrapper;
......@@ -23,12 +23,20 @@ import com.xeiam.xchart.SwingWrapper;
*
* @author timmolter
*/
public class Example6 {
public class Example6 implements ExampleChart {
public static void main(String[] args) {
ExampleChart exampleChart = new Example10();
Chart chart = exampleChart.getChart();
new SwingWrapper(chart).displayChart();
}
@Override
public Chart getChart() {
// Create Chart
Chart chart = new Chart(700, 500);
Chart chart = new Chart(800, 600);
// Customize Chart
chart.setTitle("Sample Chart");
......@@ -37,7 +45,7 @@ public class Example6 {
chart.addSeries("single point (1,1)", new double[] { 1 }, new double[] { 1 });
new SwingWrapper(chart).displayChart();
return chart;
}
}
......@@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.xeiam.xchart.example;
package com.xeiam.xchart.demo.charts;
import java.util.Arrays;
import java.util.Collection;
......@@ -26,22 +26,29 @@ import com.xeiam.xchart.SwingWrapper;
*
* @author timmolter
*/
public class Example7 {
public class Example7 implements ExampleChart {
public static void main(String[] args) throws Exception {
public static void main(String[] args) {
ExampleChart exampleChart = new Example10();
Chart chart = exampleChart.getChart();
new SwingWrapper(chart).displayChart();
}
@Override
public Chart getChart() {
Collection<Number> xData = Arrays.asList(new Number[] { 12228120L, 12228984L, 12229848L, 12230712L, 12231576L, 12232440L, 12233304L, 12234168L, 12235032L, 12235896L });
Collection<Number> yData = Arrays.asList(new Number[] { 0.0, 1.0, 2.0, 0.0, 1.0, 2.0, 0.0, 1.0, 2.0, 0.0 });
// Create Chart
Chart chart = new Chart(700, 500);
Chart chart = new Chart(800, 600);
chart.setTitle("Sample Chart");
chart.setXAxisTitle("X");
chart.setYAxisTitle("Y");
chart.addSeries("y(x)", xData, yData);
new SwingWrapper(chart).displayChart();
return chart;
}
}
......@@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.xeiam.xchart.example;
package com.xeiam.xchart.demo.charts;
import java.util.ArrayList;
import java.util.Collection;
......@@ -30,10 +30,18 @@ import com.xeiam.xchart.SwingWrapper;
*
* @author timmolter
*/
public class Example8 {
public class Example8 implements ExampleChart {
public static void main(String[] args) {
ExampleChart exampleChart = new Example10();
Chart chart = exampleChart.getChart();
new SwingWrapper(chart).displayChart();
}
@Override
public Chart getChart() {
// generates data
int size = 10;
Collection<Number> xData1 = new ArrayList<Number>();
......@@ -46,7 +54,7 @@ public class Example8 {
}
// Create Chart
Chart chart = new Chart(600, 400);
Chart chart = new Chart(800, 600);
// Customize Chart
chart.setTitleVisible(false);
......@@ -60,7 +68,7 @@ public class Example8 {
series1.setMarkerColor(SeriesColor.GREEN);
series1.setMarker(SeriesMarker.SQUARE);
new SwingWrapper(chart).displayChart();
return chart;
}
}
......@@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.xeiam.xchart.example;
package com.xeiam.xchart.demo.charts;
import java.awt.Color;
import java.awt.Font;
......@@ -38,20 +38,34 @@ import com.xeiam.xchart.SwingWrapper;
*
* @author timmolter
*/
public class Example9 {
public class Example9 implements ExampleChart {
public static void main(String[] args) throws ParseException {
public static void main(String[] args) {
ExampleChart exampleChart = new Example9();
Chart chart = exampleChart.getChart();
new SwingWrapper(chart).displayChart();
}
@Override
public Chart getChart() {
// Create Chart
Chart chart = new Chart(700, 500);
Chart chart = new Chart(800, 600);
// generates linear data
Collection<Date> xData = new ArrayList<Date>();
Collection<Number> yData = new ArrayList<Number>();
DateFormat sdf = new SimpleDateFormat("dd.MM.yyyy");
Date date = null;
for (int i = 1; i <= 10; i++) {
Date date = sdf.parse(i + ".10.2008");
try {
date = sdf.parse(i + ".10.2008");
} catch (ParseException e) {
e.printStackTrace();
}
xData.add(date);
yData.add(Math.random() * i);
}
......@@ -81,7 +95,7 @@ public class Example9 {
series.setMarker(SeriesMarker.CIRCLE);
series.setLineStyle(SeriesLineStyle.SOLID);
new SwingWrapper(chart).displayChart();
return chart;
}
}
/**
* Copyright (C) 2013 Xeiam LLC http://xeiam.com
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
* of the Software, and to permit persons to whom the Software is furnished to do
* so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* 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 OR COPYRIGHT HOLDERS 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.
*/
package com.xeiam.xchart.demo.charts;
import com.xeiam.xchart.Chart;
/**
* @author timmolter
*/
public interface ExampleChart {
public Chart getChart();
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment