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

DO NOT MERGE: Add optional title feature to ChartModule #1666

Closed
wants to merge 1 commit 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
32 changes: 32 additions & 0 deletions docs/issue_954.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
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 https://github.com/projectmesa/mesa-examples/blob/main/examples/charts/charts/server.py
for testing.

7 changes: 5 additions & 2 deletions mesa/visualization/modules/ChartVisualization.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ def __init__(
series,
canvas_height=200,
canvas_width=500,
title="",
data_collector_name="datacollector",
):
"""
Expand All @@ -67,8 +68,10 @@ 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 @@
const ChartModule = function (series, canvas_width, canvas_height) {
const ChartModule = function (series, canvas_width, canvas_height, title) {
const canvas = document.createElement("canvas");
Object.assign(canvas, {
width: canvas_width,
Expand Down Expand Up @@ -47,6 +47,10 @@ const ChartModule = function (series, canvas_width, canvas_height) {
mode: "index",
intersect: false,
},
title: {
display: true,
text: title
},
hover: {
mode: "nearest",
intersect: true,
Expand Down