This evening I learned how to use the pycha module to create various office charts. This is very simple. In this article, I have used seven charts.
This module and the API address are: Release.
I. Code:
#! /Usr/bin/ENV Python #-*-coding: UTF-8-*-import Cairo import pycha. pieimport pycha. barimport pycha. scatterimport pycha. stackedbarimport pycha. line # Set the canvas def set_charvalue (): width, height = 600,600 surface = Cairo. imagesurface (Cairo. format_argb32, width, height) return surface # Pie image def draw_pie (surface, options, dataset): Chart = pycha. pie. piechart (surface, options) chart. adddataset (Dataset) chart. render () surface. write_to_png ('d: \ pie.png ') # vertical histogram def draw_vertical_bar (surface, options, dataset): Chart = pycha. bar. verticalbarchart (surface, options) chart. adddataset (Dataset) chart. render () surface. write_to_png ('d: \ vertical_bar.png ') # Vertical Horizontal histogram def draw_horizontal_bar (surface, options, dataset): Chart = pycha. bar. horizontalbarchart (surface, options) chart. adddataset (Dataset) chart. render () surface. write_to_png ('d: \ horizontal_bar.png ') # line chart def draw_line (surface, options, dataset): Chart = pycha. line. linechart (surface, options) chart. adddataset (Dataset) chart. render () surface. write_to_png ('d: \ line.png ') # Point Graph def draw_scatterplot (surface, options, dataset): Chart = pycha. scatter. scatterplotchart (surface, options) chart. adddataset (Dataset) chart. render () surface. write_to_png ('d: \ scatterplotchart.png ') # Vertical Block image def draw_stackedverticalbarchar (surface, options, dataset): Chart = pycha. stackedbar. stackedverticalbarchart (surface, options) chart. adddataset (Dataset) chart. render () surface. write_to_png ('d: \ stackedverticalbarchart.png ') # horizontal block graph def draw_stackedhorizontalbarchart (surface, options, dataset): Chart = pycha. stackedbar. stackedhorizontalbarchart (surface, options) chart. adddataset (Dataset) chart. render () surface. write_to_png ('d: \ stackedhorizontalbarchart.png ') If _ name _ =' _ main _ ': ''' function: Use pycha to draw various charts. input: none output: None Author: Socrates blog: http://blog.csdn.net/dyx1024 Date: 2012-02-28 ''' # data source dataset = ('iphone ', (), (2, 2.5), ('htc ', (), ('hw ,), (2, 0.5), ('zte', (1.5), (,), (2 ))),) # image attribute definition Options = {'gend': {'hide ': false}, 'title': 'mobile phone sales distribution chart (by dyx1024)', 'titlecolor ': '# 0000ff', 'titlefont': 'font', 'background': {'chartcolor':' # ffff'}, 'axis ': {'labelcolor ': '# ff000000'},} surface = set_charvalue () # Call different functions to draw graphs of different shapes as needed # draw_pie (surface, options, dataset) # draw_vertical_bar (surface, options, dataset) # draw_horizontal_bar (surface, options, dataset) # draw_scatterplot (surface, options, dataset) # draw_stackedverticalbarchar (surface, options, dataset) # surface, options, dataset) draw_line (surface, options, dataset)
Ii. test:
1. Function draw_pie (surface, options, dataset ):
2. Function draw_vertical_bar (surface, options, dataset ):
3. Function draw_horizontal_bar (surface, options, dataset ):
4. Function draw_line (surface, options, dataset ):
5. Function draw_scatterplot (surface, options, dataset ):
6. Function draw_stackedverticalbarchar (surface, options, dataset ):
7. Function draw_stackedhorizontalbarchart (surface, options, dataset ):