asp.net ShieldUI bar chart dates - javascript

I’m currently trying to implement a ShieldUI bar chart with dates as the X axis as defined by a date group in my data and I’m finding that the graph is shifting the data by one month. So, data that is supposed to be for 1/2017 is being graphed as 12/2016.
The ShieldUI suite I'm using is asp.net. The data is being brought over from SQL Server using a SQLDataSource object. I know that the underlying "engine" for the ShieldUI stuff is javascript and I also know that javascript uses zero based dates. Does anyone have any suggestions on how to get the chart to graph the dates correctly?
Thanks in advance for the help.
--Edit: To help visualize what is happening, the data being passed to the graph from SQL Server looks like this:
Date Group Company A Company B Company C
1/1/2016 3 22 14 ---This row graphs out as Dec 2015
2/1/2016 3 11 18 ---This row graphs out as Jan 2016
3/1/2016 1 3 25 ---This row graphs out as Feb 2016
4/1/2016 3 14 17 ---This row graphs out as Mar 2016
5/1/2016 NULL 7 16 ---This row graphs out as Apr 2016
6/1/2016 NULL 6 10 ---This row graphs out as May 2016
7/1/2016 NULL 8 23 ---This row graphs out as Jun 2016
8/1/2016 2 12 18 ---This row graphs out as Jul 2016
9/1/2016 3 9 20 ---This row graphs out as Aug 2016
10/1/2016 2 16 17 ---This row graphs out as Sep 2016
11/1/2016 2 4 11 ---This row graphs out as Oct 2016
12/1/2016 3 9 18 ---This row graphs out as Nov 2016

Related

Javascript Formatting date not working

I am using the ionic framework and I am trying to calculate the number of days between today and Sun Apr 18 2017 15:14:36 GMT+0800 (MYT)
and today :Thu Mar 09 2017 17:52:48 GMT+0800 (MYT)
it's showing that there are 44 days but I know it's 40 days. Can someone help me fix my code
$scope.daysLeft=Math.floor(Math.abs(newDate - new Date())/86400000);
You are wrong and the result is actually ok from your program.
There are 44 days between today and the selected date.
Just try here if you don't believe it.

How To Customize Angular Date Filter

How To Customize Angular Date Filter in Thai Format or Another Country format?
17 March 2017 or 2017-03-17 change to 17 {Thai Month} 2560

How to get time in range of two hours no matter which day?

I am using momentjs and its plugin moment-range, I want to get time in range of two hours no matter which day.
For example,
Jan 9, 2014 08:30am
Jan 1, 2015 10:00am
Feb 5, 2016 08:30am
Say I want to get between 8am to 9am. The result I want is
Jan 9, 2014 08:30am
Feb 5, 2016 08:30am
Can someone give me some hint? Thanks
I would just use moment.get('hour') and moment.get('minute') of each and do some simple math comparisons

filter time stamp to date

I'm using Stripe and the way stripe returns the start and end dates for a subscription are in timestamp such as this
current_period_start: 1465271767
current_period_end: 1433649367
I'm using angular for the presentation layer. Is there a way to convert this time stamp to short date like June 7, 2015?
right now I'm displaying the date like this and it always displays Jan 17, 1970
<td>{{x.current_period_start | date:short }}</td>
Unix timestamps are in seconds and JavaScript uses milliseconds. Multiply the start and end date values by 1000. Those dates will result in Mon Jun 06 2016 22:56:07 GMT-0500 (CDT) and Sat Jun 06 2015 22:56:07 GMT-0500 (CDT) respectively.
Untested but this should work in your view:
<td>{{x.current_period_start * 1000 | date:short }}</td>

How can I parse Javascript variables using python?

