UNPKG

3.77 kBMarkdownView Raw
1This is intended to be a catch-all for legend formatting needs. This
2[comes](https://stackoverflow.com/a/25090552/2171120)
3[up](https://stackoverflow.com/a/24175648/2171120)
4[often](https://stackoverflow.com/a/18211338/2171120) in Stack Overflow
5[questions](https://stackoverflow.com/a/23354308/2171120) which can’t be
6easily answered with `valueFormatter`. Nowadays I wave my hands and say
7"write your own legend, either as a plugin or using `highlightCallback`
8and `unhighlightCallback`." This is a simpler option.
9
10The `legendFormatter` is repeatedly called with a `data` object
11describing the selection or lack of selection. It contains all the
12information you need to generate a standard legend (e.g. formatted
13values), but there’s nothing preventing you from doing something crazier
14on your own.
15
16For example, here’s what a simple `legendFormatter` might look like:
17
18```js
19function legendFormatter(data) {
20 if (data.x == null) return ''; // no selection
21 return data.xHTML + data.series.map(v => v.labelHTML + ': ' + v.yHTML).join(' ');
22}
23```
24
25Here’s what the `data` object looks like when nothing is selected:
26
27```json
28{
29 "dygraph": "(dygraph)",
30 "series": [
31 {
32 "dashHTML": "<div style=\"display: inline-block; position: relative; bottom: .5ex; padding-left: 1em; height: 1px; border-bottom: 2px solid rgb(0,128,0);\"></div>",
33 "label": "Y1",
34 "labelHTML": "Y1",
35 "visible": true,
36 "color": "rgb(0,128,0)"
37 },
38 {
39 "dashHTML": "<div style=\"display: inline-block; position: relative; bottom: .5ex; padding-left: 1em; height: 1px; border-bottom: 2px solid rgb(0,0,128);\"></div>",
40 "label": "Y2",
41 "labelHTML": "Y2",
42 "visible": true,
43 "color": "rgb(0,0,128)"
44 }
45 ]
46}
47```
48
49The `dashHTML` properties help you render lines which match each series’
50line on the chart. When `strokePattern` is set, they become dotted or
51dashed lines as needed.
52
53Each value has a corresponding `HTML` variant, which is properly
54formatted and escaped according to all the relevant options which have
55been set for the chart.
56
57Here’s what it looks like when a row is selected:
58
59```json
60{
61 "dygraph": "(dygraph)",
62 "x": 93,
63 "xHTML": 93,
64 "series": [
65 {
66 "dashHTML": "<div style=\"display: inline-block; position: relative; bottom: .5ex; padding-left: 1em; height: 1px; border-bottom: 2px solid rgb(0,128,0);\"></div>",
67 "label": "Y1",
68 "labelHTML": "Y1",
69 "visible": true,
70 "color": "rgb(0,128,0)",
71 "y": 93,
72 "yHTML": "93"
73 },
74 {
75 "dashHTML": "<div style=\"display: inline-block; position: relative; bottom: .5ex; padding-left: 1em; height: 1px; border-bottom: 2px solid rgb(0,0,128);\"></div>",
76 "label": "Y2",
77 "labelHTML": "Y2",
78 "visible": true,
79 "color": "rgb(0,0,128)",
80 "y": 26.04,
81 "yHTML": "26.04"
82 }
83 ]
84}
85```
86
87Here’s what it looks like when a single series is selected (e.g. with
88`highlightSeriesOpts`):
89
90```json
91{
92 "dygraph": "(dygraph)",
93 "x": 94,
94 "xHTML": 94,
95 "series": [
96 {
97 "dashHTML": "<div style=\"display: inline-block; position: relative; bottom: .5ex; padding-left: 1em; height: 1px; border-bottom: 2px solid rgb(0,128,0);\"></div>",
98 "label": "Y1",
99 "labelHTML": "Y1",
100 "visible": true,
101 "color": "rgb(0,128,0)",
102 "y": 94,
103 "yHTML": "94",
104 "isHighlighted": true
105 },
106 {
107 "dashHTML": "<div style=\"display: inline-block; position: relative; bottom: .5ex; padding-left: 1em; height: 1px; border-bottom: 2px solid rgb(0,0,128);\"></div>",
108 "label": "Y2",
109 "labelHTML": "Y2",
110 "visible": true,
111 "color": "rgb(0,0,128)",
112 "y": 22.56,
113 "yHTML": "22.56"
114 }
115 ]
116}
117```
118
119(Note the `isHighlighted` property set on `Y1`.)