Update: 新增 legend 选中控制

This commit is contained in:
chenjiandongx 2019-03-27 10:03:37 +08:00
parent 2b3c3839b4
commit cdf8dfe407
27 changed files with 60 additions and 24 deletions

View File

@ -12,7 +12,7 @@ def bar_base() -> Bar:
Bar()
.add_xaxis(Faker.choose())
.add_yaxis("商家A", Faker.values())
.add_yaxis("商家B", Faker.values())
.add_yaxis("商家B", Faker.values(), is_selected=False)
.set_global_opts(title_opts=opts.TitleOpts(title="Bar-基本示例", subtitle="我是副标题"))
)
return c

View File

@ -7,8 +7,8 @@ from jinja2 import Environment
from ..commons import utils
from ..commons.types import Optional, Union
from ..globals import ThemeType, NotebookType, CurrentConfig
from ..datasets import FILENAMES
from ..globals import CurrentConfig, NotebookType, ThemeType
from ..options import InitOpts
from ..render.engine import RenderEngine

View File

@ -26,6 +26,7 @@ class Bar(AxisChart):
series_name: str,
yaxis_data: Sequence,
*,
is_selected: bool = True,
xaxis_index: Optional[Numeric] = None,
yaxis_index: Optional[Numeric] = None,
color: Optional[str] = None,
@ -46,7 +47,7 @@ class Bar(AxisChart):
tooltip_opts = tooltip_opts.opts
self._append_color(color)
self._append_legend(series_name)
self._append_legend(series_name, is_selected)
self.options.get("series").append(
{
"type": ChartType.BAR,

View File

@ -22,6 +22,7 @@ class Boxplot(AxisChart):
series_name: str,
y_axis: Sequence,
*,
is_selected: bool = True,
xaxis_index: Optional[Numeric] = None,
yaxis_index: Optional[Numeric] = None,
label_opts: Union[opts.LabelOpts, dict] = opts.LabelOpts(),
@ -38,7 +39,7 @@ class Boxplot(AxisChart):
if isinstance(tooltip_opts, opts.TooltipOpts):
tooltip_opts = tooltip_opts.opts
self._append_legend(series_name)
self._append_legend(series_name, is_selected)
self.options.get("series").append(
{
"type": ChartType.BOXPLOT,

View File

@ -21,6 +21,7 @@ class Calendar(Chart):
series_name: str,
yaxis_data: Sequence,
*,
is_selected: bool = True,
label_opts: Union[opts.LabelOpts, dict] = opts.LabelOpts(),
calendar_opts: Union[opts.CalendarOpts, dict, None] = None,
tooltip_opts: Union[opts.TooltipOpts, dict, None] = None,
@ -35,7 +36,7 @@ class Calendar(Chart):
if calendar_opts:
self.options.update(calendar=calendar_opts)
self._append_legend(series_name)
self._append_legend(series_name, is_selected)
self.options.get("series").append(
{
"type": "heatmap",

View File

@ -21,6 +21,7 @@ class EffectScatter(AxisChart):
series_name: str,
y_axis: Sequence,
*,
is_selected: bool = True,
xaxis_index: Optional[Numeric] = None,
yaxis_index: Optional[Numeric] = None,
color: Optional[str] = None,
@ -38,7 +39,7 @@ class EffectScatter(AxisChart):
tooltip_opts = tooltip_opts.opts
self._append_color(color)
self._append_legend(series_name)
self._append_legend(series_name, is_selected)
self.options.get("series").append(
{
"type": ChartType.EFFECT_SCATTER,

View File

@ -17,6 +17,8 @@ class Funnel(Chart):
self,
series_name: str,
data_pair: Sequence,
*,
is_selected: bool = True,
color: Optional[str] = None,
sort_: str = "descending",
gap: Numeric = 0,
@ -31,7 +33,7 @@ class Funnel(Chart):
self._append_color(color)
data = [{"name": n, "value": v} for n, v in data_pair]
for a, _ in data_pair:
self._append_legend(a)
self._append_legend(a, is_selected)
_dset = set(self.options.get("legend")[0].get("data"))
self.options.get("legend")[0].update(data=list(_dset))

View File

@ -17,6 +17,8 @@ class Gauge(Chart):
self,
series_name: str,
data_pair: Sequence,
*,
is_selected: bool = True,
min_: Numeric = 0,
max_: Numeric = 100,
start_angle: Numeric = 225,
@ -26,7 +28,7 @@ class Gauge(Chart):
if isinstance(tooltip_opts, opts.TooltipOpts):
tooltip_opts = tooltip_opts.opts
self._append_legend(series_name)
self._append_legend(series_name, is_selected)
self.options.get("series").append(
{
"type": ChartType.GAUGE,

View File

@ -62,6 +62,7 @@ class Geo(Chart):
type_: str = "scatter",
maptype: str = "china",
*,
is_selected: bool = True,
symbol: Optional[str] = None,
symbol_size: Numeric = 12,
color: Optional[str] = None,
@ -112,7 +113,7 @@ class Geo(Chart):
}
)
self._append_color(color)
self._append_legend(series_name)
self._append_legend(series_name, is_selected)
if type_ == ChartType.SCATTER:
self.options.get("series").append(

View File

@ -22,6 +22,7 @@ class Graph(Chart):
links: List[Union[opts.GraphLink, dict]],
categories: Union[List[Union[opts.GraphCategory, dict]], None] = None,
*,
is_selected: bool = True,
is_focusnode: bool = True,
is_roam: bool = True,
is_rotate_label: bool = False,
@ -59,7 +60,7 @@ class Graph(Chart):
for c in categories:
if isinstance(c, opts.GraphCategory):
c = c.opts
self._append_legend(c)
self._append_legend(c, is_selected)
if edge_symbol is None:
edge_symbol = [None, None]

View File

@ -24,6 +24,7 @@ class HeatMap(AxisChart):
yaxis_data: Sequence,
value: Sequence,
*,
is_selected: bool = True,
xaxis_index: Optional[Numeric] = None,
yaxis_index: Optional[Numeric] = None,
label_opts: Union[opts.LabelOpts, dict] = opts.LabelOpts(),
@ -40,6 +41,7 @@ class HeatMap(AxisChart):
if isinstance(tooltip_opts, opts.TooltipOpts):
tooltip_opts = tooltip_opts.opts
self._append_legend(series_name, is_selected)
self.options.get("yAxis")[0].update(data=yaxis_data)
self.options.get("series").append(
{

View File

@ -25,6 +25,7 @@ class Kline(AxisChart):
series_name: str,
y_axis: Sequence,
*,
is_selected: bool = True,
xaxis_index: Optional[Numeric] = None,
yaxis_index: Optional[Numeric] = None,
markline_opts: Union[opts.MarkLineOpts, dict, None] = None,
@ -39,7 +40,7 @@ class Kline(AxisChart):
if isinstance(tooltip_opts, opts.TooltipOpts):
tooltip_opts = tooltip_opts.opts
self._append_legend(series_name)
self._append_legend(series_name, is_selected)
self.options.get("series").append(
{
"type": ChartType.KLINE,

View File

@ -21,6 +21,7 @@ class Line(AxisChart):
series_name: str,
y_axis: Sequence,
*,
is_selected: bool = True,
xaxis_index: Optional[Numeric] = None,
yaxis_index: Optional[Numeric] = None,
color: Optional[str] = None,
@ -51,7 +52,7 @@ class Line(AxisChart):
tooltip_opts = tooltip_opts.opts
self._append_color(color)
self._append_legend(series_name)
self._append_legend(series_name, is_selected)
# 合并 x 和 y 轴数据,避免当 X 轴的类型设置为 'value' 的时候,
# X、Y 轴均显示 Y 轴数据
data = [list(z) for z in zip(self._xaxis_data, y_axis)]

View File

@ -20,6 +20,7 @@ class Liquid(Chart):
self,
series_name: str,
data: Sequence,
*,
shape: str = "circle",
color: Optional[List[str]] = None,
is_animation: bool = True,

View File

@ -21,6 +21,7 @@ class Map(Chart):
data_pair: Sequence,
maptype: str = "china",
*,
is_selected: bool = True,
is_roam: bool = True,
symbol: Optional[str] = None,
is_map_symbol_show: bool = True,
@ -33,8 +34,8 @@ class Map(Chart):
tooltip_opts = tooltip_opts.opts
self.js_dependencies.add(maptype)
data = [{"name": n, "value": v} for (n, v) in data_pair]
self._append_legend(series_name)
data = [{"name": n, "value": v} for n, v in data_pair]
self._append_legend(series_name, is_selected)
self.options.get("series").append(
{
"type": ChartType.MAP,

View File

@ -28,6 +28,8 @@ class Parallel(Chart):
self,
series_name: str,
data: Sequence,
*,
is_selected: bool = True,
linestyle_opts: Union[opts.LineStyleOpts, dict] = opts.LineStyleOpts(),
tooltip_opts: Union[opts.TooltipOpts, dict, None] = None,
):
@ -37,7 +39,7 @@ class Parallel(Chart):
tooltip_opts = tooltip_opts.opts
self.options.update(parallel=opts.ParallelOpts().opts)
self._append_legend(series_name)
self._append_legend(series_name, is_selected)
self.options.get("series").append(
{
"type": ChartType.PARALLEL,

View File

@ -19,6 +19,7 @@ class Pie(Chart):
self,
series_name: str,
data_pair: Sequence,
*,
color: Optional[str] = None,
radius: Optional[Sequence] = None,
center: Optional[Sequence] = None,

View File

@ -31,6 +31,8 @@ class Polar(Chart):
self,
series_name: str,
data: Sequence,
*,
is_selected: bool = True,
type_: str = "line",
symbol: Optional[str] = None,
symbol_size: Numeric = 4,
@ -49,7 +51,7 @@ class Polar(Chart):
if isinstance(tooltip_opts, opts.TooltipOpts):
tooltip_opts = tooltip_opts.opts
self._append_legend(series_name)
self._append_legend(series_name, is_selected)
self.options.update(polar={})
if type_ in ("scatter", "line", "bar"):

View File

@ -57,6 +57,8 @@ class Radar(Chart):
self,
series_name: str,
data: Sequence,
*,
is_selected: bool = True,
symbol: Optional[str] = None,
color: Optional[str] = None,
label_opts: opts.LabelOpts = opts.LabelOpts(),
@ -73,7 +75,7 @@ class Radar(Chart):
if isinstance(tooltip_opts, opts.TooltipOpts):
tooltip_opts = tooltip_opts.opts
self._append_legend(series_name)
self._append_legend(series_name, is_selected)
self.options.get("series").append(
{
"type": ChartType.RADAR,

View File

@ -21,6 +21,8 @@ class Sankey(Chart):
series_name: str,
nodes: Sequence,
links: Sequence,
*,
is_selected: bool = True,
node_width: Numeric = 20,
node_gap: Numeric = 8,
label_opts: Union[opts.LabelOpts, dict] = opts.LabelOpts(),
@ -34,7 +36,7 @@ class Sankey(Chart):
if isinstance(tooltip_opts, opts.TooltipOpts):
tooltip_opts = tooltip_opts.opts
self._append_legend(series_name)
self._append_legend(series_name, is_selected)
self.options.get("series").append(
{
"type": ChartType.SANKEY,

View File

@ -22,6 +22,7 @@ class Scatter(AxisChart):
series_name: str,
y_axis: Sequence,
*,
is_selected: bool = True,
xaxis_index: Optional[Numeric] = None,
yaxis_index: Optional[Numeric] = None,
color: Optional[str] = None,
@ -42,7 +43,7 @@ class Scatter(AxisChart):
tooltip_opts = tooltip_opts.opts
self._append_color(color)
self._append_legend(series_name)
self._append_legend(series_name, is_selected)
data = [list(z) for z in zip(self._xaxis_data, y_axis)]
self.options.get("series").append(
{

View File

@ -19,6 +19,8 @@ class ThemeRiver(Chart):
self,
series_name: Sequence,
data: Sequence,
*,
is_selected: bool = True,
label_opts: Union[opts.LabelOpts, dict] = opts.LabelOpts(),
tooltip_opts: Union[opts.TooltipOpts, dict, None] = None,
singleaxis_opts: Union[opts.SingleAxisOpts, dict] = opts.SingleAxisOpts(),
@ -31,7 +33,7 @@ class ThemeRiver(Chart):
singleaxis_opts = singleaxis_opts.opts
for n in series_name:
self._append_legend(n)
self._append_legend(n, is_selected)
self.options.get("series").append(
{

View File

@ -38,6 +38,7 @@ class Tree(Chart):
self,
series_name: str,
data: Sequence,
*,
layout: str = "orthogonal",
symbol: str = "emptyCircle",
symbol_size: Numeric = 7,

View File

@ -20,6 +20,8 @@ class TreeMap(Chart):
self,
series_name: str,
data: Sequence,
*,
is_selected: bool = True,
left_depth: Optional[Numeric] = None,
pos_left: Optional[str] = None,
pos_right: Optional[str] = None,
@ -36,7 +38,7 @@ class TreeMap(Chart):
if isinstance(tooltip_opts, opts.TooltipOpts):
tooltip_opts = tooltip_opts.opts
self._append_legend(series_name)
self._append_legend(series_name, is_selected)
self.options.get("series").append(
{
"type": ChartType.TREEMAP,

View File

@ -33,6 +33,7 @@ class WordCloud(Chart):
self,
series_name: str,
data_pair: Sequence,
*,
shape: str = "circle",
word_gap: Numeric = 20,
word_size_range=None,

View File

@ -20,7 +20,9 @@ class Chart(Base):
if init_opts.theme == "white":
self.options.update(color=self._colors)
self.options.update(
series=[], legend=[{"data": []}], tooltip=opts.TooltipOpts().opts
series=[],
legend=[{"data": [], "selected": dict()}],
tooltip=opts.TooltipOpts().opts,
)
def set_series_opts(
@ -84,8 +86,10 @@ class Chart(Base):
s.update(tooltip=tooltip_opts)
return self
def _append_legend(self, name):
def _append_legend(self, name, is_selected):
self.options.get("legend")[0].get("data").append(name)
self.options.get("legend")[0].get("selected").update({name: is_selected})
print(self.options.get("legend")[0])
def _append_color(self, color: Optional[str]):
if color:

View File

@ -3,7 +3,7 @@ from jinja2 import Environment
from ...commons import utils
from ...commons.types import Optional
from ...globals import ThemeType, CurrentConfig
from ...globals import CurrentConfig, ThemeType
from ...render.engine import RenderEngine