Cartesian product for an array of json objects - cartesian-product

How can I produce all of the combinations of the values in an array of the of Objects.
var input = [
{ "colour" : "red",
"material" : "cotton" ,
"shape" : "round"
},
{ "colour" : "green",
"material" : "wool" ,
"shape" : "square"
}
];
The expected output is the Cartesian product of all the options available, creating a new array with the same keys.
var expected = [
{ 'colour': 'red', 'material': 'cotton', 'shape': 'round' },
{ 'colour': 'red', 'material': 'cotton', 'shape': 'square' },
{ 'colour': 'red', 'material': 'wool', 'shape': 'round' },
{ 'colour': 'red', 'material': 'wool', 'shape': 'square' },
{ 'colour': 'green', 'material': 'cotton', 'shape': 'round' },
{ 'colour': 'green', 'material': 'cotton', 'shape': 'square' },
{ 'colour': 'green', 'material': 'wool', 'shape': 'round' },
{ 'colour': 'green', 'material': 'wool', 'shape': 'square' }
];

I would approach this in two steps:
Step 1: collect all values for each key
var options = {};
input.forEach(function (item)
{
for (var prop in item)
{
if (options[prop])
{
options[prop].push(item[prop]);
} else
{
options[prop] = [item[prop]];
}
}
});
console.log(options);
Which will give
{ colour: [ 'red', 'green' ],
material: [ 'cotton', 'wool' ],
shape: [ 'round', 'square' ] }
Step 2: Create the cartesian product by using a triple-nested iteration:
let result = [{}];
for (var prop in options)
{
// For each object in the existing list...
result = result.map(function (object)
{
// ... create copies of that object for each option for the current key
return options[prop].map(function (option)
{
let newObject = Object.assign({}, object)
newObject[prop] = option
return newObject
})
})
// The result is an array of arrays of objects, so flatten it
.flat()
}
console.log(result)
Which gives
[ { colour: 'red', material: 'cotton', shape: 'round' },
{ colour: 'red', material: 'cotton', shape: 'square' },
{ colour: 'red', material: 'wool', shape: 'round' },
{ colour: 'red', material: 'wool', shape: 'square' },
{ colour: 'green', material: 'cotton', shape: 'round' },
{ colour: 'green', material: 'cotton', shape: 'square' },
{ colour: 'green', material: 'wool', shape: 'round' },
{ colour: 'green', material: 'wool', shape: 'square' } ]

Related

what does {} mean in hook state initialization react

I have seen a hook state initalize in this way.
const colors = [
{ value: 'ocean', text: 'Ocean', color: '#00B8D9' },
{ value: 'blue', text: 'Blue', color: '#0052CC' },
{ value: 'purple', text: 'Purple', color: '#5243AA' },
]
const [{ formBasicUsageExample }, setState] = useState({ formBasicUsageExample: colors[0] })
This looks strange to me. I never see people use {} to surround a state here. How to understand the {} around formBasicUsageExample? What is the effect here
I understand the concept of destructuring, and it looks it may be deal with that. But I dont get it in such context(normally in para list). Appreciate if a basic example provide to understand it.
Javascript Object destructuring
You are setting the state variable value with { formBasicUsageExample: { value: 'ocean', text: 'Ocean', color: '#00B8D9' }}
Then using javascript object destructuring to only fetch the value of formBasicUsageExample key.
const colors = [
{ value: 'ocean', text: 'Ocean', color: '#00B8D9' },
{ value: 'blue', text: 'Blue', color: '#0052CC' },
{ value: 'purple', text: 'Purple', color: '#5243AA' },
]
const [{ formBasicUsageExample }, setState] = useState({ formBasicUsageExample: colors[0] });
Here, formBasicUsageExample contains :
{ value: 'ocean', text: 'Ocean', color: '#00B8D9' }
If there is no {} provided,
const colors = [
{ value: 'ocean', text: 'Ocean', color: '#00B8D9' },
{ value: 'blue', text: 'Blue', color: '#0052CC' },
{ value: 'purple', text: 'Purple', color: '#5243AA' },
]
const [formBasicUsageExample , setState] = useState({ formBasicUsageExample: colors[0] });
Here, formBasicUsageExample contains :
{ formBasicUsageExample: { value: 'ocean', text: 'Ocean', color: '#00B8D9' }}
Simple Javascript example:
let person = { firstName: 'John', lastName: 'Doe'};
const {firstName} = person;
console.log(firstName);
What's being done is technically destructuring.
It's just being done in a confusing manner. What formBasicUsageExample refers to on the left hand side of the assignment is the value in the returned object associated with that key, so in our case it will be whatever the value of colors[0] is.

