From 232bf113252b8f6a38e9a3f917444869de55839b Mon Sep 17 00:00:00 2001 From: Karen Gonzalez Date: Tue, 23 Jul 2019 12:41:28 +0100 Subject: [PATCH 1/2] Modified Chart Module in order to add dinamically named properties. i.e borderWidth and pointRadius schelling_chart = ChartModule([{"label": "happy", "borderColor": "Black", "borderWidth": "3", "pointRadius": "0"}], data_collector_name="datacollector") --- mesa/visualization/modules/ChartVisualization.py | 16 ++++++++-------- mesa/visualization/templates/js/ChartModule.js | 6 +++--- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/mesa/visualization/modules/ChartVisualization.py b/mesa/visualization/modules/ChartVisualization.py index f3a039fc60d..96842306374 100644 --- a/mesa/visualization/modules/ChartVisualization.py +++ b/mesa/visualization/modules/ChartVisualization.py @@ -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 @@ -31,7 +31,7 @@ class ChartModule(VisualizationElement): Example: - schelling_chart = ChartModule([{"Label": "happy", "Color": "Black"}], + schelling_chart = ChartModule([{"label": "happy", "borderColor": "Black"}], data_collector_name="datacollector") TODO: @@ -39,7 +39,7 @@ class ChartModule(VisualizationElement): 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"] @@ -52,12 +52,12 @@ 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. """ - self.series = series + self.series = serieslabel self.canvas_height = canvas_height self.canvas_width = canvas_width self.data_collector_name = data_collector_name @@ -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): diff --git a/mesa/visualization/templates/js/ChartModule.js b/mesa/visualization/templates/js/ChartModule.js index 99c3935c7e6..6df96950b0e 100644 --- a/mesa/visualization/templates/js/ChartModule.js +++ b/mesa/visualization/templates/js/ChartModule.js @@ -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); } From d69ca6fe7ae5cbc7ebd661117167a49bd09fa50f Mon Sep 17 00:00:00 2001 From: Karen Gonzalez Date: Tue, 23 Jul 2019 12:41:28 +0100 Subject: [PATCH 2/2] Modified Chart Module in order to add dinamically named properties. Fixed variable name. --- mesa/visualization/modules/ChartVisualization.py | 14 +++++++------- mesa/visualization/templates/js/ChartModule.js | 6 +++--- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/mesa/visualization/modules/ChartVisualization.py b/mesa/visualization/modules/ChartVisualization.py index f3a039fc60d..875b53491cb 100644 --- a/mesa/visualization/modules/ChartVisualization.py +++ b/mesa/visualization/modules/ChartVisualization.py @@ -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 @@ -31,7 +31,7 @@ class ChartModule(VisualizationElement): Example: - schelling_chart = ChartModule([{"Label": "happy", "Color": "Black"}], + schelling_chart = ChartModule([{"label": "happy", "borderColor": "Black"}], data_collector_name="datacollector") TODO: @@ -39,7 +39,7 @@ class ChartModule(VisualizationElement): 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"] @@ -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. """ @@ -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): diff --git a/mesa/visualization/templates/js/ChartModule.js b/mesa/visualization/templates/js/ChartModule.js index 99c3935c7e6..6df96950b0e 100644 --- a/mesa/visualization/templates/js/ChartModule.js +++ b/mesa/visualization/templates/js/ChartModule.js @@ -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); }