Node:coord, Next:, Previous:area, Up:area



Specifying the coordinate system

Attributes x_coord and y_coord specifies the coordinate systems for the area's X and Y axes. The values of these attributes must of type coord. PyChart provides three popular subclasses of coord. Both x_coord and y_coord defaults to linear_coord.T().

linear_coord.T
This class makes the coordinate system linear.
ar = area.T(x_coord = linear_coord.T(), y_coord = linear_coord.T(), ...)

log_coord.T
This class displays a logarithmic coordinate system.
category_coord.T
This class defines a "categorical" coordinate system, in which the axis only takes discrete values. This class takes two constructors: data (see chart_data) and column from which the axis values are extracted from the data. See the following example:

../demos/failure_annot.py

from pychart import *
import failure

can = canvas.default_canvas()
can.round_rectangle(line_style.gray70_dash1, None, -60, 140, 280, 180, 10)
can.show(200, 142, "/H{}/14{}text_box.T")

tb = text_box.T(text="/H{}/14{}axis.X", loc=(-80, 0), line_style = None)
tb.add_arrow((0, 25), "rm", arrow=arrow.fat1)
tb.draw()

tb = text_box.T(text="/H{}/14{}axis.Y", loc=(00, -45), line_style = None)
tb.add_arrow((30, 0), "ct", arrow=arrow.fat1)
tb.draw()

tb = text_box.T(text="/H{}/14{}legend.T", loc=(240, 100), line_style = None)
tb.add_arrow((250, 80), "cb", arrow=arrow.fat1)
tb.draw()

tb = text_box.T(text="/o{}/14{}plots", loc=(180, 100), line_style = None)
tb.add_arrow((170, 70), "lb", arrow=arrow.fat1)
tb.draw()

The output will look like the below: categbar.png

A coord.T object must implement three methods:

coord.T:get_data_range DMIN DMAX INTERVAL Method
This method should compute the minimum and maximum values that are to be displayed on the canvas. DMIN, DMAX are the minimum and maximum values found in the sample data given to the plots. INTERVAL is the value of the area's "tic_interval" attribute (see area), or None if the attribute is omitted by the user. This method should return tuple (MIN, MAX, INTERVAL), where MIN, MAX are the minimum and maximum values to be displayed, and INTERVAL is the interval with which label and tick lines are drawn.

coord.T:get_canvas_pos SIZE VAL RANGE Method
This method converts a data value (VAL) to canvas location. SELF is the coordinate object itself, SIZE is the size of the axis, either X or Y, and RANGE is the minimum and maximum sample values obtained by calling SELF.get_data_range.

coord.T:get_tics RANGE INTERVAL Method
This method should return the list of values (not canvas locations) at which labels and "tick" lines are drawn. Parameters RANGE and INTERVAL are the values returned by get_data_range.

You can create fancier coordinate systems by subtyping coord.T. The below example shows how you can create a chart with "zap" marks. zaptest.png

../demos/zaptest.py

from pychart import *

can = canvas.default_canvas()

class zap_y_coord(linear_coord.T):
    # Method get_data_range is inherited from linear_coord.T.
    def get_canvas_pos(self, size, val, min, max):
        # Zap Y values between 70 and 240.
        # Thus, Y axis will display 0..70 and 240..280, 110 points total.
        if val <= 70:
            return linear_coord.T.get_canvas_pos(self, size, val, 0, 110)
        elif val <= 240:
            return linear_coord.T.get_canvas_pos(self, size, 70, 0, 110)
        else:
            return linear_coord.T.get_canvas_pos(self, size, val - 170, 0, 110)
    def get_tics(self, min, max, interval):
        # Don't draw tick marks between 65 and 245.
        tics = linear_coord.T.get_tics(self, min, max, interval)
        newtics = []

        # XXX should be using for..if construction ..
        for item in tics:
            if item < 65 or item > 245:
                newtics.append(item)
        return newtics

theme.get_options()
data = [(10, 20, 30, 5), (20, 265, 33, 5),
        (30, 255, 30, 5), (40, 45, 51, 7), (50, 25, 27, 3)]

chart_object.set_defaults(area.T, size = (150, 120), y_range = (0, 280),
                          y_coord = zap_y_coord(),
                          x_coord = category_coord.T(data, 0))

chart_object.set_defaults(bar_plot.T, data = data)

ar = area.T(loc=(250,0),
            x_axis=axis.X(label="X label", format="/a-30{}%d"),
            y_axis=axis.Y(label="Y label", tic_interval=10))

bar_plot.fill_styles.reset();
plot1=bar_plot.T(label="foo", cluster=(0,3))
plot2=bar_plot.T(label="bar", hcol=2, cluster=(1,3))
plot3=bar_plot.T(label="baz", hcol=3, cluster=(2,3))

ar.add_plot(plot1, plot2, plot3)
ar.draw()
for x in (ar.x_pos(10) - 20,
          ar.x_pos(20)- 10,
          ar.x_pos(30) - 10):
    zap.zap_horizontally(can, line_style.default, fill_style.white,
                         x, ar.y_pos(65),
                         x+16, ar.y_pos(65) + 4,
                         4, 4)