How to watch directions of pagination in v-data-table vuetify - pagination

I need make different get requests to backend
depending on whether I move forward or backward on the table. What event I must use and how to make this?

You could use #pagination event from data-table. It emits an object like this:
{page: 2, itemsPerPage: 5, pageStart: 5, pageStop: 10, pageCount: 2}
So you can capture it in a custom method and determine if 'page' value is higher or lower than current value in order to know if is forward or backward. An example:
<template>
<div>
<v-data-table
:headers="headers"
:items="desserts"
:items-per-page="5"
class="elevation-1"
#pagination="writeLog"
></v-data-table>
</div>
</template>
<script>
export default {
data () {
return {
headers: [
{
text: 'Dessert (100g serving)',
align: 'start',
sortable: false,
value: 'name',
},
{ text: 'Calories', value: 'calories' },
{ text: 'Fat (g)', value: 'fat' },
{ text: 'Carbs (g)', value: 'carbs' },
{ text: 'Protein (g)', value: 'protein' },
{ text: 'Iron (%)', value: 'iron' },
],
desserts: [
{
name: 'Frozen Yogurt',
calories: 159,
fat: 6.0,
carbs: 24,
protein: 4.0,
iron: '1%',
},
{
name: 'Ice cream sandwich',
calories: 237,
fat: 9.0,
carbs: 37,
protein: 4.3,
iron: '1%',
},
{
name: 'Eclair',
calories: 262,
fat: 16.0,
carbs: 23,
protein: 6.0,
iron: '7%',
},
{
name: 'Cupcake',
calories: 305,
fat: 3.7,
carbs: 67,
protein: 4.3,
iron: '8%',
},
{
name: 'Gingerbread',
calories: 356,
fat: 16.0,
carbs: 49,
protein: 3.9,
iron: '16%',
},
{
name: 'Jelly bean',
calories: 375,
fat: 0.0,
carbs: 94,
protein: 0.0,
iron: '0%',
},
{
name: 'Lollipop',
calories: 392,
fat: 0.2,
carbs: 98,
protein: 0,
iron: '2%',
},
{
name: 'Honeycomb',
calories: 408,
fat: 3.2,
carbs: 87,
protein: 6.5,
iron: '45%',
},
{
name: 'Donut',
calories: 452,
fat: 25.0,
carbs: 51,
protein: 4.9,
iron: '22%',
},
{
name: 'KitKat',
calories: 518,
fat: 26.0,
carbs: 65,
protein: 7,
iron: '6%',
},
],
}
currentPage = 1
},
methods: {
writeLog(paginationObject){
let action
console.log(paginationObject);
this.currentPage < paginationObject.page ? action = 'forward' : action ='backguard'
this.currentPage = paginationObject.page
console.log(action)
//Write code to call your backend using action...
}
},
}
</script>

Related

Issue in implementing ChartJS in my new assignment of ReactJS App, please let me know where I am wrong

I want to upload a chart in my react app but when I use this Barchar.jsx component (have used js nothing changed) it is showing following errors in console(are in image.)
import React from "react";
import { Bar, Chart } from "react-chartjs-2";
import {Chart as ChartJS, BarElement } from 'chart.js';
ChartJS.register(BarElement)
const BarChart = () => {
var data = {
labels: ['RCB', 'MI', 'RR', 'SRH', 'CSK', 'KXIP', 'DD', 'DCH', 'GL', 'RPS', 'KKR'],
datasets: [{
label: '# of toss wins',
data: [70, 85, 63, 35, 66, 68, 72, 43, 15, 6, 78],
backgroungColor: ['red', 'lightblue', 'pink', 'orange', 'yellow', 'gold', 'blue', 'black', 'gold', 'voilet', 'purple' ],
borderWidth: 1
}]
};
var options = {
maintainAspectRatio: false,
scales: {
y:{
beginAtZero: true
}
},
};
return (
<div>
<Bar
data= {data}
height={400}
width={600}
options={options}
/>
</div>
)
};
export default BarChart ;
Its because you try to use treeshaking but import and register only the element of the bar while chart.js needs a lot more.
For ease of use you are best of to change
import {Chart as ChartJS, BarElement } from 'chart.js';
ChartJS.register(BarElement)
into:
import 'chart.js/auto'
If you really want to use treeshaking you should look at the docs and import and register everything you are using:
https://www.chartjs.org/docs/3.7.1/getting-started/integration.html#bundlers-webpack-rollup-etc
SO BASSICALLY MISTAKE WHICH I DID WAS I DIDN'T IMPORT THIS BARCHART IN APPJS.CORRECT CCODE WILL BE LIKE THIS (FOR BARCHART.JSX) YOU CAN CHANGE IN IT MANY THINGS.
import React from "react";
import { Bar } from "react-chartjs-2";
import 'chart.js/auto'
const BarChart =() => {
return <div>
<Bar
options={{
scales: {
y: {
beginAtZero: true
}
}
}}
data ={{
labels: ['RCB', 'MI', 'RR', 'SRH', 'CSK', 'KXIP', 'DD', 'DCH', 'GL', 'RPS', 'KKR'],
datasets: [{
label: '# of toss wins',
data: [70, 85, 63, 35, 66, 68, 72, 43, 15, 6, 78],
backgroundColor: ['red', 'Blue', '#ff64dc', '#fc7404', 'yellow', 'red', '#000080', 'silver', 'gold', '#9400d3', 'purple' ],
borderColor:['black'],
borderWidth: 1
},
{
label: '# of match wins',
data: [73, 92, 63, 42, 79, 70, 62, 29, 13, 10, 77],
backgroundColor: ['#C9920E', 'Blue', '#ff64dc', '#fc7404', 'yellow', 'lightgrey', '#000080', 'silver', 'gold', '#9400d3', 'purple' ],
borderColor:['black'],
borderWidth: 1
}
]
}}
/></div>
};
export default BarChart ;

Post request not getting req.body

I am trying to make a post request with Postman. The only thing that returns is the id made by uuidv4() all the other information for the JSON object is not there.
I have tried req.body and req.apiGateway.event.body with no luck.
This is my post request:
const serverless = require("serverless-http");
const express = require("express");
const app = express();
// const bodyParser = require("body-parser");
const AWS = require("aws-sdk");
const db = new AWS.DynamoDB.DocumentClient();
const { v4: uuidv4 } = require("uuid");
app.use(express.urlencoded());
app.use(express.json());
// app.use(bodyParser.urlencoded({ extended: true }));
// app.unsubscribe(bodyParser.json());
app.post("/inspections", async (req, res) => {
const data = req.apiGateway.event.body;
const params = {
TableName: "inspectionTrackTable",
Item: {
id: uuidv4(),
unitNum: data.unitNum,
building: data.building,
managed: data.managed,
rental: data.rental,
inHouse: data.inHouse,
robeCount: data.robeCount,
inspected: data.inspected,
notes: data.notes
},
};
try {
await db.put(params).promise();
res.status(201).json({ unit: params.Item });
} catch (e) {
res.status(500).json({ error: e.message });
}
});
This is what I am entering in Postman:
{
"unitNum": "007",
"building": "A",
"managed": true,
"rental": false,
"inHouse": false,
"robCount": 0,
"inspected": false,
"notes": "this is a good unit"
}
I have now tried adding ...req.body to the item like this:
Item: {
id: uuidv4(),
...req.body
},
The response I am getting from that is:
{
"unit": {
"0": 123,
"1": 10,
"2": 32,
"3": 32,
"4": 32,
"5": 32,
"6": 34,
"7": 117,
"8": 110,
"9": 105,
"10": 116,
"11": 78,
"12": 117,
"13": 109,
"14": 34,
"15": 58,
"16": 32,
"17": 34,
"18": 48,
"19": 48,
"20": 55,
"21": 34,
"22": 44,
"23": 10,
"24": 32,
"25": 32,
"26": 32,
"27": 32,
"28": 34,
"29": 98,
"30": 117,
"31": 105,
"32": 108,
"33": 100,
"34": 105,
"35": 110,
"36": 103,
"37": 34,
"38": 58,
"39": 32,
"40": 34,
"41": 65,
"42": 34,
"43": 10,
"44": 32,
"45": 32,
"46": 32,
"47": 32,
"48": 10,
"49": 125,
"id": "efb90ad4-f68e-4ea0-b55c-6a4f79e3a96c"
}
}
I did not expect that...
My delete, and get requests are working I am having trouble with the put and post..

