Skip to content

Commit 38a31d5

Browse files
committed
Add support for simple horizontal bar chart
1 parent a9f75d4 commit 38a31d5

16 files changed

+1327
-32
lines changed

README.md

+23-11
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ repaint.
119119
* [x] Area charts
120120
* [x] Step Area charts
121121
* [x] Bar charts
122+
* [x] Horizontal bar charts
122123
* [x] Histogram charts
123124
* [x] Pie charts
124125
* [x] Donut charts
@@ -151,17 +152,18 @@ repaint.
151152

152153
Currently, there are 5 major chart types. Each type has its corresponding `ChartBuilder`, `Styler` and `Series`.
153154

154-
| Chart Type | Builder | Styler | Series | Allowed Data Types | Default Series Render Style |
155-
|---------------|----------------------|----------------|----------------|----------------------|-----------------------------|
156-
| XYChart | XYChartBuilder | XYStyler | XYSeries | Number, Date | Line |
157-
| CategoryChart | CategoryChartBuilder | CategoryStyler | CategorySeries | Number, Date, String | Bar |
158-
| PieChart | PieChartBuilder | PieStyler | PieSeries | String | Pie |
159-
| BubbleChart | BubbleChartBuilder | BubbleStyler | BubbleSeries | Number, Date | Round |
160-
| DialChart | DialChartBuilder | DialStyler | DialSeries | double | Round |
161-
| RadarChart | RadarChartBuilder | RadarStyler | RadarSeries | double[] | Round |
162-
| OHLCChart | OHLCChartBuilder | OHLCStyler | OHLCSeries | OHLC with Date | Candle |
163-
| BoxChart | BoxChartBuilder | BoxStyler | BoxSeries | Number, Date, String | Box |
164-
| HeatMapChart | HeatMapChartBuilder | HeatMapStyler | HeatMapSeries | Number, Date, String | -- |
155+
| Chart Type | Builder | Styler | Series | Allowed Data Types | Default Series Render Style |
156+
|--------------------|---------------------------|---------------------|---------------------|----------------------|-----------------------------|
157+
| XYChart | XYChartBuilder | XYStyler | XYSeries | Number, Date | Line |
158+
| CategoryChart | CategoryChartBuilder | CategoryStyler | CategorySeries | Number, Date, String | Bar |
159+
| PieChart | PieChartBuilder | PieStyler | PieSeries | String | Pie |
160+
| BubbleChart | BubbleChartBuilder | BubbleStyler | BubbleSeries | Number, Date | Round |
161+
| DialChart | DialChartBuilder | DialStyler | DialSeries | double | Round |
162+
| RadarChart | RadarChartBuilder | RadarStyler | RadarSeries | double[] | Round |
163+
| OHLCChart | OHLCChartBuilder | OHLCStyler | OHLCSeries | OHLC with Date | Candle |
164+
| BoxChart | BoxChartBuilder | BoxStyler | BoxSeries | Number, Date, String | Box |
165+
| HeatMapChart | HeatMapChartBuilder | HeatMapStyler | HeatMapSeries | Number, Date, String | -- |
166+
| HorizontalBarChart | HorizontalBarChartBuilder | HorizontalBarStyler | HorizontalBarSeries | Number, Date, String | Bar |
165167

166168
The different Stylers contain chart styling methods specific to the corresponding chart type as well as common styling methods common across all chart types.
167169

@@ -284,6 +286,16 @@ An example of a set of sequence numbers: 12, 15, 17, 19, 20, 23, 25, 28, 30, 33,
284286

285287
`HeatMapChart` take Date, Number or String data types for the X-Axis, Y-Axis.
286288

