From a2a15b59929c8e38b29684ae3c35fc0fe1ae38a6 Mon Sep 17 00:00:00 2001 From: anjalinair012 Date: Fri, 13 Nov 2020 02:56:27 +0530 Subject: [PATCH 1/2] Added optional title feature to ChartModule --- docs/issue_954.rst | 31 +++++++++++++++++++ examples/charts/charts/server.py | 2 +- .../modules/ChartVisualization.py | 5 +-- .../visualization/templates/js/ChartModule.js | 6 +++- 4 files changed, 40 insertions(+), 4 deletions(-) create mode 100644 docs/issue_954.rst diff --git a/docs/issue_954.rst b/docs/issue_954.rst new file mode 100644 index 00000000000..2fd9a438564 --- /dev/null +++ b/docs/issue_954.rst @@ -0,0 +1,31 @@ +issue: Optional title in ChartModule #954 + +mesa.visualization.modules package +================================== + +Submodules +---------- + +mesa.visualization.modules.ChartVisualization module +---------------------------------------------------- +Extra parameter "chart_title" added to ChartModule __init__() . + +mesa\visualization\templates\js\ChartModule.js +---------------------------------------------------- +ChartModule() signature modified to receive chart_title +js code to insert title added within chartOptions variable + +Using the new feature +---------------------------------------------------- +Within server class, add chart title as below to an existing ChartModule call: + +line_chart = ChartModule( + [ + {"Label": "Rich", "Color": RICH_COLOR}, + {"Label": "Poor", "Color": POOR_COLOR}, + {"Label": "Middle Class", "Color": MID_COLOR}, + ],chart_title = "line chart" +) + +Code block modified in C:\Users\anjal\Projects\Mesaissue\mesa\examples\charts\charts\server.py for testing. + diff --git a/examples/charts/charts/server.py b/examples/charts/charts/server.py index 5f40e34f998..a471ef97020 100644 --- a/examples/charts/charts/server.py +++ b/examples/charts/charts/server.py @@ -85,7 +85,7 @@ def person_portrayal(agent): {"Label": "Rich", "Color": RICH_COLOR}, {"Label": "Poor", "Color": POOR_COLOR}, {"Label": "Middle Class", "Color": MID_COLOR}, - ] + ], title="line chart" ) model_bar = BarChartModule( diff --git a/mesa/visualization/modules/ChartVisualization.py b/mesa/visualization/modules/ChartVisualization.py index 9da1f77f893..2900b3ffd3c 100644 --- a/mesa/visualization/modules/ChartVisualization.py +++ b/mesa/visualization/modules/ChartVisualization.py @@ -49,6 +49,7 @@ def __init__( series, canvas_height=200, canvas_width=500, + title="", data_collector_name="datacollector", ): """ @@ -68,8 +69,8 @@ def __init__( self.data_collector_name = data_collector_name series_json = json.dumps(self.series) - new_element = "new ChartModule({}, {}, {})" - new_element = new_element.format(series_json, canvas_width, canvas_height) + new_element = "new ChartModule({}, {}, {}, '{}')" + new_element = new_element.format(series_json, canvas_width, canvas_height, title) self.js_code = "elements.push(" + new_element + ");" def render(self, model): diff --git a/mesa/visualization/templates/js/ChartModule.js b/mesa/visualization/templates/js/ChartModule.js index 99c3935c7e6..51761b2e396 100644 --- a/mesa/visualization/templates/js/ChartModule.js +++ b/mesa/visualization/templates/js/ChartModule.js @@ -1,4 +1,4 @@ -var ChartModule = function(series, canvas_width, canvas_height) { +var ChartModule = function(series, canvas_width, canvas_height, title) { // Create the tag: var canvas_tag = " Date: Thu, 31 Dec 2020 11:07:18 +0530 Subject: [PATCH 2/2] Update issue_954.rst --- docs/issue_954.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/issue_954.rst b/docs/issue_954.rst index 2fd9a438564..03e97adf802 100644 --- a/docs/issue_954.rst +++ b/docs/issue_954.rst @@ -27,5 +27,5 @@ line_chart = ChartModule( ],chart_title = "line chart" ) -Code block modified in C:\Users\anjal\Projects\Mesaissue\mesa\examples\charts\charts\server.py for testing. +Code block modified in \mesa\examples\charts\charts\server.py for testing.