Package: pygame-chart
pygameChartIntroductionWelcome to pygameChart! This package provides a simple and flexible way to create charts and plots within a pygame application. With pygameChart, you can easily visualize data and display in your applications in an appealing way. Its structure to define figure parameters and charts is similar to matplotlib for ease of use.Support for line plots, bar charts, and scatter plots (more on the way)Customization options for chart appearanve and behaviorEasy integration within pygame event loop, therefore ability to change content during the game and/or app
I hope pygameChart will be useful addition to your pyhame projects. Continue reading for installation and use of the packageInstallationPlease consider project page on Github,https://github.com/enessafak/pygameChart.
The module requires python>=3.9.10 and pygame>=2.1.2QuickstartpygameChart provides a simple use (similar to matplotlib) to create figure, set properties (like title, label names, chart limits etc.) and add several charts. In this phase it supports line plot, scatter plot and bar charts. Also in this phase, it accepts x and y axis data as two separate lists. I will add support for accepting different datatypes like dictionary numpy arrays or pandas series and dataframes.Basic use of the package creates following result.Basic figure with line chartimportpygame,sysimportpygame_chartaspyc# pygame app for figure to runpygame.init()screen=pygame.display.set_mode((800,600))# Figure instance on screen with position and sizefigure=pyc.Figure(screen,50,50,700,500)whileTrue:events=pygame.event.get()foreventinevents:ifevent.type==pygame.QUIT:pygame.quit()sys.exit()# add a line chart. First argument "name" should be unique for every chartfigure.line('Chart1',[1,2,3,4,6,20,24],[3,5,7,2,7,9,1])# draw figure with specified propertiesfigure.draw()pygame.display.update()Figure with several charts and propertiesimportpygame,sysimportpygame_chartaspyc# pygame app for figure to runpygame.init()screen=pygame.display.set_mode((800,600))# Figure instance on screen with position and sizefigure=pyc.Figure(screen,50,50,700,500)whileTrue:events=pygame.event.get()foreventinevents:ifevent.type==pygame.QUIT:pygame.quit()sys.exit()# Add chart title with a titlefigure.add_title('pygameChart Example')# Add legend and gridlimesfigure.add_legend()figure.add_gridlines()# add a line chart. First argument "name" should be unique for every chartfigure.line('LineChart',[1,2,3,4,6,20,24],[3,5,7,2,7,9,1])figure.bar('BarChart',[1,2,3,4,10,18,24],[4,3,9,2,7,12,5])figure.scatter('ScatterChart',[-4,3,6,8,12,16,20],[2,4,12,5,6,3,8])# draw figure with specified propertiesfigure.draw()pygame.display.update()DocumentationThis documentation provides classes in pygameChart with relative methods and attributes. Default values for initiations are given as global variables and can be modified in settings.py.
Only class the end user should use is Figure class. For the rest, only relevant method or attributes will be mentioned.Figure classpygameChart.Figure(screen,x,y,width,height,bg_color=BG_COLOR)ParametersParameterTypeExplanationscreenpygame.displayMain display for figure to be drawnxnumberPosition of figure at x axisynumberPosition of figure at y axiswidthnumberWidth of the figureheightnumberHeight of the figurebg_colorRGB tuple (r,g,b)Background color of the figureMethodsset_xlim(xlim) : Sets xmin and xmax for all charts. Drawings out of these limits are unvisiblexlim : tuple: (xmin, xmax)set_ylim(ylim) : Sets ymin and ymax for all charts. Drawings out of these limits are unvisible \ylim : tuple: (ymin, ymax)add_title(title) : Adds chart title to the top of the figuretitle : string: chart titleadd_legend() : Adds legend to the bottom of the figureadd_yaxis_label(label) : Adds label for y-axislabel : string: y-axis labeladd_xaxis_label(label) : Adds label for x-axislabel : string: x-axis labeladd_gridlines() : Adds gridlines to the figureline(name, xdata, ydata, color=None, line_width=2) : Adds line chart to the figurename : string: Name of the chart. Name of each chart should be unique to be drawn.
Otherwise, former chart data is updated with the latterxdata : list: x-axis data. List must be all numbers or all stringsydata : list: y-axis data. List must be all numberscolor : tuple: RGB tuple (r,g,b). Default value chooses the next unused
color from settings.pyline_width : int: Width of the linebar(name, xdata, ydata, color=None, bar_width=None) : Adds bar chart to the figurename : string: Name of the chart. Name of each chart should be unique to be drawn.
Otherwise, former chart data is updated with the latterxdata : list: x-axis data. List must be all numbers or all stringsydata : list: y-axis data. List must be all numberscolor : tuple: RGB tuple (r,g,b). Default value chooses the next unused
color from settings.pybar_width : int: Width of the bar. If none, bar width is calculated according to
figure size and x data rangescatter(name, xdata, ydata, color=None, radius=3): Adds scatter chart to the figurename : string: Name of the chart. Name of each chart should be unique to be drawn.
Otherwise, former chart data is updated with the latterxdata : list: x-axis data. List must be all numbers or all stringsydata : list: y-axis data. List must be all numberscolor : tuple: RGB tuple (r,g,b). Default value chooses the next unused
color from settings.pyradius : int: Radius of the circledraw() : Draws the figure with set properties and charts. Final method to draw and show the figure on screenArea classBase object for figure areas: title, legend, x-y axis labels, x-y axis ticks and chart areapygameChart.Area(figure)AttributesAttributeTypeExplanationfigurepygame.Chart.FigureMain Figure objectxnumberPosition of area at x axis (rel. to figure)ynumberPosition of area at y axis (rel. to figure)widthnumberWidth of the areaheightnumberHeight of the areainnerxnumberInner x position for paddinginnerynumberInner x position for paddinginnerwidthnumberInner width for paddinginnerheightnumberInner height for paddingshowbooleandraw condition for the areaMethodsdraw_area() : Draws the area on figure background, with figure.bg_color filldraw_area_border(): Draws the area on figure background, with border, without fillTitle(Area) ClassClass for adding and writing chart titleLegend(Area) ClassClass for calculating legend position, content and writingyAxisLabel(Area) ClassClass for adding and writing y-axis labelxAxisLabel(Area) ClassClass for adding and writing x-axis labelyAxisTick(Area) ClassClass for calculating x-tick position, content and writingAttributesAttributeTypeExplanationyminnumberMin value for y-ticks. Might be different than chart data. Overwritten by Figure.ylimymaxnumberMin value for y-ticks. Might be different than chart data. Overwritten by Figure.ylimxAxisTick(Area) ClassClass for calculating y-tick position, content and writingAttributesAttributeTypeExplanationxminnumberMin value for xx-ticks. Might be different than chart data. Overwritten by Figure.xlimxmaxnumberMin value for x-ticks. Might be different than chart data. Overwritten by Figure.xlimChartArea(Area) ClassClass for calculating chart positioning and drawingAttributesAttributeTypeExplanationchartslistList of chart objects added to the figurechart_nameslistList of chart names added to the figurechart_marginintPadding for left and right borderxdata_typestrIndicates the type of xdata (number or string). If figure.xlim is set, it becomes number. Otherwise, it is determined by the first chart to be added. Charts with other type of xdata cannot be added latergridlinesbooleanIf gridlines will be added to the figureChartType ClassBase class for all chart typesLineChart(ChartType) ClassClass for keeping properties of line chart. Drawing is done within Figure.ChartAreaParametersParameterTypeExplanationnamestringName of the chartxdatalistx-axis data. List must be all numbers or all stringsydatalisty-axis data. List must be all numberscolortupleRGB tuple (r,g,b). Default value chooses the next unused color from settings.pyline_widthintWidth of the lineBarChart(ChartType) ClassClass for keeping properties of bar chart. Drawing is done within Figure.ChartAreaParametersParameterTypeExplanationnamestringName of the chartxdatalistx-axis data. List must be all numbers or all stringsydatalisty-axis data. List must be all numberscolortupleRGB tuple (r,g,b). Default value chooses the next unused color from settings.pybar_widthintWidth of the bar. If none, bar width is calculated according to figure size and x data rangeScatterChart(ChartType) ClassClass for keeping properties of scatter chart. Drawing is done within Figure.ChartAreaParametersParameterTypeExplanationnamestringName of the chartxdatalistx-axis data. List must be all numbers or all stringsydatalisty-axis data. List must be all numberscolortupleRGB tuple (r,g,b). Default value chooses the next unused color from settings.pyradiusintRadius of the circleLicenseThe MIT License (MIT)Copyright (c) 2023Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.