|
Revision 175, 4.1 kB
(checked in by lgs, 3 years ago)
|
|
Allow to specify the font of the ticks. Based on a patch by ged_AT_openhex.org. Fixes #26
|
| Line | |
|---|
| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | |
|---|
| 15 | |
|---|
| 16 | |
|---|
| 17 | |
|---|
| 18 | import cairo |
|---|
| 19 | |
|---|
| 20 | from pycha.chart import DEFAULT_OPTIONS |
|---|
| 21 | from pycha.bar import HorizontalBarChart, VerticalBarChart |
|---|
| 22 | from pycha.line import LineChart |
|---|
| 23 | from pycha.pie import PieChart |
|---|
| 24 | from pycha.scatter import ScatterplotChart |
|---|
| 25 | from pycha.stackedbar import StackedVerticalBarChart, StackedHorizontalBarChart |
|---|
| 26 | |
|---|
| 27 | from chavier.gui import GUI |
|---|
| 28 | |
|---|
| 29 | |
|---|
| 30 | class App(object): |
|---|
| 31 | |
|---|
| 32 | CHART_TYPES = ( |
|---|
| 33 | VerticalBarChart, |
|---|
| 34 | HorizontalBarChart, |
|---|
| 35 | LineChart, |
|---|
| 36 | PieChart, |
|---|
| 37 | ScatterplotChart, |
|---|
| 38 | StackedVerticalBarChart, |
|---|
| 39 | StackedHorizontalBarChart, |
|---|
| 40 | ) |
|---|
| 41 | |
|---|
| 42 | (VERTICAL_BAR_TYPE, |
|---|
| 43 | HORIZONTAL_BAR_TYPE, |
|---|
| 44 | LINE_TYPE, |
|---|
| 45 | PIE_TYPE, |
|---|
| 46 | SCATTER_TYPE, |
|---|
| 47 | STACKED_VERTICAL_BAR_TYPE, |
|---|
| 48 | STACKED_HORIZONTAL_BAR_TYPE) = range(len(CHART_TYPES)) |
|---|
| 49 | |
|---|
| 50 | OPTIONS_TYPES = dict( |
|---|
| 51 | axis=dict( |
|---|
| 52 | lineWidth=float, |
|---|
| 53 | lineColor=str, |
|---|
| 54 | tickSize=float, |
|---|
| 55 | labelColor=str, |
|---|
| 56 | labelFont=str, |
|---|
| 57 | labelFontSize=int, |
|---|
| 58 | labelWidth=float, |
|---|
| 59 | tickFont=str, |
|---|
| 60 | tickFontSize=int, |
|---|
| 61 | x=dict( |
|---|
| 62 | hide=bool, |
|---|
| 63 | ticks=list, |
|---|
| 64 | tickCount=int, |
|---|
| 65 | tickPrecision=int, |
|---|
| 66 | range=list, |
|---|
| 67 | rotate=float, |
|---|
| 68 | label=unicode, |
|---|
| 69 | interval=int, |
|---|
| 70 | ), |
|---|
| 71 | y=dict( |
|---|
| 72 | hide=bool, |
|---|
| 73 | ticks=list, |
|---|
| 74 | tickCount=int, |
|---|
| 75 | tickPrecision=int, |
|---|
| 76 | range=list, |
|---|
| 77 | rotate=float, |
|---|
| 78 | label=unicode, |
|---|
| 79 | interval=int, |
|---|
| 80 | ), |
|---|
| 81 | ), |
|---|
| 82 | background=dict( |
|---|
| 83 | hide=bool, |
|---|
| 84 | baseColor=str, |
|---|
| 85 | chartColor=str, |
|---|
| 86 | lineColor=str, |
|---|
| 87 | lineWidth=float, |
|---|
| 88 | ), |
|---|
| 89 | legend=dict( |
|---|
| 90 | opacity=float, |
|---|
| 91 | borderColor=str, |
|---|
| 92 | hide=bool, |
|---|
| 93 | position=dict( |
|---|
| 94 | top=int, |
|---|
| 95 | left=int, |
|---|
| 96 | bottom=int, |
|---|
| 97 | right=int, |
|---|
| 98 | ) |
|---|
| 99 | ), |
|---|
| 100 | padding=dict( |
|---|
| 101 | left=int, |
|---|
| 102 | right=int, |
|---|
| 103 | top=int, |
|---|
| 104 | bottom=int, |
|---|
| 105 | ), |
|---|
| 106 | stroke=dict( |
|---|
| 107 | color=str, |
|---|
| 108 | hide=bool, |
|---|
| 109 | shadow=bool, |
|---|
| 110 | width=int, |
|---|
| 111 | ), |
|---|
| 112 | yvals=dict( |
|---|
| 113 | show=bool, |
|---|
| 114 | inside=bool, |
|---|
| 115 | fontSize=int, |
|---|
| 116 | fontColor=str |
|---|
| 117 | ), |
|---|
| 118 | fillOpacity=float, |
|---|
| 119 | shouldFill=bool, |
|---|
| 120 | barWidthFillFraction=float, |
|---|
| 121 | pieRadius=float, |
|---|
| 122 | colorScheme=dict( |
|---|
| 123 | name=str, |
|---|
| 124 | args=dict( |
|---|
| 125 | initialColor=str, |
|---|
| 126 | colors=list, |
|---|
| 127 | ), |
|---|
| 128 | ), |
|---|
| 129 | title=unicode, |
|---|
| 130 | titleFont=str, |
|---|
| 131 | titleFontSize=int, |
|---|
| 132 | ) |
|---|
| 133 | |
|---|
| 134 | def __init__(self): |
|---|
| 135 | self.gui = GUI(self) |
|---|
| 136 | |
|---|
| 137 | def run(self): |
|---|
| 138 | self.gui.run() |
|---|
| 139 | |
|---|
| 140 | def get_default_options(self): |
|---|
| 141 | return DEFAULT_OPTIONS |
|---|
| 142 | |
|---|
| 143 | def get_chart(self, datasets, options, chart_type, width, height): |
|---|
| 144 | surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, width, height) |
|---|
| 145 | chart_factory = self.CHART_TYPES[chart_type] |
|---|
| 146 | chart = chart_factory(surface, options) |
|---|
| 147 | chart.addDataset(datasets) |
|---|
| 148 | chart.render() |
|---|
| 149 | return chart |
|---|
| 150 | |
|---|
| 151 | |
|---|
| 152 | def main(): |
|---|
| 153 | app = App() |
|---|
| 154 | app.run() |
|---|
| 155 | return 0 |
|---|
| 156 | |
|---|
| 157 | if __name__ == '__main__': |
|---|
| 158 | main() |
|---|