-
Notifications
You must be signed in to change notification settings - Fork 397
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for simple horizontal bar chart
- Loading branch information
Showing
16 changed files
with
1,339 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
...t-demo/src/main/java/org/knowm/xchart/demo/charts/horizontalbar/HorizontalBarChart01.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package org.knowm.xchart.demo.charts.horizontalbar; | ||
|
||
import org.knowm.xchart.HorizontalBarChart; | ||
import org.knowm.xchart.HorizontalBarChartBuilder; | ||
import org.knowm.xchart.SwingWrapper; | ||
import org.knowm.xchart.demo.charts.ExampleChart; | ||
import org.knowm.xchart.style.Styler.LegendPosition; | ||
|
||
import java.util.Arrays; | ||
|
||
/** | ||
* Basic Horizontal Bar Chart | ||
* | ||
* <p>Demonstrates the following: | ||
* | ||
* <ul> | ||
* <li>Integer categories as List | ||
* <li>All positive values | ||
* <li>Single series | ||
* <li>Place legend at Inside-NW position | ||
* <li>Bar Chart Annotations | ||
*/ | ||
public class HorizontalBarChart01 implements ExampleChart<HorizontalBarChart> { | ||
|
||
public static void main(String[] args) { | ||
|
||
ExampleChart<HorizontalBarChart> exampleChart = new HorizontalBarChart01(); | ||
HorizontalBarChart chart = exampleChart.getChart(); | ||
new SwingWrapper<>(chart).displayChart(); | ||
} | ||
|
||
@Override | ||
public HorizontalBarChart getChart() { | ||
|
||
// Create Chart | ||
HorizontalBarChart chart = | ||
new HorizontalBarChartBuilder() | ||
.width(800) | ||
.height(600) | ||
.title(getClass().getSimpleName()) | ||
.yAxisTitle("Score") | ||
.xAxisTitle("Number") | ||
.build(); | ||
|
||
// Customize Chart | ||
chart.getStyler().setLegendPosition(LegendPosition.InsideNW); | ||
chart.getStyler().setLabelsVisible(false); | ||
chart.getStyler().setPlotGridLinesVisible(false); | ||
|
||
// Series | ||
chart.addSeries("test 1", Arrays.asList(4, 5, 9, 6, 5), Arrays.asList(0, 1, 2, 3, 4)); | ||
|
||
return chart; | ||
} | ||
|
||
@Override | ||
public String getExampleChartName() { | ||
|
||
return getClass().getSimpleName() + " - Basic Horizontal Bar Chart"; | ||
} | ||
} |
85 changes: 85 additions & 0 deletions
85
...t-demo/src/main/java/org/knowm/xchart/demo/charts/horizontalbar/HorizontalBarChart02.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
package org.knowm.xchart.demo.charts.horizontalbar; | ||
|
||
import org.knowm.xchart.HorizontalBarChart; | ||
import org.knowm.xchart.HorizontalBarChartBuilder; | ||
import org.knowm.xchart.HorizontalBarSeries; | ||
import org.knowm.xchart.SwingWrapper; | ||
import org.knowm.xchart.demo.charts.ExampleChart; | ||
import org.knowm.xchart.style.Styler.ChartTheme; | ||
|
||
import java.awt.*; | ||
import java.text.DateFormat; | ||
import java.text.ParseException; | ||
import java.text.SimpleDateFormat; | ||
import java.util.ArrayList; | ||
import java.util.Date; | ||
import java.util.List; | ||
import java.util.Random; | ||
|
||
/** | ||
* Date Categories | ||
* | ||
* <p>Demonstrates the following: | ||
* | ||
* <ul> | ||
* <li>Date categories as List | ||
* <li>All negative values | ||
* <li>Single series | ||
* <li>No horizontal plot gridlines | ||
* <li>Change series color | ||
* <li>MATLAB Theme | ||
*/ | ||
public class HorizontalBarChart02 implements ExampleChart<HorizontalBarChart> { | ||
|
||
public static void main(String[] args) { | ||
|
||
ExampleChart<HorizontalBarChart> exampleChart = new HorizontalBarChart02(); | ||
HorizontalBarChart chart = exampleChart.getChart(); | ||
new SwingWrapper<>(chart).displayChart(); | ||
} | ||
|
||
@Override | ||
public HorizontalBarChart getChart() { | ||
|
||
// Create Chart | ||
HorizontalBarChart chart = | ||
new HorizontalBarChartBuilder() | ||
.theme(ChartTheme.Matlab) | ||
.width(800) | ||
.height(600) | ||
.title(getClass().getSimpleName()) | ||
.yAxisTitle("Year") | ||
.xAxisTitle("Units Sold") | ||
.build(); | ||
|
||
// Customize Chart | ||
chart.getStyler().setPlotGridLinesVisible(false); | ||
chart.getStyler().setDatePattern("yyyy"); | ||
|
||
// Series | ||
List<Date> yData = new ArrayList<Date>(); | ||
List<Number> xData = new ArrayList<Number>(); | ||
|
||
Random random = new Random(); | ||
DateFormat sdf = new SimpleDateFormat("yyyy"); | ||
Date date = null; | ||
for (int i = 1; i <= 8; i++) { | ||
try { | ||
date = sdf.parse("" + (2000 + i)); | ||
} catch (ParseException e) { | ||
e.printStackTrace(); | ||
} | ||
yData.add(date); | ||
xData.add(-1 * 0.00000001 * ((random.nextInt(i) + 1))); | ||
} | ||
HorizontalBarSeries series = chart.addSeries("Model 77", xData, yData); | ||
series.setFillColor(new Color(230, 150, 150)); | ||
|
||
return chart; | ||
} | ||
|
||
@Override | ||
public String getExampleChartName() { | ||
return getClass().getSimpleName() + " - Date Categories"; | ||
} | ||
} |
78 changes: 78 additions & 0 deletions
78
...t-demo/src/main/java/org/knowm/xchart/demo/charts/horizontalbar/HorizontalBarChart03.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package org.knowm.xchart.demo.charts.horizontalbar; | ||
|
||
import org.knowm.xchart.HorizontalBarChart; | ||
import org.knowm.xchart.HorizontalBarChartBuilder; | ||
import org.knowm.xchart.SwingWrapper; | ||
import org.knowm.xchart.demo.charts.ExampleChart; | ||
import org.knowm.xchart.style.Styler.ChartTheme; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
|
||
/** | ||
* GGPlot2 Theme Horizontal Bar chart | ||
* | ||
* <p>Demonstrates the following: | ||
* | ||
* <ul> | ||
* <li>String categories | ||
* <li>Positive and negative values | ||
* <li>Multiple series | ||
*/ | ||
public class HorizontalBarChart03 implements ExampleChart<HorizontalBarChart> { | ||
|
||
public static void main(String[] args) { | ||
|
||
ExampleChart<HorizontalBarChart> exampleChart = new HorizontalBarChart03(); | ||
HorizontalBarChart chart = exampleChart.getChart(); | ||
new SwingWrapper<>(chart).displayChart(); | ||
} | ||
|
||
@Override | ||
public HorizontalBarChart getChart() { | ||
|
||
// Create Chart | ||
HorizontalBarChart chart = | ||
new HorizontalBarChartBuilder() | ||
.width(800) | ||
.height(600) | ||
.title(getClass().getSimpleName()) | ||
.yAxisTitle("Color") | ||
.xAxisTitle("Temperature") | ||
.theme(ChartTheme.GGPlot2) | ||
.build(); | ||
|
||
// Customize Chart | ||
chart.getStyler().setPlotGridVerticalLinesVisible(false); | ||
|
||
// Series | ||
chart.addSeries( | ||
"fish", | ||
new ArrayList<Number>(Arrays.asList(new Number[]{-40, 30, 20, 60, 60})), | ||
new ArrayList<>(Arrays.asList(new String[]{"Blue", "Red", "Green", "Yellow", "Orange"}))); | ||
chart.addSeries( | ||
"worms", | ||
new ArrayList<Number>(Arrays.asList(new Number[]{50, 10, -20, 40, 60})), | ||
new ArrayList<>(Arrays.asList(new String[]{"Blue", "Red", "Green", "Yellow", "Orange"}))); | ||
chart.addSeries( | ||
"birds", | ||
new ArrayList<Number>(Arrays.asList(new Number[]{13, 22, -23, -34, 37})), | ||
new ArrayList<>(Arrays.asList(new String[]{"Blue", "Red", "Green", "Yellow", "Orange"}))); | ||
chart.addSeries( | ||
"ants", | ||
new ArrayList<Number>(Arrays.asList(new Number[]{50, 57, -14, -20, 31})), | ||
new ArrayList<>(Arrays.asList(new String[]{"Blue", "Red", "Green", "Yellow", "Orange"}))); | ||
chart.addSeries( | ||
"slugs", | ||
new ArrayList<Number>(Arrays.asList(new Number[]{-2, 29, 49, -16, -43})), | ||
new ArrayList<>(Arrays.asList(new String[]{"Blue", "Red", "Green", "Yellow", "Orange"}))); | ||
|
||
return chart; | ||
} | ||
|
||
@Override | ||
public String getExampleChartName() { | ||
|
||
return getClass().getSimpleName() + " - GGPlot2 Theme"; | ||
} | ||
} |
66 changes: 66 additions & 0 deletions
66
...t-demo/src/main/java/org/knowm/xchart/demo/charts/horizontalbar/HorizontalBarChart04.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package org.knowm.xchart.demo.charts.horizontalbar; | ||
|
||
import org.knowm.xchart.HorizontalBarChart; | ||
import org.knowm.xchart.HorizontalBarChartBuilder; | ||
import org.knowm.xchart.SwingWrapper; | ||
import org.knowm.xchart.demo.charts.ExampleChart; | ||
import org.knowm.xchart.style.Styler; | ||
|
||
import java.util.Arrays; | ||
|
||
/** | ||
* Multiple series, labels and tooltips | ||
* | ||
* <p>Demonstrates the following: | ||
* | ||
* <ul> | ||
* <li>Number categories | ||
* <li>Positive values | ||
* <li>Multiple series | ||
* <li>Missing point in series | ||
* <li>Labels | ||
* <li>Bar Chart Annotations | ||
* <li>Horizontal Legend OutsideS | ||
*/ | ||
public class HorizontalBarChart04 implements ExampleChart<HorizontalBarChart> { | ||
|
||
public static void main(String[] args) { | ||
|
||
ExampleChart<HorizontalBarChart> exampleChart = new HorizontalBarChart04(); | ||
HorizontalBarChart chart = exampleChart.getChart(); | ||
new SwingWrapper<>(chart).displayChart(); | ||
} | ||
|
||
@Override | ||
public HorizontalBarChart getChart() { | ||
|
||
// Create Chart | ||
HorizontalBarChart chart = | ||
new HorizontalBarChartBuilder() | ||
.width(800) | ||
.height(600) | ||
.title(getClass().getSimpleName()) | ||
.yAxisTitle("Age") | ||
.xAxisTitle("XFactor") | ||
.build(); | ||
|
||
// Customize Chart | ||
chart.getStyler().setLabelsVisible(true); | ||
chart.getStyler().setToolTipsEnabled(true); | ||
chart.getStyler().setPlotGridVerticalLinesVisible(false); | ||
chart.getStyler().setLegendPosition(Styler.LegendPosition.OutsideS); | ||
chart.getStyler().setLegendLayout(Styler.LegendLayout.Horizontal); | ||
|
||
// Series | ||
chart.addSeries("female", Arrays.asList(50, 10, 20, 40, 35), Arrays.asList(10, 20, 30, 40, 50)); | ||
chart.addSeries("male", Arrays.asList(40, 30, 20, null, 60), Arrays.asList(10, 20, 30, 40, 50)); | ||
|
||
return chart; | ||
} | ||
|
||
@Override | ||
public String getExampleChartName() { | ||
|
||
return getClass().getSimpleName() + " - Multiple series, labels and tooltips"; | ||
} | ||
} |
Oops, something went wrong.