DC.js Legend only works with integers as group name - legend

I have a composite with four linecharts. When I use legend in the composite, it only works when I use integers in each single linechart group name. When one character is used, the legend turns black and graph does not shown values.
energiePerDagCompositeChart
.height(150).width(800)
.transitionDuration(1000)
.dimension(gasPerDagDim)
.mouseZoomable(true)
.x(d3.scaleTime().domain([new Date(1900, 0, 1), new Date(1900, 11, 31)]))
.round(d3.timeMonth.round)
.xUnits(d3.timeMonths)
.elasticY(true)
.renderHorizontalGridLines(true)
.brushOn(false)
.legend(new dc.Legend().autoItemWidth(true).horizontal(true).x(50).y(10))
.compose([
dc.lineChart(energiePerDagCompositeChart)
.dimension(gasPerDagDim)
.colors(d3.scaleLinear().range(["blue", "blue"]))
.group(createCumulativeGroup(gasPerDagThisYearGrp), "2023"),
dc.lineChart(energiePerDagCompositeChart)
.dimension(gasPerDagDim)
.colors(d3.scaleLinear().range(["steelbleu", "steelblue"]))
.group(createCumulativeGroup(gasPerDagLastYearGrp), "2022"),
dc.lineChart(energiePerDagCompositeChart)
.dimension(elektraPerDagDim)
.colors(d3.scaleLinear().range(["red", "red"]))
.group(createCumulativeGroup(elektraPerDagThisYearGrp), "2023"),
dc.lineChart(energiePerDagCompositeChart)
.dimension(elektraPerDagDim)
.colors(d3.scaleLinear().range(["orange", "orange"]))
.group(createCumulativeGroup(elektraPerDagLastYearGrp), "2022")
])
;
I tried to put functions in legendText() returning d.name but with same result.

Related

Change cell color if the two values of this cell have opposite signs in Pretty tables in Julia

How to change cell color if the two values of this cell have opposite signs in Pretty tables in Julia below is my code and the table is attached.
names = string.(-1/1:1/4:1/1)
pretty_table(AStrings , header = ([-1,-3/4, -1/2, -1/4, 0, 1/4, 1/2, 3/4, 1]), row_names= names)
After digging through the docs:
using PrettyTables
# making some demo data
data = collect(zip(rand([-1.0,1.0],5,5),rand([-1.0,1.0],5,5)))
names = [-1, -1/2, 0, 1/2, 1]
# this is the Highlighter which makes text red when signs differ.
# signs differ if their product is negative.
hl = Highlighter((d,i,j)->d[i,j][1]*d[i,j][2] < 0, crayon"red")
Then the Highlighter is used as follows:
pretty_table(data ; header = names, row_names= names, highlighters=hl)
Well, colors don't go through in text, so put an image of result.
This answer is beyond what was asked by the OP, but hopefully would be informative. In addition to Dan Getz's answer, one can apply more than one rule for highlighting the values. For example, if you want to make pairs with positive value green besides the first rule, you can pass a tuple of Highlighter to the highlighters keyword argument.
I will use Dan's example to show you the results:
julia> hl = (
Highlighter((d,i,j)->d[i,j][1]*d[i,j][2]<0, crayon"red"),
Highlighter((d,i,j)->d[i,j][1]>0 && d[i,j][2]>0, crayon"green")
)
The result of pretty_table(data; header=names, row_names=names, highlighters=hl) would be:

Rstudio and ggplot, stacked bar graph: Two almost identical bits of code, fct_reorder works with one, and not the other?

