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

refactor example code

parent f6fc874e
Branches
No related tags found
No related merge requests found
......@@ -19,9 +19,11 @@ import com.xeiam.xcharts.BitmapEncoder;
import com.xeiam.xcharts.Chart;
/**
* Creates a simple charts and saves it as aPNG image file.
*
* @author timmolter
*/
public class BitmapChart {
public class Example1 {
private static final double[] xData = { 0.0, 1.0, 2.0 };
private static final double[] yData = { 0.0, 1.0, 2.0 };
......@@ -36,7 +38,7 @@ public class BitmapChart {
chart.addSeries("y(x)", xData, yData);
try {
BitmapEncoder.savePNG(chart, "/test/Chart_Small.png");
BitmapEncoder.savePNG(chart, "/test/Sample_Chart.png");
} catch (Exception e) {
e.printStackTrace();
}
......
......@@ -23,14 +23,16 @@ import com.xeiam.xcharts.series.SeriesLineStyle;
import com.xeiam.xcharts.series.SeriesMarker;
/**
* Embed a Chart in a simple Swing application
*
* @author timmolter
*/
public class SwingChart {
public class Example2 {
private static void createAndShowGUI() {
public static void main(String[] args) {
// generates sine data
int size = 100;
int size = 30;
double[] xData1 = new double[size + 1];
double[] yData1 = new double[size + 1];
for (int i = 0; i <= size; i++) {
......@@ -39,55 +41,23 @@ public class SwingChart {
yData1[i] = size * Math.sin(radians);
}
// generates linear data
int size2 = 100;
double[] xData2 = new double[size2 + 1];
double[] yData2 = new double[size2 + 1];
for (int i = 0; i <= size2; i++) {
xData2[i] = -size2 + 2 * i;
yData2[i] = -size2 + 2 * i;
}
// Create and set up the window.
// Create Chart
Chart chart = new Chart(800, 600);
Chart chart = new Chart(440, 300);
// Customize Chart
chart.setChartTitle("Sample Chart");
chart.setXAxisTitle("X");
chart.setYAxisTitle("Y");
// chart.setChartTitleVisible(false);
// chart.setChartLegendVisible(false);
// chart.setAxisTitlesVisible(false);
chart.setChartTitleVisible(false);
chart.setChartLegendVisible(false);
chart.setAxisTitlesVisible(false);
// Series 1
Series series1 = chart.addSeries("y=sin(x)", xData1, yData1);
series1.setLineColor(SeriesColor.PURPLE);
series1.setLineStyle(SeriesLineStyle.NONE);
series1.setLineStyle(SeriesLineStyle.DASH_DASH);
series1.setMarkerColor(SeriesColor.GREEN);
series1.setMarker(SeriesMarker.NONE);
// Series 2
// Series series2 = chart.addSeries("y=x", xData2, yData2);
// series2.setLineColor(SeriesColor.PURPLE);
// series2.setLineStyle(SeriesLineStyle.NONE);
// series2.setMarkerColor(SeriesColor.GREEN);
// series2.setMarker(SeriesMarker.NONE);
series1.setMarker(SeriesMarker.SQUARE);
SwingHelper swingHelper = new SwingHelper(chart);
swingHelper.displayChart();
}
public static void main(String[] args) {
// Schedule a job for the event-dispatching thread:
// creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
createAndShowGUI();
}
});
}
}
......@@ -24,7 +24,7 @@ import com.xeiam.xcharts.series.Series;
*/
public class SwingChart2 {
private static void createAndShowGUI() {
public static void main(String[] args) {
// Create Chart
Chart chart = new Chart(800, 600);
......@@ -59,15 +59,4 @@ public class SwingChart2 {
swingHelper.displayChart();
}
public static void main(String[] args) {
// Schedule a job for the event-dispatching thread:
// creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
createAndShowGUI();
}
});
}
}
......@@ -26,7 +26,7 @@ import com.xeiam.xcharts.series.Series;
*/
public class SwingChart3 {
private static void createAndShowGUI() {
public static void main(String[] args) {
// generates linear data
double[] xData1 = new double[] { 0.0, 1.0, 2.0 };
......@@ -53,9 +53,4 @@ public class SwingChart3 {
swingHelper.displayChart();
}
public static void main(String[] args) {
createAndShowGUI();
}
}
......@@ -27,7 +27,7 @@ import com.xeiam.xcharts.series.SeriesMarker;
*/
public class SwingChart4 {
private static void createAndShowGUI() {
public static void main(String[] args) {
// generates linear data
double[] yData1 = new double[] { 0.0, 0.0, 0.0, -10.0, 15.0, 15.0 };
......@@ -52,9 +52,4 @@ public class SwingChart4 {
}
public static void main(String[] args) {
createAndShowGUI();
}
}
......@@ -15,11 +15,9 @@
*/
package com.xeiam.examples;
import com.xeiam.swing.QuickXChart;
import com.xeiam.swing.QuickChart;
/**
* Demonstrated/Tests plotting horizontal and vertical lines
*
* @author timmolter
*/
public class SwingChart5 {
......@@ -40,7 +38,7 @@ public class SwingChart5 {
int numRows = 2;
int numCols = 2;
QuickXChart quickChart = new QuickXChart(2, 2);
QuickChart quickChart = new QuickChart(2, 2);
for (int i = 0; i < numRows; i++) {
for (int j = 0; j < numCols; j++) {
......
......@@ -25,7 +25,7 @@ import com.xeiam.xcharts.JChartPanel;
import com.xeiam.xcharts.series.Series;
import com.xeiam.xcharts.series.SeriesMarker;
public class QuickXChart {
public class QuickChart {
Chart[] charts;
int numRows;
......@@ -42,7 +42,7 @@ public class QuickXChart {
boolean chartLegendVisible = true;
boolean axisTitlesVisible = true;
public QuickXChart(int numRows, int numCols) {
public QuickChart(int numRows, int numCols) {
charts = new Chart[numRows * numCols];
this.numRows = numRows;
this.numCols = numCols;
......
......@@ -22,6 +22,9 @@ import javax.swing.JPanel;
import com.xeiam.xcharts.Chart;
import com.xeiam.xcharts.JChartPanel;
/**
* @author timmolter
*/
public class SwingHelper {
Chart[] charts;
......
......@@ -29,6 +29,8 @@ import javax.servlet.ServletOutputStream;
public class BitmapEncoder {
/**
* Saves a chart as a PNG file
*
* @param chart
* @param pFileName
*/
......@@ -45,6 +47,8 @@ public class BitmapEncoder {
}
/**
* Streams a chart as a PNG file
*
* @param out
* @param chart
*/
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment