laravel 9 + Vue3 + (vue-google-charts) - laravel

I'm trying to display data from laravel backend into Vue3 (vue-google-charts), but I didn't find any resources
this is a sample if my json data comming from backend
[
{
interest_oid: 1,
total_cards: 2
},
{
interest_oid: 3,
total_cards: 1
},
{
interest_oid: 5,
total_cards: 2
},
{
interest_oid: 2,
total_cards: 1
},
{
interest_oid: 4,
total_cards: 1
},
{
interest_oid: 8,
total_cards: 1
}
]
i want to display the data in GChart of type "CulomnChart"
like this one
this is my Vue component
<div class="col-lg-6">
<GChart
type="ColumnChart"
:data="chartData"
:options="chartOptions"
style="height: 100%"/>
</div>
import {GChart} from "vue-google-charts";
export default {
components: {
GChart
},
props: {
contacts_per_interests: Array,
},
data() {
return {
chartData: this.contacts_per_interests,
chartOptions: {
width: 500,
height: 300,
chart: {
title: 'Company Performance',
subtitle: 'Sales, Expenses, and Profit: 2014-2017',
}
}
}
}
}
but it's display "× Table has no columns"
any resources or suggestions please?

Your contacts_per_interests array should be look like this:
[
['interest_oid', 'total_cards'],
[1, 2],
[3, 1],
[5, 2],
[2, 1],
[4, 1],
[8, 1],
]
Adjust accordingly. Like that:
$json = '[
{
"interest_oid": 1,
"total_cards": 2
},
{
"interest_oid": 3,
"total_cards": 1
},
{
"interest_oid": 5,
"total_cards": 2
},
{
"interest_oid": 2,
"total_cards": 1
},
{
"interest_oid": 4,
"total_cards": 1
},
{
"interest_oid": 8,
"total_cards": 1
}
]';
$jsonArr = json_decode($json);
$array = [['interest_oid', 'total_cards']];
foreach ($jsonArr as $item) {
array_push($array, [$item->interest_oid, $item->total_cards]);
}

Related

Change default behaviour of legend in Amchart 5

I'm trying to make this chart (https://www.amcharts.com/demos/reversed-value-axis/) in angular and need help in changing default behaviour of legend like if i click on any legend only that axis should be show other will hide unlike now if we click on any legend, that will hide.
is there any possible way to do it?
Default Chart
If click on one legend
Yes, of course...
To do that, you need a collection of all your series. You also need hide() and appear() methods.
Here is a possible implementation:
am5.ready(() => {
let root = am5.Root.new("chartdiv");
root.setThemes([
am5themes_Animated.new(root)
]);
let chart = root.container.children.push(am5xy.XYChart.new(root, {
layout: root.verticalLayout
}));
let data = [
{
year: "1930",
italy: 1,
germany: 5,
uk: 3
},
{
year: "1934",
italy: 1,
germany: 2,
uk: 6
},
{
year: "1938",
italy: 2,
germany: 3,
uk: 1
},
{
year: "1950",
italy: 3,
germany: 4,
uk: 1
},
{
year: "1954",
italy: 5,
germany: 1,
uk: 2
},
{
year: "1958",
italy: 3,
germany: 2,
uk: 1
},
{
year: "1962",
italy: 1,
germany: 2,
uk: 3
},
{
year: "1966",
italy: 2,
germany: 1,
uk: 5
},
{
year: "1970",
italy: 3,
germany: 5,
uk: 2
},
{
year: "1974",
italy: 4,
germany: 3,
uk: 6
},
{
year: "1978",
italy: 1,
germany: 2,
uk: 4
}
];
let xAxis = chart.xAxes.push(am5xy.CategoryAxis.new(root, {
categoryField: "year",
renderer: am5xy.AxisRendererX.new(root, {})
}));
xAxis.data.setAll(data);
let yAxis = chart.yAxes.push(am5xy.ValueAxis.new(root, {
renderer: am5xy.AxisRendererY.new(root, {
inversed: true
})
}));
function createSeries(name, field) {
let series = chart.series.push(am5xy.LineSeries.new(root, {
name,
xAxis,
yAxis,
categoryXField: "year",
valueYField: field
}));
series.bullets.push(() => {
return am5.Bullet.new(root, {
sprite: am5.Circle.new(root, {
radius: 5,
fill: series.get("fill")
})
});
});
series.data.setAll(data);
return series;
}
// The interesting part starts here!
let allSeries = [];
allSeries.push(createSeries("Italy", "italy"));
allSeries.push(createSeries("Germany", "germany"));
allSeries.push(createSeries("UK", "uk"));
let legend = chart.children.push(am5.Legend.new(root, {
centerX: am5.p50,
x: am5.p50
}));
legend.itemContainers.template.events.on("click", () => {
for (let series of allSeries) {
series.hide();
}
});
legend.data.setAll(chart.series.values);
document.getElementById("reset").addEventListener("click", () => {
for (let series of allSeries) {
series.appear();
}
});
});
#chartdiv {
width: 100%;
height: 350px;
}
#reset {
width: 50%;
display: block;
margin: auto;
cursor: pointer;
}
<script src="https://cdn.amcharts.com/lib/5/index.js"></script>
<script src="https://cdn.amcharts.com/lib/5/xy.js"></script>
<script src="https://cdn.amcharts.com/lib/5/themes/Animated.js"></script>
<div id="chartdiv"></div>
<button type="button" id="reset">Reset</button>