Aligning labels in highchart donut

I'm trying to make a highchart donut with a legend on the side,
I'm really struggling to get the data labels to be more centered. At the moment each of them are in a different place,
an image of intended result:
https://codepen.io/mattdavidson/pen/qgqZyV
Highcharts.chart('container', {
credits: { enabled: false },
chart: { height: 300, width: 500, animation: false },
title: {
align: 'center',
verticalAlign: 'middle',
text: 10,
y: 25,
x: 55,
style: { color: '#333333', fontSize: '72px', fontWeight:'bold'},
},
plotOptions: {
pie: {
dataLabels: {
enabled: true,
format: '{point.y}',
distance: -25,
style: { fontSize: '32px', textOutline: 0 },
},
animation: false,
showInLegend: true,
},
},
legend: {
layout: 'vertical',
verticalAlign: 'middle',
align: 'left',
symbolHeight: 25,
symbolRadius: 5,
itemMarginTop: 10,
itemMarginBottom: 10,
},
series: [
{
type: 'pie',
innerSize: '55%',
colors: ['rgb(212,33,71)', 'rgb(250,189,43)', 'rgb(60,168,74)'],
data: [['Category 1', 4], ['Category 2', 5], ['Category 3', 1]],
},
],
});
There is an issue reported on Highcharts github about donut labels position. Check it here: https://github.com/highcharts/highcharts/issues/9005.
I've changed some options and added custom text using Highcharts.SVGRenderer#text (dynamic sum of all values) on the donut center. Perhaps it will help you: https://jsfiddle.net/BlackLabel/2jmqext9/.
Code:
Highcharts.chart('container', {
credits: {
enabled: false
},
chart: {
height: 300,
width: 500,
animation: false,
events: {
load: function() {
var chart = this,
offsetLeft = -20,
offsetTop = 10,
x = chart.plotLeft + chart.plotWidth / 2 + offsetLeft,
y = chart.plotTop + chart.plotHeight / 2 + offsetTop,
value = 0;
chart.series[0].yData.forEach(function(point) {
value += point;
});
chart.renderer.text(value, x, y).add().css({
fontSize: '30px'
}).toFront();
}
}
},
plotOptions: {
pie: {
dataLabels: {
enabled: true,
color: '#fff',
format: '{point.y}',
distance: -25,
style: {
fontSize: '20px',
textOutline: 0
},
},
animation: false,
showInLegend: true,
},
},
legend: {
layout: 'vertical',
verticalAlign: 'middle',
align: 'left',
symbolHeight: 15,
symbolRadius: 5,
symbolPadding: 10,
itemMarginTop: 10,
itemMarginBottom: 10,
itemStyle: {
fontSize: '15px'
}
},
series: [{
type: 'pie',
innerSize: '55%',
colors: ['rgb(212,33,71)', 'rgb(250,189,43)', 'rgb(60,168,74)'],
data: [
['Category 1', 4],
['Category 2', 2],
['Category 3', 12]
],
}, ],
});

Error ReferenceError: CanvasGradient is not defined

