Home The one with the line chart.
Post
Cancel

The one with the line chart.

WOW2022 Week3

This chart was part of the Week 3 Workout Wednesday  challenge.

  Dataset

The dataset can be found on the Workout Wednesday link above.

  Additional fields created

None

  Fields used

  • Date
  • Close
  • Company

  Specification

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
{
  "$schema": "https://vega.github.io/schema/vega-lite/v5.json",
  "data": {"name": "dataset"},
  "layer": [
    {
      "description": "set the date points to facilitate the click event & default value",
      "params": [
        {
          "name": "select",
          "value": [
            {
              "x": {
                "year": 2020,
                "month": 11,
                "date": 1
              }
            }
          ],
          "select": {
            "type": "point",
            "encodings": ["x"],
            "on": "click",
            "nearest": true
          }
        }
      ],
      "mark": "point",
      "encoding": {
        "x": {
          "field": "Date",
          "type": "temporal"
        },
        "opacity": {"value": 0}
      }
    },
    {
      "description": "when the date is selected, recalculated the % change",
      "transform": [
        {
          "lookup": "symbol",
          "from": {
            "param": "select",
            "key": "symbol"
          }
        },
        {
          "calculate": "datum.select && datum.select.Close > 0 ? 
            (datum.Close - datum.select.Close)/datum.select.Close : 0",
          "as": "selected_price"
        }
      ],
      "layer": [
        {
          "description": "draw the line chart for the price change",
          "mark": "line",
          "encoding": {
            "x": {
              "field": "Date",
              "type": "temporal"
            },
            "y": {
              "field": "selected_price",
              "type": "quantitative",
              "axis": {"format": "%"}
            },
            "color": {
              "field": "Company",
              "type": "nominal",
              "legend": null
            }
          }
        },
        {
          "description": "determine the price value at the end of date range",
          "encoding": {
            "x": {
              "aggregate": "max",
              "field": "Date",
              "type": "temporal"
            },
            "y": {
              "aggregate": {
                "argmax": "Date"
              },
              "field": "selected_price",
              "type": "quantitative"
            },
            "color": {
              "field": "Company",
              "type": "nominal"
            }
          },
          "layer": [
            {
              "description": "draw a circle at the end of the date range",
              "mark": {"type": "circle"}
            },
            {
              "description": "add text for the company at the end of the date range",
              "mark": {
                "type": "text",
                "align": "left",
                "dx": 4
              },
              "encoding": {
                "text": {
                  "field": "Company",
                  "type": "nominal"
                }
              }
            }
          ]
        },
        {
          "transform": [
            {
              "filter": {
                "param": "select"
              }
            }
          ],
          "encoding": {
            "x": {
              "field": "Date",
              "type": "temporal",
              "timeUnit": "yearmonth"
            }
          },
          "layer": [
            {
              "description": "show the line and text for the selected month",
              "mark": {
                "type": "text",
                "align": "right",
                "fontWeight": 100,
                "dx": -5
              },
              "encoding": {
                "text": {
                  "field": "Date",
                  "timeUnit": "yearmonth"
                },
                "y": {"value": 10},
                "color": {
                  "value": "#7D7A78"
                }
              }
            }
          ]
        },
        {
          "layer": [
            {
              "transform": [
                {
                  "filter": {
                    "param": "select"
                  }
                }
              ],
              "description": "add vertical rule for the selected month",
              "mark": {
                "type": "rule",
                "strokeWidth": 0.5,
                "strokeDash": [1, 2]
              },
              "encoding": {
                "x": {
                  "field": "Date",
                  "type": "temporal"
                },
                "color": {
                  "value": "#7D7A78"
                }
              }
            },
            {
              "transform": [
                {
                  "filter": {
                    "param": "select"
                  }
                }
              ],
              "description": "add horizontal line at baseline (0%)",
              "mark": {
                "type": "rule",
                "strokeWidth": 0.5,
                "strokeDash": [1, 2]
              },
              "encoding": {
                "y": {
                  "field": "yaxis",
                  "type": "quantitative"
                },
                "color": {
                  "value": "#7D7A78"
                }
              }
            },
            {
              "transform": [
                {
                  "filter": {
                    "param": "select"
                  }
                }
              ],
              "description": "add black circle at the selected month",
              "mark": {
                "type": "circle",
                "size": 50
              },
              "encoding": {
                "x": {
                  "field": "Date",
                  "type": "temporal"
                },
                "y": {
                  "field": "yaxis",
                  "type": "quantitative"
                },
                "color": {
                  "value": "black"
                }
              }
            }
          ]
        }
      ]
    }
  ]
}

  Config

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
{
  "autosize": {
    "type": "fit",
    "contains": "padding"
  },
  "range": {
    "category": [
      "#1BA3C6",
      "#BDBD22",
      "#F43C63",
      "#4F7CBA"
    ]
  },
  "legend": {"disable": true},
  "view": {"stroke": 0},
  "axis": {
    "domain": false,
    "grid": false,
    "labelPadding": 10,
    "ticks": false,
    "title": null,
    "labelFont": "Segoe UI",
    "labelFontSize": 10,
    "labelColor": "#7D7A78"
  },
  "text": {
    "font": "Consolas",
    "fontSize": 12
  }
}

  External references

  View chart

Open the Chart in the Vega Editor

you need to click anywhere on the chart for it to render the first time.

This post is licensed under CC BY 4.0 by the author.