usertutorial.rst 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  1. .. Licensed to the Apache Software Foundation (ASF) under one
  2. or more contributor license agreements. See the NOTICE file
  3. distributed with this work for additional information
  4. regarding copyright ownership. The ASF licenses this file
  5. to you under the Apache License, Version 2.0 (the
  6. "License"); you may not use this file except in compliance
  7. with the License. You may obtain a copy of the License at
  8. .. http://www.apache.org/licenses/LICENSE-2.0
  9. .. Unless required by applicable law or agreed to in writing,
  10. software distributed under the License is distributed on an
  11. "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  12. KIND, either express or implied. See the License for the
  13. specific language governing permissions and limitations
  14. under the License.
  15. Exploring data with Apache Superset
  16. ===================================
  17. In this tutorial, we will introduce key concepts in Apache Superset
  18. through the exploration of a real dataset which contains the flights
  19. made by employees of a UK-based organization in 2011. The following
  20. information about each flight is given:
  21. - The traveller's department. For the purposes of this tutorial the
  22. departments have been renamed Orange, Yellow and Purple.
  23. - The cost of the ticket.
  24. - The travel class (Economy, Premium Economy, Business and First
  25. Class).
  26. - Whether the ticket was a single or return.
  27. - The date of travel.
  28. - Information about the origin and destination.
  29. - The distance between the origin and destination, in kilometers (km).
  30. Enabling Upload a CSV Functionality
  31. -----------------------------------
  32. You may need to enable the functionality to upload a CSV to your
  33. database. The following section explains how to enable this
  34. functionality for the examples database.
  35. In the top menu, select :menuselection:`Sources --> Databases`. Find the
  36. :guilabel:`examples` database in the list and select the edit record
  37. button.
  38. .. image:: _static/images/usertutorial/edit-record.png
  39. Within the :guilabel:`Edit Database` page, check the
  40. :guilabel:`Allow Csv Upload` checkbox.
  41. Finally, save by selecting :guilabel:`Save` at the bottom of the page.
  42. Obtaining and loading the data
  43. ------------------------------
  44. Download the data for this tutorial to your computer from
  45. `Github <https://raw.githubusercontent.com/apache-superset/examples-data/master/tutorial_flights.csv>`__.
  46. In the top menu, select :menuselection:`Sources --> Upload a CSV`.
  47. .. image:: _static/images/usertutorial/upload_a_csv.png
  48. Then, enter the :guilabel:`Table name` as `tutorial_flights`
  49. and select the :guilabel:`CSV file` from your computer.
  50. .. image:: _static/images/usertutorial/csv_to_database_configuration.png
  51. Next enter the text `Travel Date` into the
  52. :guilabel:`Parse Dates` field.
  53. .. image:: _static/images/usertutorial/parse_dates_column.png
  54. Leaving all the other options in their default settings, select
  55. :guilabel:`Save` at the bottom of the page.
  56. Table Visualization
  57. -------------------
  58. In this section, we’ll create our first visualization: a table to show
  59. the number of flights and cost per travel class.
  60. To create a new chart, select the :menuselection:`New --> Chart`.
  61. .. image:: _static/images/usertutorial/add_new_chart.png
  62. Once in the :guilabel:`Create a new chart` dialogue, select
  63. :guilabel:`tutorial_flights` from the :guilabel:`Chose a datasource`
  64. dropdown.
  65. .. image:: _static/images/usertutorial/chose_a_datasource.png
  66. Next, select the visualization type as :guilabel:`Table`.
  67. .. image:: _static/images/usertutorial/select_table_visualization_type.png
  68. Then, select :guilabel:`Create new chart` to go into the chart view.
  69. By default, Apache Superset only shows the last week of data: in our
  70. example, we want to look at all the data in the dataset. No problem -
  71. within the :guilabel:`Time` section, remove the filter on
  72. :guilabel:`Time range` by selecting on :guilabel:`Last week` then
  73. changing the selection to :guilabel:`No filter`, with a final
  74. :guilabel:`OK` to confirm your selection.
  75. .. image:: _static/images/usertutorial/no_filter_on_time_filter.png
  76. Now, we want to specify the rows in our table by using the
  77. :guilabel:`Group by` option. Since in this example, we want to
  78. understand different Travel Classes, we select :guilabel:`Travel Class`
  79. in this menu.
  80. Next, we can specify the metrics we would like to see in our table with
  81. the :guilabel:`Metrics` option. :guilabel:`Count(*)`, which represents the number of
  82. rows in the table (in this case corresponding to the number of flights
  83. since we have a row per flight), is already there. To add cost, within
  84. :guilabel:`Metrics`, select :guilabel:`Cost`. :guilabel:`Save` the
  85. default aggregation option, which is to sum the column.
  86. .. image:: _static/images/usertutorial/sum_cost_column.png
  87. Finally, select :guilabel:`Run Query` to see the results of the table.
  88. .. image:: _static/images/usertutorial/tutorial_table.png
  89. Congratulations, you have created your first visualization in Apache
  90. Superset!
  91. To save the visualization, click on :guilabel:`Save` in the top left of
  92. the screen. Select the :guilabel:`Save as` option, and enter the chart
  93. name as Tutorial Table (you will be able to find it again through the
  94. :guilabel:`Charts` screen, accessible in the top menu). Similarly,
  95. select :guilabel:`Add to new dashboard` and enter `Tutorial Dashboard`.
  96. Finally, select :guilabel:`Save & go to dashboard`.
  97. .. image:: _static/images/usertutorial/save_tutorial_table.png
  98. Dashboard basics
  99. ----------------
  100. Next, we are going to explore the dashboard interface. If you’ve
  101. followed the previous section, you should already have the dashboard
  102. open. Otherwise, you can navigate to the dashboard by selecting
  103. :guilabel:`Dashboards` on the top menu, then :guilabel:`Tutorial dashboard`
  104. from the list of dashboards.
  105. On this dashboard you should see the table you created in the previous
  106. section. Select :guilabel:`Edit dashboard` and then hover over the
  107. table. By selecting the bottom right hand corner of the table (the
  108. cursor will change too), you can resize it by dragging and dropping.
  109. .. image:: _static/images/usertutorial/resize_tutorial_table_on_dashboard.png
  110. Finally, save your changes by selecting :guilabel:`Save changes` in the
  111. top right.
  112. Pivot Table
  113. -----------
  114. In this section, we will extend our analysis using a more complex
  115. visualization, Pivot Table. By the end of this section, you will have
  116. created a table that shows the monthly spend on flights for the first
  117. six months, by department, by travel class.
  118. As before, create a new visualization by selecting
  119. :menuselection:`New --> Chart` on the top menu. Choose tutorial_flights
  120. again as a datasource, then click on the visualization type to get to
  121. the visualization menu. Select the :guilabel:`Pivot Table` visualization
  122. (you can filter by entering text in the search box) and then
  123. :guilabel:`Create a new chart`.
  124. In the :guilabel:`Time` section, keep the Time Column as Travel Date
  125. (this is selected automatically as we only have one time column in our
  126. dataset). Then select :guilabel:`Time Grain` to be month as having daily
  127. data would be too granular to see patterns from. Then select the time
  128. range to be the first six months of 2011 by click on Last week in the
  129. :guilabel:`Time Range` section, then in :guilabel:`Custom` selecting a
  130. :guilabel:`Start / end` of 1\ :sup:`st` January 2011 and 30\ :sup:`th`
  131. June 2011 respectively by either entering directly the dates or using
  132. the calendar widget (by selecting the month name and then the year, you
  133. can move more quickly to far away dates).
  134. .. image:: _static/images/usertutorial/select_dates_pivot_table.png
  135. Next, within the :guilabel:`Query` section, remove the default COUNT(*)
  136. and add Cost, keeping the default SUM aggregate. Note that Apache
  137. Superset will indicate the type of the metric by the symbol on the left
  138. hand column of the list (ABC for string, # for number, a clock face for
  139. time, etc.).
  140. In :guilabel:`Group by` select :guilabel:`Time`: this will automatically
  141. use the Time Column and Time Grain selections we defined in the Time
  142. section.
  143. Within :guilabel:`Columns`, select first :guilabel:`Department` and then
  144. :guilabel:`Travel Class`. All set – let’s :guilabel:`Run Query` to see
  145. some data!
  146. .. image:: _static/images/usertutorial/tutorial_pivot_table.png
  147. You should see months in the rows and Department and Travel Class in the
  148. columns. To get this in our dashboard, select :guilabel:`Save`, name the
  149. chart Tutorial Pivot and using
  150. :guilabel:`Add chart to existing dashboard` select
  151. :guilabel:`Tutorial Dashboard`, and then finally
  152. :guilabel:`Save & go to dashboard`.
  153. Line Chart
  154. ----------
  155. In this section, we are going to create a line chart to understand the
  156. average price of a ticket by month across the entire dataset. As before,
  157. select :menuselection:`New --> Chart`, and then
  158. :guilabel:`tutorial_flights` as the datasource and
  159. :guilabel:`Line Chart` as the visualization type.
  160. In the Time section, as before, keep the :guilabel:`Time Column` as
  161. Travel Date and :guilabel:`Time Grain` as month but this time for the
  162. :guilabel:`Time range` select :guilabel:`No filter` as we want to look
  163. at entire dataset.
  164. Within :guilabel:`Metrics`, remove the default :guilabel:`COUNT(*)` and
  165. add :guilabel:`Cost`. This time, we want to change how this column is
  166. aggregated to show the mean value: we can do this by selecting
  167. :guilabel:`AVG` in the :guilabel:`aggregate` dropdown.
  168. .. image:: _static/images/usertutorial/average_aggregate_for_cost.png
  169. Next, select :guilabel:`Run Query` to show the data on the chart.
  170. How does this look? Well, we can see that the average cost goes up in
  171. December. However, perhaps it doesn’t make sense to combine both single
  172. and return tickets, but rather show two separate lines for each ticket
  173. type.
  174. Let’s do this by selecting :guilabel:`Ticket Single or Return` in the
  175. :guilabel:`Group by` box, and the selecting :guilabel:`Run Query` again.
  176. Nice! We can see that on average single tickets are cheaper than returns
  177. and that the big spike in December is caused by return tickets.
  178. Our chart is looking pretty good already, but let’s customize some more
  179. by going to the :guilabel:`Customize` tab on the left hand pane. Within
  180. this pane, try changing the :guilabel:`Color Scheme`, removing the range
  181. filter by selecting No in the :guilabel:`Show Range Filter` drop down
  182. and adding some labels using :guilabel:`X Axis Label` and
  183. :guilabel:`Y Axis Label`.
  184. .. image:: _static/images/usertutorial/tutorial_line_chart.png
  185. Once you’re done, :guilabel:`Save` as Tutorial Line Chart, use
  186. :guilabel:`Add chart to
  187. existing dashboard` to add this chart to the previous ones on the
  188. Tutorial Dashboard and then :guilabel:`Save & go to dashboard`.
  189. Markup
  190. ------
  191. In this section, we will add some text to our dashboard. If you’re there
  192. already, you can navigate to the dashboard by selecting
  193. :guilabel:`Dashboards` on the top menu, then
  194. :guilabel:`Tutorial dashboard` from the list of dashboards. Got into
  195. edit mode by selecting :guilabel:`Edit dashboard`.
  196. Within the Insert components pane, drag and drop a :guilabel:`Markdown`
  197. box on the dashboard. Look for the blue lines which indicate the anchor
  198. where the box will go.
  199. .. image:: _static/images/usertutorial/blue_bar_insert_component.png
  200. Now, to edit the text, select the box. You can enter text, in markdown
  201. format (see `this Markdown
  202. Cheatsheet <https://github.com/adam-p/markdown-here/wiki/Markdown-Cheatsheet>`__
  203. for more information about this format). You can toggle between
  204. :guilabel:`Edit` and :guilabel:`Preview` using the menu on the top of
  205. the box.
  206. .. image:: _static/images/usertutorial/markdown.png
  207. To exit, select any other part of the dashboard. Finally, don’t forget
  208. to keep your changes using :guilabel:`Save changes`.
  209. Filter box
  210. ----------
  211. In this section, you will learn how to add a filter to your dashboard.
  212. Specifically, we will create a filter that allows us to look at those
  213. flights that depart from a particular country.
  214. A filter box visualization can be created as any other visualization by
  215. selecting :menuselection:`New --> Chart`, and then
  216. :guilabel:`tutorial_flights` as the datasource and
  217. :guilabel:`Filter Box` as the visualization type.
  218. First of all, in the :guilabel:`Time` section, remove the filter from
  219. the :guilabel:`Time
  220. range` selection by selecting :guilabel:`No filter`.
  221. Next, in :guilabel:`Filters Configurations` first add a new filter by
  222. selecting the plus sign and then edit the newly created filter by
  223. selecting the pencil icon.
  224. For our use case, it makes most sense to present a list of countries in
  225. alphabetical order. First, enter the column as
  226. :guilabel:`Origin Country` and keep all other options the same and then
  227. select :guilabel:`Run Query`. This gives us a preview of our filter.
  228. Next, remove the date filter by unchecking the :guilabel:`Date Filter`
  229. checkbox.
  230. .. image:: _static/images/usertutorial/filter_on_origin_country.png
  231. Finally, select :guilabel:`Save`, name the chart as Tutorial Filter, add
  232. the chart to our existing Tutorial Dashboard and then
  233. :guilabel:`Save & go to
  234. dashboard`. Once on the Dashboard, try using the filter to show only
  235. those flights that departed from the United Kingdom – you will see the
  236. filter is applied to all of the other visualizations on the dashboard.
  237. Publishing your dashboard
  238. -------------------------
  239. If you have followed all of the steps outlined in the previous section,
  240. you should have a dashboard that looks like the below. If you would
  241. like, you can rearrange the elements of the dashboard by selecting
  242. :guilabel:`Edit dashboard` and dragging and dropping.
  243. If you would like to make your dashboard available to other users,
  244. simply select :guilabel:`Draft` next to the title of your dashboard on
  245. the top left to change your dashboard to be in :guilabel:`Published`
  246. state. You can also favorite this dashboard by selecting the star.
  247. .. image:: _static/images/usertutorial/publish_dashboard.png
  248. Taking your dashboard further
  249. -----------------------------
  250. In the following sections, we will look at more advanced Apache Superset
  251. topics.
  252. Annotations
  253. -----------
  254. Annotations allow you to add additional context to your chart. In this
  255. section, we will add an annotation to the Tutorial Line Chart we made in
  256. a previous section. Specifically, we will add the dates when some
  257. flights were cancelled by the UK's Civil Aviation Authority in response
  258. to the eruption of the Grímsvötn volcano in Iceland (23-25 May 2011).
  259. First, add an annotation layer by navigating to
  260. :menuselection:`Manage --> Annotation Layers`. Add a new annotation
  261. layer by selecting the green plus sign to add a new record. Enter the
  262. name Volcanic Eruptions and save. We can use this layer to refer to a
  263. number of different annotations.
  264. Next, add an annotation by navigating to
  265. :menuselection:`Manage --> Annotations` and then create a new annotation
  266. by selecting the green plus sign. Then, select the
  267. :guilabel:`Volcanic Eruptions` layer, add a short description Grímsvötn
  268. and the eruption dates (23-25 May 2011) before finally saving.
  269. .. image:: _static/images/usertutorial/edit_annotation.png
  270. Then, navigate to the line chart by going to :guilabel:`Charts` then
  271. selecting :guilabel:`Tutorial
  272. Line Chart` from the list. Next, go to the
  273. :guilabel:`Annotations and Layers` section and select
  274. :guilabel:`Add Annotation Layer`. Within this dialogue:
  275. - name the layer as `Volcanic Eruptions`
  276. - change the :guilabel:`Annotation Layer Type` to :guilabel:`Event`
  277. - set the :guilabel:`Annotation Source` as :guilabel:`Superset annotation`
  278. - specify the :guilabel:`Annotation Layer` as :guilabel:`Volcanic Eruptions`
  279. .. image:: _static/images/usertutorial/annotation_settings.png
  280. Select :guilabel:`Apply` to see your annotation shown on the chart.
  281. .. image:: _static/images/usertutorial/annotation.png
  282. If you wish, you can change how your annotation looks by changing the
  283. settings in the :guilabel:`Display configuration` section. Otherwise,
  284. select :guilabel:`OK` and finally :guilabel:`Save` to save your chart.
  285. If you keep the default selection to overwrite the chart, your
  286. annotation will be saved to the chart and also appear automatically in
  287. the Tutorial Dashboard.
  288. Advanced Analytics
  289. ------------------
  290. In this section, we are going to explore the Advanced Analytics feature
  291. of Apache Superset that allows you to apply additional transformations
  292. to your data. The three types of transformation are:
  293. Moving Average
  294. Select a rolling window [#f1]_, and then apply a calculation on it (mean,
  295. sum or standard deviation). The fourth option, cumsum, calculates the
  296. cumulative sum of the series [#f2]_.
  297. Time Comparison
  298. Shift your data in time and, optionally, apply a calculation to compare the
  299. shifted data with your actual data (e.g. calculate the absolute difference
  300. between the two).
  301. Python Functions
  302. Resample your data using one of a variety of methods [#f3]_.
  303. Setting up the base chart
  304. ~~~~~~~~~~~~~~~~~~~~~~~~~
  305. In this section, we're going to set up a base chart which we can then
  306. apply the different Advanced Analytics features to. Start off by
  307. creating a new chart using the same :guilabel:`tutorial_flights`
  308. datasource and the :guilabel:`Line Chart` visualization type. Within the
  309. Time section, set the :guilabel:`Time Range` as 1\ :sup:`st` October
  310. 2011 and 31\ :sup:`st` October 2011.
  311. Next, in the query section, change the :guilabel:`Metrics` to the sum of
  312. :guilabel:`Cost`. Select :guilabel:`Run Query` to show the chart. You
  313. should see the total cost per day for each month in October 2011.
  314. .. image:: _static/images/usertutorial/advanced_analytics_base.png
  315. Finally, save the visualization as Tutorial Advanced Analytics Base,
  316. adding it to the Tutorial Dashboard.
  317. Rolling mean
  318. ~~~~~~~~~~~~
  319. There is quite a lot of variation in the data, which makes it difficult
  320. to identify any trend. One approach we can take is to show instead a
  321. rolling average of the time series. To do this, in the
  322. :guilabel:`Moving Average` subsection of :guilabel:`Advanced Analytics`,
  323. select mean in the :guilabel:`Rolling` box and enter 7 into both Periods
  324. and Min Periods. The period is the length of the rolling period
  325. expressed as a multiple of the :guilabel:`Time Grain`. In our example,
  326. the :guilabel:`Time Grain` is day, so the rolling period is 7 days, such
  327. that on the 7th October 2011 the value shown would correspond to the
  328. first seven days of October 2011. Lastly, by specifying
  329. :guilabel:`Min Periods` as 7, we ensure that our mean is always
  330. calculated on 7 days and we avoid any ramp up period.
  331. After displaying the chart by selecting :guilabel:`Run Query` you will
  332. see that the data is less variable and that the series starts later as
  333. the ramp up period is excluded.
  334. .. image:: _static/images/usertutorial/rolling_mean.png
  335. Save the chart as Tutorial Rolling Mean and add it to the Tutorial
  336. Dashboard.
  337. Time Comparison
  338. ~~~~~~~~~~~~~~~
  339. In this section, we will compare values in our time series to the value
  340. a week before. Start off by opening the Tutorial Advanced Analytics Base
  341. chart, by going to :guilabel:`Charts` in the top menu and then selecting
  342. the visualization name in the list (alternatively, find the chart in the
  343. Tutorial Dashboard and select Explore chart from the menu for that
  344. visualization).
  345. Next, in the :guilabel:`Time Comparison` subsection of
  346. :guilabel:`Advanced Analytics`, enter the :guilabel:`Time Shift` by
  347. typing in "minus 1 week" (note this box accepts input in natural
  348. language). :guilabel:`Run Query` to see the new chart, which has an
  349. additional series with the same values, shifted a week back in time.
  350. .. image:: _static/images/usertutorial/time_comparison_two_series.png
  351. Then, change the :guilabel:`Calculation type` to
  352. :guilabel:`Absolute difference` and select :guilabel:`Run
  353. Query`. We can now see only one series again, this time showing the
  354. difference between the two series we saw previously.
  355. .. image:: _static/images/usertutorial/time_comparison_absolute_difference.png
  356. Save the chart as Tutorial Time Comparison and add it to the Tutorial
  357. Dashboard.
  358. Resampling the data
  359. ~~~~~~~~~~~~~~~~~~~
  360. In this section, we'll resample the data so that rather than having
  361. daily data we have weekly data. As in the previous section, reopen the
  362. Tutorial Advanced Analytics Base chart.
  363. Next, in the :guilabel:`Python Functions` subsection of
  364. :guilabel:`Advanced Analytics`, enter 7D, corresponding to seven days,
  365. in the :guilabel:`Rule` and median as the :guilabel:`Method` and show
  366. the chart by selecting :guilabel:`Run Query`.
  367. .. image:: _static/images/usertutorial/resample.png
  368. Note that now we have a single data point every 7 days. In our case, the
  369. value showed corresponds to the median value within the seven daily data
  370. points. For more information on the meaning of the various options in
  371. this section, refer to the `Pandas
  372. documentation <https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.resample.html>`__.
  373. Lastly, save your chart as Tutorial Resample and add it to the Tutorial
  374. Dashboard. Go to the tutorial dashboard to see the four charts side by
  375. side and compare the different outputs.
  376. .. rubric:: Footnotes
  377. .. [#f1] See the Pandas `rolling method documentation <https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.rolling.html>`_ for more information.
  378. .. [#f2] See the Pandas `cumsum method documentation <https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.cumsum.html>`_ for more information.
  379. .. [#f3] See the Pandas `resample method documentation <https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.resample.html>`_ for more information.