I wrote this which perfectly reorders the variable mcr_variant by count on my bar graph.
mcrxgenus %>%
mutate(mcr_variant = fct_reorder(mcr_variant, count)) %>%
ggplot( aes(fill=isolate_genus, y=count, x=mcr_variant)) +
geom_bar(position="stack", stat="identity") +
coord_flip() +
labs(x="MCR variant", y="Count", fill="Isolate genus")
I wrote this to display the same dataset a bit differently.
mcrxgenus %>%
mutate(isolate_genus = fct_reorder(isolate_genus, count)) %>%
ggplot( aes(fill=mcr_variant, y=count, x=isolate_genus)) +
geom_bar(position="stack", stat="identity")+
coord_flip()+
labs(x="Isolate genus", y="Count", fill="MCR variant")
It does NOT reorder my bar graph by count. I have absolutely no idea what is going on. It seems to me there should be no reason for this. mcr_variant and isolate_genus are both categorical variables. mcr_variant has 12 levels and isolate_genus has 6 possible levels. That is the only difference I can think of. Anyone run in to this problem before? It's been driving me mad! I have no idea what is happening here!
When you stack bars up, you're adding their values. fct_reorder, by default, takes the median of the values. So if MCR Variant A has counts 1, 1, 1, 2, 1, its order will be determined by the median count, 1, while its height is the sum of the counts, 6. Meanwhile if MCR Variant B has counts 2, 3, its order will be the median 2.5, but its sum is 5.
You need to make fct_reorder use sum, just like your stacked bar graph. Replace fct_reorder(isolate_genus, count) with fct_reorder(isolate_genus, count, sum).
If this doesn't work, please share a reproducible sample of data, preferably with dput so the classes are preserved and everything is copy/pasteable e.g., dput(mcrxgenus[1:10, ]) for the first 10 rows. Pick a suitable sample to illustrate the problem.

icCube: multiple dimensions in MDX output

The documentation of icCube states:
However, a SELECT is not limited to two axes. We could have columns,
rows, pages, chapters, and sections. And you could still continue
beyond these by specifying a number for the axis.
Indeed, when I try using three dimensions on the demo Sales cube, it works:
select
{[paris], [london]} on 0,
{[2005], [2006]} on 1,
product.members on 2
from sales
However, when I try four dimensions:
select
{[paris], [london]} on 0,
{[2005], [2006]} on 1,
product.members on 2,
measures.members on 3
from sales
I get an error message: Unexpected number of axes (4) for the pivot table (expected:0..3)
What am I missing?
There is nothing wrong with using a 4 axes query. However, it is left up to the client your are using to be able to display it.
For example, Excel accepts 2D results, the icCube pivot table is able to display results up to (and including) 3 axes.
Hope that helps.

rdlc (piechart) customized colors for different segments

I have written a code like below, my problem was sometimes I am getting same color for different segments in the piechart if the value came from the same range.
ref:
rdlc expression iif use?
my code sample
=SWITCH(Fields!ID__Share_of_Costs.Value <= 0.99, "Yellow",
Fields!ID__Share_of_Costs.Value <= 30, "Teal",
Fields!ID__Share_of_Costs.Value <= 60, "SteelBlue",
Fields!ID__Share_of_Costs.Value <= 100, "Crimson",
)
for eg: suppose my chart value is dynamic and it will come like 22 and 29, in this case the segment will show the same color (<= 30, "Teal",) as it is difficult to differntiate. Is there is any way to give different colors for each segment like no repeated color ?
Thanks in advance...cheers
It depends what do you mean by segment (range of values or particular value) or in other words is number of your segments limited?
If yes, then you could set up as much segments as you want with particular color.
If no, then write a custom function which will return hex value as your color.

Convert a Dynamic[] construct to a numerical list

