{ "info": { "author": "Iridium IO, ImminentFate", "author_email": "iridium.io@outlook.com", "bugtrack_url": null, "classifiers": [ "Development Status :: 3 - Alpha", "Framework :: Django", "Intended Audience :: Developers", "License :: OSI Approved :: MIT License", "Natural Language :: English", "Operating System :: OS Independent", "Programming Language :: Python :: 3", "Programming Language :: Python :: 3.7", "Topic :: Software Development :: Libraries :: Python Modules" ], "description": "

\n\n \n\n

An easy to use, class-based approach to implementing Chart.js into Python projects.

\n\n-----\n\nInitially designed as a Django app, it is now self-contained and outputs chart data in JSON, meaning it can easily be used in: \n\n- Django\n- Flask\n- AJAX/Rest API requests\n- Other Python projects\n\n\n## Getting Started\n\nInstall with `pip`\n\n```sh\n> pip install pychart.js\n```\nYou will need to have `chart.js` or `chart.min.js` ready for use in your HTML document. The following is a drop-in CDN script to use:\n\n```HTML\n\n```\n\n## Basic Usage\n\n### 1. Chart Element in HTML and JS\nAs stated above, the output is a JSON object which can be used directly in any template. The following is an example HTML document with a simple chart element. Here, the chart object is going to be passed into the `{{ chartJSON | safe }}` tag. Note here that it has to be flagged as safe otherwise it will not work. \n\n```html\n\n\n\n\n\n```\n\n\n### 2. Python Code\nThe following is a minimal example of a chart you can generate and pass into your HTML using a Django view. \n\n\n\n\n
Click to see more:\n
Create Chart
\n\n```python\nfrom pychartjs import BaseChart, ChartType, Color \n\nclass MyBarGraph(BaseChart):\n\n type = ChartType.Bar\n\n class data:\n label = \"Numbers\"\n data = [12, 19, 3, 17, 10]\n backgroundColor = Color.Green\n```\n

\n
Update data label and use it in a Django View
\n\n```python\ndef homepage(request):\n\n NewChart = MyBarGraph()\n NewChart.data.label = \"My Favourite Numbers\" # can change data after creation\n\n ChartJSON = NewChart.get()\n\n return render(request=request,\n template_name='main/home.html',\n context={\"chartJSON\": ChartJSON})\n\n```\n
\n\n### 3. The Result\n\n\n\n\n## Extending With More Features\nThe above is what's achievable with minimal code but you can do almost anything the JS equivalent can do. Where a feature isn't implemented natively in ObjectiveChartJS, you can use a dictionary or list as per normal.\n\nHere's a skeleton of what can be used: \n\n```python\n\nclass MyChart(BaseChart):\n\n type = ChartType.Line\n\n class labels:\n # ...\n\n class data: \n # ...\n\n class options:\n # ...\n\n class pluginOptions:\n # ...\n\n```\n\n### Base Variables\n\n#### Chart Type\nCan use the variables in ChartType or can be entered directly\n\n```python\n\ntype = ChartType.Line #...from pychartjs import ChartType\ntype = 'Bar'\n\n```\n\n#### In-line JS\nCallbacks to Javascript functions or direct code can be implemented anywhere in the chart class as long as it is a string encapsulated within `<<>>`\n\n- note: Using this will render the output non-compliant to the JSON standard, and as such it likely will not work with AJAX/REST\n\n```html\n\ncallback = \"<>\"\ninlineJS = \"<>\"\n\n```\n\n-----\n## Subclasses\n\n\n\n### Labels Class\nUsed to define the labels used for each data item. If it is left blank, labels will be generated automatically from the first data collection. \n\nCan be any of: \n- A single list of strings for all labels\n- Independent variables for each label\n- (Planned) Select from pre-determined lists for common datasets, e.g. Days, Months\n\n```python\n\nclass labels:\n grouped = ['Mon', 'Tue', 'Wed']\n # or\n day1 = 'Mon'\n day2 = 'Tue'\n day3 = 'Wed'\n\n```\n\n\n### Data Class\nUsed to define data *or* datasets. If you only have one dataset, this can be defined directly in the class. Otherwise, use subclasses for each dataset. For each subclass, the name of the class is used as the label if one isn't specified. \n\nCan be either of: \n\n- A single dataset, defined directly as variables in the class\n- Multiple datasets, each with their own subclass. \n\nRules: \n\n- Must include a `data` variable of type `list`\n- Must not have functions/methods. These will not work due to the reference methods used internally. However, you can use in-line operators or call to a function *outside* the Chart class. You just can't define a function within the dataset class. \n- If you don't want a variable to be compiled, prefix it with an underscore, e.g. `_color`\n\n``` python\n\n#One Dataset:\nclass data: \n data = [12, 19, 3, 17, 10]\n label = \"Fruit Eaten\"\n backgroundColor = Color.Palette(Color.Green)\n borderColor = Color.Hex(0xA2E6B1FF)\n\n#Multiple Datasets:\nclass data: \n\n class Apples:\n data = [2, 8, 3, 3, 2]\n\n class Oranges:\n data = [2, 3, 0, 12, 1]\n label = \"Bananas\" # Overrides the generated label 'Oranges'\n```\n\n### Options Class\nDefine extended options here. Note however that plugin options get defined under their own heading, *not* in here (to avoid over-nesting). \nOptions can be defined either as dictionaries or by using the `Options.Generic()` object. builders for `Title`, `Legend`, `Legend Labels` and `Layout` are provided for convenience. \n\nCan include: \n\n- Top-level options as variables\n- Deeper options as dictionaries or `Options.Generic` objects\n- callbacks or javascript functions can be included if the variable is surrounded by `<< >>` tags\n\n```python\n\nclass options: \n\n # Object-based\n title = Options.Title(\"My Fruit Consumption\") \n animation = Options.Generic(duration=1000)\n\n _labels = Options.Legend_Labels(fonColor=Color.Gray, fullwidth=True)\n legend = Options.Legend(position='bottom', labels=_labels)\n\n\n # Dictionary-based\n title = {\"text\": \"My Fruit Consumption\", \"display\": True} \n animation = {\"duration\": 1000}\n hover = {\"animationDuration\": 500 }\n responsiveAnimationDuration = 0\n\n legend = {\n 'position': 'bottom', \n 'labels': {\n 'fontColor': Color.Gray, \n 'fullWidth': True\n }\n }\n\n```\n\n\n### Plugin Options Class\nUsed to define options for plugins. Could theorectically be included in the above options class, but has been split out here to reduce clutter.\n\nCan include: \n\n- One subclass per plugin (class name = plugin name)\n- To disable a plugin, no subclass is required; simply put `pluginName = False` at the top of the pluginOptions class\n\n```python\n\nclass pluginOptions:\n\n stacked100 = False # Disables the plugin 'stacked100'\n\n class colorSchemes: \n scheme = \"brewer.Paired12\"\n custom = \"<< customColorFunction >>\"\n\n```\n\n-----\n\n## Colors\nSome rudimentary color functions are provided to make generating charts and graphs easier. \n\n- All colors are returned in a formatted string `'rgba(R, G, B, A)'` regardless of input type\n - Color.Hex() accepts a string or a Hex Integer. \n - Color.RGBA() accepts either RGB or RGBA values.\n - Color.HSLA()\n - Color.HSVA()\n\n```python\n\nfrom pychartjs import Color\n\ncolor1 = Color.Magenta #22 basic colors available\ncolor2 = Color.Hex(\"#242424\")\ncolor3 = Color.Hex(0x242424FF)\ncolor4 = Color.RGBA(35, 22, 225)\ncolor5 = Color.RGBA(35, 22, 255, 1.0)\n\n>>> 'rgba(240, 50, 230, 1.0)'\n\n```\n\n### Color Palettes\nColor palettes can be generated using the `Color.Palette()` function. It returns a list of `rgba()` formatted colors which can be used directly in the chart. \n\n- `BaseColor` = Color to use as the generator for the palette. Must be a formatted string as above, which means it can accept any of the Color.X() functions as an input. \n- `n` = Number of colors to generate. Defaults to 5.\n- `generator` = Component to use to generate palette. Can be `'hue'`, `'saturation'`, `'lightness'` or `'alpha'`. Defaults to `saturation`\n\n\n```python\n\np1 = Color.Palette(Color.Red)\np2 = Color.Palette(Color.Hex(\"#432475\"), n=3, generator='lightness')\n\n>>> ['rgba(55, 30, 97, 1.0)', 'rgba(111, 60, 195, 1.0)', 'rgba(183, 157, 224, 1.0)']\n\n```\n\n### Color Gradients\nLinear and Radial gradients can be generated using the `Color.JSLinearGradient` and `Color.JSRadialGradient` objects. **Note that this returns an escaped javascript generator function.**\nConstruction can be done in a similar fashion to the javascript way as seen [here](https://www.w3schools.com/tags/canvas_createlineargradient.asp), or alternatively the whole gradient can be build in the constructor. \n\n- `chartContextName` = javascript chart context that you use in your HTML. Defaults to `'ctx'`. \n- `x1, y1, x2, y2` (Linear) = start and stop gradient coordinates\n- `x1, y1, r1, x2, y2, r2` (Radial) = start, stop and radii of the circles to generate the gradient between. \n- `*colorStops` = optional colorstops can be passed through as tuples to condense creation of gradients into one line\n\nStops can be added by calling the `addColorStop()` function\n\n\n\n
Click to see more examples:\n
Example One
\n\n```python\n#Define and apply color separately\n\n_color = Color.JSLinearGradient('ctx', 0, 0, 1000, 0) \n_color.addColorStop(0, Color.Blue)\n_color.addColorStop(1, Color.Green)\n\nbackgroundColor = _color.returnGradient()\n```\n
\n
Example Two
\n\n```python\n#Inline construction of gradient\n\nbackgroundColor = _color.JSLinearGradient('ctx2', 0, 0, 0, 1000,\n (0, Color.Red),\n (1, Color.Blue)\n ).returnGradient()\n```\n\n
Example Three
\n\n```python\n#Radial Gradient\n\nbackgroundColor = _color.JSRadialGradient('ctx2', 50, 50, 0, 50, 50, 100\n (0, Color.Red),\n (1, Color.Blue)\n ).returnGradient()\n```\n
\n\n-----\n\n## Putting it all together\nThe following is an example of a complex chart that can be created with many of the above features: \n\n```python\n\nclass MyChart(BaseChart):\n\n type = ChartType.Bar\n\n class labels:\n group = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']\n\n class data:\n\n class apples: \n data = [2, 8, 11, 7, 2, 4, 3]\n backgroundColor = Color.Palette(Color.Hex('#30EE8090'), 7, 'lightness')\n borderColor = Color.Green\n yAxisID = 'apples'\n\n class totalEnergy: \n label = \"Total Daily Energy Consumption (kJ)\"\n type = ChartType.Line\n data = [5665, 5612, 7566, 8763, 5176, 5751, 6546]\n backgroundColor = Color.RGBA(0,0,0,0)\n borderColor = Color.Purple\n yAxisID = 'totalenergy'\n\n class options: \n\n title = Options.Title(\"Apples I've eaten compared to total daily energy\")\n\n scales = {\n \"yAxes\": [\n {\"id\": \"apples\",\n \"ticks\": {\n \"beginAtZero\": True,\n \"callback\": \"<>\",\n }\n },\n {\"id\": \"totalenergy\",\n \"position\": \"right\",\n \"ticks\": {\"beginAtZero\": True}\n }\n ]\n }\n\n```\n\n### Output\n\n\n\n\n### Another example\n\n```python\n\nclass NewChart(BaseChart):\n\n type = ChartType.Line\n\n class labels:\n Years = list(range(2017, 2023))\n\n class data:\n class Whales:\n data = [80, 60, 100, 80, 90, 60]\n\n _color = Color.JSLinearGradient('ctx', 0, 0, 1000, 0)\n _color.addColorStop(0, Color.Green)\n _color.addColorStop(1, Color.Purple)\n\n borderColor = _color.returnGradient()\n fill = False\n pointBorderWidth = 10\n pointRadius = 3\n\n class Bears:\n data = [60, 50, 80, 120, 140, 180]\n borderColor = Color.JSLinearGradient('ctx', 0, 0, 1000, 0,\n (0, Color.Red), \n (1, Color.Magenta)\n ).returnGradient()\n fill = False\n pointBorderWidth = 10\n pointRadius = 3\n\n class Dolphins:\n data = [150, 80, 60, 30, 50, 30]\n borderColor = Color.JSLinearGradient('ctx', 0, 0, 1000, 0,\n (0, Color.Yellow), \n (1, Color.Orange)\n ).returnGradient()\n fill = False\n pointBorderWidth = 10\n pointRadius = 3\n\n class options:\n\n title = Options.Title(text=\"Wildlife Populations\", fontSize=18)\n\n _lables = Options.Legend_Labels(fontColor=Color.Gray, fullWidth=True)\n legend = Options.Legend(position='Bottom', labels=_lables)\n\n _yAxes = [Options.General(ticks=Options.General(beginAtZero=True, padding=15, max=200))]\n\n scales = Options.General(yAxes=_yAxes)\n\n```\n### Output\n\n\n\n-----\n\n## More Examples\nCan be found [here](https://github.com/IridiumIO/pyChart.js/wiki/Line-Charts)\n\n\n", "description_content_type": "text/markdown", "docs_url": null, "download_url": "", "downloads": { "last_day": -1, "last_month": -1, "last_week": -1 }, "home_page": "https://github.com/IridiumIO/pyChart.JS", "keywords": "django python objective class-based chart chartjs chart.js", "license": "", "maintainer": "", "maintainer_email": "", "name": "pyChart.JS", "package_url": "https://pypi.org/project/pyChart.JS/", "platform": "", "project_url": "https://pypi.org/project/pyChart.JS/", "project_urls": { "Homepage": "https://github.com/IridiumIO/pyChart.JS" }, "release_url": "https://pypi.org/project/pyChart.JS/0.3.0/", "requires_dist": null, "requires_python": ">=3.6", "summary": "A clean, class-based implementation of Chart.JS for Django, Flask and any other Python project", "version": "0.3.0", "yanked": false, "yanked_reason": null }, "last_serial": 6029487, "releases": { "0.1.0": [ { "comment_text": "", "digests": { "md5": "fd6efb884ab177388c4a3da7d7fe8224", "sha256": "42fdfa51236dec8f1367fe29b4a7687a280642bdff247697aae8fdaab6726d29" }, "downloads": -1, "filename": "pyChart.JS-0.1.0-py3-none-any.whl", "has_sig": false, "md5_digest": "fd6efb884ab177388c4a3da7d7fe8224", "packagetype": "bdist_wheel", "python_version": "py3", "requires_python": ">=3.0", "size": 9223, "upload_time": "2019-10-12T19:58:55", "upload_time_iso_8601": "2019-10-12T19:58:55.598781Z", "url": "https://files.pythonhosted.org/packages/75/0e/0d089b3b5b9274b070d64f4b3a754434eabe2713858d275bb0d4d6ab9513/pyChart.JS-0.1.0-py3-none-any.whl", "yanked": false, "yanked_reason": null }, { "comment_text": "", "digests": { "md5": "4ca3f1d2b723597fdb47813eb45ae2ed", "sha256": "ef026e615eb8aa9c65940b8013421c73be6baed5efb890c14af036d9e33f0675" }, "downloads": -1, "filename": "pyChart.JS-0.1.0.tar.gz", "has_sig": false, "md5_digest": "4ca3f1d2b723597fdb47813eb45ae2ed", "packagetype": "sdist", "python_version": "source", "requires_python": ">=3.0", "size": 8397, "upload_time": "2019-10-12T19:58:58", "upload_time_iso_8601": "2019-10-12T19:58:58.165378Z", "url": "https://files.pythonhosted.org/packages/ad/cf/76ee157d08777f6573ac163300e266e8e2beb1c828f4058b30d2ab58edea/pyChart.JS-0.1.0.tar.gz", "yanked": false, "yanked_reason": null } ], "0.2.0": [ { "comment_text": "", "digests": { "md5": "b4f51f5bde05bbac02a4f141247c6148", "sha256": "f82449f8c4d35372414a693d09567faac30f9f78ee60c804ccd59d4874092e75" }, "downloads": -1, "filename": "pyChart.JS-0.2.0-py3-none-any.whl", "has_sig": false, "md5_digest": "b4f51f5bde05bbac02a4f141247c6148", "packagetype": "bdist_wheel", "python_version": "py3", "requires_python": ">=3.0", "size": 11442, "upload_time": "2019-10-14T14:02:24", "upload_time_iso_8601": "2019-10-14T14:02:24.050805Z", "url": "https://files.pythonhosted.org/packages/3e/59/de1ce80ff43f09fb1ae414f793fdbf79a92be571ff8878cf434072e12ca0/pyChart.JS-0.2.0-py3-none-any.whl", "yanked": false, "yanked_reason": null }, { "comment_text": "", "digests": { "md5": "fbf5c7e747444c63b66d8e6ba258ebc5", "sha256": "2a230dc284f2665b21d3bf0320ae6096f4f329c9e2caf2f92ab927030dce031b" }, "downloads": -1, "filename": "pyChart.JS-0.2.0.tar.gz", "has_sig": false, "md5_digest": "fbf5c7e747444c63b66d8e6ba258ebc5", "packagetype": "sdist", "python_version": "source", "requires_python": ">=3.0", "size": 12538, "upload_time": "2019-10-14T14:02:26", "upload_time_iso_8601": "2019-10-14T14:02:26.000364Z", "url": "https://files.pythonhosted.org/packages/7e/ce/9aae3732d3cef38a0c06400aa104a19e28685a11b112f559a72b8a85a9b1/pyChart.JS-0.2.0.tar.gz", "yanked": false, "yanked_reason": null } ], "0.3.0": [ { "comment_text": "", "digests": { "md5": "9ec64c3f276f4a138560395f55e1da5b", "sha256": "5d9c135ccabceb86a556520401dd0660a1137da64e23a38d6703c3de0f96dd05" }, "downloads": -1, "filename": "pyChart.JS-0.3.0-py3-none-any.whl", "has_sig": false, "md5_digest": "9ec64c3f276f4a138560395f55e1da5b", "packagetype": "bdist_wheel", "python_version": "py3", "requires_python": ">=3.6", "size": 12620, "upload_time": "2019-10-25T13:09:20", "upload_time_iso_8601": "2019-10-25T13:09:20.819148Z", "url": "https://files.pythonhosted.org/packages/b1/c5/34e2c818ee1bebb7e12106f7064b0a0d09f2fea5604919f88a81086ff332/pyChart.JS-0.3.0-py3-none-any.whl", "yanked": false, "yanked_reason": null }, { "comment_text": "", "digests": { "md5": "23644bf5f72d6cfda8c5ce7eca0530b8", "sha256": "dc425aea72c4ab276799acf0837f7b45290147984ece1ede8bede6f1d46308ee" }, "downloads": -1, "filename": "pyChart.JS-0.3.0.tar.gz", "has_sig": false, "md5_digest": "23644bf5f72d6cfda8c5ce7eca0530b8", "packagetype": "sdist", "python_version": "source", "requires_python": ">=3.6", "size": 13469, "upload_time": "2019-10-25T13:09:22", "upload_time_iso_8601": "2019-10-25T13:09:22.567272Z", "url": "https://files.pythonhosted.org/packages/4d/ef/4a90f63495a2cc758bc46b6a47cfee7f28902088bd4ccf1418d101ac5baf/pyChart.JS-0.3.0.tar.gz", "yanked": false, "yanked_reason": null } ] }, "urls": [ { "comment_text": "", "digests": { "md5": "9ec64c3f276f4a138560395f55e1da5b", "sha256": "5d9c135ccabceb86a556520401dd0660a1137da64e23a38d6703c3de0f96dd05" }, "downloads": -1, "filename": "pyChart.JS-0.3.0-py3-none-any.whl", "has_sig": false, "md5_digest": "9ec64c3f276f4a138560395f55e1da5b", "packagetype": "bdist_wheel", "python_version": "py3", "requires_python": ">=3.6", "size": 12620, "upload_time": "2019-10-25T13:09:20", "upload_time_iso_8601": "2019-10-25T13:09:20.819148Z", "url": "https://files.pythonhosted.org/packages/b1/c5/34e2c818ee1bebb7e12106f7064b0a0d09f2fea5604919f88a81086ff332/pyChart.JS-0.3.0-py3-none-any.whl", "yanked": false, "yanked_reason": null }, { "comment_text": "", "digests": { "md5": "23644bf5f72d6cfda8c5ce7eca0530b8", "sha256": "dc425aea72c4ab276799acf0837f7b45290147984ece1ede8bede6f1d46308ee" }, "downloads": -1, "filename": "pyChart.JS-0.3.0.tar.gz", "has_sig": false, "md5_digest": "23644bf5f72d6cfda8c5ce7eca0530b8", "packagetype": "sdist", "python_version": "source", "requires_python": ">=3.6", "size": 13469, "upload_time": "2019-10-25T13:09:22", "upload_time_iso_8601": "2019-10-25T13:09:22.567272Z", "url": "https://files.pythonhosted.org/packages/4d/ef/4a90f63495a2cc758bc46b6a47cfee7f28902088bd4ccf1418d101ac5baf/pyChart.JS-0.3.0.tar.gz", "yanked": false, "yanked_reason": null } ], "vulnerabilities": [] }