How to create a tooltip showing multiple values of a field in Altair? - tooltip

I have successfully created an interactive chart as the tutorial here and below is my result:
I want to create a tooltip as the image below:
But I cannot find any hint in Altair tutorial document.
Can anyone provide a suggestion about how to proceed?

You can create a single tooltip for multiple lines using the pivot transform. Here is an example using one of the vega datasets.
THe idea is that the tooltip is tied to the vertical rule mark, which represents a pivoted version of the data where each relevant value is in the same row:
import altair as alt
from vega_datasets import data
source = data.stocks()
base = alt.Chart(source).encode(x='date:T')
columns = sorted(source.symbol.unique())
selection = alt.selection_single(
fields=['date'], nearest=True, on='mouseover', empty='none', clear='mouseout'
)
lines = base.mark_line().encode(y='price:Q', color='symbol:N')
points = lines.mark_point().transform_filter(selection)
rule = base.transform_pivot(
'symbol', value='price', groupby=['date']
).mark_rule().encode(
opacity=alt.condition(selection, alt.value(0.3), alt.value(0)),
tooltip=[alt.Tooltip(c, type='quantitative') for c in columns]
).add_selection(selection)
lines + points + rule

Related

Jupyter notebook split output on 2 columns

Often I compare two models using different metrics in the notebook. It would be pretty good if I can just split cell output into 2 columns, fill the first column, then fill the second column.
Now I am calling a function that prints all metrics one by one
check_metrics(model_path)
check_metrics(producion_model_pathes[label])
The output of check metrics looks like output example
Is it possible to split the output into 2 columns, then set as default output the first column, then before calling the second function set the second column as default output? So the output must be looks like the 2 pictures (like above) stacked vertically
Just use the ipywidgets.widgets to create an output widget and set any thing with you want inside:
output1 = widgets.Output()
with output1:
display(check_metrics(model_path))
output2 = widgets.Output()
with output2:
display(check_metrics(model_path))
two_columns = widgets.HBox([output1, output2])
display(two_columns)

New Columns not showing up in 'Add Series' on existing chart in Google Sheets

I have a chart which was created from a data range (lets say A:C) on Google Sheets.
If I add a new column (so we now have A:D), I am not able to see the new column represented in data points I can add on under Chart Editor -> Setup -> Series.
Does the chart need to be refreshed or something in order to see new Columns? I tried looking here but it didn't seem to help: How to Force New Google Spreadsheets to refresh and recalculate?
To achieve this, you can use Google Apps Script. Using a trigger that fires when the document is edited onEdit() and reflecting the new content within the chart using the class EmbeddedChart.
Example
Assuming the table looks something like this (the example should work with another format):
Name
Measure 1
Measure 2
Marshall
1
2
Barney
2
3
Ted
3
4
It will generate a chart like this:
First you need to open add a new script via Tools>Script Editor
Inside the Code.gs file, add the following:
Code.gs
const onEdit = (event) => {
// Get the active SpreadSheet
let ss = SpreadsheetApp.getActiveSheet()
// If you want to limitate the range within
// you should use the range within the Event Objects
Logger.log(event.range)
let chart = ss.getCharts()[0]
// Get to A1 to the last available cell
let newRanges = ss.getRange(1, 1, ss.getLastRow(), ss.getLastColumn())
chart = chart.modify()
// Clear the old ones
.clearRanges()
// Add the updated values
.addRange(newRanges)
.build()
ss.updateChart(chart)
}
The above script triggers every time you edit the sheet where it is included. Get the updated data from the spreadsheet, and include it in the chart. Right now the script detects any change in the spreadsheet and adds from the first cell to the last to the chart.
Note: onEdit is too broad, you can control it using different methods, for example:
if(e.range.getRow()>20) return
For example, adding this new data:
Name
Measure 1
Measure 2
Measure 3
Marshall
1
2
3
Barney
2
2
4
Ted
3
4
5
Lily
5
6
5
It will automatically generate this chart:
If you think this should be included in Sheets, you can also send feedback to Google using Help>Help Sheets Improve or via link.
Documentation
Google Sheets Event
Google Apps Script
Spreadsheet Service
Class Range

Query + Transpose based on value in Column B if Column A contains certain text

I am currently working with Google Forms and want to rearrange the way the responses are being displayed on the "Response Sheet". The only way I can think of doing this is by importing or moving the data to another sheet that would select and transpose certain columns if Column A contains key value.
This is what I'm seeing as part of the input and would like to see as the output if Column A Contains certain text:
Input & Output
Thank you in advance for your help!
O.K.
I rewrite headings a2:e2,
I take whole first five columns without headings e3:e6
I display content of columns A,B,F,G,H for all the rows that have 'A1' in column 1
I take tables built in point 1 and 2 together and sort them by first column
My solution is here:
https://docs.google.com/spreadsheets/d/1n7Ppd8v75mb3qrnJz_Jh_b4HNaj4i56X9wRGnz0l6i8/copy
={A2:E2;
sort({A3:E6;
query(A3:H6,"select A,B,F,G,H where A ='A1'",0)})
}

