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

Modified Chart Module in order to add dinamically named properties. #714

Closed
wants to merge 3 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
14 changes: 7 additions & 7 deletions mesa/visualization/modules/ChartVisualization.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ class ChartModule(VisualizationElement):

Attributes:
series: A list of dictionaries containing information on series to
plot. Each dictionary must contain (at least) the "Label" and
"Color" keys. The "Label" value must correspond to a
plot. Each dictionary must contain (at least) the "label" and
"borderColor" keys. The "label" value must correspond to a
model-level series collected by the model's DataCollector, and
"Color" must have a valid HTML color.
"borderColor" must have a valid HTML color.
canvas_height, canvas_width: The width and height to draw the chart on
the page, in pixels. Default to 200 x 500
data_collector_name: Name of the DataCollector object in the model to
Expand All @@ -31,15 +31,15 @@ class ChartModule(VisualizationElement):


Example:
schelling_chart = ChartModule([{"Label": "happy", "Color": "Black"}],
schelling_chart = ChartModule([{"label": "happy", "borderColor": "Black"}],
data_collector_name="datacollector")

TODO:
Have it be able to handle agent-level variables as well.

More Pythonic customization; in particular, have both series-level and
chart-level options settable in Python, and passed to the front-end
the same way that "Color" is currently.
the same way that "borderColor" is currently.

"""
package_includes = ["Chart.min.js", "ChartModule.js"]
Expand All @@ -52,7 +52,7 @@ def __init__(self, series, canvas_height=200, canvas_width=500,
Args:
series: A list of dictionaries containing series names and
HTML colors to chart them in, e.g.
[{"Label": "happy", "Color": "Black"},]
[{"label": "happy", "borderColor": "Black"},]
canvas_height, canvas_width: Size in pixels of the chart to draw.
data_collector_name: Name of the DataCollector to use.
"""
Expand All @@ -73,7 +73,7 @@ def render(self, model):
data_collector = getattr(model, self.data_collector_name)

for s in self.series:
name = s["Label"]
name = s["label"]
try:
val = data_collector.model_vars[name][-1] # Latest value
except (IndexError, KeyError):
Expand Down
6 changes: 3 additions & 3 deletions mesa/visualization/templates/js/ChartModule.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ var ChartModule = function(series, canvas_width, canvas_height) {
for (var i in series) {
var s = series[i];
var new_series = {
label: s.Label,
borderColor: s.Color,
backgroundColor: convertColorOpacity(s.Color),
data: []
};
for (var property in s){
new_series[property] = s[property];
}
datasets.push(new_series);
}

Expand Down