Useing amChart serias - charts

We have created something like this, and now I wont to add some periods to my chart codepen. but the code that I wrote doesn't work. Thank you
am4core.useTheme(am4themes_animated);
let items = [
{
"X": 1,
"Y": 2,
"code": "ExecDate",
"date": "May 06, 2010",
"enabled": true,
"name": "Execution Date",
"refTxt": null,
"refX": null
},
{
"X": 1,
"Y": -1,
"code": "EffDate",
"date": "May 07, 2010",
"enabled": true,
"name": "Effective Date",
"refTxt": null,
"refX": null
},
....
];
let periods = [
{
"Xend": "May 08, 2010",
"Xstart": "May 07, 2010",
"Y": 1,
"code": "PunchPeriod",
"name": "Punch List Completion Period"
},
...
];
Please help me

I already writed correct code
am4core.useTheme(am4themes_animated);
let items = [
{
"X": 1,
"Y": 2,
"code": "ExecDate",
"date": "May 06, 2010",
"enabled": true,
"name": "Execution Date",
"refTxt": null,
"refX": null
},
{
"X": 1,
"Y": -1,
"code": "EffDate",
"date": "May 07, 2010",
"enabled": true,
"name": "Effective Date",
"refTxt": null,
"refX": null
},
];
let chart = am4core.create("chart-am", am4charts.XYChart);
chart.data = items;
chart.dateFormatter.inputDateFormat = "MMM d, yyyy";
chart.maskBullets = false;
chart.paddingRight = 30;
// Create axes
let dateAxis = chart.xAxes.push(new am4charts.DateAxis());
dateAxis.dateFormatter.dateFormat = "MMM d, yyyy";
dateAxis.renderer.tooltipLocation = 0;
dateAxis.extraMin = 0.1;
dateAxis.extraMax = 0.1;
let valueAxis = chart.yAxes.push(new am4charts.ValueAxis());
valueAxis.title.text = "Transaction Volume";
valueAxis.renderer.baseGrid.disabled = true;
valueAxis.renderer.grid.template.strokeOpacity = 0.07;
// Create series
let series = chart.series.push(new am4charts.ColumnSeries());
series.dataFields.valueY = "Y";
series.dataFields.dateX = "date";
series.tooltip.pointerOrientation = "vertical";
series.columns.template.fill = am4core.color("#000")
// green label
let label = series.columns.template.createChild(am4core.Label);
label.text = "{name}";
label.align = "left";
label.valign = "top";
label.zIndex = 2;
label.fill = am4core.color("#000");
label.background.fill = am4core.color("green");
label.background.opacity = 0.6
label.strokeWidth = 0;
label.width = 100
label.height = 70
label.wrap = true
label.textValign = 'middle'
//label.textAlign = 'center'
label.fontSize = 11
label.bbox.y = -100
let columnTemplate = series.columns.template;
columnTemplate.layout = "horizontal";
columnTemplate.width = 3;
columnTemplate.column.tooltipText = "Series: {name}\nDate: {categoryX}\nCount: {valueY}";
columnTemplate.column.tooltipY = 0;
columnTemplate.column.cornerRadiusTopLeft = 0;
columnTemplate.column.cornerRadiusTopRight = 0;
columnTemplate.strokeOpacity = 0;
columnTemplate.adapter.add("fill", (fill, target) => {
let color = chart.colors.getIndex(target.dataItem.index * 3);
return color;
});
let periods = [
{
"Xstart": "May 07, 2010",
"Xend": "May 08, 2010",
"openY": -1.5,
"Y": -2,
"code": "PunchPeriod",
"name": "Punch List Completion Period"
},
{
"Xstart": "May 10, 2010", // was may 12
"Xend": "May 12, 2010", // was may 10
"openY": -2.5,
"Y": -3,
"code": "TitlePeriod",
"name": "Title Transfer Period"
},
{
"Xstart": "May 13, 2010", // was may 18
"Xend": "May 18, 2010", // was may 13
"openY": -3.5,
"Y": -4,
"code": "ConstPeriod",
"name": "Construction Period"
}
];
let periodSeries = chart.series.push(new am4charts.ColumnSeries());
periodSeries.data = periods;
periodSeries.dataFields.openValueY = "openY";
periodSeries.dataFields.valueY = "Y";
periodSeries.dataFields.openDateX = "Xstart";
periodSeries.dataFields.dateX = "Xend";
periodSeries.columns.template.tooltipText = "{name}: {openDateX} - {dateX}";
periodSeries.columns.template.propertyFields.fill = "color";
periodSeries.columns.template.propertyFields.stroke = "color";
periodSeries.columns.template.strokeOpacity = 1;
let periodLabel = periodSeries.columns.template.createChild(am4core.Label);
periodLabel.text = "{name}";
periodLabel.align = "center";
periodLabel.color = "color";
label.align = "center";
periodLabel.valign = "top";
periodLabel.zIndex = 500;
periodLabel.fill = am4core.color("#000");
periodLabel.background.fill = am4core.color("red");
periodLabel.background.opacity = 0.6
periodLabel.strokeWidth = 0;
periodLabel.width = 100
periodLabel.height = 70
periodLabel.wrap = true
periodLabel.textValign = 'middle'
periodLabel.textAlign = 'center'
periodLabel.fontSize = 11
periodLabel.bbox.y = -100
chart.scrollbarX = new am4core.Scrollbar();

