from .abstract_notation_builder import AbstractChartNotationBuilder
from .forest_plot_binding_notation import ForestPlotBinding
[docs]
class ForestPlot(AbstractChartNotationBuilder):
OPTS = dict(
labels = dict(
hr="HR",
lo="CI Low",
hi="CI High",
),
)
@property
def bindings(self):
return [
ForestPlotBinding(
measure="",
hr=self.opts["labels"]["hr"],
lo=self.opts["labels"]["lo"],
hi=self.opts["labels"]["hi"],
),
*self._bindings
]
[docs]
def build(self, params=None) -> dict:
base_schema = {
"$schema": "https://vega.github.io/schema/vega-lite/v5.json",
"data": {
"values": []
},
"title": {
"text": [],
"color": "#414042",
"subtitleColor": "#767777",
"align": "left",
"fontSize": 12,
"lineHeight": {"expr": "16"},
"subtitleFontSize": 11.5,
"limit": 650,
"subtitlePadding": 8,
"offset": 25
},
"spacing": {"row": -1},
"facet": {
"row": {
"field": "cohort",
"header": {
"labelAngle": 360,
"labelAlign": "left",
"labelOrient": "left",
"labelAnchor": "middle",
"labelFontWeight": "bold",
"labelFontSize": 10.5,
"labelColor": "#767777",
"labelLimit": 500,
"labelPadding": 25
},
"title": None,
"bandPosition": 0
}
},
"spec": {
"encoding": {
"y": {
"field": "measure",
"type": "nominal",
"scale": {"paddingInner": {"expr": "1.2"}},
"axis": {
"title": None,
"labelFontSize": 10,
"labelPadding": 200,
"labelColor": "#767777",
"labelAlign": "left",
"domain": False,
"ticks": False,
"grid": False
}
},
"x": {
"type": "quantitative",
"axis": {
"title": None,
"labelFontSize": 9,
"labelPadding": 6,
"labelColor": "#767777",
"gridOpacity": {
"condition": {"test": "datum.value % 1 != 0", "value": 0},
"value": 1
},
"gridDash": {
"condition": {
"test": "datum.value % 1 === 0 && datum.value !=1",
"value": [3, 3]
},
"value": []
},
"gridColor": {
"condition": {"test": "datum.value === 1", "value": "#40C6F2"},
"value": "#CCC"
}
}
}
},
"layer": [
{
"mark": {"type": "rule"},
"encoding": {
"x": {"field": "lo"},
"x2": {"field": "hi"},
"stroke": {"value": "#004C70"},
"strokeWidth": {"value": 1.5}
}
},
{
"mark": {
"type": "point",
"filled": True,
"stroke": "#004C70",
"opacity": 1
},
"encoding": {
"x": {"field": "hr"},
"color": {
"field": "measure",
"type": "nominal",
"scale": {"range": ["#004C70"]},
"legend": None
}
}
},
{
"mark": {
"type": "text",
"fontWeight": "bold",
"fontSize": {"expr": "10"},
"color": "#767777",
"baseline": "middle",
"align": "right",
"dx": 260,
"tooltip": True
},
"encoding": {"text": {"field": "hr"}}
},
{
"mark": {
"type": "text",
"fontSize": {"expr": "10"},
"color": "#767777",
"baseline": "middle",
"align": "right",
"dx": 310,
"tooltip": True
},
"encoding": {"text": {"field": "lo"}}
},
{
"mark": {
"type": "text",
"fontSize": {"expr": "10"},
"color": "#767777",
"baseline": "middle",
"align": "right",
"dx": 360,
"tooltip": True
},
"encoding": {"text": {"field": "hi"}}
}
]
},
"config": {
"background": "#F7F7F7",
"view": {"stroke": "transparent"},
"scale": {"continuousPadding": 0},
"font": "Barlow, Lato, Roboto, sans-serif",
"padding": {"left": 35, "top": 30, "right": 35, "bottom": 35},
"axis": {
"domainWidth": 1.3,
"tickSize": 7,
"tickWidth": 1,
"domainColor": "#40C6F2",
"tickColor": "#40C6F2"
}
}
}
notation = base_schema.copy()
values = [binding.solved_data for binding in self.bindings]
notation["data"]["values"] = values
if self.title:
notation["title"]["text"] = self.title
if self.subtitle:
notation["title"]["subtitle"] = self.subtitle
if self.chart_width:
notation["spec"]["width"] = self.chart_width
if self.chart_height:
notation["spec"]["height"] = self.chart_height
return {"notation": notation, "filename": self.filename}