Related
I want to create a line chart by Google Chart API. I want to get data from AJAX method and set data to chart as javascript JSON array, but I have a problem with datetime format inside. Additional, a AJAX data is a string which generated from PHP. I parse returned string to JSON array which I want to set as chart data.
v1 = '{"Date(2023, 1, 1, 20, 00, 00)", ... }'; # returned string AJAX
v1 = jQuery.parseJSON(v1);
data = new google.visualization.DataTable();
data.addColumn('datetime', 'Time');
data.addColumn({...something else...});
data.addRows(v1);
options = { ... };
chart = new google.visualization.LineChart(document.getElementById('linechart_material'));
chart.draw(data, options);
And I have this error message in console, when I try use Date(year, month, day, hour, minute, second) constructor (Google "Date String Representation" method) as first element of v1 array: Unhandled Promise Rejection: Error: Type mismatch. Value Date(2023, 1, 1, 20, 00, 00) does not match type datetime in column index 0
How to prepare datetime in JSON array and Google Chart API?
google's "Date String Representation" method only works when you are passing the json to the data table's constructor.
see following working snippet.
the json must be in a specific format, which you can see below,
or here: Format of the Constructor's JavaScript Literal data Parameter
however, they use a bad example on their page because they use the new keyword in front of the date value, which isn't required.
point being, you must have the json formatted as below, with keys for rows and columns...
google.charts.load('current', {
packages: ['corechart']
}).then(function () {
var jsonData = {
"cols": [
{"label": "datetime", "type": "date"},
{"label": "value", "type": "number"}
],
"rows": [
{"c":[{"v": "Date(2023, 1, 1, 0, 0, 0)"}, {"v": 1}]}, // = Feb 1, 2023 (month number index is zero-based)
{"c":[{"v": "Date(2023, 1, 2, 0, 0, 0)"}, {"v": 2}]},
{"c":[{"v": "Date(2023, 1, 3, 0, 0, 0)"}, {"v": 3}]},
]
};
var dataTable = new google.visualization.DataTable(jsonData);
console.log('data table rows', dataTable.getNumberOfRows());
console.log('row 1 date', dataTable.getValue(0, 0));
});
<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
from here: https://developers.google.com/chart/interactive/docs/datesandtimes#dates-and-times-using-the-date-string-representation
When serializing data using the JavaScript DataTable object literal notation to build your DataTable, the new Date() constructor cannot be used. Instead, Google Charts provides a Date string representation that allows your date or datetime to be serialized and parsed properly when creating a DataTable. This Date string format simply drops the new keyword and wraps the remaining expression in quotation marks:
"Date(Year, Month, Day, Hours, Minutes, Seconds, Milliseconds)"
I've built a web-app that where users can upload contacts using CSV files.
Once the CSV file is uploaded, I'd like to map the header fields to the app's fields.
The issue I'm facing is figuring out an efficient and simple way to return a list of CSV headers from a CSV link.
Ideally, I'd like the list to return via an HTTP request; or alternatively, using JavaScript, so I can pass it back to the app.
For example, here is a CSV file containing hurricane counts:
https://people.sc.fsu.edu/~jburkardt/data/csv/hurricanes.csv
The headers are:
Month, "Average", "2005", "2006", "2007", "2008", "2009", "2010", "2011", "2012", "2013", "2014", "2015"
My idea is to run a Python script in AWS Lambda that gets the CSV headers, then sends them to the app using an HTTP request.
Is there another way to get this done client-side or without having to setup a backend infrustructure like AWS Lambda?
There's an option to stream from requests and you can process CSV per line. See: Body Content Workflow
Example:
import requests
url = "https://raw.githubusercontent.com/codeforamerica/ohana-api/master/data/sample-csv/addresses.csv"
r = requests.get(url, stream=True)
for line in r.iter_lines():
header = line.decode("utf-8").split(",")
print(header)
break
This will give you only the header. I've used an example of raw CSV file from github: sample-csv
The "simple" answer is obviously to download the whole file and work with that. But you may be able to save some time. If the file accepts partial downloads, you are in luck. To check that:
import requests
requests.head("https://people.sc.fsu.edu/%7Ejburkardt/data/csv/hurricanes.csv").headers
Yields
{'Date': 'Mon, 27 Dec 2021 14:00:21 GMT',
'Server': 'Apache/2.4.46 (Fedora) OpenSSL/1.1.1g',
'Last-Modified': 'Mon, 27 Jun 2016 12:37:53 GMT',
'ETag': '"1f6-53641c9fb0a40"',
'Accept-Ranges': 'bytes',
'Content-Length': '502',
'X-Frame-Options': 'SAMEORIGIN',
'Keep-Alive': 'timeout=5, max=100',
'Connection': 'Keep-Alive',
'Content-Type': 'text/csv'}
Note the Accept-Ranges: 'bytes'. You can ask for specific portions of the file. Jackpot!
Then it's a question of working with requests and the headers:
resume_headers = {'Range':'bytes=0-2048'}
r = requests.get(url, stream=True, headers=resume_headers)
for chunk in r.iter_content(chunk_size=1024):
print(chunk)
Output:
b'"Month", "Average", "2005", "2006", "2007", "2008", "2009", "2010", "2011", "2012", "2013", "2014", "2015"\n"May", 0.1, 0, 0, 1, 1, 0, 0, 0, 2, 0, 0, 0 \n"Jun", 0.5, 2, 1, 1, 0, 0, 1, 1, 2, 2, 0, 1\n"Jul", 0.7, 5, 1, 1, 2, 0, 1, 3, 0, 2, 2, 1\n"Aug", 2.3, 6, 3, 2, 4, 4, 4, 7, 8, 2, 2, 3\n"Sep", 3.5, 6, 4, 7, 4, 2, 8, 5, 2, 5, 2, 5\n"Oct", 2.0, 8, 0, 1, 3, 2, 5, 1, 5, 2, 3, 0\n"Nov", 0.5, 3, 0, 0, 1, 1, 0, 1, 0, 1, 0, 1\n"Dec", 0.0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1\n\n'
So we have our header, as well as a couple more lines.
Note that I print, but you can save into a variable, a large string, write to a local CSV file,... whatever suits your need. Also, I selected a range of 2048 bytes, and chunks of 1024 bytes; adapt to the situation!
I am trying to integrate Plaid API with salesforce in a visualforce page and I have done all the procedures my only problem is that while the response is being returned there are no transactions in the response returned is there any particular parameter that I need to add for the transactions to appear in the response.
const request: TransactionsGetRequest = {
client_id: client_id,
secret: secret
access_token: access_token
start_date: '2018-01-01',
end_date: '2020-02-01',
options: {
count: 250,
offset: 0,
}
Response Example
{
"accounts": [
{
"account_id": "BxBXxLj1m4HMXBm9WZZmCWVbPjX16EHwv99vp",
"balances": {
"available": 110,
"current": 110,
"iso_currency_code": "USD",
"limit": null,
"unofficial_currency_code": null
},
"mask": "0000",
"name": "Plaid Checking",
"official_name": "Plaid Gold Standard 0% Interest Checking",
"subtype": "checking",
"type": "depository"
}
],
"transactions": [],
"item": {
"available_products": [
"balance",
"credit_details",
"identity",
"investments"
],
"billed_products": ["assets", "auth", "liabilities", "transactions"],
"consent_expiration_time": null,
"error": null,
"institution_id": "ins_3",
"item_id": "eVBnVMp7zdTJLkRNr33Rs6zr7KNJqBFL9DrE6",
"webhook": "https://www.genericwebhookurl.com/webhook"
},
"total_transactions": 0,
"request_id": "45QSn"
}
This is the response containing the number of transactions still I don't get any transactions when I use API calls in salesforce.
Thank you for posting your query. I think the issue is just that the dates you're using for your test query are too old for the Sandbox test environment -- I don't think the Sandbox loads transaction data for that far in the past. Can you try with a set of dates from this year?
Edit: Or it could be a timing issue / transactions haven't loaded yet, which seems likely given that it works in Postman for you but not in Salesforce.
I know ton of library can prettyprint json just by indenting/newline stuff but here is a line of my heavy json:
"Shape6":{"bounds_start":[0,-6,0],"bounds_end":[3,1,3],"origin":[2,15,-1],"mirror":true,"rotation":[0,0,0.837758],"uv":[15,30]}
All the libraries i found output something like this:
"Shape6": {
"bounds_start": [
0,
-6,
0
],
"bounds_end": [
3,
1,
3
],
"origin": [
2,
15,
-1
],
"mirror": true,
"rotation": [
0,
0,
0.837758
],
"uv": [
15,
30
]
}
But i'm looking for a more human-readable way which not add new lines for small arrays that can fit in a line like:
"Shape6": {
"bounds_start": [0, -6, 0],
"bounds_end": [3, 1, 3],
"origin": [2, 15, -1],
"mirror": true,
"rotation": [0, 0, 0.837758],
"uv": [15, 30]
}
i do want this because my json file is like 6k+ lines on the first example
if you know a js or a php library (for ajax purposes)
i thank you in advance (and sorry for my poor english :))
you can simply do that with using JSON.stringify() function (see the reference https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify)
the first argument is the object you want to beautify, the second one is the function where we can specify the logic of line indention and new line breakings, the 3rd argument of this function (now 4) is the number for indention for your beautified JSON data
JSON.stringify(obj, function(k,v)
{
if (v instanceof Array)
return JSON.stringify(v);
return v;
}, 4);
I need to create a stacked bar chart showing the engine status in a day. Here is the example of what I would like to have:
It looks like a gantt chart, but probably much simpler than a normal gantt chart. I am badly looking for a JavaScript/jQuery charting library which supports this kind of chart. I know lots of gantt chart library available, but wondering which library have the option/setting for the chart I want.
My data would be in this format:
[
{
"day": "2009-07-13",
"work": ["11:16:35-12:03:12", "12:32:48-13:26:28", "13:39:09-13:39:12", "13:41:03-13:41:05", "14:18:09-24:00:00"]
}, {
"day": "2009-07-14",
"work": ["00:00:00-07:22:25", "07:22:25-07:22:28", "10:10:04-10:10:31", "10:10:32-10:15:33", "10:18:07-10:21:19", "11:04:49-11:06:15", "11:12:50-11:19:05", "11:19:11-11:19:19", "11:45:50-11:51:42", "11:51:43-11:53:55", "14:03:13-14:13:04", "14:23:55-14:31:28", "14:31:28-14:38:00", "14:38:00-14:49:04", "16:34:56-16:44:33", "16:46:37-16:48:10", "16:48:11-24:00:00"]
}, {
"day": "2009-07-15",
"work": ["00:00:00-08:16:23", "09:57:57-10:15:05"]
}, {
"day": "2009-07-16",
"work": ["10:02:40-10:05:56", "10:07:16-10:09:26", "10:09:27-10:09:28", "13:18:31-24:00:00"]
}, {
"day": "2009-07-17",
"work": ["00:00:00-08:56:41", "16:07:58-16:08:23"]
}, {
"day": "2009-07-20",
"work": ["14:44:47-14:48:35", "15:09:14-16:47:06", "16:47:05-16:47:10", "16:47:13-16:47:15", "16:47:16-16:47:20"]
}, {
"day": "2009-07-21",
"work": ["10:52:51-16:37:07"]
}, {
"day": "2009-07-24",
"work": ["14:54:38-16:03:07", "16:16:23-16:35:14", "16:35:17-16:41:22", "16:43:37-23:56:37"]
}, {
"day": "2009-07-25",
"work": ["20:36:34-21:24:28", "21:24:43-23:45:53"]
}, {
"day": "2009-07-26",
"work": ["13:46:59-18:09:09"]
}, {
"day": "2009-07-28",
"work": ["13:48:30-13:51:10", "13:51:18-13:51:27", "13:52:17-14:57:31"]
}, {
"day": "2009-07-29",
"work": ["14:50:15-14:50:16", "15:36:17-15:43:51", "15:53:31-16:29:30", "16:57:50-23:07:28"]
}, {
"day": "2009-07-30",
"work": ["11:25:29-11:41:32", "16:06:37-16:33:09", "21:14:04-21:20:18", "21:53:57-22:18:59"]
}
]
The work attribute time slot is when the engine is working, the slots between work time slots is when the engine is off.
Have been looking for this for long. Any suggestion would be greatly appreaciated!
You could use JavaScript InfoVis Toolkit or make your own custom renderer.
Maybe you could modify BarChart example, so that it would display time in y-axis.
If you decide to write your own control then library like Raphael will help you a lot.
Anyways, it seems to be quite simple control, so there isn't any need for external dependencies to Flash, Silverlight etc.
You can try Flot which has a good looking Gantt chart plugin.
Example data:
var d1 = [
[Date.UTC(2010, 0, 1, 11, 23), 5, Date.UTC(2010, 0, 1, 11, 25), "Put Water into Pot"],
[Date.UTC(2010, 0, 1, 11, 35), 5, Date.UTC(2010, 0, 1, 11, 47), "Clean Cooker"],
[Date.UTC(2010, 0, 1, 11, 25), 4, Date.UTC(2010, 0, 1, 11, 27), "Put Pot on Cooker"]
]
From plugin specification:
var data = [
[Date.UTC(2010,0,25,12,45),1,Date.UTC(2010,0,25,13,15],"Name of Step"]
First Parameter is Start of Step.
Second is number of resource.
Third is End of step.
Fourth describes Name for step (used for tooltip).
I'm working on a timeline feature for jqplot. http://www.jqplot.com/
I've a JsFiddle with an example here: http://jsfiddle.net/NVbjv/8/
It's still work in progress and I need to figure out a few things. Take a look at some of my questions here at stackoverflow if you like to learn more. I hope to be able to evolve it in somekind of a plug-in.
( display pointlabel in highlighter jqplot , jqplot text labels on y-axis )