Skip to content

Commit

Permalink
Merge pull request #272 from hakantkn/customTooltips
Browse files Browse the repository at this point in the history
added custom tool tips, always visible tool tips & example chart tester
  • Loading branch information
timmolter committed Jun 25, 2018
2 parents b36629d + 64b2e76 commit f22ae8d
Show file tree
Hide file tree
Showing 20 changed files with 787 additions and 51 deletions.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package org.knowm.xchart.demo.charts;

public interface RealtimeExampleChart {

public void updateData();
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import org.knowm.xchart.XYChart;
import org.knowm.xchart.XYChartBuilder;
import org.knowm.xchart.demo.charts.ExampleChart;
import org.knowm.xchart.demo.charts.RealtimeExampleChart;
import org.knowm.xchart.style.Styler.ChartTheme;

/**
Expand All @@ -19,7 +20,7 @@
* <li>real-time chart updates with SwingWrapper
* <li>Matlab Theme
*/
public class RealtimeChart01 implements ExampleChart<XYChart> {
public class RealtimeChart01 implements ExampleChart<XYChart>, RealtimeExampleChart {

private XYChart xyChart;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import org.knowm.xchart.PieSeries.PieSeriesRenderStyle;
import org.knowm.xchart.SwingWrapper;
import org.knowm.xchart.demo.charts.ExampleChart;
import org.knowm.xchart.demo.charts.RealtimeExampleChart;
import org.knowm.xchart.style.PieStyler.AnnotationType;
import org.knowm.xchart.style.Styler.ChartTheme;

Expand All @@ -23,7 +24,7 @@
* <li>Matlab theme
* <li>Pie Chart
*/
public class RealtimeChart02 implements ExampleChart<PieChart> {
public class RealtimeChart02 implements ExampleChart<PieChart>, RealtimeExampleChart {

private PieChart pieChart;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import org.knowm.xchart.XYChart;
import org.knowm.xchart.XYChartBuilder;
import org.knowm.xchart.demo.charts.ExampleChart;
import org.knowm.xchart.demo.charts.RealtimeExampleChart;

/**
* Real-time XY Chart with Error Bars
Expand All @@ -21,7 +22,7 @@
* <li>fixed window
* <li>error bars
*/
public class RealtimeChart03 implements ExampleChart<XYChart> {
public class RealtimeChart03 implements ExampleChart<XYChart>, RealtimeExampleChart {

private XYChart xyChart;

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

/**
Expand All @@ -23,7 +24,7 @@
* <li>Bubble chart
* <li>GGPlot2 theme
*/
public class RealtimeChart04 implements ExampleChart<BubbleChart> {
public class RealtimeChart04 implements ExampleChart<BubbleChart>, RealtimeExampleChart {

private BubbleChart bubbleChart;

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

/**
Expand All @@ -21,7 +22,7 @@
* <ul>
* <li>real-time chart updates with SwingWrapper
*/
public class RealtimeChart05 implements ExampleChart<CategoryChart> {
public class RealtimeChart05 implements ExampleChart<CategoryChart>, RealtimeExampleChart {

private CategoryChart categoryChart;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.util.*;
import org.knowm.xchart.*;
import org.knowm.xchart.demo.charts.ExampleChart;
import org.knowm.xchart.demo.charts.RealtimeExampleChart;
import org.knowm.xchart.demo.charts.ohlc.OHLCChart01;
import org.knowm.xchart.style.Styler;
import org.knowm.xchart.style.Styler.ChartTheme;
Expand All @@ -16,7 +17,7 @@
* <li>real-time chart updates with SwingWrapper
* <li>Matlab Theme
*/
public class RealtimeChart06 implements ExampleChart<OHLCChart> {
public class RealtimeChart06 implements ExampleChart<OHLCChart>, RealtimeExampleChart {

private OHLCChart ohlcChart;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
package org.knowm.xchart.standalone.issues;

import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.Map;

import org.knowm.xchart.BubbleSeries;
import org.knowm.xchart.CategorySeries;
import org.knowm.xchart.OHLCSeries;
import org.knowm.xchart.PieSeries;
import org.knowm.xchart.RadarSeries;
import org.knowm.xchart.demo.ExampleChartTester;
import org.knowm.xchart.demo.charts.ExampleChart;
import org.knowm.xchart.demo.charts.dial.DialChart01;
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.RealtimeChart06;
import org.knowm.xchart.internal.chartpart.Chart;
import org.knowm.xchart.internal.series.AxesChartSeries;
import org.knowm.xchart.internal.series.AxesChartSeriesNumericalNoErrorBars;
import org.knowm.xchart.internal.series.Series;

public class TestForIssue227 {

static final int WIDTH = 465;
static final int HEIGHT = 320;

public static void main(String[] args) {

ExampleChartTester tester = new ExampleChartTester() {

@Override
protected Map<String, Chart> getCharts(ExampleChartInfo chartInfo) {

LinkedHashMap<String, Chart> map = new LinkedHashMap<String, Chart>();
ExampleChart ec = chartInfo.getExampleChart();
Chart c = getChartWithCustomTooltip(ec);
if (c != null) {
c.getStyler().setToolTipsAlwaysVisible(true);
map.put("Custom tooltips always visible", c);
}

map.put("Custom tooltips", getChartWithCustomTooltip(ec));
map.put("Default tooltips", getChart(ec));

return map;
}

};

HashSet<Class> excludeSet = new HashSet();
// dial has annotation
excludeSet.add(DialChart01.class);

// in real time charts tooltips must be set for each data point
excludeSet.add(RealtimeChart01.class);
excludeSet.add(RealtimeChart02.class);
excludeSet.add(RealtimeChart03.class);
excludeSet.add(RealtimeChart04.class);
excludeSet.add(RealtimeChart05.class);
excludeSet.add(RealtimeChart06.class);

tester.setExcludeSet(excludeSet);
tester.createAndShowGUI();
}

private static Chart getChartWithCustomTooltip(ExampleChart ec) {

Chart chart = getChart(ec);
Map<String, Series> seriesMap = chart.getSeriesMap();

boolean flag = false;
for (Series series : seriesMap.values()) {
if (series instanceof PieSeries) {
String[] toolTips = getToolTips(series.getName(), 1);
((PieSeries)series).setToolTip(toolTips[0]);
flag = true;
continue;
} else if (series instanceof RadarSeries) {
int count = ((RadarSeries) series).getValues().length;
String[] toolTips = getToolTips(series.getName(), count);
((RadarSeries)series).setTooltipOverrides(toolTips);
flag = true;
continue;
}
if (!(series instanceof AxesChartSeries)) {
System.out.println(series.getClass());
continue;
}
int count = 0;
if (series instanceof AxesChartSeriesNumericalNoErrorBars) {
count = ((AxesChartSeriesNumericalNoErrorBars) series).getXData().length;
} else if(series instanceof CategorySeries) {
count = ((CategorySeries) series).getYData().size();
} else if(series instanceof OHLCSeries) {
count = ((OHLCSeries) series).getOpenData().length;
} else if(series instanceof BubbleSeries) {
count = ((BubbleSeries) series).getXData().length;
} else {
System.out.println(series.getClass());
}


if (count <= 0) {
continue;
}
String[] toolTips = getToolTips(series.getName(), count);
((AxesChartSeries) series).setToolTips(toolTips);
flag = true;
}
if (!flag) {
System.out.println("Skipping " + ec.getClass().getSimpleName());
return null;
}
return chart;
}

private static Chart getChart(ExampleChart ec) {

Chart chart = ec.getChart();
chart.getStyler().setToolTipsEnabled(true);
return chart;
}

private static String[] getToolTips(String name, int count) {

//only show 10 tooltips
int x = (int) Math.ceil(count / 10.0);
String[] t = new String[count];
for (int i = 0; i < t.length; i++) {
if (i % x == 0) {
t[i] = "Custom tt - " + name + " Point " + (i + 1);
} else {
t[i] = null; // No tooltip
}
}
return t;
}

}
11 changes: 11 additions & 0 deletions xchart/src/main/java/org/knowm/xchart/PieSeries.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public class PieSeries extends Series {

private PieSeriesRenderStyle chartPieSeriesRenderStyle = null;
private Number value;
private String toolTip;

/**
* Constructor
Expand Down Expand Up @@ -69,4 +70,14 @@ public enum PieSeriesRenderStyle {

Donut();
}

public String getToolTip() {

return toolTip;
}

public void setToolTip(String toolTip) {

this.toolTip = toolTip;
}
}
5 changes: 5 additions & 0 deletions xchart/src/main/java/org/knowm/xchart/RadarSeries.java
Original file line number Diff line number Diff line change
Expand Up @@ -148,4 +148,9 @@ public LegendRenderType getLegendRenderType() {
// Radar charts are always rendered as a Box in the legend
return null;
}

public void setTooltipOverrides(String[] tooltipOverrides) {

this.tooltipOverrides = tooltipOverrides;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,17 @@ public void doPaint(Graphics2D g) {
xMax = Math.log10(xMax);
}

boolean toolTipsEnabled = chart.getStyler().isToolTipsEnabled();
Map<String, S> map = chart.getSeriesMap();
for (S series : map.values()) {

if (!series.isEnabled()) {
continue;
}

String[] toolTips = series.getToolTips();
boolean hasCustomToolTips = toolTips != null;

double yMin = chart.getYAxis(series.getYAxisGroup()).getMin();
double yMax = chart.getYAxis(series.getYAxisGroup()).getMax();
if (stylerBubble.isYAxisLogarithmic()) {
Expand Down Expand Up @@ -136,13 +140,27 @@ public void doPaint(Graphics2D g) {
g.setStroke(series.getLineStyle());
g.draw(bubble);
// add data labels
chart.toolTips.addData(
bubble,
xOffset,
yOffset,
0,
chart.getXAxisFormat().format(x),
chart.getYAxisFormat().format(yOrig));
if (toolTipsEnabled) {
if (hasCustomToolTips) {
String tt = toolTips[i];
if (tt != null) {
chart.toolTips.addData(
bubble,
xOffset,
yOffset,
0,
tt);
}
} else {
chart.toolTips.addData(
bubble,
xOffset,
yOffset,
0,
chart.getXAxisFormat().format(x),
chart.getYAxisFormat().format(yOrig));
}
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,11 +123,15 @@ public void doPaint(Graphics2D g) {
double[] accumulatedStackOffsetPos = new double[numCategories];
double[] accumulatedStackOffsetNeg = new double[numCategories];

boolean toolTipsEnabled = chart.getStyler().isToolTipsEnabled();

for (S series : seriesMap.values()) {

if (!series.isEnabled()) {
continue;
}
String[] toolTips = series.getToolTips();
boolean hasCustomToolTips = toolTips != null;

yMin = chart.getYAxis(series.getYAxisGroup()).getMin();
yMax = chart.getYAxis(series.getYAxisGroup()).getMax();
Expand Down Expand Up @@ -474,7 +478,7 @@ else if (series.getChartCategorySeriesRenderStyle() == CategorySeriesRenderStyle
g.draw(line);
}
// add data labels
if (chart.toolTips != null) {
if (toolTipsEnabled) {
Rectangle2D.Double rect =
new Rectangle2D.Double(xOffset, yOffset, barWidth, Math.abs(yOffset - zeroOffset));
double yPoint;
Expand All @@ -483,13 +487,26 @@ else if (series.getChartCategorySeriesRenderStyle() == CategorySeriesRenderStyle
} else {
yPoint = yOffset;
}
chart.toolTips.addData(
rect,
xOffset,
yPoint,
barWidth,
chart.getXAxisFormat().format(nextCat),
chart.getYAxisFormat().format(y));

if (hasCustomToolTips) {
String tt = toolTips[categoryCounter-1];
if (tt != null) {
chart.toolTips.addData(
rect,
xOffset,
yPoint,
barWidth,
tt);
}
} else {
chart.toolTips.addData(
rect,
xOffset,
yPoint,
barWidth,
chart.getXAxisFormat().format(nextCat),
chart.getYAxisFormat().format(y));
}
}
}

Expand Down
Loading

0 comments on commit f22ae8d

Please sign in to comment.