public class ChartComponent extends Component
The top level component for displaying charts
The charts
package enables Codename One developers to add charts
and visualizations to their apps without having to include external libraries
or embedding web views. We also wanted to harness the new features in the
graphics pipeline to maximize performance.
com.codename1.charts,ChartComponent
class includes optional pinch
zoom support.com.codename1.charts,ChartComponent
class includes optional support
for panning.
The com.codename1.charts
package includes models and renderers
for many different types of charts. It is also extensible so that you can add
your own chart types if required. The following screen shots demonstrate a
small sampling of the types of charts that can be created.
The above screenshots were taken from the ChartsDemo app. Y ou can start playing with this app by checking it out from our git repository. |
Adding a chart to your app involves four steps:
com.codename1.charts.models
package. Essentially, this is just
where you add the data that you want to display.com.codename1.charts.renderers
package. The renderer allows you
to specify how the chart should look. E.g. the colors, fonts, styles, to use.
com.codename1.charts.views
package.
com.codename1.charts,ChartComponent
.
In order to add your chart to the UI, you need to wrap it in a
com.codename1.charts,ChartComponent
object.You can check out the ChartsDemo app for specific examples, but here is a high level view of some code that creates a Pie Chart.
The charts package is derived work from the excellent open source aChartEngine API.
BASELINE, BOTTOM, BRB_CENTER_OFFSET, BRB_CONSTANT_ASCENT, BRB_CONSTANT_DESCENT, BRB_OTHER, CENTER, CROSSHAIR_CURSOR, DEFAULT_CURSOR, DRAG_REGION_IMMEDIATELY_DRAG_X, DRAG_REGION_IMMEDIATELY_DRAG_XY, DRAG_REGION_IMMEDIATELY_DRAG_Y, DRAG_REGION_LIKELY_DRAG_X, DRAG_REGION_LIKELY_DRAG_XY, DRAG_REGION_LIKELY_DRAG_Y, DRAG_REGION_NOT_DRAGGABLE, DRAG_REGION_POSSIBLE_DRAG_X, DRAG_REGION_POSSIBLE_DRAG_XY, DRAG_REGION_POSSIBLE_DRAG_Y, E_RESIZE_CURSOR, HAND_CURSOR, LEFT, MOVE_CURSOR, N_RESIZE_CURSOR, NE_RESIZE_CURSOR, NW_RESIZE_CURSOR, RIGHT, S_RESIZE_CURSOR, SE_RESIZE_CURSOR, SW_RESIZE_CURSOR, TEXT_CURSOR, TOP, W_RESIZE_CURSOR, WAIT_CURSOR
Constructor and Description |
---|
ChartComponent(AbstractChart chart)
Creates a new chart component to display the provided chart.
|
Modifier and Type | Method and Description |
---|---|
protected Dimension |
calcPreferredSize()
Calculates the preferred size based on component content.
|
protected void |
chartBoundsChanged()
Subclasses can override this method to be informed when the chart bounds change
due to panning or zooming.
|
Point |
chartToScreenCoord(int x,
int y)
Returns the screen position from a chart coordinate
|
Shape |
chartToScreenShape(Shape s)
Converts a screen coordinate spaced shape to the same shape in the chart coordinate space
|
void |
clearPanLimits()
Removes the pan limits which may have been previously set with
setPanLimits(double, double, double, double) |
AbstractChart |
getChart()
Gets the chart that is being displayed in this component.
|
Transform |
getTransform()
Gets the transform for the chart.
|
boolean |
isPanEnabled()
Checks if panning is enabled.
|
boolean |
isPanXEnabled()
Checks whether panning is enabled along the X-axis.
|
boolean |
isPanYEnabled()
Checks whether panning is enabled along the Y-axis.
|
boolean |
isZoomEnabled()
Checks whether zoom is enabled.
|
boolean |
isZoomXEnabled()
Checks whether zoom is enabled on the X-axis.
|
boolean |
isZoomYEnabled()
Checks whether zoom is enabled on the Y-axis.
|
void |
paint(Graphics g)
Paints the chart.
|
void |
pointerDragged(int[] x,
int[] y)
If this Component is focused, the pointer dragged event
will call this method
|
void |
pointerPressed(int x,
int y)
If this Component is focused, the pointer pressed event
will call this method
|
void |
pointerReleased(int x,
int y)
If this Component is focused, the pointer released event
will call this method
|
Point |
screenToChartCoord(int x,
int y)
Converts screen coordinates to chart coordinates.
|
Shape |
screenToChartShape(Shape s)
Converts a chart coordinate spaced shape to the same shape in the screen coordinate space
|
protected void |
seriesPressed(SeriesSelection sel)
Called when a pointer is pressed on a series in the chart.
|
protected void |
seriesReleased(SeriesSelection sel)
Called when a pointer is released from a series in the chart.
|
void |
setChart(AbstractChart chart)
Sets the chart to be displayed in this component.
|
void |
setPanEnabled(boolean panEnabled) |
void |
setPanEnabled(boolean panXEnabled,
boolean panYEnabled)
Enables or disables pan on x and y axes separately.
|
void |
setPanLimits(double minX,
double maxX,
double minY,
double maxY)
Sets the pan limits if panning is enabled.
|
void |
setTransform(Transform transform)
Sets the transform for the chart.
|
void |
setZoomEnabled(boolean zoomEnabled)
Enables or disables zoom on both x and y axes.
|
void |
setZoomEnabled(boolean zoomX,
boolean zoomY)
Enables or disables zoom on x and y axes separately.
|
void |
setZoomLimits(double minRangeX,
double maxRangeX,
double minRangeY,
double maxRangeY)
Sets the zoom limits.
|
void |
zoomTo(double minX,
double maxX,
double minY,
double maxY,
int duration)
Zooms the chart in an animated fashion to the specified axis ranges.
|
void |
zoomToShapeInChartCoords(Shape s)
Zooms the view port to show a specified shape.
|
void |
zoomToShapeInChartCoords(Shape s,
int duration)
Zooms the view port to show a specified shape.
|
addDragFinishedListener, addDragOverListener, addDropListener, addFocusListener, addLongPressListener, addPointerDraggedListener, addPointerPressedListener, addPointerReleasedListener, addPullToRefresh, addScrollListener, addStateChangeListener, animate, bindProperty, blocksSideSwipe, calcScrollSize, cancelRepaints, clearClientProperties, contains, containsOrOwns, createStyleAnimation, deinitialize, deinitializeCustomStyle, dragEnter, dragExit, dragFinished, draggingOver, dragInitiated, drawDraggedImage, drop, fireClicked, focusGained, focusLost, getAbsoluteX, getAbsoluteY, getAllStyles, getAnimationManager, getBaseline, getBaselineResizeBehavior, getBindablePropertyNames, getBindablePropertyTypes, getBorder, getBottomGap, getBoundPropertyValue, getBounds, getBounds, getClientProperty, getCloudBoundProperty, getCloudDestinationProperty, getComponentForm, getComponentState, getCursor, getDefaultDragTransparency, getDirtyRegion, getDisabledStyle, getDraggedx, getDraggedy, getDragImage, getDragRegionStatus, getDragSpeed, getDragTransparency, getEditingDelegate, getGridPosX, getGridPosY, getHeight, getInlineAllStyles, getInlineDisabledStyles, getInlinePressedStyles, getInlineSelectedStyles, getInlineStylesTheme, getInlineUnselectedStyles, getInnerHeight, getInnerPreferredH, getInnerPreferredW, getInnerWidth, getInnerX, getInnerY, getLabelForComponent, getName, getNativeOverlay, getNextFocusDown, getNextFocusLeft, getNextFocusRight, getNextFocusUp, getOuterHeight, getOuterPreferredH, getOuterPreferredW, getOuterWidth, getOuterX, getOuterY, getOwner, getParent, getPreferredH, getPreferredSize, getPreferredSizeStr, getPreferredTabIndex, getPreferredW, getPressedStyle, getPropertyNames, getPropertyTypeNames, getPropertyTypes, getPropertyValue, getSameHeight, getSameWidth, getScrollable, getScrollAnimationSpeed, getScrollDimension, getScrollOpacity, getScrollOpacityChangeSpeed, getScrollX, getScrollY, getSelectCommandText, getSelectedRect, getSelectedStyle, getSideGap, getStyle, getTabIndex, getTensileLength, getTextSelectionSupport, getTooltip, getUIID, getUIManager, getUnselectedStyle, getVisibleBounds, getVisibleBounds, getWidth, getX, getY, growShrink, handlesInput, hasFixedPreferredSize, hasFocus, hideNativeOverlay, initComponent, initCustomStyle, initDisabledStyle, initLaf, initPressedStyle, initSelectedStyle, initUnselectedStyle, installDefaultPainter, isAlwaysTensile, isBlockLead, isCellRenderer, isChildOf, isDragActivated, isDragAndDropOperation, isDraggable, isDragRegion, isDropTarget, isEditable, isEditing, isEnabled, isFlatten, isFocusable, isGrabsPointerEvents, isHidden, isHidden, isHideInLandscape, isHideInPortrait, isIgnorePointerEvents, isInClippingRegion, isInitialized, isOpaque, isOwnedBy, isPinchBlocksDragAndDrop, isRippleEffect, isRTL, isScrollable, isScrollableX, isScrollableY, isScrollVisible, isSelectableInteraction, isSetCursorSupported, isSmoothScrolling, isSnapToGrid, isStickyDrag, isTactileTouch, isTactileTouch, isTensileDragEnabled, isTraversable, isVisible, keyPressed, keyReleased, keyRepeated, laidOut, longKeyPress, longPointerPress, onScrollX, onScrollY, paintBackground, paintBackgrounds, paintBorder, paintBorderBackground, paintComponent, paintComponent, paintIntersectingComponentsAbove, paintLock, paintLockRelease, paintRippleOverlay, paintScrollbars, paintScrollbarX, paintScrollbarY, paintShadows, paramString, parsePreferredSize, pinch, pinch, pinchReleased, pointerDragged, pointerHover, pointerHoverPressed, pointerHoverReleased, pointerPressed, pointerReleased, putClientProperty, refreshTheme, refreshTheme, refreshTheme, remove, removeDragFinishedListener, removeDragOverListener, removeDropListener, removeFocusListener, removeLongPressListener, removePointerDraggedListener, removePointerPressedListener, removePointerReleasedListener, removeScrollListener, removeStateChangeListener, repaint, repaint, requestFocus, resetFocusable, respondsToPointerEvents, scrollRectToVisible, scrollRectToVisible, setAlwaysTensile, setBlockLead, setBoundPropertyValue, setCellRenderer, setCloudBoundProperty, setCloudDestinationProperty, setComponentState, setCursor, setDefaultDragTransparency, setDirtyRegion, setDisabledStyle, setDraggable, setDragTransparency, setDropTarget, setEditingDelegate, setEnabled, setFlatten, setFocus, setFocusable, setGrabsPointerEvents, setHandlesInput, setHeight, setHidden, setHidden, setHideInLandscape, setHideInPortrait, setIgnorePointerEvents, setInitialized, setInlineAllStyles, setInlineDisabledStyles, setInlinePressedStyles, setInlineSelectedStyles, setInlineStylesTheme, setInlineUnselectedStyles, setIsScrollVisible, setLabelForComponent, setName, setNextFocusDown, setNextFocusLeft, setNextFocusRight, setNextFocusUp, setOpaque, setOwner, setPinchBlocksDragAndDrop, setPreferredH, setPreferredSize, setPreferredSizeStr, setPreferredTabIndex, setPreferredW, setPressedStyle, setPropertyValue, setRippleEffect, setRTL, setSameHeight, setSameSize, setSameWidth, setScrollAnimationSpeed, setScrollOpacityChangeSpeed, setScrollSize, setScrollVisible, setScrollX, setScrollY, setSelectCommandText, setSelectedStyle, setShouldCalcPreferredSize, setSize, setSmoothScrolling, setSnapToGrid, setTabIndex, setTactileTouch, setTensileDragEnabled, setTensileLength, setTooltip, setTraversable, setUIID, setUIID, setUnselectedStyle, setVisible, setWidth, setX, setY, shouldBlockSideSwipe, shouldBlockSideSwipeLeft, shouldBlockSideSwipeRight, shouldRenderComponentSelection, showNativeOverlay, startEditingAsync, stopEditing, stripMarginAndPadding, styleChanged, toImage, toString, unbindProperty, updateNativeOverlay, visibleBoundsContains
public ChartComponent(AbstractChart chart)
chart
- The chart to be displayed in this component.public AbstractChart getChart()
protected Dimension calcPreferredSize()
Component
calcPreferredSize
in class Component
public void setChart(AbstractChart chart)
chart
- public void paint(Graphics g)
public Point screenToChartCoord(int x, int y)
x
- screen x positiony
- screen y positionpublic Point chartToScreenCoord(int x, int y)
x
- the x position within the charty
- the y position within the chartpublic Shape screenToChartShape(Shape s)
s
- shape in screen coordinatespublic Shape chartToScreenShape(Shape s)
s
- shape in chart coordinatespublic void zoomToShapeInChartCoords(Shape s)
s
- The shape that should be shown.public void zoomToShapeInChartCoords(Shape s, int duration)
s
- The shape that should be shown.duration
- The duration of the transition.zoomTo(double, double, double, double, int)
public void zoomTo(double minX, double maxX, double minY, double maxY, int duration)
zoomToShapeInChartCoords(com.codename1.ui.geom.Shape, int)
except
it allows you to specify coordinates as doubles.minX
- The lower bound of the X-axis after zoom.maxX
- The upper bound of the X-axis after zoom.minY
- The lower bound of the Y-axis after zoom.maxY
- THe upper bound of the Y-axis after zoom.duration
- Transition time (ms).public void pointerPressed(int x, int y)
Component
pointerPressed
in class Component
x
- the pointer x coordinatey
- the pointer y coordinateprotected void seriesPressed(SeriesSelection sel)
sel
- public void pointerReleased(int x, int y)
Component
pointerReleased
in class Component
x
- the pointer x coordinatey
- the pointer y coordinateprotected void seriesReleased(SeriesSelection sel)
sel
- public Transform getTransform()
public void setTransform(Transform transform)
transform
- the transform to setpublic void pointerDragged(int[] x, int[] y)
Component
pointerDragged
in class Component
x
- the pointer x coordinatey
- the pointer y coordinatepublic boolean isPanEnabled()
public void setPanEnabled(boolean panEnabled)
panEnabled
- the panEnabled to setpublic void setPanEnabled(boolean panXEnabled, boolean panYEnabled)
panXEnabled
- True to enable panning along the x-axis.panYEnabled
- True to enable panning along the y-axis.public boolean isPanXEnabled()
public boolean isPanYEnabled()
public void setPanLimits(double minX, double maxX, double minY, double maxY)
minX
- The minimum X-axis value for panning.maxX
- The maximum X-axis value for panning.minY
- The minimum Y-axis value for panning.maxY
- The maximum Y-axis value for panning.public void clearPanLimits()
setPanLimits(double, double, double, double)
public boolean isZoomEnabled()
public boolean isZoomXEnabled()
public boolean isZoomYEnabled()
public void setZoomEnabled(boolean zoomEnabled)
zoomEnabled
- the zoomEnabled to setpublic void setZoomLimits(double minRangeX, double maxRangeX, double minRangeY, double maxRangeY)
NOTE: This method is only applicable when showing an XYChart
It will throw a
RuntimeException if called while a different kind of chart is being shown.
minRangeX
- The minimum distance from XYMultipleSeriesRenderer.getXAxisMin()
to
XYMultipleSeriesRenderer.getXAxisMax()
that can be achieved by zooming in. 0 means no limit.maxRangeX
- The maximum distance from XYMultipleSeriesRenderer.getXAxisMin()
to
XYMultipleSeriesRenderer.getXAxisMax()
that can be achieved by zooming out. 0 means no limit.minRangeY
- The minimum distance from XYMultipleSeriesRenderer.getYAxisMin()
to
XYMultipleSeriesRenderer.getYAxisMax()
that can be achieved by zooming in. 0 means no limit.maxRangeY
- The maximum distance from XYMultipleSeriesRenderer.getYAxisMin()
to
XYMultipleSeriesRenderer.getYAxisMax()
that can be achieved by zooming out. 0 means no limit.public void setZoomEnabled(boolean zoomX, boolean zoomY)
zoomX
- True to enable zooming x axis.zoomY
- True to enable zooming y axis.protected void chartBoundsChanged()