Skip to content

Commit

Permalink
Add OHLC, Step and StepArea chart types
Browse files Browse the repository at this point in the history
  • Loading branch information
timmolter committed Aug 30, 2017
1 parent 2c39376 commit d2a26b7
Show file tree
Hide file tree
Showing 55 changed files with 1,946 additions and 286 deletions.
14 changes: 13 additions & 1 deletion README.md
Expand Up @@ -111,8 +111,10 @@ To make it real-time, simply call `updateXYSeries` on the `XYChart` instance to
* [x] ~182KB Jar
* [x] Multiple Y-Axis charts
* [x] Line charts
* [x] Step charts
* [x] Scatter charts
* [x] Area charts
* [x] Step Area charts
* [x] Bar charts
* [x] Histogram charts
* [x] Pie charts
Expand All @@ -121,6 +123,7 @@ To make it real-time, simply call `updateXYSeries` on the `XYChart` instance to
* [x] Stick charts
* [x] Dial charts
* [x] Radar charts
* [x] OHLC charts
* [x] Error bars
* [x] Logarithmic axes
* [x] Number, Date, Bubble and Category X-Axis
Expand Down Expand Up @@ -150,6 +153,7 @@ Currently, there are 5 major chart types. Each type has its corresponding `Chart
| BubbleChart | BubbleChartBuilder | BubbleStyler | BubbleSeries | Number, Date | Round |
| DialChart | DialChartBuilder | DialStyler | DialSeries | double | Round |
| RadarChart | RadarChartBuilder | RadarStyler | RadarSeries | double[] | Round |
| OHLCChart | OHLCChartBuilder | OHLCStyler | OHLCSeries | OHLC with Date | Candle |

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

Expand All @@ -159,7 +163,7 @@ The different Stylers contain chart styling methods specific to the correspondin

`XYChart` charts take Date or Number data types for the X-Axis and Number data types for the Y-Axis. For both axes, the tick marks are auto generated to span the range and domain of the data in evenly-spaced intervals.

Series render styles include: `Line`, `Scatter` and `Area`.
Series render styles include: `Line`, `Scatter`, `Area`, `Step` and `StepArea`.

### CategoryChart

Expand Down Expand Up @@ -199,6 +203,14 @@ Series render styles include: `Round` and in the near future `Square`.

Series render styles include: `Polygon` and `Circle`.

### OHLCChart

![](https://raw.githubusercontent.com/timmolter/XChart/develop/etc/XChart_Candle.png)

`OHLCChart` charts take Date data types for the X-Axis and 4 Number data types for the Y-Axis. For both axes, the tick marks are auto generated to span the range and domain of the data in evenly-spaced intervals.

Series render styles include: `Candle`, `HiLo`.

## Real-time Java Charts using XChart

![](https://raw.githubusercontent.com/timmolter/XChart/develop/etc/XChart_SimpleRealtime.gif)
Expand Down
Binary file added etc/XChart_Candle.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
55 changes: 43 additions & 12 deletions xchart-demo/src/main/java/org/knowm/xchart/demo/XChartDemo.java
Expand Up @@ -35,6 +35,7 @@
import org.knowm.xchart.XChartPanel;
import org.knowm.xchart.demo.charts.area.AreaChart01;
import org.knowm.xchart.demo.charts.area.AreaChart02;
import org.knowm.xchart.demo.charts.area.AreaChart03;
import org.knowm.xchart.demo.charts.area.AreaLineChart03;
import org.knowm.xchart.demo.charts.bar.BarChart01;
import org.knowm.xchart.demo.charts.bar.BarChart02;
Expand All @@ -58,24 +59,17 @@
import org.knowm.xchart.demo.charts.date.DateChart07;
import org.knowm.xchart.demo.charts.date.DateChart08;
import org.knowm.xchart.demo.charts.dial.DialChart01;
import org.knowm.xchart.demo.charts.line.LineChart01;
import org.knowm.xchart.demo.charts.line.LineChart02;
import org.knowm.xchart.demo.charts.line.LineChart03;
import org.knowm.xchart.demo.charts.line.LineChart04;
import org.knowm.xchart.demo.charts.line.LineChart05;
import org.knowm.xchart.demo.charts.line.LineChart06;
import org.knowm.xchart.demo.charts.line.LineChart07;
import org.knowm.xchart.demo.charts.line.*;
import org.knowm.xchart.demo.charts.ohlc.OHLCChart01;
import org.knowm.xchart.demo.charts.ohlc.OHLCChart02;
import org.knowm.xchart.demo.charts.ohlc.OHLCChart03;
import org.knowm.xchart.demo.charts.pie.PieChart01;
import org.knowm.xchart.demo.charts.pie.PieChart02;
import org.knowm.xchart.demo.charts.pie.PieChart03;
import org.knowm.xchart.demo.charts.pie.PieChart04;
import org.knowm.xchart.demo.charts.pie.PieChart05;
import org.knowm.xchart.demo.charts.radar.RadarChart01;
import org.knowm.xchart.demo.charts.realtime.RealtimeChart01;
import org.knowm.xchart.demo.charts.realtime.RealtimeChart02;
import org.knowm.xchart.demo.charts.realtime.RealtimeChart03;
import org.knowm.xchart.demo.charts.realtime.RealtimeChart04;
import org.knowm.xchart.demo.charts.realtime.RealtimeChart05;
import org.knowm.xchart.demo.charts.realtime.*;
import org.knowm.xchart.demo.charts.scatter.ScatterChart01;
import org.knowm.xchart.demo.charts.scatter.ScatterChart02;
import org.knowm.xchart.demo.charts.scatter.ScatterChart03;
Expand Down Expand Up @@ -116,6 +110,7 @@ public class XChartDemo extends JPanel implements TreeSelectionListener {
final RealtimeChart03 realtimeChart03 = new RealtimeChart03();
final RealtimeChart04 realtimeChart04 = new RealtimeChart04();
final RealtimeChart05 realtimeChart05 = new RealtimeChart05();
final RealtimeChart06 realtimeChart06 = new RealtimeChart06();
Timer timer = new Timer();

/**
Expand Down Expand Up @@ -248,6 +243,20 @@ public void run() {
};
timer = new Timer();
timer.scheduleAtFixedRate(chartUpdaterTask, 0, 500);
} else if (chartInfo.getExampleChartName().startsWith("RealtimeChart06")) {
// set up real-time chart simulated data feed
TimerTask chartUpdaterTask = new TimerTask() {

@Override
public void run() {

realtimeChart06.updateData();
chartPanel.revalidate();
chartPanel.repaint();
}
};
timer = new Timer();
timer.scheduleAtFixedRate(chartUpdaterTask, 0, 500);
}
}
}
Expand Down Expand Up @@ -277,6 +286,9 @@ private void createNodes(DefaultMutableTreeNode top) {
defaultMutableTreeNode = new DefaultMutableTreeNode(new ChartInfo("AreaLineChart03 - Combination Area & Line Chart", new AreaLineChart03().getChart()));
category.add(defaultMutableTreeNode);

defaultMutableTreeNode = new DefaultMutableTreeNode(new ChartInfo("AreaChart03 - Step area rendering", new AreaChart03().getChart()));
category.add(defaultMutableTreeNode);

// Pie
category = new DefaultMutableTreeNode("Pie Charts");
top.add(category);
Expand Down Expand Up @@ -321,6 +333,9 @@ private void createNodes(DefaultMutableTreeNode top) {
defaultMutableTreeNode = new DefaultMutableTreeNode(new ChartInfo("LineChart07 - Category Chart with Line Rendering", new LineChart07().getChart()));
category.add(defaultMutableTreeNode);

defaultMutableTreeNode = new DefaultMutableTreeNode(new ChartInfo("LineChart08 - Step rendering", new LineChart08().getChart()));
category.add(defaultMutableTreeNode);

// Scatter
category = new DefaultMutableTreeNode("Scatter Charts");
top.add(category);
Expand Down Expand Up @@ -405,6 +420,19 @@ private void createNodes(DefaultMutableTreeNode top) {
defaultMutableTreeNode = new DefaultMutableTreeNode(new ChartInfo("BubbleChart01 - Basic Bubble Chart", new BubbleChart01().getChart()));
category.add(defaultMutableTreeNode);

// OHLC
category = new DefaultMutableTreeNode("OHLC Charts");
top.add(category);

defaultMutableTreeNode = new DefaultMutableTreeNode(new ChartInfo("OHLCChart01 - HiLo rendering", new OHLCChart01().getChart()));
category.add(defaultMutableTreeNode);

defaultMutableTreeNode = new DefaultMutableTreeNode(new ChartInfo("OHLCChart02 - Candle rendering", new OHLCChart02().getChart()));
category.add(defaultMutableTreeNode);

defaultMutableTreeNode = new DefaultMutableTreeNode(new ChartInfo("OHLCChart03 - Candle with custom colors", new OHLCChart03().getChart()));
category.add(defaultMutableTreeNode);

// Theme
category = new DefaultMutableTreeNode("Chart Themes");
top.add(category);
Expand Down Expand Up @@ -467,6 +495,9 @@ private void createNodes(DefaultMutableTreeNode top) {

defaultMutableTreeNode = new DefaultMutableTreeNode(new ChartInfo("RealtimeChart05 - Real-time Category Chart", realtimeChart05.getChart()));
category.add(defaultMutableTreeNode);

defaultMutableTreeNode = new DefaultMutableTreeNode(new ChartInfo("RealtimeChart06 - Real-time OHLC Chart", realtimeChart06.getChart()));
category.add(defaultMutableTreeNode);
}

/**
Expand Down
@@ -0,0 +1,62 @@
/**
* Copyright 2015-2017 Knowm Inc. (http://knowm.org) and contributors.
* Copyright 2011-2015 Xeiam LLC (http://xeiam.com) and contributors.
*
* 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 org.knowm.xchart.demo.charts.area;

import org.knowm.xchart.SwingWrapper;
import org.knowm.xchart.XYChart;
import org.knowm.xchart.XYChartBuilder;
import org.knowm.xchart.XYSeries.XYSeriesRenderStyle;
import org.knowm.xchart.demo.charts.ExampleChart;
import org.knowm.xchart.style.Styler.LegendPosition;

/**
* Area Chart with 3 series
* <p>
* Demonstrates the following:
* <ul>
* <li>Step Area Chart
* <li>Place legend at Inside-NE position
* <li>ChartBuilder
*/
public class AreaChart03 implements ExampleChart<XYChart> {

public static void main(String[] args) {

ExampleChart<XYChart> exampleChart = new AreaChart03();
XYChart chart = exampleChart.getChart();
new SwingWrapper<XYChart>(chart).displayChart();
}

@Override
public XYChart getChart() {

// Create Chart
XYChart chart = new XYChartBuilder().width(800).height(600).title(getClass().getSimpleName()).xAxisTitle("X").yAxisTitle("Y").build();

// Customize Chart
chart.getStyler().setLegendPosition(LegendPosition.InsideNE);
chart.getStyler().setAxisTitlesVisible(false);
chart.getStyler().setDefaultSeriesRenderStyle(XYSeriesRenderStyle.StepArea);

// Series
chart.addSeries("a", new double[]{0, 3, 5, 7, 9}, new double[]{-3, 5, 9, 6, 5});
chart.addSeries("b", new double[]{0, 2, 4, 6, 9}, new double[]{-1, 6, 4, 0, 4});
chart.addSeries("c", new double[]{0, 1, 3, 8, 9}, new double[]{-2, -1, 1, 0, 1});

return chart;
}
}
@@ -0,0 +1,78 @@
/**
* Copyright 2015-2017 Knowm Inc. (http://knowm.org) and contributors.
* Copyright 2011-2015 Xeiam LLC (http://xeiam.com) and contributors.
*
* 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 org.knowm.xchart.demo.charts.line;

import org.knowm.xchart.SwingWrapper;
import org.knowm.xchart.XYChart;
import org.knowm.xchart.XYChartBuilder;
import org.knowm.xchart.XYSeries;
import org.knowm.xchart.demo.charts.ExampleChart;
import org.knowm.xchart.style.Styler.LegendPosition;

import java.util.ArrayList;
import java.util.List;

/**
* Logarithmic Y-Axis
* <p>
* Demonstrates the following:
* <ul>
* <li>Step renderer
* <li>Logarithmic Y-Axis
* <li>Building a Chart with ChartBuilder
* <li>Place legend at Inside-NW position
*/
public class LineChart08 implements ExampleChart<XYChart> {

public static void main(String[] args) {

ExampleChart<XYChart> exampleChart = new LineChart08();
XYChart chart = exampleChart.getChart();
new SwingWrapper<XYChart>(chart).displayChart();
}

@Override
public XYChart getChart() {

// generates Log data
List<Integer> xData = new ArrayList<Integer>();
List<Double> yData = new ArrayList<Double>();
for (int i = -3; i <= 3; i++) {
xData.add(i);
yData.add(Math.pow(10, i));
}

// Create Chart
XYChart chart = new XYChartBuilder().width(800).height(600).title("Powers of Ten").xAxisTitle("Power").yAxisTitle("Value").build();

// Customize Chart
chart.getStyler().setChartTitleVisible(true);
chart.getStyler().setLegendPosition(LegendPosition.InsideNW);
chart.getStyler().setYAxisLogarithmic(true);
chart.getStyler().setXAxisLabelRotation(45);
chart.getStyler().setDefaultSeriesRenderStyle(XYSeries.XYSeriesRenderStyle.Step);

// chart.getStyler().setXAxisLabelAlignment(TextAlignment.Right);
// chart.getStyler().setXAxisLabelRotation(90);
// chart.getStyler().setXAxisLabelRotation(0);

// Series
chart.addSeries("10^x", xData, yData);

return chart;
}
}

0 comments on commit d2a26b7

Please sign in to comment.