package com.googlecode.gchart.gcharttestapp.client; import com.google.gwt.event.dom.client.ClickEvent; import com.google.gwt.event.dom.client.ClickHandler; import com.google.gwt.user.client.Event; import com.google.gwt.user.client.Window; import com.googlecode.gchart.client.GChart; /** * * This chart allows the user to delete points by clicking * on them, and to add points at the mouse location, by * clicking on empty space.
* * New points are inserted at the current insertion index, * which begins at 0 and is maintained at the position of * the last deleted, or just after the last inserted, point. *
* * Note: The equations contained in this example, * appropriately modified, would make very helpful * clientToModel (and, inverted, modelToClient) methods of * GChart's Axis-derived classes. If you get sick of typing * them in and decided to solve the problem of implementing * such generalized methods, it would be a big help to * the GChart project. All contributions gratefully * acknowledged and properly documented. * */ public class GChartExample22 extends GChart implements ClickHandler { int insertionPoint = 0; public void onClick(ClickEvent event) { if (null == getTouchedPoint()) { // add point at mouse // 1st, translate mouse client x,y coordinates into // pixel distances from upper-left of plot area: double xPx = Window.getScrollLeft() + Event.getCurrentEvent().getClientX() - getAbsoluteLeft() - getYAxis().getAxisLabelThickness() - getYAxis().getTickLabelThickness() - getYAxis().getTickLength(); double yPx = Window.getScrollTop() + Event.getCurrentEvent().getClientY() - getAbsoluteTop() - getChartTitleThickness(); // 2nd, transform those pixel offsets to model units: double x = getXAxis().getAxisMin()*(1-xPx/getXChartSize()) + getXAxis().getAxisMax()*(xPx/getXChartSize()); // note the pixelY-to-cartesianY min/max swap double y = getYAxis().getAxisMax()*(1-yPx/getYChartSize()) + getYAxis().getAxisMin()*(yPx/getYChartSize()); // add point, using the model units: getCurve().addPoint(insertionPoint++, x, y); } else { // delete the clicked on point insertionPoint = getTouchedCurve().getPointIndex( getTouchedPoint()); getTouchedCurve().removePoint(getTouchedPoint()); } update(); } GChartExample22() { setChartSize(300, 300); setBorderStyle("none"); setChartTitle("