Related

Invisible chart area during print and in exported PDF file in spreadsheet created by service account with Google Sheets API (Node.js) in shared folder

I'm creating a spreadsheet in Node.js environment in shared folder using service account with Google Sheets API v.4 in few steps:
Create spreadsheet itself in shared folder with "Can Edit" permission for service account.
Inserting some data and performing some text formats using spreadsheet ID received as callback from previous step.
Inserting chart using as income data the data inserted in prevoius step.
As the result I have a spreadsheet with expected result (text data and horizontal bar chart on same sheet). But when I'm trying to send it on printer, or download as PDF-file - chart area becomes completely invisible. I didn't find any option in official documentation about possible chart visibility during printing or something like this. And when I'm replacing this created chart with manually created one - everything is ok, I can print it and export to PDF.
So, what is the problem? Am I missing something? Or it's some bug?
index.js
const fs = require('fs');
const { google } = require('googleapis');
const express = require('express');
const bodyParser = require("body-parser");
const app = express();
const PORT = 3000;
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.listen(PORT, () => {
console.log(`Server started at http://localhost:${PORT}`)
})
const SCOPES = ['https://www.googleapis.com/auth/drive', 'https://www.googleapis.com/auth/spreadsheets'];
const FOLDER_ID = '1xG3xHhrucB4AGLmnd8T2TmCyqhmPux5Q';
var timeStamp = new Date().getTime();
console.log(`timeStamp at startup = ${timeStamp}`);
const auth = new google.auth.GoogleAuth({
keyFile: 'credentials.json',
scopes: SCOPES
});
process.env.HTTPS_PROXY = 'http://10.5.0.20:3128';
const sheets = google.sheets({
version: 'v4',
auth: auth,
proxy: 'http://10.5.0.20:3128'
});
const drive = google.drive({
version: 'v3',
auth: auth,
proxy: 'http://10.5.0.20:3128'
});
function createFileName() {
const now = new Date();
let date = String(now.toISOString().slice(0, 10));
let hours = String(now.getHours()).padStart(2, "0");
let minutes = String(now.getMinutes()).padStart(2, "0");
let seconds = String(now.getSeconds()).padStart(2, "0");
let humanDate = date.replaceAll('-', '.');
humanDate = `${humanDate}_${hours}-${minutes}-${seconds}`;
return humanDate;
}
async function saveFileLocally(filePath, data) {
fs.writeFile(filePath, JSON.stringify(data), error => {
if (error) {
console.error(error);
return;
}
});
return true;
}
app.get("/check", (req, res) => {
res.send('server is online...');
});
app.post("/motivation", async (req, res) => {
if(!req.body) return res.sendStatus(400);
try {
const prizv = req.body.prizv;
const name = req.body.name;
const father = req.body.father;
const sex = req.body.sex;
const age = req.body.age;
const factors = req.body.factors;
const testName = 'motivation';
const clientData = { prizv: prizv, name: name, father: father, sex: sex, age: age, factors: factors, testName: testName };
const fileName = `${createFileName()}_${prizv}`;
const filePath = `files/${testName}/${fileName}.txt`;
const isSaved = await saveFileLocally(filePath, clientData);
if (isSaved) {
console.log(`file is saved locally....`);
const sheetID = await createSheetToGoogleDIsk(clientData, fileName);
res.send(sheetID);
}
} catch (error) {
console.log(error)
}
});
async function createSheetToGoogleDIsk(clientData, fileName) {
const file = fileName;
var sheetsMetadata = {
name: file,
mimeType: 'application/vnd.google-apps.spreadsheet',
parents: [FOLDER_ID]
};
const res2 = drive.files.create({
resource: sheetsMetadata,
fields: 'id'
}, function (err, file) {
if (err) {
console.error(err);
} else {
console.log('SheetID: ', file.data.id);
const gSheetID = file.data.id;
pasteDataToGoogleSheet(clientData, gSheetID);
insertChartToGoogleSheet(gSheetID);
return file.data.id;
}
});
}
async function insertChartToGoogleSheet(spreadsheetId) {
spreadsheetId = spreadsheetId;
let requests = [];
// set font size for whole Sheet as 14
requests.push({
"repeatCell": {
"range": {
"sheetId": 0,
"startRowIndex": 0,
"endRowIndex": 100,
},
"cell": {
"userEnteredFormat": {
"textFormat": {
"fontSize": 13,
},
},
},
"fields": "userEnteredFormat.textFormat.fontSize"
},
});
// set header text format as Bold and 18 pt
requests.push({
"repeatCell": {
"range": {
"sheetId": 0,
"startRowIndex": 0,
"endRowIndex": 2,
},
"cell": {
"userEnteredFormat": {
"textFormat": {
"fontSize": 18,
"bold": true
},
},
},
"fields": "userEnteredFormat(textFormat)"
},
});
// set subheader text format as Bold and 15 pt
requests.push({
"repeatCell": {
"range": {
"sheetId": 0,
"startRowIndex": 3,
"endRowIndex": 4,
},
"cell": {
"userEnteredFormat": {
"textFormat": {
"fontSize": 15,
"bold": true
},
},
},
"fields": "userEnteredFormat(textFormat)"
},
});
// set client data as Bold
requests.push({
"repeatCell": {
"range": {
"sheetId": 0,
"startRowIndex": 5,
"endRowIndex": 10,
"startColumnIndex": 3,
"endColumnIndex": 5
},
"cell": {
"userEnteredFormat": {
"textFormat": {
"fontSize": 13,
"bold": true
},
},
},
"fields": "userEnteredFormat(textFormat)"
},
});
// set 1st column width as 20px
requests.push({
"updateDimensionProperties": {
"range": {
"sheetId": 0,
"dimension": "COLUMNS",
"startIndex": 0,
"endIndex": 1
},
"properties": {
"pixelSize": 20
},
"fields": "pixelSize"
}
});
// set 4st column width as 150px
requests.push({
"updateDimensionProperties": {
"range": {
"sheetId": 0,
"dimension": "COLUMNS",
"startIndex": 3,
"endIndex": 4
},
"properties": {
"pixelSize": 150
},
"fields": "pixelSize"
}
});
// set bold factors Values
requests.push({
"repeatCell": {
"range": {
"sheetId": 0,
"startRowIndex": 33,
"endRowIndex": 45,
"startColumnIndex": 4,
"endColumnIndex": 5
},
"cell": {
"userEnteredFormat": {
"textFormat": {
"fontSize": 13,
"bold": true
},
},
},
"fields": "userEnteredFormat(textFormat)"
},
});
requests.push({
"addChart": {
"chart": {
"chartId": 1,
"spec": {
"titleTextFormat": {
},
"basicChart": {
"chartType": "BAR",
"axis": [
{
"position": "BOTTOM_AXIS",
},
{
"position": "LEFT_AXIS",
}
],
"domains": [
{
"domain": {
"sourceRange": {
"sources": [
{
"sheetId": 0,
"startRowIndex": 33,
"endRowIndex": 45,
"startColumnIndex": 1,
"endColumnIndex": 2
}
]
},
},
}
],
"series": [
{
"series": {
"sourceRange": {
"sources": [
{
"sheetId": 0,
"startRowIndex": 33,
"endRowIndex": 45,
"startColumnIndex": 4,
"endColumnIndex": 5
}
]
}
},
"targetAxis": "BOTTOM_AXIS"
}
],
},
},
"position": {
"overlayPosition": {
"anchorCell": {
"sheetId": 0,
"rowIndex": 11,
"columnIndex": 1
},
"offsetXPixels": 0,
"offsetYPixels": -7,
"widthPixels": 800,
"heightPixels": 450
},
},
"border": {
"color": {
"red": 1,
"green": 1,
"blue": 1,
"alpha": 0
},
}
}
}
});
const batchUpdateRequest = { requests };
sheets.spreadsheets.batchUpdate({
spreadsheetId,
resource: batchUpdateRequest,
}, (err, result) => {
if (err) {
// Handle error
console.log(err);
return false
} else {
console.log(`${result.updatedCells} chart inserted`);
return spreadsheetId
}
});
}
async function pasteDataToGoogleSheet(clientData, sheetId) {
ecxelID = sheetId;
let data1 = [
["Тест «Мотиваційний особистісний профіль»"], [""], ["Результати тестування"], [""], ["Прізвище"], ["Імя"], ["По-батькові"], ["Вік"], ["Стать"]
];
let data2 = [
[clientData.prizv], [clientData.name], [clientData.father], [clientData.age], [clientData.sex]
];
let factorsLabels1_6 = [
["1. Матеріальна винагорода:"], ["2. Комфортні умови:"], ["3. Структурованість роботи:"], ["4. Соціальні контакти:"], ["5. Довірливі стосунки:"], ["6. Визнання:"]
];
let factors1_6 = [
[clientData.factors.factor1], [clientData.factors.factor2], [clientData.factors.factor3], [clientData.factors.factor4], [clientData.factors.factor5], [clientData.factors.factor6]
];
let factorsLabels7_12 = [
["7. Досягнення мети:"], ["8. Влада і вплив:"], ["9. Відсутність рутини:"], ["10. Креативність:"], ["11. Самовдосконалення і розвиток:"], ["12. Цікава і корисна діяльність:"]
];
let factors7_12 = [
[clientData.factors.factor7], [clientData.factors.factor8], [clientData.factors.factor9], [clientData.factors.factor10], [clientData.factors.factor11], [clientData.factors.factor12]
];
const data = [{
range: "B2:B10",
values: data1,
},
{
range: "D6:D10",
values: data2,
},
{
range: "B34:B39",
values: factorsLabels1_6,
},
{
range: "E34:E39",
values: factors1_6,
},
{
range: "B40:B45",
values: factorsLabels7_12,
},
{
range: "E40:E45",
values: factors7_12,
}
];
const resource = {
data,
valueInputOption: 'RAW',
};
sheets.spreadsheets.values.batchUpdate({
spreadsheetId: ecxelID,
resource: resource,
}, (err, result) => {
if (err) {
// Handle error
console.log(err);
return false
} else {
console.log(`${result.updatedCells} cells data inserted`);
return spreadsheetId;
}
});
}
I could confirm your situation. In this case, how about the following modification?
From:
"offsetXPixels": 0,
"offsetYPixels": -7,
"widthPixels": 800,
"heightPixels": 450
To:
"offsetXPixels": 0,
"offsetYPixels": 0, // Modified
"widthPixels": 800,
"heightPixels": 450
or
"widthPixels": 800,
"heightPixels": 450
When the values of offsetXPixels and offsetYPixels are the negative values, it was found that such an issue occurs.
When the values of offsetXPixels and offsetYPixels are 0, these values are not required to be included because of the default value.
Note:
When I tested the above modification, I could confirm that your issue could be removed.
Reference:
EmbeddedObjectPosition