289+
### HorizontalBarChart
290+
291+
![](https://raw.githubusercontent.com/knowm/XChart/develop/etc/XChart_HorizontalBarChart.png)
292+
293+
`HorizontalBarChart` charts take Date, Number or String data types for the Y-Axis and Number data types for the X-Axis. For the Y-Axis, each category is given its own tick mark.
294+
295+
It supports `labels` and `tooltips`, but more advanced features like `error bars` or `stacking` are not yet implemented.
296+
297+
Series render style is `Bar`.
298+
287299
## Real-time Java Charts using XChart
288300

289301
![](https://raw.githubusercontent.com/knowm/XChart/develop/etc/XChart_SimpleRealtime.gif)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package org.knowm.xchart.demo.charts.horizontalbar;
2+
3+
import org.knowm.xchart.HorizontalBarChart;
4+
import org.knowm.xchart.HorizontalBarChartBuilder;
5+
import org.knowm.xchart.SwingWrapper;
6+
import org.knowm.xchart.demo.charts.ExampleChart;
7+
import org.knowm.xchart.style.Styler.LegendPosition;
8+
9+
import java.util.Arrays;
10+
11+
/**
12+
* Basic Horizontal Bar Chart
13+
*
14+
* <p>Demonstrates the following:
15+
*
16+
* <ul>
17+
* <li>Integer categories as List
18+
* <li>All positive values
19+
* <li>Single series
20+
* <li>Place legend at Inside-NW position
21+
* <li>Bar Chart Annotations
22+
*/
23+
public class HorizontalBarChart01 implements ExampleChart<HorizontalBarChart> {
24+
25+
public static void main(String[] args) {
26+
27+
ExampleChart<HorizontalBarChart> exampleChart = new HorizontalBarChart01();
28+
HorizontalBarChart chart = exampleChart.getChart();
29+
new SwingWrapper<>(chart).displayChart();
30+
}
31+
32+
@Override
33+
public HorizontalBarChart getChart() {
34+
35+
// Create Chart
36+
HorizontalBarChart chart =
37+
new HorizontalBarChartBuilder()
38+
.width(800)
39+
.height(600)
40+
.title(getClass().getSimpleName())
41+
.yAxisTitle("Score")
42+
.xAxisTitle("Number")
43+
.build();
44+
45+
// Customize Chart
46+
chart.getStyler().setLegendPosition(LegendPosition.InsideNW);
47+
chart.getStyler().setLabelsVisible(false);
48+
chart.getStyler().setPlotGridLinesVisible(false);
49+
50+
// Series
51+
chart.addSeries("test 1", Arrays.asList(4, 5, 9, 6, 5), Arrays.asList(0, 1, 2, 3, 4));
52+
53+
return chart;
54+
}
55+
56+
@Override
57+
public String getExampleChartName() {
58+
59+
return getClass().getSimpleName() + " - Basic Horizontal Bar Chart";
60+
}
61+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
package org.knowm.xchart.demo.charts.horizontalbar;
2+
3+
import org.knowm.xchart.HorizontalBarChart;
4+
import org.knowm.xchart.HorizontalBarChartBuilder;
5+
import org.knowm.xchart.HorizontalBarSeries;
6+
import org.knowm.xchart.SwingWrapper;
7+
import org.knowm.xchart.demo.charts.ExampleChart;
8+
import org.knowm.xchart.style.Styler.ChartTheme;
9+
10+
import java.awt.*;
11+
import java.text.DateFormat;
12+
import java.text.ParseException;
13+
import java.text.SimpleDateFormat;
14+
import java.util.ArrayList;
15+
import java.util.Date;
16+
import java.util.List;
17+
import java.util.Random;
18+
19+
/**
20+
* Date Categories
21+
*
22+
* <p>Demonstrates the following:
23+
*
24+
* <ul>
25+
* <li>Date categories as List
26+
* <li>All negative values
27+
* <li>Single series
28+
* <li>No horizontal plot gridlines
29+
* <li>Change series color
30+
* <li>MATLAB Theme
31+
*/
32+
public class HorizontalBarChart02 implements ExampleChart<HorizontalBarChart> {
33+
34+
public static void main(String[] args) {
35+
36+
ExampleChart<HorizontalBarChart> exampleChart = new HorizontalBarChart02();
37+
HorizontalBarChart chart = exampleChart.getChart();
38+
new SwingWrapper<>(chart).displayChart();
39+
}
40+
41+
@Override
42+
public HorizontalBarChart getChart() {
43+
44+
// Create Chart
45+
HorizontalBarChart chart =
46+
new HorizontalBarChartBuilder()
47+
.theme(ChartTheme.Matlab)
48+
.width(800)
49+
.height(600)
50+
.title(getClass().getSimpleName())
51+
.yAxisTitle("Year")
52+
.xAxisTitle("Units Sold")
53+
.build();
54+
55+
// Customize Chart
56+
chart.getStyler().setPlotGridLinesVisible(false);
57+
chart.getStyler().setDatePattern("yyyy");
58+
59+
// Series
60+
List<Date> yData = new ArrayList<Date>();
61+
List<Number> xData = new ArrayList<Number>();
62+
63+
Random random = new Random();
64+
DateFormat sdf = new SimpleDateFormat("yyyy");
65+
Date date = null;
66+
for (int i = 1; i <= 8; i++) {
67+
try {
68+
date = sdf.parse("" + (2000 + i));
69+
} catch (ParseException e) {
70+
e.printStackTrace();
71+
}
72+
yData.add(date);
73+
xData.add(-1 * 0.00000001 * ((random.nextInt(i) + 1)));
74+
}
75+
HorizontalBarSeries series = chart.addSeries("Model 77", xData, yData);
76+
series.setFillColor(new Color(230, 150, 150));
77+
78+
return chart;
79+
}
80+
81+
@Override
82+
public String getExampleChartName() {
83+
return getClass().getSimpleName() + " - Date Categories";
84+
}
85+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
package org.knowm.xchart.demo.charts.horizontalbar;
2+
3+
import org.knowm.xchart.HorizontalBarChart;
4+
import org.knowm.xchart.HorizontalBarChartBuilder;
5+
import org.knowm.xchart.SwingWrapper;
6+
import org.knowm.xchart.demo.charts.ExampleChart;
7+
import org.knowm.xchart.style.Styler.ChartTheme;
8+
9+
import java.util.ArrayList;
10+
import java.util.Arrays;
11+
12+
/**
13+
* GGPlot2 Theme Horizontal Bar chart
14+
*
15+
* <p>Demonstrates the following:
16+
*
17+
* <ul>
18+
* <li>String categories
19+
* <li>Positive and negative values
20+
* <li>Multiple series
21+
*/
22+
public class HorizontalBarChart03 implements ExampleChart<HorizontalBarChart> {
23+
24+
public static void main(String[] args) {
25+
26+
ExampleChart<HorizontalBarChart> exampleChart = new HorizontalBarChart03();
27+
HorizontalBarChart chart = exampleChart.getChart();
28+
new SwingWrapper<>(chart).displayChart();
29+
}
30+
31+
@Override
32+
public HorizontalBarChart getChart() {
33+
34+
// Create Chart
35+
HorizontalBarChart chart =
36+
new HorizontalBarChartBuilder()
37+
.width(800)
38+
.height(600)
39+
.title(getClass().getSimpleName())
40+
.yAxisTitle("Color")
41+
.xAxisTitle("Temperature")
42+
.theme(ChartTheme.GGPlot2)
43+
.build();
44+
45+
// Customize Chart
46+
chart.getStyler().setPlotGridVerticalLinesVisible(false);
47+
48+
// Series
49+
chart.addSeries(
50+
"fish",
51+
new ArrayList<Number>(Arrays.asList(new Number[]{-40, 30, 20, 60, 60})),
52+
new ArrayList<>(Arrays.asList(new String[]{"Blue", "Red", "Green", "Yellow", "Orange"})));
53+
chart.addSeries(
54+
"worms",
55+
new ArrayList<Number>(Arrays.asList(new Number[]{50, 10, -20, 40, 60})),
56+
new ArrayList<>(Arrays.asList(new String[]{"Blue", "Red", "Green", "Yellow", "Orange"})));
57+
chart.addSeries(
58+
"birds",
59+
new ArrayList<Number>(Arrays.asList(new Number[]{13, 22, -23, -34, 37})),
60+
new ArrayList<>(Arrays.asList(new String[]{"Blue", "Red", "Green", "Yellow", "Orange"})));
61+
chart.addSeries(
62+
"ants",
63+
new ArrayList<Number>(Arrays.asList(new Number[]{50, 57, -14, -20, 31})),
64+
new ArrayList<>(Arrays.asList(new String[]{"Blue", "Red", "Green", "Yellow", "Orange"})));
65+
chart.addSeries(
66+
"slugs",
67+
new ArrayList<Number>(Arrays.asList(new Number[]{-2, 29, 49, -16, -43})),
68+
new ArrayList<>(Arrays.asList(new String[]{"Blue", "Red", "Green", "Yellow", "Orange"})));
69+
70+
return chart;
71+
}
72+
73+
@Override
74+
public String getExampleChartName() {
75+
76+
return getClass().getSimpleName() + " - GGPlot2 Theme";
77+
}
78+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package org.knowm.xchart.demo.charts.horizontalbar;
2+
3+
import org.knowm.xchart.HorizontalBarChart;
4+
import org.knowm.xchart.HorizontalBarChartBuilder;
5+
import org.knowm.xchart.SwingWrapper;
6+
import org.knowm.xchart.demo.charts.ExampleChart;
7+
import org.knowm.xchart.style.Styler;
8+
9+
import java.util.Arrays;
10+
11+
/**
12+
* Multiple series, labels and tooltips
13+
*
14+
* <p>Demonstrates the following:
15+
*
16+
* <ul>
17+
* <li>Number categories
18+
* <li>Positive values
19+
* <li>Multiple series
20+
* <li>Missing point in series
21+
* <li>Labels
22+
* <li>Bar Chart Annotations
23+
* <li>Horizontal Legend OutsideS
24+
*/
25+
public class HorizontalBarChart04 implements ExampleChart<HorizontalBarChart> {
26+
27+
public static void main(String[] args) {
28+
29+
ExampleChart<HorizontalBarChart> exampleChart = new HorizontalBarChart04();
30+
HorizontalBarChart chart = exampleChart.getChart();
31+
new SwingWrapper<>(chart).displayChart();
32+
}
33+
34+
@Override
35+
public HorizontalBarChart getChart() {
36+
37+
// Create Chart
38+
HorizontalBarChart chart =
39+
new HorizontalBarChartBuilder()
40+
.width(800)
41+
.height(600)
42+
.title(getClass().getSimpleName())
43+
.yAxisTitle("Age")
44+
.xAxisTitle("XFactor")
45+
.build();
46+
47+
// Customize Chart
48+
chart.getStyler().setLabelsVisible(true);
49+
chart.getStyler().setToolTipsEnabled(true);
50+
chart.getStyler().setPlotGridVerticalLinesVisible(false);
51+
chart.getStyler().setLegendPosition(Styler.LegendPosition.OutsideS);
52+
chart.getStyler().setLegendLayout(Styler.LegendLayout.Horizontal);
53+
54+
// Series
55+
chart.addSeries("female", Arrays.asList(50, 10, 20, 40, 35), Arrays.asList(10, 20, 30, 40, 50));
56+
chart.addSeries("male", Arrays.asList(40, 30, 20, null, 60), Arrays.asList(10, 20, 30, 40, 50));
57+
58+
return chart;
59+
}
60+
61+
@Override
62+
public String getExampleChartName() {
63+
64+
return getClass().getSimpleName() + " - Multiple series, labels and tooltips";
65+
}
66+
}

0 commit comments

Comments
 (0)