GeoShape not storing lat/lng

In ElasticSearch 5 I'm making a property:
var prop = new Property
{
Id = page.Id,
GeoLocation =
new PointGeoShape(
new GeoCoordinate((double) geoPoint.Latitude, (double) geoPoint.Longitude))
};
With a definition of:
public class Property
{
[Number]
public int Id { get; set; }
[GeoShape]
public PointGeoShape GeoLocation { get; set; }
}
When it's being sent to elastic it's got a type of point, and the lat and lng are both doubles.
However when it inserts into elastic and I get it back it looks like:
"GeoLocation": {
"coordinates": [
{
"s": -1,
"e": -2,
"c": [
7,
9,
0,
2,
8,
9,
9,
0,
2,
1,
3,
0,
3,
2,
5,
5,
7
]
},
{
"s": 1,
"e": 1,
"c": [
5,
1,
5,
2,
6,
5,
2,
0,
3,
7,
4,
7,
1,
9,
8,
3
]
}
],
"type": "point"
This was due to postman 'prettify', when I checked the raw response it was correct.

How to use export excel and print for selected columns in datatable?

I want to use export excel and print the datatable .I use following code but not able to use both options for selected columns.
$('#example').DataTable( {
dom: 'Blfrtip',
buttons: [
{
extend: 'excel','print',
exportOptions: {
columns: [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 ]
},
}
],
"lengthMenu": [[200, 250, 500, -1], [200, 250, 500, "All"]],
"bLengthChange" : true,
initComplete: function () {
this.api().columns().every( function () {
var column = this;
var select = $('<select><option value="">Select</option></select>')
.appendTo( $(column.footer()).empty() )
.on( 'change', function () {
var val = $.fn.dataTable.util.escapeRegex(
$(this).val()
);
column
.search( val ? '^'+val+'$' : '', true, false )
.draw();
} );
column.data().unique().sort().each( function ( d, j ) {
select.append( '<option value="'+d+'">'+d+'</option>' )
} );
} );
}
} );
I am not figure out what happens wrong.Please help me.
{
extend: 'excel'
exportOptions: {
columns: [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 ]
},
{
extend: 'print'
exportOptions: {
columns: [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 ]
}
One of the most commonly used is the columns option which defines the columns that should be used as part of the export. This is given as a column-selector, making it simple to tell it if you want only visible columns, or a mix of the columns available.
and this is a simple of how it can be done :
function attachDataTable(tableId: string) {
let companyFunctionTable = $('#' + tableId).DataTable({
dom: 'Bfrtip',
buttons: [
//'csvHtml5'
{
extend: 'csvHtml5',
exportOptions: {
columns: [0, 1, 2]
},
className: 'btn btn-sm btn-outline-secondary',
text: `<i class="fa fa-file-csv"></i> ${i18next.t('Export to CSV')}`, titleAttr: i18next.t('Export to CSV'),
bom: true
},
],
"initComplete": function (settings, json) {
placeToolbarButton(tableId);
},
"paging": globalVar.dataTablesSettings.paging,
"stateSave": false,
"stateSaveParams": function (settings, data: any) {
data.search.search = "";
},
"order": [],
"lengthChange": false,
"pageLength": globalVar.dataTablesSettings.pageLength,
"language": {
"url": globalVar.dataTablesSettings.languageUrl
}
});
}
Please fellow that link :
https://datatables.net/extensions/buttons/examples/html5/columns.html
Don't forget to include the required JS libraries needed to display the exporting functionality.
Just modify your buttons option this way
buttons: [
{
extend: 'excel',
exportOptions: {
columns: [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 ]
},
},
{
extend: 'print',
exportOptions: {
columns: [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 ]
},
},
]
elaborated #tdymicroit answer

RethinkDB: How to get data from dictionary field and transform them to lists

I have objects that have a following structure:
[
{
'record': 1,
'tags': [1, 2],
'data': {
'1': 10,
'2': 15
}
},
...
{
'record': 1,
'tags': [3, 4, 5],
'data': {
'1': 100,
'2': 150
}
}
]
How can I get distinct lists of data: [10, ..., 100] and [15, ..., 150] to process each of them?
Thanks
If all data have same key, I proposed this map-reduce process.
.map(function(doc) {
return doc('data').keys().map(function(key) {
return [key, [doc('data')(key)]]
}).coerceTo('object')
})
.reduce(function(left, right) {
return left.keys().map(function(key) {
return [key, left(key).setUnion(right(key))]
})
.coerceTo('object')
})
With your data set, we have:
r.expr(
[
{
'record': 1,
'tags': [1, 2],
'data': {
'1': 10,
'2': 15
}
},
{
'record': 1,
'tags': [1, 2],
'data': {
'1': 19,
'2': 100
}
},
{
'record': 1,
'tags': [3, 4, 5],
'data': {
'1': 100,
'2': 150
}
}
]
)
.map(function(doc) {
return doc('data').keys().map(function(key) {
return [key, [doc('data')(key)]]
}).coerceTo('object')
})
.reduce(function(left, right) {
return left.keys().map(function(key) {
return [key, left(key).setUnion(right(key))]
})
.coerceTo('object')
})
Which produces:
{
"1": [10, 19, 100],
"2": [15, 100, 150]
}
You can call values on an object to get an array of values from it: https://www.rethinkdb.com/api/javascript/#values
If not all of the indexes are present as keys, you could write something like r.range(obj.keys().max()).map(function(i) { return obj(i.coerceTo('string')).default(nil); }).
If the data fields could be any string, something like this could work:
r.db('test')
.table('test')
.getAll(1, {index: 'record'})
.getField('data')
.concatMap(r.row.coerceTo('array'))
.group(r.row(0))
.concatMap([r.row(1)])
.ungroup()
Otherwise, mlucy's suggestion might be used to make it more efficient.
EDIT: on further experimentation, I could not get obj.keys() to be faster than group.

RethinkDB - How to merge and average objects

I have documents structured as follows:
[
{
'post': 1,
'tags': [1, 2],
'poll': {
'a': 2,
'b': 2
}
},
{
'post': 1,
'tags': [3],
'poll': {
'a': 4,
'b': 6
}
},
]
How can I merge them so that tags will be the union of all tags across these posts and poll will be averaged? From the example above the result should look like this:
[
{
'post': 1,
'tags': [1, 2, 3],
'poll': {
'a': 3,
'b': 4
}
}
]
Thanks.
I think we can use map-reduce since we turn many document into a single one. Something like this should works:
r.expr([
{
'post': 1,
'tags': [1, 2],
'poll': {
'a': 2,
'b': 2
}
},
{
'post': 1,
'tags': [3],
'poll': {
'a': 4,
'b': 6,
}
},
])
.merge(function(doc) {
return {
'poll': doc('poll').coerceTo('array').map(function(poll) { return [poll(0), [poll(1)]] }).coerceTo('object')
}
})
.reduce(function(left, right) {
return {
'post': left('post'),
'tags': left('tags').setUnion(right('tags')),
'poll': left('poll').keys().setUnion(right('poll').keys()).map(function(k) {
return [k,
left('poll')(k).default([]).union(right('poll')(k).default([]))
]
})
}
})
.merge(function(doc) {
return {
poll: doc('poll').map(function(poll) {
return [poll(0), poll(1).avg()]
}).coerceTo('object')
}
})
This produces:
{
"poll": {
"a": 3,
"b": 4
},
"post": 1,
"tags": [1, 2, 3]
}
Fixed version originally proposed by kureikain:
r.expr([
{
'post': 1,
'tags': [1, 2],
'poll': {
'a': 2,
'b': 2
}
},
{
'post': 1,
'tags': [3],
'poll': {
'a': 4,
'b': 6,
}
},
{
'post': 1,
'tags': [2],
'poll': {
'a': 1,
'b': 3,
}
},
])
.merge(function(doc) {
return {
'poll': doc('poll').coerceTo('array').map(function(poll) { return [poll(0), [poll(1)]] }).coerceTo('object')
}
})
.reduce(function(left, right) {
return {
'post': left('post'),
'tags': left('tags').setUnion(right('tags')),
'poll': left('poll').keys().map(function(k) {
return [k, left('poll')(k).default([]).union(right('poll')(k).default([]))]
}).coerceTo('object')
}
})
.merge(function(doc) {
return {
poll: doc('poll').keys().map(function(k) {
return [k, doc('poll')(k).avg()]
}).coerceTo('object')
}
})

Resources