Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added optional title feature to ChartModule #955

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions docs/issue_954.rst
Original file line number Diff line number Diff line change
@@ -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 \mesa\examples\charts\charts\server.py for testing.

2 changes: 1 addition & 1 deletion examples/charts/charts/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
5 changes: 3 additions & 2 deletions mesa/visualization/modules/ChartVisualization.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ def __init__(
series,
canvas_height=200,
canvas_width=500,
title="",
data_collector_name="datacollector",
):
"""
Expand All @@ -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):
Expand Down
6 changes: 5 additions & 1 deletion mesa/visualization/templates/js/ChartModule.js
Original file line number Diff line number Diff line change
@@ -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 = "<canvas width='" + canvas_width + "' height='" + canvas_height + "' ";
canvas_tag += "style='border:1px dotted'></canvas>";
Expand Down Expand Up @@ -45,6 +45,10 @@ var ChartModule = function(series, canvas_width, canvas_height) {
mode: 'index',
intersect: false
},
title: {
display: true,
text: title
},
hover: {
mode: 'nearest',
intersect: true
Expand Down