How to pass data in Laravel with Chart.js

I want to display total of men and woman from learnings table in chart using Chartjs in Laravel.
My controller
public function index()
{
$men_learning = DB::table('learnings')->where('active', 1)->whereYear('created_at', $year)->sum('men');
$women_learning = DB::table('learnings')->where('active', 1)->whereYear('created_at', $year)->sum('women');
$learning = $men_learning + $women_learning ;
return view('home', compact('learning'));
}
My script in blade view.
<script>
var ctx = document.getElementById('myChart').getContext('2d');
var myChart = new Chart(ctx, {
type: 'bar',
data: {
labels: ['total'],
datasets: [{
label: '# of Votes',
data: [12],
backgroundColor: [
'rgba(255, 99, 132, 0.2)',
],
borderColor: [
'rgba(255, 99, 132, 1)',
],
borderWidth: 1
}]
},
options: {
scales: {
yAxes: [{
ticks: {
beginAtZero: true
}
}]
}
}
});
</script>
How can I propagate loaded statistic from my script to the chart?
public function index()
{
$men_learning = DB::table('learnings')->where('active', 1)->whereYear('created_at', $year)->sum('men');
$women_learning = DB::table('learnings')->where('active', 1)->whereYear('created_at', $year)->sum('women');
return view('home', compact('men_learning', 'women_learning'));
}
<script type="text/javascript">
$(function(){
var ctx = document.getElementById("myChart").getContext('2d');
var myChart = new Chart(ctx, {
type: 'bar',
data: {
labels: ['Men', 'Women'],
datasets: [{
label: 'Men',
data: [{!!$men_learning!!}],
borderWidth: 2,
backgroundColor: 'rgba(40,167,69,1)',
borderWidth: 0,
borderColor: 'transparent',
pointBorderWidth: 0 ,
pointRadius: 3.5,
pointBackgroundColor: 'transparent',
pointHoverBackgroundColor: 'rgba(254,86,83,.8)',
},
{
label: 'Women',
data: [{!!$women_learning!!}],
borderWidth: 2,
backgroundColor: 'rgba(220,53,69,.8)',
borderWidth: 0,
borderColor: 'transparent',
pointBorderWidth: 0,
pointRadius: 3.5,
pointBackgroundColor: 'transparent',
pointHoverBackgroundColor: 'rgba(63,82,227,.8)',
}]
},
options: {
legend: {
display: false
},
scales: {
yAxes: [{
gridLines: {
display: true,
drawBorder: false,
color: '#f2f2f2',
},
ticks: {
beginAtZero: true,
stepSize: 100,
callback: function(value, index, values) {
return value;
}
}
}],
xAxes: [{
gridLines: {
display: false,
tickMarkLength: 15,
}
}]
},
}
});
});
</script>

Radar chart mi and max axis values

How to limit my Radar chart from 0 to 10?
I have tried to set minimum and maximum keys, but this did not work.
am4core.useTheme(am4themes_animated);
const chart = am4core.createFromConfig({
'xAxes': [{
'type': 'CategoryAxis',
'minimum': 0,
'maximum': 10,
'dataFields': {
'category': 'category'
},
'tooltip': {
'disabled': true
},
'renderer': {
'labels': {
'fill': '#757575',
'fontSize': 12
}
}
}],
'yAxes': [{
'type': 'ValueAxis',
'minimum': 0,
'maximum': 10,
'tooltip': {
'disabled': true
},
'renderer': {
'labels': {
'fill': '#757575',
'fontSize': 12
}
}
}],
'legend': {
'position': 'bottom',
'fontSize': '1rem',
'fontWeight': '400'
},
'cursor': {},
'series': [
{
'type': 'RadarSeries',
'dataFields': {
'valueY': 'value1',
'categoryX': 'category'
},
'fill': '#4a90e2',
'stroke': '#4a90e2',
'strokeWidth': 3,
'tooltipText': '{valueY}',
'renderer': {
'tooltip': {
'fill': '#fff'
}
},
'tooltip': {
'getFillFromObject': false,
'background': {
'fill': '#4a90e2'
}
},
'name': 'Средняя оценка',
'bullets': [{
'type': 'CircleBullet'
}]
}],
'data': data.map(el => {
return {
'category': el.sLabel,
'value1': el.iRating
};
})
}, 'radar-chart', am4charts.RadarChart);
I expect my chart axis to always start at 0 and end at 10.

