KDE 4.7 PyKDE API Reference

- KDE's Python API

# KPlotWidget Class Reference

`from PyKDE4.kdeui import *`

Inherits: QFrame → QWidget → QObject

## Detailed Description

KPlotWidget

Generic data plotting widget.

Widget for drawing plots. The basic idea behind KPlotWidget is that you don't have to worry about any transformation from your data's natural units to screen pixel coordinates; this is handled internally by the widget.

Data to be plotted are represented by one or more instances of KPlotObject. KPlotObject contains a list of QPointFs to be plotted (again, in the data's natural units), as well as information about how the data are to be rendered in the plot (i.e., as separate points or connected by lines? With what color and point style? etc). See KPlotObject for more information.

KPlotWidget automatically adds axis labels with tickmarks and tick labels. These are encapsulated in the KPlotAxis class. All you have to do is set the limits of the plotting area in data units, and KPlotWidget wil figure out the optimal positions and labels for the tickmarks on the axes.

Example of usage:

    KPlotWidget *kpw = new KPlotWidget( parent );
    // setting our limits for the plot
    kpw->setLimits( 1.0, 5.0, 1.0, 25.0 );

    // creating a plot object whose points are connected by red lines ...
    KPlotObject *kpo = new KPlotObject( Qt.red, KPlotObject.Lines );
    // ... adding some points to it ...
    for ( float x = 1.0; x <= 5.0; x += 0.1 )
    kpo->addPoint( x, x*x );

    // ... and adding the object to the plot widget
    kpw->addPlotObject( kpo );

Note:
  KPlotWidget will take care of the objects added to it, so when clearing the objects list (eg with removeAllPlotObjects()) any previous reference to a KPlotObject already added to a KPlotWidget will be invalid.

Author:
  Jason Harris

Version: 1.1

## Enumerations
Axis | { LeftAxis, BottomAxis, RightAxis, TopAxis }
## Methods
| __init__ (self, QWidget parent=0)
| __init__ (self, KPlotWidget a0)
| addPlotObject (self, KPlotObject object)
| addPlotObjects (self, [KPlotObject] objects)
bool | antialiasing (self)
KPlotAxis | axis (self, KPlotWidget.Axis type)
QColor | backgroundColor (self)
int | bottomPadding (self)
| clearSecondaryLimits (self)
QRectF | dataRect (self)
| drawAxes (self, QPainter p)
bool | event (self, QEvent a0)
QColor | foregroundColor (self)
QColor | gridColor (self)
bool | isGridShown (self)
bool | isObjectToolTipShown (self)
int | leftPadding (self)
QPointF | mapToWidget (self, QPointF p)
| maskAlongLine (self, QPointF p1, QPointF p2, float value=1.0)
| maskRect (self, QRectF r, float value=1.0)
QSize | minimumSizeHint (self)
| paintEvent (self, QPaintEvent a0)
QRect | pixRect (self)
| placeLabel (self, QPainter painter, KPlotPoint pp)
[KPlotObject] | plotObjects (self)
[KPlotPoint] | pointsUnderPoint (self, QPoint p)
| removeAllPlotObjects (self)
| replacePlotObject (self, int i, KPlotObject o)
| resetPlot (self)
| resetPlotMask (self)
| resizeEvent (self, QResizeEvent a0)
int | rightPadding (self)
QRectF | secondaryDataRect (self)
| setAntialiasing (self, bool b)
| setBackgroundColor (self, QColor bg)
| setBottomPadding (self, int padding)
| setDefaultPaddings (self)
| setForegroundColor (self, QColor fg)
| setGridColor (self, QColor gc)
| setLeftPadding (self, int padding)
| setLimits (self, float x1, float x2, float y1, float y2)
| setObjectToolTipShown (self, bool show)
| setPixRect (self)
| setRightPadding (self, int padding)
| setSecondaryLimits (self, float x1, float x2, float y1, float y2)
| setShowGrid (self, bool show)
| setTopPadding (self, int padding)
QSize | sizeHint (self)
int | topPadding (self)

## Method Documentation

__init__ | ( | self, |
|  | QWidget | parent=0
| ) |  |  |

Constructor.

Parameters:

  | parent | the parent widget

__init__ | ( | self, |
|  | KPlotWidget | a0
| ) |  |  |

addPlotObject | ( | self, |
|  | KPlotObject | object
| ) |  |  |

Add an item to the list of KPlotObjects to be plotted.

Note:
  do not use this multiple time if many objects have to be added, addPlotObjects() is strongly suggested in this case

Parameters:

  | object | the KPlotObject to be added

addPlotObjects | ( | self, |
|  | [KPlotObject] | objects
| ) |  |  |

Add more than one KPlotObject at one time.

Parameters:

  | objects | the list of KPlotObjects to be added

bool antialiasing | ( |  | self ) |

Returns:
  whether the antialiasing is active Antialiasing is not active by default.

KPlotAxis axis | ( | self, |
|  | KPlotWidget.Axis | type
| ) |  |  |

Returns:
  the axis of the specified type, or 0 if no axis has been set.

@sa Axis

QColor backgroundColor | ( |  | self ) |

Returns:
  the background color of the plot.

The default color is black.

int bottomPadding | ( |  | self ) |

Returns:
  the number of pixels below the plot area. Padding values are set to -1 by default; if unchanged, this function will try to guess a good value, based on whether ticklabels and/or axis labels are to be drawn.

clearSecondaryLimits | ( |  | self ) |

Unset the secondary limits, so the top and right axes show the same tickmarks as the bottom and left axes (no tickmark labels will be drawn for the top and right axes in this case)

QRectF dataRect | ( |  | self ) |

Returns:
  the rectangle representing the boundaries of the current plot, in natural data units.

@sa setLimits()

drawAxes | ( | self, |
|  | QPainter | p
| ) |  |  |

Draws the plot axes and axis labels.

Internal:
  Internal use only; one should simply call update() to draw the widget with axes and all objects.

Parameters:

  | p | pointer to the painter on which we are drawing

bool event | ( | self, |
|  | QEvent | a0
| ) |  |  |

Generic event handler.

QColor foregroundColor | ( |  | self ) |

Returns:
  the foreground color, used for axes, tickmarks and associated labels.

The default color is white.

QColor gridColor | ( |  | self ) |

Returns:
  the grid color.

The default color is gray.

bool isGridShown | ( |  | self ) |

Returns:
  whether the grid lines are shown Grid lines are not shown by default.

bool isObjectToolTipShown | ( |  | self ) |

Returns:
  whether the tooltip for the point objects is shown. Tooltips are enabled by default.

int leftPadding | ( |  | self ) |

Returns:
  the number of pixels to the left of the plot area.

Padding values are set to -1 by default; if unchanged, this function will try to guess a good value, based on whether ticklabels and/or axis labels need to be drawn.

QPointF mapToWidget | ( | self, |
|  | QPointF | p
| ) |  |  |

Map a coordinate

Parameters:

  | p | from the data rect to the physical pixel rect. Used mainly when drawing.
  | p | the point to be converted, in natural data units

Returns:
  the coordinate in the pixel coordinate system

maskAlongLine | ( | self, |
|  | QPointF | p1,
|  | QPointF | p2,
|  | float | value=1.0
| ) |  |  |

Indicate that object labels should try to avoid the line joining the two given points (in pixel coordinates).

Note:
  You should not normally call this function directly. It is called by KPlotObject when lines are drawn in the plot.

Parameters:

  | p1 | the starting point for the line
  | p2 | the ending point for the line
  | value | Allows you to determine how strongly the line should be avoided. Larger values are avoided more strongly.

maskRect | ( | self, |
|  | QRectF | r,
|  | float | value=1.0
| ) |  |  |

Indicate that object labels should try to avoid the given rectangle in the plot. The rectangle is in pixel coordinates.

Note:
  You should not normally call this function directly. It is called by KPlotObject when points, bars and labels are drawn.

Parameters:

  | r | the rectangle defining the region in the plot that text labels should avoid (in pixel coordinates)
  | value | Allows you to determine how strongly the rectangle should be avoided. Larger values are avoided more strongly.

QSize minimumSizeHint | ( |  | self ) |

Returns:
  suggested minimum size for the plot widget

paintEvent | ( | self, |
|  | QPaintEvent | a0
| ) |  |  |

The paint event handler, executed when update() or repaint() is called.

QRect pixRect | ( |  | self ) |

Returns:
  the rectangle representing the boundaries of the current plot, in screen pixel units.

placeLabel | ( | self, |
|  | QPainter | painter,
|  | KPlotPoint | pp
| ) |  |  |

Place an object label optimally in the plot. This function will attempt to place the label as close as it can to the point to which the label belongs, while avoiding overlap with regions of the plot that have been masked.

Note:
  You should not normally call this function directly. It is called internally in KPlotObject.draw().

Parameters:

  | painter | Pointer to the painter on which to draw the label
  | pp | pointer to the KPlotPoint whose label is to be drawn.

[KPlotObject] plotObjects | ( |  | self ) |

Returns:
  the current list of plot objects

[KPlotPoint] pointsUnderPoint | ( | self, |
|  | QPoint | p
| ) |  |  |

Returns:
  a list of points in the plot which are within 4 pixels of the screen position given as an argument.

Parameters:

  | p | The screen position from which to check for plot points.

removeAllPlotObjects | ( |  | self ) |

Remove and delete all items from the list of KPlotObjects

replacePlotObject | ( | self, |
|  | int | i,
|  | KPlotObject | o
| ) |  |  |

Replace an item in the KPlotObject list.

Parameters:

  | i | the index of the item to be replaced
  | o | pointer to the replacement KPlotObject

resetPlot | ( |  | self ) |

Clear the object list, reset the data limits, and remove axis labels

resetPlotMask | ( |  | self ) |

Reset the mask used for non-overlapping labels so that all regions of the plot area are considered empty.

resizeEvent | ( | self, |
|  | QResizeEvent | a0
| ) |  |  |

The resize event handler, called when the widget is resized.

int rightPadding | ( |  | self ) |

Returns:
  the number of pixels to the right of the plot area. Padding values are set to -1 by default; if unchanged, this function will try to guess a good value, based on whether ticklabels and/or axis labels are to be drawn.

QRectF secondaryDataRect | ( |  | self ) |

Returns:
  the rectangle representing the boundaries of the secondary data limits, if they have been set. Otherwise, this function behaves the same as dataRect().

@sa setSecondaryLimits()

setAntialiasing | ( | self, |
|  | bool | b
| ) |  |  |

Toggle antialiased drawing.

Parameters:

  | b | if true, the plot graphics will be antialiased.

setBackgroundColor | ( | self, |
|  | QColor | bg
| ) |  |  |

Set the background color

Parameters:

  | bg | the new background color

setBottomPadding | ( | self, |
|  | int | padding
| ) |  |  |

Set the number of pixels below the plot area. Set this to -1 to revert to automatic determination of padding values.

setDefaultPaddings | ( |  | self ) |

Revert all four padding values to -1, so that they will be automatically determined.

setForegroundColor | ( | self, |
|  | QColor | fg
| ) |  |  |

Set the foreground color

Parameters:

  | fg | the new foreground color

setGridColor | ( | self, |
|  | QColor | gc
| ) |  |  |

Set the grid color

Parameters:

  | gc | the new grid color

setLeftPadding | ( | self, |
|  | int | padding
| ) |  |  |

Set the number of pixels to the left of the plot area. Set this to -1 to revert to automatic determination of padding values.

setLimits | ( | self, |
|  | float | x1,
|  | float | x2,
|  | float | y1,
|  | float | y2
| ) |  |  |

Set new data limits for the plot.

Parameters:

  | x1 | the minimum X value in data units
  | x2 | the maximum X value in data units
  | y1 | the minimum Y value in data units
  | y2 | the maximum Y value in data units

setObjectToolTipShown | ( | self, |
|  | bool | show
| ) |  |  |

Toggle the display of a tooltip for point objects.

Parameters:

  | show | whether show the tooltip.

@sa isObjectToolTipShown()

setPixRect | ( |  | self ) |

Synchronize the PixRect with the current widget size and padding settings.

setRightPadding | ( | self, |
|  | int | padding
| ) |  |  |

Set the number of pixels to the right of the plot area. Set this to -1 to revert to automatic determination of padding values.

setSecondaryLimits | ( | self, |
|  | float | x1,
|  | float | x2,
|  | float | y1,
|  | float | y2
| ) |  |  |

Reset the secondary data limits, which control the values displayed along the top and right axes.

All data points are *plotted* using the coordinates defined by setLimits(), so this function is only useful for showing alternate tickmark labels along the top and right edges. For example, if you were plotting temperature on the X-axis, you could use Centigrade units for the primary (bottom) axis, using setLimits( 0.0, 100.0, 0.0, 1.0 ). If you also wanted to show Farenheit units along the secondary (top) axis, you would additionally use setSecondaryLimits( 32.0, 212.0, 0.0, 1.0 ). The data added to the plot would have x-coordinates in Centigrade degrees.

Parameters:

  | x1 | the minimum X value in secondary data units
  | x2 | the maximum X value in secondary data units
  | y1 | the minimum Y value in secondary data units
  | y2 | the maximum Y value in secondary data units

@sa setLimits()

setShowGrid | ( | self, |
|  | bool | show
| ) |  |  |

Toggle whether grid lines are drawn at major tickmarks.

Parameters:

  | show | if true, grid lines will be drawn.

@sa isGridShown()

setTopPadding | ( | self, |
|  | int | padding
| ) |  |  |

Set the number of pixels above the plot area. Set this to -1 to revert to automatic determination of padding values.

QSize sizeHint | ( |  | self ) |

Returns:
  suggested size for the plot widget

int topPadding | ( |  | self ) |

Returns:
  the number of pixels above the plot area. Padding values are set to -1 by default; if unchanged, this function will try to guess a good value, based on whether ticklabels and/or axis labels are to be drawn.

## Enumeration Documentation

Axis

The four types of plot axes.

Enumerator:

  LeftAxis = 0 |
  BottomAxis |
  RightAxis |
  TopAxis |