D3 Multi-line Chart with categorical data on X-axis - d3.js

I'm trying to draw a multiple line chart by using company branches data consisting of week days and values.
var data = [
{
"label": "branch-1",
"values": [
{ "day": "Monday", "value": 20},
{ "day": "Tuesday", "value": 18},
{ "day": "Wednesday", "value": 29},
{ "day": "Thursday", "value": 31},
{ "day": "Friday", "value": 37},
{ "day": "Saturday", "value": 25},
{ "day": "Sunday", "value": 19}
]
},
{
"label": "branch-2",
"values": [
{ "day": "Monday", "value": 32},
{ "day": "Tuesday", "value": 29},
{ "day": "Wednesday", "value": 37},
{ "day": "Thursday", "value": 41},
{ "day": "Friday", "value": 31},
{ "day": "Saturday", "value": 28},
{ "day": "Sunday", "value": 17}
]
}
]
FIDDLE LINK
Firstly, I'm having problem with days on x-axis. Lines are not generated due to "d" attributes have Nan values instead of correct x-coordinates on svg paths. I know that I should not use minX, maxX for x.domain, but I can't find what to use instead.
Secondly, I use d3.scale.category20() function to generate colors. But I'd like to know if I can use more than 20 colors in case of having more than 20 branches. Is it possible by using CSS?
Thanks in advance.

Ordinal scales need to have their domain set explicitly, that is, there's no notion of "min" or "max" for an ordinal domain. You have to do something like
x.domain(data[0].values.map(function (d) { return d.day; }));
Complete demo here. As for the colours, you can set anything you want in CSS or in the D3 code.

Related

How to use field value as a color value in vega lite pie chart?

I came across this pie chart vega lite visualization:
{
"$schema": "https://vega.github.io/schema/vega-lite/v5.json",
"description": "A simple pie chart with labels.",
"data": {
"values": [
{"category": "a", "value": 4},
{"category": "b", "value": 6},
{"category": "c", "value": 10},
{"category": "d", "value": 3},
{"category": "e", "value": 7},
{"category": "f", "value": 8}
]
},
"encoding": {
"theta": {"field": "value", "type": "quantitative", "stack": true},
"color": {"field": "category", "type": "nominal", "legend": null}
},
"layer": [{
"mark": {"type": "arc", "outerRadius": 80}
}, {
"mark": {"type": "text", "radius": 90},
"encoding": {
"text": {"field": "category", "type": "nominal"}
}
}]
}
It renders as follows:
My data contains color key:
{
"$schema": "https://vega.github.io/schema/vega-lite/v5.json",
"description": "A simple pie chart with labels.",
"data": {
"values": [
{"category": "a", "value": 4, "color": "rgb(121, 199, 227)"},
{"category": "b", "value": 6, "color": "rgb(140, 129, 22)"},
{"category": "c", "value": 10, "color": "rgb(96, 43, 199)"},
{"category": "d", "value": 3, "color": "rgb(196, 143, 99)"},
{"category": "e", "value": 7, "color": "rgb(12, 103, 19)"},
{"category": "f", "value": 8, "color": "rgb(196, 243, 129)"}
]
},
"encoding": {
"theta": {"field": "value", "type": "quantitative", "stack": true},
"color": {"field": "color", "type": "nominal", "legend": null}
},
"layer": [{
"mark": {"type": "arc", "outerRadius": 80}
}, {
"mark": {"type": "text", "radius": 90},
"encoding": {
"text": {"field": "category", "type": "nominal"}
}
}]
}
I want to use the rgb() color value specified in this color key's value to color individual pie. I tried specifying this field in color channel: "field": "color".
"color": {"field": "color", "type": "nominal", "legend": null}
However, no use. It still renders the same as above. How can use color value specified in field's value as actual color?
PS: Link to above visualization.
The documentation says:
To directly encode the data value, the scale property can be set to null.
So you need to set the scale to null.
{
"$schema": "https://vega.github.io/schema/vega-lite/v5.json",
"description": "A simple pie chart with labels.",
"data": {
"values": [
{"category": "a", "value": 4, "color": "rgb(121, 199, 227)"},
{"category": "b", "value": 6, "color": "rgb(140, 129, 22)"},
{"category": "c", "value": 10, "color": "rgb(96, 43, 199)"},
{"category": "d", "value": 3, "color": "rgb(196, 143, 99)"},
{"category": "e", "value": 7, "color": "rgb(12, 103, 19)"},
{"category": "f", "value": 8, "color": "rgb(196, 243, 129)"}
]
},
"encoding": {
"theta": {"field": "value", "type": "quantitative", "stack": true},
"color": {"field": "color", "type": "nominal", "legend": null, "scale":null}
},
"layer": [
{"mark": {"type": "arc", "outerRadius": 80}},
{
"mark": {"type": "text", "radius": 90},
"encoding": {"text": {"field": "category", "type": "nominal"}}
}
]
}
This outputs:

LUIS consider TIME and DURATION as number ENTITY

I was expecting entities such DATE, TIME, DURATION & No Of People Joining the Call in below JSON.
Now i got entities back such as DATE,TIME and DURATION correctly but for No Of People there is problem for me.
I am getting four entities as NUMBER so now i am confused as how to pick exact entity that represent No Of People. Ideally it is No. 6, but i am not getting on which basis i should decide that 6 is the No Of People
{
"query": "book audio bridge tomorrow for 6 people for 30 mins starts at 5:30 PM",
"topScoringIntent": {
"intent": "BookAudioBridge",
"score": 0.9895838
},
"intents": [
{
"intent": "BookAudioBridge",
"score": 0.9895838
},
{
"intent": "ListBooking",
"score": 0.00677821552
}
],
"entities": [
{
"entity": "tomorrow",
"type": "builtin.datetimeV2.date",
"startIndex": 18,
"endIndex": 25,
"resolution": {
"values": [
{
"timex": "2018-06-21",
"type": "date",
"value": "2018-06-21"
}
]
}
},
{
"entity": "30 mins",
"type": "builtin.datetimeV2.duration",
"startIndex": 44,
"endIndex": 50,
"resolution": {
"values": [
{
"timex": "PT30M",
"type": "duration",
"value": "1800"
}
]
}
},
{
"entity": "5:30 pm",
"type": "builtin.datetimeV2.time",
"startIndex": 62,
"endIndex": 68,
"resolution": {
"values": [
{
"timex": "T17:30",
"type": "time",
"value": "17:30:00"
}
]
}
},
{
"entity": "6",
"type": "builtin.number",
"startIndex": 31,
"endIndex": 31,
"resolution": {
"value": "6"
}
},
{
"entity": "30",
"type": "builtin.number",
"startIndex": 44,
"endIndex": 45,
"resolution": {
"value": "30"
}
},
{
"entity": "5",
"type": "builtin.number",
"startIndex": 62,
"endIndex": 62,
"resolution": {
"value": "5"
}
},
{
"entity": "30",
"type": "builtin.number",
"startIndex": 64,
"endIndex": 65,
"resolution": {
"value": "30"
}
}
]
}
You could build a composite entity of number and simple entity for "people" so that the entity that comes back indicates 6 as number and simple entity name as people.

How to set locale (globally) in Vega