Setting a min & max on the axis

I am using anuglar-nvd3 and can't find any documentation on the API. The issue I'm facing right now is how can I set a maximum & minimum value on the xAxis and yAxis.
In other words regardless of the data, the axis would have a minimum of -1 and a maximum of 1.
Plunker: http://plnkr.co/edit/LKt3UJe5PnJOf8uQEwxr?p=preview
Code:
var app = angular.module('plunker', ['nvd3']);
app.controller('MainCtrl', function($scope) {
$scope.options = {
chart: {
type: 'scatterChart',
height: 450,
color: d3.scale.category10().range(),
scatter: {
onlyCircles: false
},
showDistX: true,
showDistY: true,
tooltipContent: function(key) {
return '<h3>' + key + '</h3>';
},
duration: 350,
xAxis: {
scale: [0,5],
axisLabel: 'X Axis',
tickFormat: function(d){
return d3.format('.02f')(d);
}
},
yAxis: {
axisLabel: 'Y Axis',
tickFormat: function(d){
return d3.format('.02f')(d);
},
axisLabelDistance: -5
},
zoom: {
//NOTE: All attributes below are optional
enabled: false,
scaleExtent: [1, 10],
useFixedDomain: false,
useNiceScale: false,
horizontalOff: false,
verticalOff: false,
unzoomEventType: 'dblclick.zoom'
},
margin: {
top: 100,
right: 100,
left: 100,
bottom: 100
}
}
};
$scope.data = [
{
"key":"static",
"color":"#fff",
"values":[
{
"x":-1,
"y":-1,
"size":0.0000001,
"shape":"circle",
"series":0
},
{
"x":1,
"y":1,
"size":0.0000001,
"shape":"circle",
"series":0
}
]
},
{
"key":"Group 0",
"color":"#1f77b4",
"values":[
{
"x":-0.5,
"y":-0.5,
"size":0.5,
"shape":"circle",
"series":0
}
]
},
{
"key":"Group 1",
"color":"#ff7f0e",
"values":[
{
"x":-0.5,
"y":0.5,
"size":0.5,
"shape":"circle",
"series":0
}
]
},
{
"key":"Group 2",
"color":"#2ca02c",
"values":[
{
"x":0.5,
"y":-0.5,
"size":0.5,
"shape":"circle",
"series":0
}
]
},
{
"key":"Group 3",
"color":"#d62728",
"values":[
{
"x":0.5,
"y":0.5,
"size":0.5,
"shape":"circle",
"series":0
}
]
}
];
});
Have you tried
forceY: [-1, 1],
forceX: [-1, 1],

change legend color using highchart-ng

I want to change legend color in highchart. I have used highchart-ng, the below code its not working, Please help me
scope.dvBarChartNG = {
options: {
chart: {
type: 'column',
backgroundColor: '#ffffff'
}
},
xAxis: {
},
yAxis: {
},
plotOptions: {
column: {
pointPadding: 0.2,
borderWidth: 0
},
},
*legend: {
itemStyle: {
'color' : '#000'
},
itemHoverStyle: {
'color' : '#000'
},
itemHiddenStyle: {
'color' : '#000'
}
},*
series: [{
name: 'xxxx',
data: scope.yyyy.series.user,
color: '#cfffac',
borderColor: "#cfffac",
showInLegend: true
}, {
name: 'xxxx',
data: scope.yyyy.series.entity,
color: '#82c84c',
borderColor: "#82c84c",
showInLegend: true
}]
};
my html code is

Resources