I am trying to create a line chart using chartjs-node package. Below is the code. When I give the data of dataset as a small set like data: [20, 15, 60, 60, 65, 30, 70], it works fine. But if I change it to a list that I want to populate as in dataFirst dataset below, I get the error "ReferenceError: CanvasGradient is not defined". I donot understand the reason. Please help. Thanks.
public createChart(cData) {
const chartNode = new chart(1000, 800);
const dataFirst = {
label: 'Heat Sink',
data: cData,
lineTension: 0.3,
fill: false,
borderColor: 'red',
backgroundColor: 'transparent',
pointBorderColor: 'red',
pointBackgroundColor: 'lightgreen',
pointRadius: 5,
pointHoverRadius: 15,
pointHitRadius: 30,
pointBorderWidth: 2,
pointStyle: 'rect',
};
const dataSecond = {
label: 'TVK Channel 9',
data: [20, 15, 60, 60, 65, 30, 70],
lineTension: 0.3,
fill: false,
borderColor: 'purple',
backgroundColor: 'transparent',
pointBorderColor: 'purple',
pointBackgroundColor: 'lightgreen',
pointRadius: 5,
pointHitRadius: 30,
pointBorderWidth: 2,
};
const chartOptions = {
legend: {
display: true,
position: 'top',
labels: {
boxWidth: 80,
fontColor: 'black',
},
},
};
const speedData = {
labels: ['0s', '10s', '20s', '30s', '40s', '50s', '60s'],
datasets: [dataFirst, dataSecond],
};
const chartJsOptions = {
type: 'line',
data: speedData,
options: chartOptions,
};
I found the solution. The number of labels should match the number of data points.
In canvas for node implementation you need to export CanvasGradient class yourself. According to this issue: https://github.com/Automattic/node-canvas/issues/990
You can do this:
["CanvasRenderingContext2D", "CanvasPattern", "CanvasGradient"].forEach(obj => {
console.log(obj, canvas[obj])
global[obj] = canvas[obj]
})

HighCharts XAxis logarithmic type

I have a specific query.
Following is my script for highcharts. When I run this sript, I get a memory leak issue. Maybe someone can help. I need to show the x-axis in exponential scale, like
100,000
1,000,000
10,000,000
and so on.
var chart = new Highcharts.Chart({
chart: {
renderTo: 'dvCon'
},
title: {
text: 'Construction: Duration vs Productive Hours'
},
xAxis: [{
type: 'logarithmic',
title: {
text: 'Construction Hours'
}
}],
yAxis: [{
labels: {
formatter: function () {
return this.value;
},
style: {}
},
showEmpty: true,
title: {
text: 'Duration',
style: {}
}
}, ],
tooltip: {
formatter: function () {
return '' + this.x + ': ' + this.y;
}
},
legend: {
layout: 'horizontal',
align: 'center',
verticalAlign: 'bottom',
backgroundColor: '#FFFFFF'
},
series: [{
name: 'Other Projects',
type: 'scatter',
data: [[560000, 13], [185250, 11], [3625788, 23], [1648510, 21], [265000, 14], [13000000, 43], [28000000, 34], [1567000, 19], [1190000, 20], [21000000, 31], [7000000, 33], [3805200, 30], [17000000, 29], [1503267, 21], [11332332, 29], [1485067, 20], [5000000, 30], [5400000, 22], [13000000, 23], [3810000, 26], [810000, 18], [27528218, 26], [377319, 14], [840000, 22], [550000, 13], [2643142, 26], [412800, 13], [2500000, 22], [4510000, 19], [523116, 15], [17600000, 28], [2500000, 21], [21000000, 29], [3500000, 17], [620000, 15], [163000000, 46], [134000000, 41], [45000000, 39], [13677454, 31], [167000000, 52], [47000000, 33], [49000000, 38], [31000000, 38]]
},
{
name: 'User Data',
type: 'scatter',
data: [[40050000, 35]]
}]
});
The code works fine, except the additional , after the yAxis object.
jsFiddle > http://jsfiddle.net/SSCEk/
Maybe the issue happens with a particular version of highcharts, jquery or a browser?

Resources