I would like to configure the locale for a visualization in Vega. I need a German number format, which means that I need a comma (,) as decimal separator. Without any configuration in Vega, a decimal point (.) is used as separator.
Here is a complete working example which shows a simple bar chart. As you can see in the image below, I want to format the numbers on the y-Axis and the values inside the bars with two decimal places.
How can I set a specific locale (e.g. German) either globally for the whole visualization or for each number format separately? (I would prefer a global setting.)
(Note: You can use the Vega Editor to paste and try out my bar chart example.)
{
"width": 600,
"height": 300,
"padding": {"top": 10, "left": 35, "bottom": 30, "right": 10},
"data": [
{
"name": "table",
"values": [
{"x": 1, "y": 0.5},
{"x": 2, "y": 0.8},
{"x": 3, "y": 0.3},
{"x": 4, "y": 0.6}
]
}
],
"scales": [
{
"name": "x",
"type": "ordinal",
"range": "width",
"domain": {"data": "table", "field": "x"},
"padding": 0.1
},
{
"name": "y",
"type": "linear",
"range": "height",
"domain": {"data": "table", "field": "y"}
}
],
"axes": [
{"type": "x", "scale": "x"},
{"type": "y", "scale": "y", "format": ".2f"}
],
"marks": [
{
"type": "rect",
"from": {"data": "table"},
"properties": {
"enter": {
"x": {"scale": "x", "field": "x"},
"width": {"scale": "x", "band": true, "offset": -1},
"y": {"scale": "y", "field": "y"},
"y2": {"scale": "y", "value": 0}
},
"update": {
"fill": {"value": "steelblue"}
}
}
},
{
"type": "text",
"from": {"mark": "bars"},
"properties": {
"enter": {
"y": {"field": "y", "offset": 10},
"x": {"field": "x"},
"dx": {"field": "width", "mult": 0.6},
"fill": {"value": "white"},
"align": {"value": "right"},
"baseline": {"value": "middle"},
"text": {"template": "{{datum.datum.y | number:'.2f'}}"}
}
}
}
]
}
I found a pull request in the Vega GitHub repository which allows to set the number and time locale. Also runtime changes are supported.
Example for changing the number format to German:
vg.util.format.numberLocale({
decimal: ",",
thousands: ".",
grouping: [3],
currency: ["", "\xa0€"]
});
Example for changing the time format to German:
vg.util.format.timeLocale({
dateTime: "%A, der %e. %B %Y, %X",
date: "%d.%m.%Y",
time: "%H:%M:%S",
periods: ["AM", "PM"], // unused
days: ["Sonntag", "Montag", "Dienstag", "Mittwoch", "Donnerstag", "Freitag", "Samstag"],
shortDays: ["So", "Mo", "Di", "Mi", "Do", "Fr", "Sa"],
months: ["Januar", "Februar", "März", "April", "Mai", "Juni", "Juli", "August", "September", "Oktober", "November", "Dezember"],
shortMonths: ["Jan", "Feb", "Mrz", "Apr", "Mai", "Jun", "Jul", "Aug", "Sep", "Okt", "Nov", "Dez"]
});

Blender exporter (v70) to json three.js skinning animations