FlSpot data Flutter

I use FL Chart in my Flutter Apps
but i too many repeat my code, can i make it to use i++
here my code
final LineChartBarData lineChartBarData1 = LineChartBarData(
spots: [
FlSpot(00.00, (dataAcc[50]['x'] / 1.00)),
FlSpot(01.00, (dataAcc[49]['x'] / 1.00)),
FlSpot(02.00, (dataAcc[48]['x'] / 1.00)),
FlSpot(03.00, (dataAcc[47]['x'] / 1.00)),
FlSpot(04.00, (dataAcc[46]['x'] / 1.00)),
FlSpot(05.00, (dataAcc[45]['x'] / 1.00)),
and here my array data from json using
dataAcc = json.decode(response.body);
[
{
"id": 40,
"time": 1614116099,
"x": 10.27,
},
{
"id": 39,
"time": 1614116001,
"x": 10.25,
},
{
"id": 38,
"x": 10.26,
}
]
Update:
i has been found solution for my problem, thanks all
List<FlSpot> getAccData(String acc) {
List<FlSpot> accList = [];
for (int i = 0; i <= 50; i++) {
accList.add(FlSpot((i * 1.00), dataAcc[i][acc] / 1.00));
}
return accList;
}

How to add multiple dictionary into array

I've got a dictionary added into array. I would like to add second dictionary into the array. How to do it?
var dict = [String: AnyObject]()
dict = ["description": self.odrRefTxt.text as AnyObject,
"weight": self.pWeight as AnyObject,
"quantity": self.qtyTxt.text as AnyObject,
"unitPrice": self.valueTxt.text as AnyObject,
"currency": self.pCurrency as AnyObject]
// saving to memory
UserDefaults.standard.set(dict, forKey: "addItem")
// getting from memory
let addItem = UserDefaults.standard.value(forKey: "addItem")
print(addItem as Any)
//add new item into list of items
self.itemArr.append(addItem as Any)
print(self.itemArr)
Below are the result from print:
//print addItem
Optional({
currency = "";
description = "Toyo Proxes";
quantity = 4;
unitPrice = 100;
weight = 0;
})
//print self.itemArr
[Optional({
currency = "";
description = "Toyo Proxes";
quantity = 4;
unitPrice = 100;
weight = 0;
})]
For example, I would like to add the 2nd dictionary into the array to print out the outcome like this:
//print self.itemArr
[Optional({
currency = "";
description = "Toyo Proxes";
quantity = 4;
unitPrice = 100;
weight = 0;
},
{
currency = "";
description = "Yokohama Advan";
quantity = 2;
unitPrice = 250;
weight = 0;
})]
You save something in UserDefaults as Any, so just cast it to the original type and here you go.
Modified with #rmaddy's suggestion.
var itemArr = [[String: Any]]()
var dict = [String: Any]()
dict = ["description": 1,
"weight": 2,
"quantity": 3,
"unitPrice": "abc",
"currency": "123"]
// saving to memory
UserDefaults.standard.set(dict, forKey: "addItem")
// getting from memory
if let addItem = UserDefaults.standard.dictionary(forKey: "addItem") {
itemArr.append(addItem)
}
let dict2:[String: Any] = ["description": 4,
"weight": 5,
"quantity": 6,
"unitPrice": "xyz",
"currency": "456"]
itemArr.append(dict2)
print(itemArr)
// prints out:
// [["description": 1, "quantity": 3, "weight": 2, "currency": 123, "unitPrice": abc], ["description": 4, "weight": 5, "quantity": 6, "currency": "456", "unitPrice": "xyz"]]

Can't print google's vision API results to app screen

I'm using Google's Vision API to identify certain features in an image. I have the Logo Detection working as the logo comes up in my terminal, but I can't get it to appear on my app screen. It continually prints "No logos found" - here's my code :
//Get logo annotations
let logoAnnotations: JSON = logoResponses["logoAnnotations"]
let numLogos: Int = logoAnnotations.count
var logos: Array<String> = []
if numLogos > 0 {
var allResultsText:String = "Logos: "
for index in 0..<numLogos {
let logo = logoAnnotations[index]["logo"].stringValue
logos.append(logo)
}
for logo in logos {
if logos[logos.count - 1] != logo {
allResultsText += "\(logo), "
} else {
allResultsText += "\(logo)."
}
}
self.allResults.text = allResultsText
} else {
self.allResults.text = "No logos found"
}
}
This is the JSON response I'm getting:
[
{
"boundingPoly": {
"vertices": [
{
"x": 210,
"y": 139
},
{
"x": 229,
"y": 139
},
{
"x": 229,
"y": 179
},
{
"x": 210,
"y": 179
}
]
},
"mid": "/m/04lg33",
"score": 0.18314756,
"description": "Ralph Lauren Corporation"
}
]
How am I to access the value returned for the logo description, this case Ralph Lauren Corporation?
I corrected the issue in my code. Here is the functioning code for anyone that needs it:
let logoAnnotations: JSON = responses["logoAnnotations"]
let numLogos: Int = logoAnnotations.count
var logos: Array<String> = []
if numLogos > 0 {
var logoResultsText:String = " ,"
for index in 0..<numLogos {
let logo = logoAnnotations[index]["description"].stringValue
logos.append(logo)
}
for logo in logos {
if logos[logos.count - 1] != logo {
logoResultsText += ", \(logo), "
} else {
logoResultsText += "\(logo)."
}
}
self.logoResults.text = logoResultsText
} else {
self.logoResults.text = ""
}
I was calling the incorrect index from the logoAnnotations array.

How to concatenate a value from a dictionary with a string

Given the following, what is the proper way to concatenate
var sets = [[ "reps": 10, "weight": 100 ], [ "reps": 10, "weight": 100 ], [ "reps": 10, "weight": 100 ]]
var text = sets[0]["reps"] + " reps"
The only thing that worked was the code below, but I don't understand why. Ideally I can simply use string interpolation for this, but that seems to generate a syntax error.
var text = sets[0]["reps"]!.description + " reps"
Updated for Swift 2
sets is of type [[String : Int]]. So in order to get a String from your Int, you have to use string interpolation or use a String initializer.
According to your needs, you may choose one of the following Playground examples:
Optional binding + String initializer:
let sets = [[ "reps": 10, "weight": 100 ], [ "reps": 10, "weight": 100 ], [ "reps": 10, "weight": 100 ]]
if let int = sets[0]["reps"] {
let text = String(int) + " reps"
print(text) // prints: "10 reps"
}
Optional binding + string interpolation:
let sets = [[ "reps": 10, "weight": 100 ], [ "reps": 10, "weight": 100 ], [ "reps": 10, "weight": 100 ]]
if let int = sets[0]["reps"] {
let text = "\(int)" + " reps"
print(text) // prints: "10 reps"
}
Optional forced unwrapping:
let sets = [[ "reps": 10, "weight": 100 ], [ "reps": 10, "weight": 100 ], [ "reps": 10, "weight": 100 ]]
let int = sets[0]["reps"]! // Use forced unwrapping with caution
let text = "\(int)" + " reps"
print(text) // prints: "10 reps"
sets[0]["reps"] is an int. You can use formatting to specify the types:
var weight = sets[0]["reps"]!
var text = String(format: "%d reps", weight)
or all in one:
var text = String(format: "%d reps", sets[0]["reps"]!)
println("text: \(text)") // text: 10 reps