-
Notifications
You must be signed in to change notification settings - Fork 29
Refactor tooltip handling in TmfCommonXLineChart #383
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
TranHoangThanhDuy
wants to merge
1
commit into
eclipse-tracecompass:master
Choose a base branch
from
TranHoangThanhDuy:patch-1
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+102
−74
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -28,6 +28,7 @@ | |
| import org.eclipse.tracecompass.tmf.core.timestamp.ITmfTimestamp; | ||
| import org.eclipse.tracecompass.tmf.core.timestamp.TmfTimestamp; | ||
| import org.eclipse.tracecompass.tmf.ui.viewers.TmfAbstractToolTipHandler; | ||
| import org.eclipse.tracecompass.tmf.ui.viewers.TmfAbstractToolTipHandler.ToolTipString; | ||
| import org.eclipse.tracecompass.tmf.ui.viewers.xychart.IAxis; | ||
| import org.eclipse.tracecompass.tmf.ui.viewers.xychart.ITmfChartTimeProvider; | ||
| import org.eclipse.tracecompass.tmf.ui.viewers.xychart.IXYSeries; | ||
|
|
@@ -43,80 +44,8 @@ | |
| */ | ||
| public class TmfCommonXLineChartTooltipProvider extends TmfBaseProvider { | ||
|
|
||
| private final class XYToolTipHandler extends TmfAbstractToolTipHandler { | ||
| private static final String HTML_COLOR_TOOLTIP = "<span style=\"color:%s;\">%s</span>"; //$NON-NLS-1$ | ||
|
|
||
| private boolean isValid(int index, IXYSeries serie) { | ||
| double[] ySeries = serie.getYSeries(); | ||
| return serie.isVisible() && ySeries != null && ySeries.length > index; | ||
| } | ||
|
|
||
| @Override | ||
| public void fill(Control control, MouseEvent event, Point pt) { | ||
| if (getChartViewer().getWindowDuration() != 0) { | ||
| IAxis xAxis = getXAxis(); | ||
|
|
||
| double xCoordinate = xAxis.getDataCoordinate(pt.x); | ||
|
|
||
| List<IXYSeries> series = getSeries(); | ||
|
|
||
| if ((xCoordinate < 0) || (series.isEmpty())) { | ||
| return; | ||
| } | ||
|
|
||
| /* Find the index of the value we want */ | ||
| double[] xS = series.get(0).getXSeries(); | ||
| if (xS == null) { | ||
| return; | ||
| } | ||
| int index = Arrays.binarySearch(xS, xCoordinate); | ||
| index = index >= 0 ? index : -index - 1; | ||
| int maxLen = 0; | ||
| for (IXYSeries serie : series) { | ||
| /* Make sure the series values and the value at index exist */ | ||
| if (isValid(index, serie)) { | ||
| maxLen = Math.max(maxLen, serie.getId().length()); | ||
| } | ||
| } | ||
|
|
||
| TmfCommonXAxisChartViewer viewer = null; | ||
| Format format = null; | ||
| ITmfChartTimeProvider timeProvider = getChartViewer(); | ||
| if (timeProvider instanceof TmfCommonXAxisChartViewer) { | ||
| viewer = (TmfCommonXAxisChartViewer) timeProvider; | ||
| format = viewer.getSwtChart().getAxisSet().getYAxes()[0].getTick().getFormat(); | ||
| } | ||
| ITmfTimestamp time = TmfTimestamp.fromNanos((long) xCoordinate + getChartViewer().getTimeOffset()); | ||
| addItem(null, ToolTipString.fromString(Messages.TmfCommonXLineChartTooltipProvider_time), ToolTipString.fromTimestamp(time.toString(), time.toNanos())); | ||
| /* For each series, get the value at the index */ | ||
| for (IXYSeries serie : series) { | ||
| double[] yS = serie.getYSeries(); | ||
| /* Make sure the series values and the value at index exist */ | ||
| if (isValid(index, serie)) { | ||
| String key = serie.getId(); | ||
| Color color = serie.getColor(); | ||
| if (key != null && color != null && viewer != null) { | ||
| RGBA rgba = color.getRGBA(); | ||
| RGBAColor rgbaColor = new RGBAColor(rgba.rgb.red, rgba.rgb.green, rgba.rgb.blue, rgba.alpha); | ||
| key = String.format(HTML_COLOR_TOOLTIP, rgbaColor, key); | ||
| } | ||
| if (key == null) { | ||
| key = ""; //$NON-NLS-1$ | ||
| } | ||
| double yValue = yS[index]; | ||
| if (format == null) { | ||
| addItem(null, ToolTipString.fromHtml(key), ToolTipString.fromDecimal(yValue)); | ||
| } else { | ||
| addItem(null, ToolTipString.fromHtml(key), ToolTipString.fromString(format.format(yValue))); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| } | ||
|
|
||
| private XYToolTipHandler fToolTipHandler = new XYToolTipHandler(); | ||
| private static final String HTML_COLOR_TOOLTIP = "<span style=\"color:%s;\">%s</span>"; //$NON-NLS-1$ | ||
| private final CommonToolTipHandler fToolTipHandler = new CommonToolTipHandler(); | ||
|
|
||
| /** | ||
| * Constructor for the tooltip provider | ||
|
|
@@ -142,4 +71,103 @@ public TmfAbstractToolTipHandler getTooltipHandler() { | |
| public void refresh() { | ||
| // nothing to do | ||
| } | ||
|
|
||
| protected boolean isTooltipAvailable() { | ||
| return getChartViewer().getWindowDuration() != 0; | ||
| } | ||
|
|
||
| protected int getHoveredIndex(List<IXYSeries> series, double xCoordinate) { | ||
| if (series.isEmpty()) { | ||
| return -1; | ||
| } | ||
| double[] xSeries = series.get(0).getXSeries(); | ||
| if ((xSeries == null) || (xSeries.length == 0)) { | ||
| return -1; | ||
| } | ||
| int index = Arrays.binarySearch(xSeries, xCoordinate); | ||
| index = (index >= 0) ? index : -index - 1; | ||
| return (index < xSeries.length) ? index : -1; | ||
| } | ||
|
|
||
| protected boolean isValidSeriesIndex(IXYSeries series, int index) { | ||
| double[] ySeries = series.getYSeries(); | ||
| return series.isVisible() && ySeries != null && index >= 0 && index < ySeries.length; | ||
| } | ||
|
|
||
| protected void addAdditionalTooltipItems(double xCoordinate, String seriesKey) { | ||
| ITmfTimestamp time = TmfTimestamp.fromNanos((long) xCoordinate + getChartViewer().getTimeOffset()); | ||
| addItem(null, | ||
| ToolTipString.fromString(Messages.TmfCommonXLineChartTooltipProvider_time), | ||
| ToolTipString.fromTimestamp(time.toString(), time.toNanos())); | ||
| } | ||
|
|
||
| protected void addSeriesTooltipItem(IXYSeries series, int index, Format format) { | ||
| double[] ySeries = series.getYSeries(); | ||
| if (ySeries == null || index < 0 || index >= ySeries.length) { | ||
| return; | ||
| } | ||
|
|
||
| String label = formatSeriesLabel(series); | ||
| double yValue = ySeries[index]; | ||
| if (format == null) { | ||
| addItem(null, ToolTipString.fromHtml(label), ToolTipString.fromDecimal(yValue)); | ||
| } else { | ||
| addItem(null, ToolTipString.fromHtml(label), ToolTipString.fromString(format.format(yValue))); | ||
| } | ||
| } | ||
|
|
||
| protected String formatSeriesLabel(IXYSeries series) { | ||
| String key = series.getId(); | ||
| String label = (key == null) ? "" : key; //$NON-NLS-1$ | ||
| Color color = series.getColor(); | ||
| if (color != null) { | ||
| RGBA rgba = color.getRGBA(); | ||
| RGBAColor rgbaColor = new RGBAColor(rgba.rgb.red, rgba.rgb.green, rgba.rgb.blue, rgba.alpha); | ||
| label = String.format(TmfCommonXLineChartTooltipProvider.HTML_COLOR_TOOLTIP, rgbaColor, label); | ||
| } | ||
| return label; | ||
| } | ||
|
|
||
| private final class CommonToolTipHandler extends TmfAbstractToolTipHandler { | ||
|
|
||
| @Override | ||
| public void fill(Control control, MouseEvent event, Point pt) { | ||
| if (!isTooltipAvailable()) { | ||
| return; | ||
| } | ||
|
|
||
| IAxis xAxis = getXAxis(); | ||
| double xCoordinate = xAxis.getDataCoordinate(pt.x); | ||
| if (xCoordinate < 0) { | ||
| return; | ||
| } | ||
|
|
||
| List<IXYSeries> series = getSeries(); | ||
| int index = getHoveredIndex(series, xCoordinate); | ||
| if (index < 0) { | ||
| return; | ||
| } | ||
|
|
||
| Format format = null; | ||
| if (getChartViewer() instanceof TmfCommonXAxisChartViewer chartViewer) { | ||
| format = chartViewer.getSwtChart().getAxisSet().getYAxes()[0].getTick().getFormat(); | ||
| } | ||
|
|
||
| String firstValidSeriesKey = null; | ||
| for (IXYSeries xySeries : series) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can you make a copy of the list which is valid only? this could simplify the logic |
||
| if (isValidSeriesIndex(xySeries, index)) { | ||
| firstValidSeriesKey = xySeries.getId(); | ||
| break; | ||
| } | ||
| } | ||
| addAdditionalTooltipItems(xCoordinate, firstValidSeriesKey); | ||
|
|
||
| for (IXYSeries xySeries : series) { | ||
| if (!isValidSeriesIndex(xySeries, index)) { | ||
| continue; | ||
| } | ||
| addSeriesTooltipItem(xySeries, index, format); | ||
| } | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does this need to be new API? Can extenders have a different algorithm to calculate the index?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point. It doesn’t strictly need to be new API. I extracted it mainly to keep fill() smaller/readable and to avoid repeating the index calculation logic.
If you prefer not to expose this as an extension point, I can make getHoveredIndex(...) private.
How about this idea?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good idea - having the functionality extracted to make
filleasier to read is a good idea. Lets keep all these new methods private unless explicitly needed to support extenders.