The problem: A website I am trying to gather data from uses Javascript to produce a graph. I'd like to be able to pull the data that is being used in the graph, but I am not sure where to start. For example, the data might be as follows:
var line1=
[["Wed, 12 Jun 2013 01:00:00 +0000",22.4916114807,"2 sold"],
["Fri, 14 Jun 2013 01:00:00 +0000",27.4950008392,"2 sold"],
["Sun, 16 Jun 2013 01:00:00 +0000",19.5499992371,"1 sold"],
["Tue, 18 Jun 2013 01:00:00 +0000",17.25,"1 sold"],
["Sun, 23 Jun 2013 01:00:00 +0000",15.5420341492,"2 sold"],
["Thu, 27 Jun 2013 01:00:00 +0000",8.79045295715,"3 sold"],
["Fri, 28 Jun 2013 01:00:00 +0000",10,"1 sold"]];
This is pricing data (Date, Price, Volume). I've found another question here - Parsing variable data out of a js tag using python - which suggests that I use JSON and BeautifulSoup, but I am unsure how to apply it to this particular problem because the formatting is slightly different. In fact, in this problem the code looks more like python than any type of JSON dictionary format.
I suppose I could read it in as a string, and then use XPATH and some funky string editing to convert it, but this seems like too much work for something that is already formatted as a Javascript variable.
So, what can I do here to pull this type of organized data from this variable while using python? (I am most familiar with python and BS4)
If your format really is just one or more var foo = [JSON array or object literal];, you can just write a dotall regex to extract them, then parse each one as JSON. For example:
>>> j = '''var line1=
[["Wed, 12 Jun 2013 01:00:00 +0000",22.4916114807,"2 sold"],
["Fri, 14 Jun 2013 01:00:00 +0000",27.4950008392,"2 sold"],
["Sun, 16 Jun 2013 01:00:00 +0000",19.5499992371,"1 sold"],
["Tue, 18 Jun 2013 01:00:00 +0000",17.25,"1 sold"],
["Sun, 23 Jun 2013 01:00:00 +0000",15.5420341492,"2 sold"],
["Thu, 27 Jun 2013 01:00:00 +0000",8.79045295715,"3 sold"],
["Fri, 28 Jun 2013 01:00:00 +0000",10,"1 sold"]];\s*$'''
>>> values = re.findall(r'var.*?=\s*(.*?);', j, re.DOTALL | re.MULTILINE)
>>> for value in values:
... print(json.loads(value))
[[['Wed, 12 Jun 2013 01:00:00 +0000', 22.4916114807, '2 sold'],
['Fri, 14 Jun 2013 01:00:00 +0000', 27.4950008392, '2 sold'],
['Sun, 16 Jun 2013 01:00:00 +0000', 19.5499992371, '1 sold'],
['Tue, 18 Jun 2013 01:00:00 +0000', 17.25, '1 sold'],
['Sun, 23 Jun 2013 01:00:00 +0000', 15.5420341492, '2 sold'],
['Thu, 27 Jun 2013 01:00:00 +0000', 8.79045295715, '3 sold'],
['Fri, 28 Jun 2013 01:00:00 +0000', 10, '1 sold']]]
Of course this makes a few assumptions:
A semicolon at the end of the line must be an actual statement separator, not the middle of a string. This should be safe because JS doesn't have Python-style multiline strings.
The code actually does have semicolons at the end of each statement, even though they're optional in JS. Most JS code has those semicolons, but it obviously isn't guaranteed.
The array and object literals really are JSON-compatible. This definitely isn't guaranteed; for example, JS can use single-quoted strings, but JSON can't. But it does work for your example.
Your format really is this well-defined. For example, if there might be a statement like var line2 = [[1]] + line1; in the middle of your code, it's going to cause problems.
Note that if the data might contain JavaScript literals that aren't all valid JSON, but are all valid Python literals (which isn't likely, but isn't impossible, either), you can use ast.literal_eval on them instead of json.loads. But I wouldn't do that unless you know this is the case.
Okay, so there are a few ways to do it, but I ended up simply using a regular expression to find everything between line1= and ;
#Read page data as a string
pageData = sock.read()
#set p as regular expression
p = re.compile('(?<=line1=)(.*)(?=;)')
#find all instances of regular expression in pageData
parsed = p.findall(pageData)
#evaluate list as python code => turn into list in python
newParsed = eval(parsed[0])
Regex is nice when you have good coding, but is this method better (EDIT: or worse!) than any of the other answers here?
EDIT: I ultimately used the following:
#Read page data as a string
pageData = sock.read()
#set p as regular expression
p = re.compile('(?<=line1=)(.*)(?=;)')
#find all instances of regular expression in pageData
parsed = p.findall(pageData)
#load as JSON instead of using evaluate to prevent risky execution of unknown code
newParsed = json.loads(parsed[0])
The following makes a few assumptions such as knowing how the page is formatted, but a way of getting your example into memory on Python is like this
# example data
data = 'foo bar foo bar foo bar foo bar\r\nfoo bar foo bar foo bar foo bar \r\nvar line1=\r\n[["Wed, 12 Jun 2013 01:00:00 +0000",22.4916114807,"2 sold"],\r\n["Fri, 14 Jun 2013 01:00:00 +0000",27.4950008392,"2 sold"],\r\n["Sun, 16 Jun 2013 01:00:00 +0000",19.5499992371,"1 sold"],\r\n["Tue, 18 Jun 2013 01:00:00 +0000",17.25,"1 sold"],\r\n["Sun, 23 Jun 2013 01:00:00 +0000",15.5420341492,"2 sold"],\r\n["Thu, 27 Jun 2013 01:00:00 +0000",8.79045295715,"3 sold"],\r\n["Fri, 28 Jun 2013 01:00:00 +0000",10,"1 sold"]];\r\nfoo bar foo bar foo bar foo bar\r\nfoo bar foo bar foo bar foo bar'
# find your variable's start and end
x = data.find('line1=') + 6
y = data.find(';', x)
# so you can get just the relevant bit
interesting = data[x:y].strip()
# most dangerous step! don't do this on unknown sources
parsed = eval(interesting)
# maybe you'd want to use JSON instead, if the data has the right syntax
from json import loads as JSON
parsed = JSON(interesting)
# now parsed is your data
Assuming you have a python variable with a javascript line/block as a string like"var line1 = [[a,b,c], [d,e,f]];", you could use the following few lines of code.
>>> code = """var line1 = [['a','b','c'], ['d','e','f'], ['g','h','i']];"""
>>> python_readable_code = code.strip("var ;")
>>> exec(python_readable_code)
>>> print(line1)
[['a', 'b', 'c'], ['d', 'e', 'f'], ['g', 'h', 'i']]
exec() Will run the code that is formatted as a string. In this case it will set the variable line1 to a list with lists.
And than you could use something like this:
for list in line1:
print(list[0], list[1], list[2])
# Or do something else with those values, like save them to a file

Categories