|
| 1 | +<h3 align="left"><img src="https://cdn2.hubspot.net/hubfs/100006/images/super_logo_sb23.png" alt="SeleniumBase" width="290" /></h3> |
| 2 | + |
| 3 | +# 📊 Chart Maker 📊 |
| 4 | + |
| 5 | +SeleniumBase Chart Maker allows you to create HTML charts with Python.<br /> |
| 6 | +The HighCharts library is used for creating charts. |
| 7 | +(Currently only <b>pie charts</b> are supported.) |
| 8 | + |
| 9 | +**Here's a sample chart:** |
| 10 | + |
| 11 | +<a href="https://seleniumbase.io/other/chart_presentation.html"><img width="500" src="https://seleniumbase.io/other/sample_pie_chart.png" title="Screenshot"></a><br> |
| 12 | + |
| 13 | +([Click on the image for the actual chart](https://seleniumbase.io/other/chart_presentation.html)) |
| 14 | + |
| 15 | +Here's how to run the example (a chart in a presentation): |
| 16 | +``` |
| 17 | +cd examples/chart_maker |
| 18 | +pytest my_chart.py |
| 19 | +``` |
| 20 | + |
| 21 | + |
| 22 | +### Creating a new chart: |
| 23 | + |
| 24 | +```python |
| 25 | +self.create_pie_chart(chart_name=None, title="My Chart") |
| 26 | +""" Creates a JavaScript pie chart using "HighCharts". """ |
| 27 | +``` |
| 28 | + |
| 29 | +If creating multiple charts at the same time, you can pass the ``chart_name`` parameter to distinguish between different charts. |
| 30 | + |
| 31 | + |
| 32 | +### Adding a data point to a chart: |
| 33 | + |
| 34 | +```python |
| 35 | +self.add_data_point(label, value, color=None, chart_name=None): |
| 36 | +""" Add a data point to a SeleniumBase-generated chart. |
| 37 | + @Params |
| 38 | + label - The label name for the data point. |
| 39 | + value - The numeric value of the data point. |
| 40 | + color - The HTML color of the data point. |
| 41 | + Can be an RGB color. Eg: "#55ACDC". |
| 42 | + Can also be a named color. Eg: "Teal". |
| 43 | + chart_name - If creating multiple charts, |
| 44 | + use this to select which one. |
| 45 | +""" |
| 46 | +``` |
| 47 | + |
| 48 | + |
| 49 | +### Saving a chart to a file: |
| 50 | + |
| 51 | +```python |
| 52 | +self.save_chart(chart_name=None, filename=None): |
| 53 | +""" Saves a SeleniumBase-generated chart to a file for later use. |
| 54 | + @Params |
| 55 | + chart_name - If creating multiple charts at the same time, |
| 56 | + use this to select the one you wish to use. |
| 57 | + filename - The name of the HTML file that you wish to |
| 58 | + save the chart to. (filename must end in ".html") |
| 59 | +""" |
| 60 | +``` |
| 61 | + |
| 62 | +The full HTML of the chart is saved to the ``saved_charts/`` folder. |
| 63 | + |
| 64 | + |
| 65 | +### Extracting the HTML of a chart: |
| 66 | + |
| 67 | +```python |
| 68 | +self.extract_chart(chart_name=None): |
| 69 | +""" Extracts the HTML from a SeleniumBase-generated chart. |
| 70 | + @Params |
| 71 | + chart_name - If creating multiple charts at the same time, |
| 72 | + use this to select the one you wish to use. |
| 73 | +""" |
| 74 | +``` |
| 75 | + |
| 76 | + |
| 77 | +### Displaying a chart in the browser window: |
| 78 | + |
| 79 | +```python |
| 80 | +self.display_chart(chart_name=None, filename=None): |
| 81 | +""" Displays a SeleniumBase-generated chart in the browser window. |
| 82 | + @Params |
| 83 | + chart_name - If creating multiple charts at the same time, |
| 84 | + use this to select the one you wish to use. |
| 85 | + filename - The name of the HTML file that you wish to |
| 86 | + save the chart to. (filename must end in ".html") |
| 87 | +""" |
| 88 | +``` |
| 89 | + |
| 90 | +All methods have the optional ``chart_name`` argument, which is only needed if you're creating multiple charts at once. |
| 91 | + |
| 92 | + |
| 93 | +### Here's an example of using SeleniumBase Chart Maker: |
| 94 | + |
| 95 | +```python |
| 96 | +from seleniumbase import BaseCase |
| 97 | + |
| 98 | + |
| 99 | +class MyChartMakerClass(BaseCase): |
| 100 | + |
| 101 | + def test_chart_maker(self): |
| 102 | + self.create_pie_chart(title="Automated Tests") |
| 103 | + self.add_data_point("Passed", 7, color="#95d96f") |
| 104 | + self.add_data_point("Untested", 2, color="#eaeaea") |
| 105 | + self.add_data_point("Failed", 1, color="#f1888f") |
| 106 | + self.create_presentation() |
| 107 | + self.add_slide(self.extract_chart()) |
| 108 | + self.begin_presentation() |
| 109 | + |
| 110 | +``` |
| 111 | + |
| 112 | +#### This example is from [my_chart.py](https://github.com/seleniumbase/SeleniumBase/blob/master/examples/chart_maker/my_chart.py), which you can run from the ``examples/chart_maker`` folder with the following command: |
| 113 | + |
| 114 | +```bash |
| 115 | +pytest my_chart.py |
| 116 | +``` |
0 commit comments