How to import information from sheets in another Document based on two values? Google Sheets

I have 2 documents:
Document 1 contains a list with so called Set-names, displayed as "Set". And within each "Set" there is name that can be found that corresponds to the number within that set.
Document 2 contains all the Set-names with each their own numbers and Name. Each Set-name has it's own Sheet, and there is a lot of them.
I want to fill in just the Set-name "Set" and Set-number "Set#", to automatically display the name corresponding to these two values. The question is however, how do I do that?
Document 1: Collection
Shared document link: Click here (File > Make copy)
Document 2: All Set-names
Shared document link: Click here (File > Make copy)
In this example above I would like to output "Dark Raichu" within Document 1 at Q74:T74 and at Q75:T75. Is this even possible to make? INDIRECT might help as well so I don't have to manually edit each Set-name. Any idea/suggestion/solution is much appreciated!
EDIT
(following OP's explanations)
First of all I have to say that you should AVOID making such radical changes to the original question, since it completely throws readers off balance.
Having said that there is a completely different approach that should be followed.
In your Document 2: All Set-names
Step 1
Go to your Wizards Black Star Promos tab.
Using a formula like =ArrayFormula(A4:A59&"/555")
change the Set # from 1,2,3...59 to 1/555,2/555,3/555...59/555
(555 can be any unique number that does NOT conflict with the other sets)
Step 2
Create a new tab. Name this tab AllSets
Step 3
Use this formula in cell A2
={FILTER('Base Set'!A5:C,'Base Set'!A5:A<>"");
FILTER(Fossil!A5:C,Fossil!A5:A<>"");
FILTER('Base Set 2'!A5:C,'Base Set 2'!A5:A<>"");
FILTER('Wizards Black Star Promos'!A5:C,'Wizards Black Star Promos'!A5:A<>"");
FILTER(Jungle!A5:C,Jungle!A5:A<>"")}
Using this pattern complete the formula for the rest of your tabs. You will end up with a 3 columns list of all your sets in one place.
In your Document 1: Collection
Step 4
Go to your Collection tab.
In cell Q2 (where column Q is exactly before your Name column R) use the following formula.
(As before, complete the formula for the rest of your tabs)
=INDEX(IFERROR(N2:N&(SWITCH(H2:H,
"Base Set","/96",
"Jungle","/64",
"Wizards Black Star Promos","/555",
"Fossil","/62"))))
This way you will bring the complete Set # back.
This will be our helper column which can be hidden and will be only used in the next formula.
Step 5
In the same Collection tab and in cell R2 place this formula
=ArrayFormula(IFERROR(
VLOOKUP(Q2:Q,IMPORTRANGE("1n8iWAl7ZQhsue6Opefvh_9yDwMk06PvqUdQoVuEJw00","AllSets!A2:C"),{2,3},0)))
The above final formula will give you everything.
The Card name, the Type as well as the Link and the Image of the card.
If you do not want the Type just change this part of the formula {2,3} to just 2.
Step 6
Enjoy :)
Additional functions used:
FILTER
INDEX
SWITCH
Original answer
(For the question as originally posted )
You can use a combination formula like
=ArrayFormula(IFERROR(
VLOOKUP(V2:V,{REGEXEXTRACT(Sheet1!R2:R,"\d+")*1,Sheet1!S2:S},2,0)))
(do adjust ranges and locale syntax to meet your needs)
Functions used:
ArrayFormula
IFERROR
VLOOKUP
REGEXEXTRACT
You can easily import a range from one Spreadsheet to another using IMPORTRANGE. IN your case described as you wanted to import the range A5:A (i.e all cells in column A minus the first 5 cells in the column) of the sheet Team Rocket of the Spreadsheet Document 2 into Document 1 cell Q73 you would need to use this function in cell Q73:
=IMPORTRANGE("YOUR DOCUMENT 2 SPREADSHEET URL","Team Rocket!A5:A")
Note that when you first use this function it might ask you to grant access between these Spreadsheeets to let one get the data from the other

How to use Split array of a single cell for data validation dropdown?

I am trying to create data validation dropdown from a single cell which has all , separated values and I am splitting them using SPLIT(K4,","). But when I apply the formula drop-down just goes away. Here is how it looks:
And here is where I have applied validation:
It just happens and I can't see any drop-down here. Even validation doesn't work as I type a value from given values, it still identifies it as invalid:
Here it says that it is actually not possible, but otherwise, my data column will grow very big, that's why I wanted to keep it in a single cell.
Method to reproduce: Just make a copy of this sheet and experiment on your own whatever you want it to be like:
It is not possible to do this from the sheets editor, but you could use Google Apps Script to accomplish this:
Open the script editor by selecting Tools > Script editor.
Copy and run this function:
function createDataValidation() {
const sheet = SpreadsheetApp.getActiveSheet();
const values = sheet.getRange("A1").getValue().split(","); // Get array with values from A1
const rule = SpreadsheetApp.newDataValidation().requireValueInList(values); // Create DV
sheet.getRange("F8").setDataValidation(rule); // Set DV to F8
}
Reference:
Class DataValidationBuilder

Resources