I've created a plot using Vega-Lite that allows me to use a binder to alter the parameters of a functions that I'm visualizing. It's similar to this sample code:
{
"$schema": "https://vega.github.io/schema/vega-lite/v4.json",
"description": "Plots two functions using a generated sequence.",
"width": 300,
"height": 150,
"data": {"sequence": {"start": 0, "stop": 12.7, "step": 0.1, "as": "x"}},
"transform": [
{"calculate": "amp.sin * sin(datum.x)", "as": "sin(x)"},
{"calculate": "amp.cos * cos(datum.x)", "as": "cos(x)"},
{"fold": ["sin(x)", "cos(x)"]}
],
"mark": "line",
"encoding": {
"x": {"type": "quantitative", "field": "x"},
"y": {"field": "value", "type": "quantitative"},
"color": {"field": "key", "type": "nominal", "title": null}
},
"selection": {
"amp": {
"type": "single",
"fields": ["sin", "cos"],
"init": {"sin": 1, "cos": 1},
"bind": {
"sin": {"input": "range", "min": 0, "max": 10, "step": 0.1},
"cos": {"input": "range", "min": 0, "max": 10, "step": 0.1}
}
}
}
}
Here is the code above in the Vega editor.
Now, what I'd like to do is to create another visualization parallel to this one, but with another function, but that would also vary with the same binder.
Is this possible? Note that in my code, each plot uses a different dataset, but share the variable of the binder in common.
Yes, for example you can do this using a "concat". Here is an example based on your chart (open in editor):
{
"$schema": "https://vega.github.io/schema/vega-lite/v4.json",
"description": "Plots two functions using a generated sequence.",
"data": {"sequence": {"start": 0, "stop": 12.7, "step": 0.1, "as": "x"}},
"concat": [
{
"width": 300,
"height": 150,
"transform": [
{"calculate": "amp.sin * sin(datum.x)", "as": "sin(x)"},
{"calculate": "amp.cos * cos(datum.x)", "as": "cos(x)"},
{"fold": ["sin(x)", "cos(x)"]}
],
"mark": "line",
"encoding": {
"x": {"type": "quantitative", "field": "x"},
"y": {"field": "value", "type": "quantitative"},
"color": {"field": "key", "type": "nominal", "title": null}
},
"selection": {
"amp": {
"type": "single",
"fields": ["sin", "cos"],
"init": {"sin": 1, "cos": 1},
"bind": {
"sin": {"input": "range", "min": 0, "max": 10, "step": 0.1},
"cos": {"input": "range", "min": 0, "max": 10, "step": 0.1}
}
}
}
},
{
"width": 300,
"height": 150,
"transform": [
{
"calculate": "amp.cos * cos(datum.x) - amp.sin * sin(datum.x)",
"as": "cos(x) - sin(x)"
}
],
"mark": "line",
"encoding": {
"x": {"type": "quantitative", "field": "x"},
"y": {"field": "cos(x) - sin(x)", "type": "quantitative"}
}
}
],
"resolve": {"scale": {"y": "shared", "color": "independent"}}
}
Sample Code
JsFiddle Example for below code.
Highcharts.chart('container', {
chart: {
plotBackgroundColor: null,
plotBorderWidth: null,
plotShadow: false,
type: 'pie'
},
title: {
text: 'Browser market shares in January, 2018'
},
tooltip: {
pointFormat: '{series.name}: <b>{point.percentage:.1f}%</b>'
},
plotOptions: {
pie: {
allowPointSelect: true,
cursor: 'pointer',
dataLabels: {
enabled: true,
format: '<b>{point.name}</b>: {point.percentage:.1f} %'
}
}
},
series: [{
name: 'Brands',
colorByPoint: true,
data: [
{
"name": "Display",
"y": 0.1,
"value": 5
},
{
"name": "Paid Social",
"y": 0,
"value": 0,
sliced: true,
selected: true
},
{
"name": "Direct",
"y": 14.5,
"value": 559
},
{
"name": "Referral",
"y": 2,
"value": 77
},
{
"name": "Email",
"y": 4,
"value": 152
},
{
"name": "Other",
"y": 0,
"value": 1
},
{
"name": "Organic Search",
"y": 23.4,
"value": 901
},
{
"name": "Meta Search",
"y": 0.2,
"value": 5
},
{
"name": "Organic Social",
"y": 2.4,
"value": 93
},
{
"name": "Directory",
"y": 0.2,
"value": 9
},
{
"name": "Other Advertising",
"y": 0.1,
"value": 3
},
{
"name": "OTA Referral Traffic",
"y": 0.7,
"value": 26
},
{
"name": "Paid Search",
"y": 27.8,
"value": 1068
},
{
"name": "Local",
"y": 24.5,
"value": 941
}]
}]
});
Scenario
I want to display dataLabels for all even y value is zero.
In Above code We have "name": "Display" y=0.1 but still it is not displaying in Pie Chart don't know why. If any one have idea about this problem please let me know.
The dataLabels are hidden due to lack of space and overlapping. As a solution you can set: padding: 0
plotOptions: {
pie: {
...,
dataLabels: {
padding: 0,
...
}
}
}
Live demo: https://jsfiddle.net/BlackLabel/g5s27tyb/
API Reference: https://api.highcharts.com/highcharts/plotOptions.pie.dataLabels.padding
I have a field group named 'Sales Item' and 'Invoice'. The invoice field group have a 'Items' field which is a repeater field, the 'Items' field contains the 'Sales Item' field group as clone field.
I need to create an Invoice post type programmatically. All the field of the 'Invoice' is created except the 'Items' field. How do I add the data to the 'Items' reaper field which contain the 'Sales Item' clone field?
Sales Item and Invoice field group.
[
{
"key": "group_5c4033565a799",
"title": "Invoice",
"fields": [
{
"key": "field_5c44150d123de",
"label": "Type",
"name": "ims_invoice_type",
"type": "select",
"instructions": "",
"required": 1,
"conditional_logic": 0,
"wrapper": {
"width": "",
"class": "",
"id": ""
},
"choices": {
"sales": "Sales",
"purchases": "Purchases"
},
"default_value": [
"purchases"
],
"allow_null": 0,
"multiple": 0,
"ui": 0,
"return_format": "value",
"ajax": 0,
"placeholder": ""
},
{
"key": "field_5c4033c40bce3",
"label": "Invoice Number",
"name": "ims_invoice_invoice_number",
"type": "text",
"instructions": "",
"required": 1,
"conditional_logic": 0,
"wrapper": {
"width": "",
"class": "",
"id": ""
},
"default_value": "",
"maxlength": "",
"placeholder": "",
"prepend": "",
"append": ""
},
{
"key": "field_5c4e943170485",
"label": "Customer",
"name": "ims_invoice_customer",
"type": "relationship",
"instructions": "",
"required": 1,
"conditional_logic": [
[
{
"field": "field_5c44150d123de",
"operator": "==",
"value": "sales"
}
]
],
"wrapper": {
"width": "",
"class": "",
"id": ""
},
"post_type": [
"ims_customer"
],
"taxonomy": "",
"filters": [
"search",
"post_type",
"taxonomy"
],
"elements": "",
"min": 1,
"max": 1,
"return_format": "object"
},
{
"key": "field_5c4033620bce1",
"label": "Vendor",
"name": "ims_invoice_vendor",
"type": "relationship",
"instructions": "",
"required": 1,
"conditional_logic": [
[
{
"field": "field_5c44150d123de",
"operator": "==",
"value": "purchases"
}
]
],
"wrapper": {
"width": "",
"class": "",
"id": ""
},
"post_type": [
"ims_vendor"
],
"taxonomy": "",
"filters": [
"search",
"post_type",
"taxonomy"
],
"elements": "",
"min": 1,
"max": 1,
"return_format": "object"
},
{
"key": "field_5c40339b0bce2",
"label": "Date",
"name": "ims_invoice_date",
"type": "date_picker",
"instructions": "",
"required": 1,
"conditional_logic": 0,
"wrapper": {
"width": "",
"class": "",
"id": ""
},
"display_format": "F j, Y",
"return_format": "d\/m\/Y",
"first_day": 0
},
{
"key": "field_5c4034030cff9",
"label": "Items",
"name": "ims_invoice_sales_items",
"type": "repeater",
"instructions": "",
"required": 1,
"conditional_logic": [
[
{
"field": "field_5c44150d123de",
"operator": "==",
"value": "sales"
}
]
],
"wrapper": {
"width": "",
"class": "",
"id": ""
},
"collapsed": "",
"min": 0,
"max": 0,
"layout": "table",
"button_label": "Add Product",
"sub_fields": [
{
"key": "field_5c443f4dd87de",
"label": "Item",
"name": "ims_invoice_sales_items",
"type": "clone",
"instructions": "",
"required": 0,
"conditional_logic": 0,
"wrapper": {
"width": "",
"class": "",
"id": ""
},
"clone": [
"group_5c443e86ad330"
],
"display": "seamless",
"layout": "block",
"prefix_label": 0,
"prefix_name": 1
}
]
},
{
"key": "field_5c78f715c5efe",
"label": "Items",
"name": "ims_invoice_purchases_items",
"type": "repeater",
"instructions": "",
"required": 1,
"conditional_logic": [
[
{
"field": "field_5c44150d123de",
"operator": "==",
"value": "purchases"
}
]
],
"wrapper": {
"width": "",
"class": "",
"id": ""
},
"collapsed": "",
"min": 0,
"max": 0,
"layout": "table",
"button_label": "Add Product",
"sub_fields": [
{
"key": "field_5c78f730c5eff",
"label": "Items",
"name": "ims_invoice_purchases_items",
"type": "clone",
"instructions": "",
"required": 0,
"conditional_logic": 0,
"wrapper": {
"width": "",
"class": "",
"id": ""
},
"clone": [
"group_5c78e0e32443c"
],
"display": "seamless",
"layout": "block",
"prefix_label": 0,
"prefix_name": 1
}
]
},
{
"key": "field_5c41b2ff17b09",
"label": "Sub Total",
"name": "ims_invoice_sub_total",
"type": "read_only",
"instructions": "",
"required": 1,
"conditional_logic": 0,
"wrapper": {
"width": "",
"class": "",
"id": ""
},
"copy_to_clipboard": 0,
"display_type": "text"
},
{
"key": "field_5c41b31a17b0b",
"label": "Discount",
"name": "ims_invoice_discount",
"type": "number",
"instructions": "",
"required": 0,
"conditional_logic": 0,
"wrapper": {
"width": "",
"class": "",
"id": ""
},
"default_value": "0.0",
"placeholder": "",
"prepend": "",
"append": "",
"min": 0,
"max": "",
"step": ""
},
{
"key": "field_5c41b32617b0c",
"label": "V.A.T",
"name": "ims_invoice_vat",
"type": "number",
"instructions": "",
"required": 1,
"conditional_logic": 0,
"wrapper": {
"width": "",
"class": "",
"id": ""
},
"default_value": "0.0",
"placeholder": "",
"prepend": "",
"append": "",
"min": 0,
"max": "",
"step": ""
},
{
"key": "field_5c41b33317b0d",
"label": "Total",
"name": "ims_invoice_total",
"type": "read_only",
"instructions": "",
"required": 1,
"conditional_logic": 0,
"wrapper": {
"width": "",
"class": "",
"id": ""
},
"copy_to_clipboard": 0,
"display_type": "text"
},
{
"key": "field_5c41b33b17b0e",
"label": "Paid",
"name": "ims_invoice_paid",
"type": "number",
"instructions": "",
"required": 1,
"conditional_logic": 0,
"wrapper": {
"width": "",
"class": "",
"id": ""
},
"default_value": "0.0",
"placeholder": "",
"prepend": "",
"append": "",
"min": 0,
"max": "",
"step": ""
},
{
"key": "field_5c41b34517b0f",
"label": "Due",
"name": "ims_invoice_due",
"type": "read_only",
"instructions": "",
"required": 1,
"conditional_logic": 0,
"wrapper": {
"width": "",
"class": "",
"id": ""
},
"copy_to_clipboard": 0,
"display_type": "text"
}
],
"location": [
[
{
"param": "post_type",
"operator": "==",
"value": "ims_invoice"
}
]
],
"menu_order": 0,
"position": "normal",
"style": "default",
"label_placement": "left",
"instruction_placement": "label",
"hide_on_screen": "",
"active": 1,
"description": ""
},
{
"key": "group_5c443e86ad330",
"title": "Sales Item",
"fields": [
{
"key": "field_5c443eab22fe1",
"label": "Item",
"name": "sales_item_item",
"type": "post_object",
"instructions": "",
"required": 1,
"conditional_logic": 0,
"wrapper": {
"width": "",
"class": "",
"id": ""
},
"post_type": [
"ims_product"
],
"taxonomy": "",
"allow_null": 0,
"multiple": 0,
"return_format": "object",
"ui": 1
},
{
"key": "field_5c779ac395834",
"label": "SKU",
"name": "sales_item_sku",
"type": "read_only",
"instructions": "",
"required": 0,
"conditional_logic": 0,
"wrapper": {
"width": "",
"class": "",
"id": ""
},
"copy_to_clipboard": 0,
"display_type": "text"
},
{
"key": "field_5c443eba22fe2",
"label": "Quantity",
"name": "sales_item_quantity",
"type": "number",
"instructions": "",
"required": 1,
"conditional_logic": 0,
"wrapper": {
"width": "",
"class": "",
"id": ""
},
"default_value": 1,
"placeholder": "",
"prepend": "",
"append": "",
"min": "",
"max": "",
"step": ""
},
{
"key": "field_5c443ec622fe3",
"label": "Unit Price",
"name": "sales_item_unit_price",
"type": "read_only",
"instructions": "",
"required": 1,
"conditional_logic": 0,
"wrapper": {
"width": "",
"class": "",
"id": ""
},
"copy_to_clipboard": 0,
"display_type": "text"
},
{
"key": "field_5c443edf22fe4",
"label": "Price",
"name": "sales_item_price",
"type": "read_only",
"instructions": "",
"required": 0,
"conditional_logic": 0,
"wrapper": {
"width": "",
"class": "",
"id": ""
},
"copy_to_clipboard": 0,
"display_type": "text"
},
{
"key": "field_5c443efd22fe5",
"label": "Discount",
"name": "sales_item_discount",
"type": "number",
"instructions": "",
"required": 0,
"conditional_logic": 0,
"wrapper": {
"width": "",
"class": "",
"id": ""
},
"default_value": "0.0",
"placeholder": "",
"prepend": "",
"append": "",
"min": "",
"max": "",
"step": ""
},
{
"key": "field_5c443f0a22fe6",
"label": "Amount",
"name": "sales_item_amount",
"type": "read_only",
"instructions": "",
"required": 0,
"conditional_logic": 0,
"wrapper": {
"width": "",
"class": "",
"id": ""
},
"copy_to_clipboard": 0,
"display_type": "text"
}
],
"location": [
[
{
"param": "post_type",
"operator": "==",
"value": "post"
}
]
],
"menu_order": 0,
"position": "normal",
"style": "default",
"label_placement": "top",
"instruction_placement": "label",
"hide_on_screen": "",
"active": 0,
"description": ""
}
]
Here is the code, I have written so far.
$invoice_key = 'ims_invoice_sales_items_';
$customer_id = $_POST['customer_id'];
$ims_products = $_POST['ims_product'];
$basket_discount = $_POST['basket_discount'];
$basket_vat = $_POST['basket_vat'];
$paid = $_POST['paid'];
$sub_total = 0.0;
foreach( $ims_products as $ims_product ) {
$id = $ims_product['id'];
$quantity = intval( $ims_product['quantity'] );
$unit_price = floatval( get_field( 'ims_product_mrp', $id ) );
$discount = floatval( $ims_product['discount'] );
$price = $quantity * $unit_price;
$amount = $price - ( $discount * $price ) / 100.0;
$sub_total += $amount;
$items [] = array(
$invoice_key . 'sales_item_item' => $id,
$invoice_key . 'sales_item_sku' => get_field( 'ims_product_sku', $id ),
$invoice_key . 'sales_item_quantity' => $quantity,
$invoice_key . 'sales_item_unit_price' => $unit_price,
$invoice_key . 'sales_item_price' => $price,
$invoice_key . 'sales_item_discount' => $discount,
$invoice_key . 'sales_item_amount' => $amount
);
}
// var_dump( $items );
// Calculate grand total after discount and vat.
$grand_total = $sub_total - ( $basket_discount * $sub_total ) / 100.0;
$grand_total = $grand_total + ( $basket_vat * $grand_total ) / 100.0;
// Calculate due.
$due = $grand_total - $paid;
$meta_inputs = array(
'ims_invoice_type' => 'sales',
'ims_invoice_invoice_number' => uniqid(),
'ims_invoice_customer' => $customer_id,
'ims_invoice_date' => current_time('d/m/Y', false ),
'ims_invoice_sub_total' => $sub_total,
'ims_invoice_discount' => $basket_discount,
'ims_invoice_vat' => $basket_vat,
'ims_invoice_total' => $grand_total,
'ims_invoice_paid' => $paid,
'ims_invoice_due' => $due
);
$invoice_args = array(
'post_type' => 'ims_invoice',
'post_status' => 'publish',
'meta_input' => $meta_inputs
);
// var_dump( $invoice_args );
$post_id = wp_insert_post( $invoice_args, true );
var_dump( "<h1>Post Id = ${post_id}</h1>");
foreach( $items as $item ) {
var_dump( $item );
add_row( 'ims_invoice_sales_items', $item, $post_id );
}
die;
You don't actually need to call add_row and add each repeater entry in sequence. You can push the entire items array in one go. You will need to do following things to make sure that the below conditions are met and the code will work for you.
You have the correct field key for your repeater field (Something to the effect of field_5bfed0e63a470).
The slug of each repeating element in the repeater group is correct(For example repeater group has a field sales item and the slug for it is in the format sales_item_item).
Once You have made sure of that you just need to create an array of the new items set as you have already created with $items variable and call the below code:
$items_inserted = update_field('field_5bfed0e63a470', $items, $post_id);
Things should work fine for you with this. Good Luck!!!
Trying to transcript audio from Telegram voice message but I got "unable to transcode data stream audio/opus -> audio/x-float-array" error from watson's speech to text node.
I'm using Node-Red on Raspberry to simply transcript audio from Telegram voice message with node-red-contrib-telegrambot and node-red-node-watson.
With text messages, my code works as a charm.
With Voice Messages, I got "unable to transcode data stream audio/opus -> audio/x-float-array" error from watson's speech to text node.
node-red flow images I don't have enough reputation point to post images :(
JSON flow export
[
{
"id": "b4106ec1.63dd58",
"type": "tab",
"label": "Telegram",
"disabled": false,
"info": ""
},
{
"id": "d1198164.e38f68",
"type": "telegram receiver",
"z": "b4106ec1.63dd58",
"name": "FMWatsonBot",
"bot": "5f347711.7876d8",
"saveDataDir": "",
"x": 110,
"y": 100,
"wires": [
[
"f4b4ab25.5dde18",
"f5d126df.5b6928"
],
[]
]
},
{
"id": "c6ec445d.0840d8",
"type": "telegram sender",
"z": "b4106ec1.63dd58",
"name": "Send2Telegram",
"bot": "5f347711.7876d8",
"x": 780,
"y": 80,
"wires": [
[]
]
},
{
"id": "f4b4ab25.5dde18",
"type": "debug",
"z": "b4106ec1.63dd58",
"name": "",
"active": true,
"tosidebar": true,
"console": false,
"tostatus": false,
"complete": "false",
"x": 290,
"y": 60,
"wires": []
},
{
"id": "f5d126df.5b6928",
"type": "function",
"z": "b4106ec1.63dd58",
"name": "Save chat context",
"func": "msg.chatId = msg.payload.chatId;\nmsg.type = msg.payload.type;\nmsg.content = msg.payload.content;\nreturn msg;",
"outputs": 1,
"noerr": 0,
"x": 230,
"y": 160,
"wires": [
[
"c3d1a92d.227568"
]
]
},
{
"id": "276cfad7.cef62e",
"type": "function",
"z": "b4106ec1.63dd58",
"name": "Set Chat Context",
"func": "msg.payload = {\n chatId : msg.chatId,\n topic : msg.type,\n type : \"message\",\n content : msg.payload};\nreturn msg;\n",
"outputs": 1,
"noerr": 0,
"x": 730,
"y": 220,
"wires": [
[
"c6ec445d.0840d8"
]
]
},
{
"id": "c3d1a92d.227568",
"type": "switch",
"z": "b4106ec1.63dd58",
"name": "Check msg type",
"property": "type",
"propertyType": "msg",
"rules": [
{
"t": "eq",
"v": "message",
"vt": "str"
},
{
"t": "eq",
"v": "voice",
"vt": "str"
},
{
"t": "else"
}
],
"checkall": "true",
"repair": false,
"outputs": 3,
"x": 300,
"y": 240,
"wires": [
[
"e8452a44.f967c8"
],
[
"6aea6224.578d8c"
],
[]
]
},
{
"id": "e8452a44.f967c8",
"type": "function",
"z": "b4106ec1.63dd58",
"name": "Echo message",
"func": "msg.payload = {\n chatId : msg.chatId,\n topic : \"Text Echo\",\n type : msg.type,\n content : msg.content};\nreturn msg;",
"outputs": 1,
"noerr": 0,
"x": 540,
"y": 80,
"wires": [
[
"c6ec445d.0840d8"
]
]
},
{
"id": "6aea6224.578d8c",
"type": "change",
"z": "b4106ec1.63dd58",
"name": "Set voice URL",
"rules": [
{
"t": "set",
"p": "payload",
"pt": "msg",
"to": "payload.weblink",
"tot": "msg"
}
],
"action": "",
"property": "",
"from": "",
"to": "",
"reg": false,
"x": 440,
"y": 300,
"wires": [
[
"fc7b1590.557c"
]
]
},
{
"id": "493d1bac.216d3c",
"type": "change",
"z": "b4106ec1.63dd58",
"name": "Set transcription",
"rules": [
{
"t": "set",
"p": "payload",
"pt": "msg",
"to": "transcription",
"tot": "msg"
}
],
"action": "",
"property": "",
"from": "",
"to": "",
"reg": false,
"x": 680,
"y": 300,
"wires": [
[
"276cfad7.cef62e"
]
]
},
{
"id": "fc7b1590.557c",
"type": "watson-speech-to-text",
"z": "b4106ec1.63dd58",
"name": "S2T",
"alternatives": 1,
"speakerlabels": false,
"smartformatting": false,
"lang": "en-GB",
"langhidden": "en-GB",
"langcustom": "NoCustomisationSetting",
"langcustomhidden": "",
"custom-weight": "0.5",
"band": "BroadbandModel",
"bandhidden": "BroadbandModel",
"keywords": "",
"keywords-threshold": "0.5",
"word-confidence": false,
"password": "",
"apikey": "#########CHANGED VALUE TO POST###########",
"payload-response": false,
"streaming-mode": false,
"streaming-mute": true,
"auto-connect": false,
"discard-listening": false,
"disable-precheck": false,
"default-endpoint": true,
"service-endpoint": "https://stream.watsonplatform.net/speech-to-text/api",
"x": 530,
"y": 360,
"wires": [
[
"493d1bac.216d3c"
]
]
},
{
"id": "5f347711.7876d8",
"type": "telegram bot",
"z": "",
"botname": "FMWatsonBot",
"usernames": "",
"chatids": "",
"baseapiurl": "",
"updatemode": "polling",
"pollinterval": "300",
"bothost": "",
"localbotport": "8443",
"publicbotport": "8443",
"privatekey": "",
"certificate": "",
"verboselogging": false
}
]
Any hint?
Thanks in advance
Ferruccio
It boils down to how you are fetching the audio from Telegram. Check the answer to this related question - https://developer.ibm.com/answers/questions/424777/help-how-do-i-use-speech-to-text-with-my-telegram/
which shows how to build the url to send through to the Speech to text node.
Update: Flow perfectly works with telegram node v4.4.0, but fails with new version 5.1.5
So, it's not a problem regarding Speech to Text node.