It took me a few weeks to master the Blender to json skinning export process, but the final result was working great prior three.js exporter v70.
I had to rebuild my dev machine and I installed Blender three.js exporter v70.
Today, I am doing a simple update to a blender file with skinning animations that was working fine prior the update and I am not able to get multiple animations to export as before.
I have tested Blender exporter with a simple object and animations and I get the same results.
Exporter output:
{
"metadata": {
"bones": 3,
"faces": 10,
"vertices": 12,
"generator": "io_three",
"normals": 12,
"type": "Geometry",
"version": 3
},
"name": "CubeGeometry.3",
"bones": [{
"pos": [0.0,0.0,-0.0],
"name": "center",
"rotq": [0,0,0,1],
"parent": -1
},{
"pos": [0.0,1.0,-0.0],
"name": "right",
"rotq": [0,0,0,1],
"parent": 0
},{
"pos": [0.0,1.0,-0.0],
"name": "left",
"rotq": [0,0,0,1],
"parent": 0
}],
"animation": {
"name": "right",
"length": 4.125,
"hierarchy": [{
"keys": [{
"scl": [1,1,1],
"pos": [0.0,0.0,-0.0],
"rot": [0.0,0.0,-0.0,1.0],
"time": 0.0
},{
"pos": [0.0,0.0,-0.0],
"rot": [0.0,0.0,-0.0,1.0],
"time": 2.0416666666666665
},{
"scl": [1,1,1],
"pos": [0.0,0.0,-0.0],
"rot": [0.0,0.0,-0.0,1.0],
"time": 4.125
}],
"parent": -1
},{
"keys": [{
"scl": [1,1,1],
"pos": [0.0,1.0,-0.0],
"rot": [0.0,0.0,-0.0,1.0],
"time": 0.0
},{
"pos": [0.0,1.0,-0.0],
"rot": [0.0,0.0,0.33748486638069153,0.9413309693336487],
"time": 2.0416666666666665
},{
"scl": [1,1,1],
"pos": [0.0,1.0,-0.0],
"rot": [0.0,0.0,-0.0,1.0],
"time": 4.125
}],
"parent": 0
},{
"keys": [{
"scl": [1,1,1],
"pos": [0.0,1.0,-0.0],
"rot": [-6.0461669172803476e-15,1.149220128782158e-11,0.00026304941275157034,0.9999613761901855],
"time": 0.0
},{
"pos": [0.0,1.0,-0.0],
"rot": [-6.0461669172803476e-15,1.149220128782158e-11,0.00026304941275157034,0.9999613761901855],
"time": 2.0416666666666665
},{
"scl": [1,1,1],
"pos": [0.0,1.0,-0.0],
"rot": [-6.0461669172803476e-15,1.149220128782158e-11,0.00026304941275157034,0.9999613761901855],
"time": 4.125
}],
"parent": 1
}],
"fps": 24
},
"faces": [33,0,1,2,3,0,1,2,3,33,4,7,6,5,4,5,6,7,33,0,4,5,1,0,4,7,1,33,1,5,6,2,1,7,6,2,33,6,7,11,10,6,5,8,9,33,4,0,3,7,4,0,3,5,33,8,10,11,9,10,9,8,11,33,7,3,9,11,5,3,11,8,33,2,6,10,8,2,6,9,10,33,3,2,8,9,3,2,10,11],
"vertices": [1.8709198236465454,-0.05566583573818207,-0.9999999403953552,1.8709198236465454,-0.055665746331214905,0.9999999403953552,-0.12908029556274414,-0.055665746331214905,0.9999998211860657,-0.12907981872558594,-0.05566583573818207,-1.0000003576278687,1.8709203004837036,1.9443342685699463,-0.9999995231628418,1.8709192276000977,1.9443342685699463,1.0000004768371582,-0.12908053398132324,1.9443342685699463,0.9999995827674866,-0.12908011674880981,1.9443342685699463,-1.0000001192092896,-1.9279574155807495,-0.055665984749794006,0.9999994039535522,-1.9279569387435913,-0.05566607415676117,-1.0000007152557373,-1.9279578924179077,1.9443340301513672,0.9999991655349731,-1.9279574155807495,1.9443340301513672,-1.0000004768371582],
"influencesPerVertex": 2,
"skinWeights": [1.0,0,1.0,0,1.0,0,1.0,0,1.0,0,1.0,0,1.0,0,1.0,0,1.0,0,1.0,0,1.0,0,1.0,0],
"normals": [0.5773491859436035,-0.5773491859436035,-0.5773491859436035,0.5773491859436035,-0.5773491859436035,0.5773491859436035,0.0,-0.7070833444595337,0.7070833444595337,0.0,-0.7070833444595337,-0.7070833444595337,0.5773491859436035,0.5773491859436035,-0.5773491859436035,0.0,0.7070833444595337,-0.7070833444595337,0.0,0.7070833444595337,0.7070833444595337,0.5773491859436035,0.5773491859436035,0.5773491859436035,-0.5773491859436035,0.5773491859436035,-0.5773491859436035,-0.5773491859436035,0.5773491859436035,0.5773491859436035,-0.5773491859436035,-0.5773491859436035,0.5773491859436035,-0.5773491859436035,-0.5773491859436035,-0.5773491859436035],
"skinIndices": [2,0,2,0,0,0,0,0,2,0,2,0,0,0,0,0,1,0,1,0,1,0,1,0]
}
Any idea what should I be doing to get multiple skinning animations working again?
Cordially,
j3zusla
Until r71 is released it is advised to use the dev version. Multiple animations have been restored plus numerous optimizations, features, and bug fixes have been addressed (specifically to animation exports).

d3 json counting the most inner loop

{
"name": "Max",
"value": 107,
"children": [
{
"name": "Don",
"value": 60,
"children" [
{"name": "CC", "value": 25},
{"name": "Jim", "value": 35}
]
},
{
"name": "David",
"value": 47,
"children": [
{"name": "Jeff", "value": 32},
{"name": "Buffy", "value": 15}
]
}
]
}
How do you count the children of the group?
data.children.length;
does not seem to work.
(data is set to equal the js)
What you want is the number of leaves in a tree. You can recursively traverse the tree to do it:
getNumberOfLeaves = function (obj) {
if (obj.children) {
var res = 0;
obj.children.forEach(function(d){res+=getNumberOfLeaves(d)})
return res
}else {
return 1
}
}
jsFiddle: http://jsfiddle.net/chrisJamesC/PVhUn/

Resources