Skip to content
Merged
58 changes: 35 additions & 23 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -239,11 +239,12 @@ BoxChart chart =

// Choose a calculation method
chart.getStyler().setBoxplotCalCulationMethod(BoxplotCalCulationMethod.N_LESS_1_PLUS_1);
chart.getStyler().setToolTipsEnabled(true);

// Series
chart.addSeries("boxOne",Arrays.asList(1,2,3,4));
new SwingWrapper<BoxChart>(chart).displayChart();
chart.addSeries("boxOne", Arrays.asList(1, 2, 3, 4));
SwingWrapper<BoxChart> sw = new SwingWrapper<>(chart);
sw.displayChart();
sw.getXChartPanel().setToolTipsEnabled(true);
```

Four calculation methods for boxplots:
Expand Down Expand Up @@ -474,14 +475,16 @@ double-clicking on the chart or by clicking on the "reset" button, which can be

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

The following example zoom style options show which are available:
Zoom is a Swing-panel feature and is configured on `XChartPanel`, not on the Styler:

```java
chart.getStyler().setZoomEnabled(true);
chart.getStyler().setZoomResetButtomPosition(Styler.CardinalPosition.InsideS);
chart.getStyler().setZoomResetByDoubleClick(false);
chart.getStyler().setZoomResetByButton(true);
chart.getStyler().setZoomSelectionColor(new Color(0,0,192,128));
SwingWrapper<XYChart> sw = new SwingWrapper<>(chart);
sw.displayChart();
sw.getXChartPanel()
.setZoomEnabled(true)
.setZoomResetByDoubleClick(false)
.setZoomResetByButton(true)
.setZoomSelectionColor(new Color(0, 0, 192, 128));
```

A working example can be found at [DateChart01](https://github.com/knowm/XChart/blob/develop/xchart-demo/src/main/java/org/knowm/xchart/demo/charts/date/DateChart01.java).
Expand Down Expand Up @@ -532,49 +535,58 @@ A working example can be found at [ScatterChart04](https://github.com/knowm/XCha

### Tool Tips

For all chart types, tool tips can be activated on an `XChartPanel` via
Tool tips come in two flavours:

**1. Hover tooltips** — interactive labels that follow the mouse, visible only in a Swing panel. Configured on `XChartPanel`:

```java
chart.getStyler().setToolTipsEnabled(true);
SwingWrapper<XYChart> sw = new SwingWrapper<>(chart);
sw.displayChart();
sw.getXChartPanel().setToolTipsEnabled(true);
```

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

The following example tooltip options show which are available:
**2. Always-visible labels** — data-point labels baked into the chart image itself. These appear in Swing panels *and* in headless output such as `BitmapEncoder`. Configured on the Styler:

```java
chart.getStyler().setToolTipsEnabled(true);
chart.getStyler().setToolTipsAlwaysVisible(true);
chart.getStyler().setToolTipFont( new Font("Verdana", Font.BOLD, 12));
chart.getStyler().setToolTipType(ToolTipType.yLabels); // xAndYLabels (default), xLabels, yLabels
```

Both can be active at the same time. Tooltip visual appearance (font, colors) is always on the Styler:

```java
chart.getStyler().setToolTipFont(new Font("Verdana", Font.BOLD, 12));
chart.getStyler().setToolTipHighlightColor(Color.CYAN);
chart.getStyler().setToolTipBorderColor(Color.BLACK);
chart.getStyler().setToolTipBackgroundColor(Color.LIGHT_GRAY);
chart.getStyler().setToolTipType(Styler.ToolTipType.xAndYLabels);
```

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

A working example can be found at [LineChart05](https://github.com/knowm/XChart/blob/develop/xchart-demo/src/main/java/org/knowm/xchart/demo/charts/line/LineChart05.java).

### Cursor

For the `XYChart` chart type, it is possible to add an interactive cursor on an `XChartPanel` via
For the `XYChart` chart type, it is possible to add an interactive cursor on an `XChartPanel`. Cursor is a Swing-panel feature configured on `XChartPanel`:

```java
chart.getStyler().setCursorEnabled(true);
SwingWrapper<XYChart> sw = new SwingWrapper<>(chart);
sw.displayChart();
sw.getXChartPanel().setCursorEnabled(true);
```

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

The following example cursor options show which are available:
Cursor visual appearance is configured on the Styler:

```java
chart.getStyler().setCursorEnabled(true);
chart.getStyler().setCursorColor(Color.GREEN);
chart.getStyler().setCursorLineWidth(30f);
chart.getStyler().setCursorFont(new Font("Verdana", Font.BOLD, 12));
chart.getStyler().setCursorFontColor(Color.ORANGE);
chart.getStyler().setCursorBackgroundColor(Color.BLUE);
chart.getStyler().setCustomCursorXDataFormattingFunction(x ->"hello xvalue: "+x);
chart.getStyler().setCustomCursorYDataFormattingFunction(y ->"hello yvalue divided by 2: "+y /2);
chart.getStyler().setCustomCursorXDataFormattingFunction(x -> "hello xvalue: " + x);
chart.getStyler().setCustomCursorYDataFormattingFunction(y -> "hello yvalue divided by 2: " + y / 2);
```

A working example can be found at [LineChart09](https://github.com/knowm/XChart/blob/develop/xchart-demo/src/main/java/org/knowm/xchart/demo/charts/line/LineChart09.java).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,9 @@ public void valueChanged(TreeSelectionEvent e) {
if (node.isLeaf()) {
ChartInfo chartInfo = (ChartInfo) nodeInfo;
// displayURL(chartInfo.bookURL);
chartPanel = new XChartPanel(chartInfo.getExampleChart().getChart());
ExampleChart exampleChart = chartInfo.getExampleChart();
chartPanel = new XChartPanel(exampleChart.getChart());
exampleChart.customizePanel(chartPanel);
splitPane.setBottomComponent(chartPanel);

// start running a simulated data feed for the sample real-time plot
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,20 @@
package org.knowm.xchart.demo.charts;

import org.knowm.xchart.XChartPanel;
import org.knowm.xchart.internal.chartpart.Chart;

public interface ExampleChart<C extends Chart<?, ?>> {

C getChart();

String getExampleChartName();

/**
* Optional hook for configuring {@link XChartPanel} interaction features (tooltips, zoom,
* cursor) after the panel is created by the demo app. The default implementation does nothing.
* Override to enable hover tooltips, zoom, etc. for this example.
*
* @param panel the panel that was just created with {@link #getChart()}
*/
default void customizePanel(XChartPanel<C> panel) {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import org.knowm.xchart.CategoryChartBuilder;
import org.knowm.xchart.Histogram;
import org.knowm.xchart.SwingWrapper;
import org.knowm.xchart.ToolTipType;
import org.knowm.xchart.XChartPanel;
import org.knowm.xchart.demo.charts.ExampleChart;
import org.knowm.xchart.style.Styler;
import org.knowm.xchart.style.Styler.LegendPosition;
Expand All @@ -28,7 +30,9 @@ public static void main(String[] args) {

ExampleChart<CategoryChart> exampleChart = new BarChart07();
CategoryChart chart = exampleChart.getChart();
new SwingWrapper<>(chart).displayChart();
SwingWrapper<CategoryChart> wrapper = new SwingWrapper<>(chart);
wrapper.displayChart();
wrapper.getXChartPanel().setToolTipsEnabled(true);
}

@Override
Expand All @@ -48,9 +52,7 @@ public CategoryChart getChart() {
chart.getStyler().setLegendPosition(LegendPosition.InsideNW);
chart.getStyler().setAvailableSpaceFill(.96);
chart.getStyler().setPlotGridVerticalLinesVisible(false);
chart.getStyler().setToolTipsEnabled(true);
chart.getStyler().setToolTipType(Styler.ToolTipType.yLabels);

chart.getStyler().setToolTipType(ToolTipType.yLabels);
// Series
Histogram histogram1 = new Histogram(getGaussianData(1000), 10, -30, 30);
chart.addSeries("histogram 1", histogram1.getxAxisData(), histogram1.getyAxisData());
Expand All @@ -60,6 +62,12 @@ public CategoryChart getChart() {
return chart;
}

@Override
public void customizePanel(XChartPanel<CategoryChart> panel) {

panel.setToolTipsEnabled(true);
}

private List<Integer> getGaussianData(int count) {

List<Integer> data = new ArrayList<Integer>(count);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import org.knowm.xchart.BoxChart;
import org.knowm.xchart.BoxChartBuilder;
import org.knowm.xchart.SwingWrapper;
import org.knowm.xchart.XChartPanel;
import org.knowm.xchart.demo.charts.ExampleChart;
import org.knowm.xchart.style.BoxStyler.BoxplotCalCulationMethod;
import org.knowm.xchart.style.Styler.ChartTheme;
Expand All @@ -17,7 +18,9 @@ public class BoxChart02 implements ExampleChart<BoxChart> {
public static void main(String[] args) {
ExampleChart<BoxChart> exampleChart = new BoxChart02();
BoxChart chart = exampleChart.getChart();
new SwingWrapper<BoxChart>(chart).displayChart();
SwingWrapper<BoxChart> wrapper = new SwingWrapper<BoxChart>(chart);
wrapper.displayChart();
wrapper.getXChartPanel().setToolTipsEnabled(true);
}

@Override
Expand All @@ -40,10 +43,15 @@ public BoxChart getChart() {
chart.addSeries("bbb", Arrays.asList(1, 2, 3, 4, 5, 6, 17));
chart.addSeries("ccc", Arrays.asList(-10, -8, 1, 2, 3, 4, 5, 6, 7, 8, 9, 20, 21));
chart.getStyler().setShowWithinAreaPoint(true);
chart.getStyler().setToolTipsEnabled(true);
return chart;
}

@Override
public void customizePanel(XChartPanel<BoxChart> panel) {

panel.setToolTipsEnabled(true);
}

@Override
public String getExampleChartName() {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import org.knowm.xchart.BoxChart;
import org.knowm.xchart.BoxChartBuilder;
import org.knowm.xchart.SwingWrapper;
import org.knowm.xchart.XChartPanel;
import org.knowm.xchart.demo.charts.ExampleChart;
import org.knowm.xchart.style.Styler.ChartTheme;

Expand All @@ -17,7 +18,9 @@ public class BoxChart03 implements ExampleChart<BoxChart> {
public static void main(String[] args) {
ExampleChart<BoxChart> exampleChart = new BoxChart03();
BoxChart chart = exampleChart.getChart();
new SwingWrapper<BoxChart>(chart).displayChart();
SwingWrapper<BoxChart> wrapper = new SwingWrapper<BoxChart>(chart);
wrapper.displayChart();
wrapper.getXChartPanel().setToolTipsEnabled(true);
}

@Override
Expand All @@ -35,14 +38,19 @@ public BoxChart getChart() {
.build();

// Customize Chart
chart.getStyler().setToolTipsEnabled(true);
chart.getStyler().setYAxisLogarithmic(true);

// Series
chart.addSeries("aaa", Arrays.asList(10, 40, 80, 120, 350));
return chart;
}

@Override
public void customizePanel(XChartPanel<BoxChart> panel) {

panel.setToolTipsEnabled(true);
}

@Override
public String getExampleChartName() {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import org.knowm.xchart.BubbleChart;
import org.knowm.xchart.BubbleChartBuilder;
import org.knowm.xchart.SwingWrapper;
import org.knowm.xchart.XChartPanel;
import org.knowm.xchart.demo.charts.ExampleChart;
import org.knowm.xchart.style.Styler;

Expand All @@ -21,7 +22,9 @@ public static void main(String[] args) {

ExampleChart<BubbleChart> exampleChart = new BubbleChart01();
BubbleChart chart = exampleChart.getChart();
new SwingWrapper<>(chart).displayChart();
SwingWrapper<BubbleChart> wrapper = new SwingWrapper<>(chart);
wrapper.displayChart();
wrapper.getXChartPanel().setToolTipsEnabled(true);
}

@Override
Expand All @@ -38,7 +41,6 @@ public BubbleChart getChart() {
.build();
chart.getStyler().setLegendPosition(Styler.LegendPosition.InsideN);
chart.getStyler().setLegendLayout(Styler.LegendLayout.Horizontal);
chart.getStyler().setToolTipsEnabled(true);

// Customize Chart

Expand All @@ -57,6 +59,12 @@ public BubbleChart getChart() {
return chart;
}

@Override
public void customizePanel(XChartPanel<BubbleChart> panel) {

panel.setToolTipsEnabled(true);
}

@Override
public String getExampleChartName() {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import java.util.Random;
import java.util.TimeZone;
import org.knowm.xchart.SwingWrapper;
import org.knowm.xchart.XChartPanel;
import org.knowm.xchart.XYChart;
import org.knowm.xchart.XYChartBuilder;
import org.knowm.xchart.XYSeries;
Expand All @@ -33,7 +34,9 @@ public static void main(String[] args) {

ExampleChart<XYChart> exampleChart = new DateChart01();
XYChart chart = exampleChart.getChart();
new SwingWrapper<>(chart).displayChart();
SwingWrapper<XYChart> wrapper = new SwingWrapper<>(chart);
wrapper.displayChart();
wrapper.getXChartPanel().setZoomEnabled(true);
}

@Override
Expand All @@ -46,7 +49,6 @@ public XYChart getChart() {
// Customize Chart
chart.getStyler().setLegendPosition(Styler.LegendPosition.OutsideS);
chart.getStyler().setLegendLayout(Styler.LegendLayout.Horizontal);
chart.getStyler().setZoomEnabled(true);
// chart.getStyler().setZoomResetButtomPosition(Styler.CardinalPosition.InsideS);
// chart.getStyler().setZoomResetByDoubleClick(false);
// chart.getStyler().setZoomResetByButton(true);
Expand Down Expand Up @@ -84,6 +86,12 @@ public XYChart getChart() {
return chart;
}

@Override
public void customizePanel(XChartPanel<XYChart> panel) {

panel.setZoomEnabled(true);
}

@Override
public String getExampleChartName() {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import org.knowm.xchart.SwingWrapper;
import org.knowm.xchart.XChartPanel;
import org.knowm.xchart.XYChart;
import org.knowm.xchart.XYChartBuilder;
import org.knowm.xchart.demo.charts.ExampleChart;
Expand All @@ -28,7 +29,9 @@ public static void main(String[] args) {

ExampleChart<XYChart> exampleChart = new DateChart09();
XYChart chart = exampleChart.getChart();
new SwingWrapper<>(chart).displayChart();
SwingWrapper<XYChart> wrapper = new SwingWrapper<>(chart);
wrapper.displayChart();
wrapper.getXChartPanel().setCursorEnabled(true);
}

@Override
Expand Down Expand Up @@ -63,7 +66,6 @@ public XYChart getChart() {
x -> startTime.plusDays(x.longValue()).format(xTickFormatter));

// set custom cursor tool tip text
chart.getStyler().setCursorEnabled(true);
DateTimeFormatter cursorXFormatter = DateTimeFormatter.ofPattern("LLL dd");
chart
.getStyler()
Expand All @@ -73,6 +75,12 @@ public XYChart getChart() {
return chart;
}

@Override
public void customizePanel(XChartPanel<XYChart> panel) {

panel.setCursorEnabled(true);
}

@Override
public String getExampleChartName() {

Expand Down
Loading
Loading