I have been trying to put together something that allows me to extract points from a ListPlot in order to use them in further computations. My current approach is to select points with a Locator[]. This works fine for displaying points, but I cannot figure out how to extract numerical values from a construct with head Dynamic[]. Below is a self-contained example. By dragging the gray locator, you should be able to select points (indicated by the pink locator and stored in q, a list of two elements). This is the second line below the plot. Now I would like to pass q[[2]] to a function, or perhaps simply display it. However, Mathematica treats q as a single entity with head Dynamic, and thus taking the second part is impossible (hence the error message). Can anyone shed light on how to convert q into a regular list?
EuclideanDistanceMod[p1_List, p2_List, fac_: {1, 1}] /;
Length[p1] == Length[p2] :=
Plus ## (fac.MapThread[Abs[#1 - #2]^2 &, {p1, p2}]) // Sqrt;
test1 = {{1.`, 6.340196001221532`}, {1.`,
13.78779876355869`}, {1.045`, 6.2634018978377295`}, {1.045`,
13.754947081416544`}, {1.09`, 6.178367702583522`}, {1.09`,
13.72055251752498`}, {1.135`, 1.8183153704413153`}, {1.135`,
6.082497198000075`}, {1.135`, 13.684582525399742`}, {1.18`,
1.6809452373465104`}, {1.18`, 5.971583107298081`}, {1.18`,
13.646996905469383`}, {1.225`, 1.9480537697339537`}, {1.225`,
5.838386922625636`}, {1.225`, 13.607746407088161`}, {1.27`,
2.1183174369679234`}, {1.27`, 5.669799095595362`}, {1.27`,
13.566771130126131`}, {1.315`, 2.2572975468163463`}, {1.315`,
5.444014254828522`}, {1.315`, 13.523998701347882`}, {1.36`,
2.380307009155079`}, {1.36`, 5.153024664297602`}, {1.36`,
13.479342200528283`}, {1.405`, 2.4941312539733285`}, {1.405`,
4.861423833512566`}, {1.405`, 13.432697814928654`}, {1.45`,
2.6028066447609426`}, {1.45`, 4.619367407525507`}, {1.45`,
13.383942212133244`}};
DynamicModule[{p = {1.2, 10}, q = {1.3, 11}},
q := Dynamic#
First#test1[[
Ordering[{#, EuclideanDistanceMod[p, #, {1, .1}]} & /# test1,
1, #1[[2]] < #2[[2]] &]]];
Grid[{{Show[{ListPlot[test1, Frame -> True, ImageSize -> 300],
Graphics#Locator[Dynamic[p]],
Graphics#
Locator[q, Appearance -> {Small},
Background -> Pink]}]}, {Dynamic#p}, {q},{q[[2]]}}]]
There are several ways to extract values from a dynamic expression. What you probably want is Setting (documentation), which resolves all dynamic values into their values at the time Setting is evaluated.
In[75]:= Slider[Dynamic[x]] (* evaluate then move the slider *)
In[76]:= FullForm[Dynamic[x]]
Out[76]//FullForm= Dynamic[x]
In[77]:= FullForm[Setting[Dynamic[x]]]
Out[77]//FullForm= 0.384`
Here's a slightly more complicated example:
DynamicModule[{x},
{Dynamic[x], Slider[Dynamic[x]],
Button["Set y to the current value of x", y = Setting[Dynamic[x]]]}
]
If you evaluate the above expression, move the slider and then click the button, the current value of x as set by the slider is assigned to y. If you then move the slider again, the value of y doesn't change until you update it again by clicking the button.
Instead of assigning to a variable, you can of course paste values into the notebook, call a function, export a file, etc.
After a little more research, it appears that the answer revolves around the fact that Dynamic[] is a wrapper for updating and displaying the expression. Any computations that you want dynamically updated must be placed inside the wrapper: for instance, instead of doing something like q = Dynamic[p] + 1 one should use something like Dynamic[q = p + 1; q]}]. For my example, where I wanted to split q into two parts, here's the updated code:
DynamicModule[{p = {1.2, 10}, q = {1.3, 11}, qq, q1, q2},
q := Dynamic[
qq = First#
test1[[Ordering[{#, EuclideanDistanceMod[p, #, {1, .1}]} & /#
test1, 1, #1[[2]] < #2[[2]] &]]];
{q1, q2} = qq;
qq
];
Grid[{{Show[{ListPlot[test1, Frame -> True, ImageSize -> 300],
Graphics#Locator[Dynamic[p]],
Graphics#
Locator[q, Appearance -> {Small},
Background -> Pink]}]}, {Dynamic#p}, {Dynamic#q}, {Dynamic#
q1}}]]
If I am still missing something, or if there's a cleaner way to do this, I welcome any suggestions...