Skip to content

Commit

Permalink
Support/v4x mouse details on legend click - ticket 3005 (#578)
Browse files Browse the repository at this point in the history
* Charts add-on modifications for adding MouseEventDetails to legend click events to server-side logic
* Added TB Tests for Legend click MouseEventDetails
  • Loading branch information
petrixh authored and alvarezguille committed Jun 17, 2019
1 parent 8290f21 commit f279440
Show file tree
Hide file tree
Showing 8 changed files with 286 additions and 19 deletions.
4 changes: 2 additions & 2 deletions addon/src/main/java/com/vaadin/addon/charts/Chart.java
Original file line number Diff line number Diff line change
Expand Up @@ -259,11 +259,11 @@ public void onSelection(final double selectionStart,

@Override
public void onLegendItemClick(final int seriesIndex,
int seriesItemIndex) {
int seriesItemIndex, MouseEventDetails details) {
Series series = resolveSeriesFor(seriesIndex);
final LegendItemClickEvent legendItemClickEvent =
new LegendItemClickEvent(
Chart.this, series, seriesItemIndex);
Chart.this, series, seriesItemIndex, details);
fireEvent(legendItemClickEvent);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,33 @@
*/

import com.vaadin.addon.charts.model.Series;
import com.vaadin.addon.charts.shared.MouseEventDetails;

/**
* The LegendItemClickEvent class stores information on click events on the
* charts's legend items.
*/
public class LegendItemClickEvent extends AbstractSeriesEvent {

private MouseEventDetails mouseEventDetails;

/**
* Constructs a LegendItemClickEvent
*
* @param source
* @param seriesName
* @param mouseEventDetails
*/
public LegendItemClickEvent(Chart source, Series series, int seriesItemIndex) {
public LegendItemClickEvent(Chart source, Series series, int seriesItemIndex, MouseEventDetails mouseEventDetails) {
super(source, series, seriesItemIndex);
this.mouseEventDetails = mouseEventDetails;
}



/**
* Returns the mouse event details for this legend item click
* @return
*/
public MouseEventDetails getMouseEventDetails() {
return mouseEventDetails;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,16 @@
* %%
* This program is available under Commercial Vaadin Add-On License 3.0
* (CVALv3).
*
*
* See the file licensing.txt distributed with this software for more
* information about licensing.
*
*
* You should have received a copy of the CVALv3 along with this program.
* If not, see <https://vaadin.com/license/cval-3>.
* #L%
*/

import com.google.gwt.dom.client.NativeEvent;
import com.google.gwt.user.client.ui.UIObject;
import com.vaadin.addon.charts.shared.MouseEventDetails;
import com.vaadin.addon.charts.shared.MouseEventDetails.MouseButton;
Expand All @@ -30,13 +31,13 @@ public class MouseEventDetailsBuilder {
/**
* Construct a {@link MouseEventDetails} object from the given
* {@link ChartClickEvent}
*
*
* @param event
* The event to use as a source for the details
* @param relativeToObject
* The element used to calculate
* {@link MouseEventDetails#getxValue()} and
* {@link MouseEventDetails#getyValue()}
* {@link MouseEventDetails#getxValue()} and
* {@link MouseEventDetails#getyValue()}
* @return mouseEventDetails containing information from the event
*/
public static MouseEventDetails buildMouseEventDetails(
Expand All @@ -63,13 +64,13 @@ public static MouseEventDetails buildMouseEventDetails(
/**
* Construct a {@link MouseEventDetails} object from the given
* {@link PointClickEvent}
*
*
* @param event
* The event to use as a source for the details
* @param relativeToObject
* The element used to calculate
* {@link MouseEventDetails#getxValue()} and
* {@link MouseEventDetails#getyValue()}
* {@link MouseEventDetails#getxValue()} and
* {@link MouseEventDetails#getyValue()}
* @return mouseEventDetails containing information from the event
*/
public static MouseEventDetails buildMouseEventDetails(
Expand All @@ -87,16 +88,60 @@ public static MouseEventDetails buildMouseEventDetails(
return mouseEventDetails;
}


/**
* Construct a {@link MouseEventDetails} object from the given event.
*
* @param evt
* The event to use as a source for the details
* @param relativeToObject
* The element whose position
* {@link MouseEventDetails#getxValue()} ()} and
* {@link MouseEventDetails#getyValue()} ()} are relative to.
* @return a MouseEventDetails containing information from the event
*/
public static MouseEventDetails buildMouseEventDetails(NativeEvent evt,
UIObject relativeToObject) {
MouseEventDetails mouseEventDetails = new MouseEventDetails();

mouseEventDetails.setAbsoluteX(evt.getClientX());
mouseEventDetails.setAbsoluteY(evt.getClientY());

int relativeX = evt.getClientX() - relativeToObject.getAbsoluteLeft();
int relativeY = evt.getClientY() - relativeToObject.getAbsoluteTop();

mouseEventDetails.setxValue(relativeX);
mouseEventDetails.setyValue(relativeY);

if (evt.getButton() == NativeEvent.BUTTON_LEFT) {
mouseEventDetails.setButton(MouseButton.LEFT);
} else if (evt.getButton() == NativeEvent.BUTTON_RIGHT) {
mouseEventDetails.setButton(MouseButton.RIGHT);
} else if (evt.getButton() == NativeEvent.BUTTON_MIDDLE) {
mouseEventDetails.setButton(MouseButton.MIDDLE);
} else {
// No button reported? Assume left.
mouseEventDetails.setButton(MouseButton.LEFT);
}
mouseEventDetails.setAltKey(evt.getAltKey());
mouseEventDetails.setCtrlKey(evt.getCtrlKey());
mouseEventDetails.setMetaKey(evt.getMetaKey());
mouseEventDetails.setShiftKey(evt.getShiftKey());
return mouseEventDetails;

}


/**
* Init {@link MouseEventDetails} with {@link AbstractClickEvent} info
*
*
* @param details
* object to be initialized
* @param event
* The event to use as a source for the details
*/
private static void initCommonValues(MouseEventDetails details,
AbstractClickEvent event) {
AbstractClickEvent event) {
if (event.getButton() == 0) {
details.setButton(MouseButton.LEFT);
} else if (event.getButton() == 2) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
*/

import com.google.gwt.core.client.JavaScriptObject;
import com.google.gwt.dom.client.NativeEvent;

public class SeriesEvent extends JavaScriptObject {

Expand Down Expand Up @@ -52,4 +53,13 @@ public native final int getSeriesItemIndex()
return this.target.x;
}-*/;

/**
* Returns the native (actual event) that triggered this SeriesEvent
* @return the native browser event for this SeriesEvent
*/
public native final NativeEvent getBrowserEvent()
/*-{
return this.browserEvent;
}-*/;

}
Original file line number Diff line number Diff line change
Expand Up @@ -326,8 +326,10 @@ public boolean onClick(SeriesEvent event) {
if (hasEventListener(LEGENDITEM_CLICK_EVENT_ID)) {
int seriesIndex = getWidget().getSeriesIndex(
event.getSeries());

MouseEventDetails mouseEventDetails = MouseEventDetailsBuilder.buildMouseEventDetails(event.getBrowserEvent(), getWidget());
rpc.onLegendItemClick(seriesIndex,
event.getSeriesItemIndex());
event.getSeriesItemIndex(), mouseEventDetails);
}
return !getState().seriesVisibilityTogglingDisabled;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
* %%
* This program is available under Commercial Vaadin Add-On License 3.0
* (CVALv3).
*
*
* See the file licensing.txt distributed with this software for more
* information about licensing.
*
*
* You should have received a copy of the CVALv3 along with this program.
* If not, see <https://vaadin.com/license/cval-3>.
* #L%
Expand All @@ -33,7 +33,7 @@ void onPointClick(MouseEventDetails details, int seriesIndex,
void onSelection(double selectionStart, double selectionEnd,
double valueStart, double valueEnd);

void onLegendItemClick(int seriesIndex, int seriesItemIndex);
void onLegendItemClick(int seriesIndex, int seriesItemIndex, MouseEventDetails mouseEventDetails);

void onCheckboxClick(boolean isChecked, int seriesIndex, int seriesItemIndex);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package com.vaadin.addon.charts.examples.pie;

import com.vaadin.addon.charts.Chart;
import com.vaadin.addon.charts.LegendItemClickEvent;
import com.vaadin.addon.charts.LegendItemClickListener;
import com.vaadin.addon.charts.examples.SkipFromDemo;
import com.vaadin.addon.charts.shared.MouseEventDetails;
import com.vaadin.ui.Component;
import com.vaadin.ui.Label;

@SkipFromDemo
public class PieWithLegendAndMouseDetails extends PieWithLegend {

public static final String MOUSE_DETAILS = "mouse-details";
private Label mouseDetails = new Label();

@Override
protected Component getChart() {

Chart chart = (Chart) super.getChart();
chart.addLegendItemClickListener(new LegendItemClickListener() {

@Override
public void onClick(LegendItemClickEvent event) {
updateMouseEventDetailsForLegendClick(
event.getMouseEventDetails());

}
});
return chart;
}

@Override
protected void setup() {
super.setup();
addComponent(mouseDetails);
mouseDetails.setId(MOUSE_DETAILS);
}

protected void updateMouseEventDetailsForLegendClick(
MouseEventDetails mouseEventDetails) {

StringBuilder sb = new StringBuilder();

sb.append("AbsX: " + mouseEventDetails.getAbsoluteX());
sb.append(" ").append("AbsY: " + mouseEventDetails.getAbsoluteY());
sb.append(" ").append("RelX: " + mouseEventDetails.getxValue());
sb.append(" ").append("RelY: " + mouseEventDetails.getyValue());
sb.append(" ").append("Bttn: " + mouseEventDetails.getButtonName());
sb.append(" ").append("Alt: " + mouseEventDetails.isAltKey());
sb.append(" ").append("Ctrl: " + mouseEventDetails.isCtrlKey());
sb.append(" ").append("Meta: " + mouseEventDetails.isMetaKey());
sb.append(" ").append("Shift: " + mouseEventDetails.isShiftKey());

mouseDetails.setValue(sb.toString());

}

}
Loading

0 comments on commit f279440

Please sign in to comment.