I have created a compound bar chart in Altair with an interactive legend, where bars from one chart and legend elements can both be selected to filter another chart. This works well when only a single legend element is selected. However, when multiple legend elements are selected, selecting a bar alters the legend selection.
See the minimal example below. In this image, the Crookston and Duluth sites have been selected, with the result that the chart of sum of yield by year is filtered to those two sites. However, if I then click on the Manchuria bar in the sum of yield by variety chart, the legend selection changes so that only Duluth is selected, as seen in this image. (If I instead shift-click on the Manchuria bar, then only Crookston is selected in the legend.) The desired outcome is for both Crookston and Duluth to remain selected in the legend.
Is it possible to prevent the legend's selection from changing when the chart is clicked?
import altair as alt
from vega_datasets import data
source = data.barley()
bar_selection = alt.selection_multi(encodings=["y"])
legend_selection = alt.selection_multi(encodings=["color"], bind="legend")
varieties = alt.Chart(source).mark_bar().encode(
x="sum(yield)",
y="variety",
color="site",
opacity=alt.condition(
bar_selection, alt.value(1.0), alt.value(0.25)
),
).add_selection(bar_selection)
years = alt.Chart(source).mark_bar().encode(
x="sum(yield)",
y="year:N",
color="site",
).transform_filter(
bar_selection
).transform_filter(
legend_selection
)
(varieties & years).add_selection(legend_selection)
Related
For the interactive legend example, I was wondering if there is a way to display only one series and hide others on the chart after running the code. Then, when clicking on the legend, the chart can change to highlight different series.
The current example code displays all series at first, then highlights different series when clicking on the legend.
Here is the example code:
import altair as alt
from vega_datasets import data
source = data.unemployment_across_industries.url
selection = alt.selection_multi(fields=['series'], bind='legend')
alt.Chart(source).mark_area().encode(
alt.X('yearmonth(date):T', axis=alt.Axis(domain=False, format='%Y', tickSize=0)),
alt.Y('sum(count):Q', stack='center', axis=None),
alt.Color('series:N', scale=alt.Scale(scheme='category20b')),
opacity=alt.condition(selection, alt.value(1), alt.value(0.2))
).add_selection(
selection
)
You can do this using the init argument of the selection; for example:
selection = alt.selection_multi(fields=['series'], bind='legend', init=[{'series': 'Agriculture'}])
I want to have on the same page a pie chart and a bar chart, with trellis pages.
They both have 5 columns as trellis, so 5 different pages that you can visualize by scrolling down.
However, I would like to display the same pages at the same time for both graphs. For instance, let's imagine I'm on the pie chart, I scroll down one page - and so my pie chart is on page2 - I want my bar chart to automatically go to page 2 as well
I tried to use markings but although it links a bit the two graphs, it doesn't change the trellis pages automatically
Does anyone know how to do this ?
as #scsimon mentions, there's no native feature (nor API method, AFAIK) for this using a trellis. however, you can create a Property Control (I'd use a dropdown) in a Text Area, then use the same Document Property to your chart in a Limit By expression.
little more detail, assuming a data table like this:
A B CATEGORY
1 2 red
3 4 red
5 6 green
7 8 green
9 10 blue
11 12 blue
add a Text Area to the page and edit its contents
add a Property Control
click New to create a Document Property, give it a type String and a name category
change Set property value through to Unique values in column and choose the "category" column and click OK
save the Text Area contents
in your Pie Chart's Properties dialog, go to the Data page and look for Limit data using expression. click Edit
set the expression to: [CATEGORY] = "${category}" (don't forget the quotes or the Document Property won't be treated as a string and you'll get an error)
repeat steps 6 & 7 for your other chart
As #scsimon mentioned in the comments, it seems to be impossible. I found a way to have a similar representation though without using trellis visualization, by using text arena
I created a list box that contains the 5 columns I want to visualize, and then I select this list box as the vertical axis variable of the bar chart and the sector size of the pie chart
Then, by clicking on my list box, I can have a synchronized Pie chart and Bar chart (a bit different from what I expected but it also has some advantages like a multiple scale since a new graph is generated independently of the other variables)
I have a grouped bar chart in QlikView - the bars are displayed in different colors. Is it possible display the last bar in a group so that the border of the bar is red, and the middel of the bar is white?
Short answer; you can't do what you want to do.
You can individually set the thickness/presence of the bars but you can not change the colours of those borders. You will also need to set-up an expression for each you can not affect the border thickness based on the dimensions
And you can colour the bars as per the dimension quite effectively, so without bothering about the borders you can make the last bar always red, you just need to know the dimension you are working with
So this:
Will give you this:
Unfortunately not, within the bar chart object bars do not have a border.
I have a calculated dimension that yields following values: 'N', 'P', 'K'.
On top of that is an expresion. Also I am using PIE CHART to represent the data.
The numbers are calculated ok. I need help in selecting colors for the dimension values. There are predefined colors that each dimension values needs to have. How can I imprement that? In "Expression" Tab -> "Background Color" I can't reference the dimension, since it it a calculated dimension.
I appreciate the constructive feedback.
With some chart types comes a 'Colors' tab in the chart properties window. If you go to that tab you will be presented with numerous color options for your different dimensions (calculated or not):
Change the colors to your liking and watch the Pie Chart update.
Check the 'Persistent Colors' checkbox to keep colors assigned to dimensional values.
I'm using Visual Studio 2010 reporting to generate a report which has a table and a pie chart. Both work just fine except that for the pie chart, I'm to put the % value on each pie chart slice.
1. Is this even possible?
2. If it is, how do I do it?
Thanks
To put the % value on each pie chart slice do this (as shown here):
On the design surface, right-click on the pie and select Show Data Labels. The data labels should appear within each slice on the pie chart.
On the design surface, right-click on the labels and select Series Label Properties. The Series Label Properties dialog box appears.
Type #PERCENT for the Label data option.
Beware: If you use multiple data fields like this:
you need to move every field to first position and perform above steps as shown here otherwise you end up with only one pie chart slice showing the percentage and the others are blank...