Deduction List - ASP Arrays - asp-classic

I want to display a breakdown of total investment deductions as an array list. The total amount of deductions is $-20.24 and my list breakdown is not adding up to that amount. I'm not too sure where I got it wrong. Please review my code and provide feedback. See total value being returned below:
Date Units Unit price Value
30/04/2018 -4.203 $ 1.99143 $ -8.37
30/04/2018 -0.366 $ 1.99454 $ -0.73
30/04/2018 -1.576 $ 3.54061 $ -5.58
30/04/2018 -0.138 $ 3.55072 $ -0.49
30/04/2018 -1.871 $ 2.49065 $ -4.66
30/04/2018 -0.164 $ 2.50000 $ -0.41
Total amount $ 16.98
<%
Dim objMemberClient, SwitchList
set objMemberClient = Server.createObject("MemberServiceProxy")
SwitchList= objMemberClient.GetInvestmentTransactionObjList(session("MemberId"),session("FundCode"), request.Querystring("date"), request.Querystring("date"), request.Querystring("description"))
%>
<h1>Investments</h1>
<div class="table-responsive">
<%if request.Querystring("description") = "Deduction" then %>
<TABLE class="table">
<%for i = LBound(SwitchList) to UBound(SwitchList)%>
<%if SwitchList(i).DeductionCode = getDesc(request.Querystring("subtype")) then%>
<tr>
<%if SwitchList(i).DeductionSign = true then%>
<td class="table_Header" width="200px">Investment sold</td>
<%exit for%>
<%end if%>
<%end if%>
<%Next%>
<td class="table_Header" width="125px">Date</td>
<td class="table_Header" width="125px">Units</td>
<td class="table_Header" width="125px">Unit price</td>
<td class="table_Header" width="125px">Value</td>
</tr>
<%for i = LBound(SwitchList) to UBound(SwitchList)%>
<%if SwitchList(i).DeductionCode = getDesc(request.Querystring("subtype")) then%>
<tr>
<td valign="top" class="border_Bottom"> <%=SwitchList(i).InvestmentOption.Name%></td>
<td valign="top" class="border_Bottom"><%=SwitchList(i).InvestmentDate%></td>
<td valign="top" class="border_Bottom"> <%=SwitchList(i).NumberUnits%></td>
<%if SwitchList(i).DeductionSign = true then %>
<%total = total + SwitchList(i).SwitchOutDollarValue%>
<%total = total * -1%>
<td valign="top" class="border_Bottom">$ <%=FormatNumber(SwitchList(i).SwitchOutDollarValue/Replace(SwitchList(i).NumberUnits,"-",""),5)%></td>
<td valign="top" class="border_Bottom">$ -<%=FormatNumber(SwitchList(i).SwitchOutDollarValue,2)%></td>
<%end if%>
</TABLE>

It would be easier to debug if we had something we could execute, but just guessing I think your problem is here:
<%total = total + SwitchList(i).SwitchOutDollarValue%>
<%total = total * -1%>
Total is a negative value, SwitchOutDollarValue I believe is a positive value, so comibining them in this way is not going to get the result you want.
As #SearchAndResQ mentioned, I would remove <%total = total * -1%> from inside the for loop and move it to just before you display the total.

Related

Adding class to item in array based on it's value

so I simply want to change the colour of the text of a list of arrays based on the word that's in the string.
Here's my array of combinations currently being outputted in the view with <%= #output %>:
[{"Mer lec sai ham"=>{:price=>13.1, :points=>53.4}}, {"Mer rus sai ham"=>{:price=>12.1, :points=>32.2}}, {"Fer rus sai ham"=>{:price=>13.1, :points=>31.4}}, {"Mcl rus sai ham"=>{:price=>13.5, :points=>14.9}}]
CSS:
span.mer{color:red;}
span.lec{color:green;}
And then I am just calling it in my view using <%= #combo %>
The ouput should be the list of combinations but this time just the words are coloured correctly. This is currently just showing nothing, it's not throwing errors though.
EDIT
Added my full controller code in case it's needed:
# DEFINE VARIABLE AND TARGET
teams = team_price.keys
drivers = driver_price.keys
target = 13.5
# CREATE METHOD TO SUM BOTH PRICES AND POINTS FOR GIVEN COMBINATION
def add_up(combo, ht, hd)
t, d = combo
ht[t] + hd.values_at(*d).sum
end
# METHOD TO DOUBLE HIGHEST DRIVER POINTS
def add_dbl(combo, team_points, driver_points)
t, ds = combo
dmax = ds.max_by { |d| driver_points[d] }
driver_points[dmax] + add_up(combo, team_points, driver_points)
end
# ALL POSSIBLE COMBINATIONS OF TEAM AND DRIVERS
all_driver_combos = drivers.combination(3).to_a
all_combos = teams.product(all_driver_combos)
# SHOW ALL COMBOS WHERE SUM DOES NOT EXCEED TARGET
valid_combos = all_combos.select do |c|
add_up(c, team_price, driver_price) <= target
end
# SORT VALID COMBOS BY SUM OF POINTS FOR EACH ELEMENT
ordered = valid_combos.sort_by do |c|
-add_dbl(c, team_points, driver_points)
end
# SORT DRIVERS BY HIGHEST POINTS
ordered.each do |_t,ds|
ds.sort_by! { |d| -driver_points[d] }
end
# OUTPUT
output = ordered.map do |c|
{ c.join(" ")=>{ price: add_up(c, team_price, driver_price),
points: add_dbl(c, team_points, driver_points).round(2)} }
end
# CONVERT OUTPUT TO HASH
#output = output.reduce Hash.new, :merge
Table Edit
I would now like to place this combination output in a table as follows (whilst keeping the colour coding solution):
Team
Double
Drivers
Price
Points
Mer
lec
sai ham
13.1
53.4
Mer
rus
sai ham
12.1
32.2
Fer
rus
sai ham
13.1
31.4
In my view, I now have:
<table class="table table-condensed">
<thead>
<tr>
<th>Combo</th>
<th>Price</th>
<th>Points</th>
</tr>
</thead>
<tbody>
<% #output.each do |key,value| %>
<tr>
<td><%= raw(key.gsub(/Mer/, '<span class="mer">Mer</span>')) %></td>
<td><%= value[:price] %></td>
<td><%= value[:points] %></td>
</tr>
<% end %>
</tbody>
</table>
Which is currently outputting:
Combo Price Points
Mer lec sai ham 13.1 53.4
Mer rus sai ham 12.1 32.2
Fer rus sai ham 13.1 31.4
Mcl rus sai ham 13.5 14.9
However as shown in the table example above I would like it so that 'Combo' is split up into the correct columns. Like so:
Before:
Mer lec sai ham | 13.1 | 53.4
After:
Mer | lec | sai ham | 13.1 | 53.4
Price and points work fine, but it's just a case of splitting up the combo. Looking at the table I would like to create, the 'team' (e.g Mer, fer) is always going to be the first value in the array. 'Double' is always the second value in the array (lec in the above case) and then 'Driver' is just the remaining values (sai ham in the above case). Because the array is ordered in this way is there a way to pull each out using something like: .key[1] or somehow assign each key a new name? Thanks for the help!
It looks like you are using assignment (=) when you should be using comparison (==)?
#combo = #output.each do |p|
if p = 'Mer'
class_name = 'red'
elsif p = 'lec'
class_name = 'green'
else
class_name = 'black'
end
end
Instead of
#combo = #output.each do |p|
if p == 'Mer'
class_name = 'red'
elsif p == 'lec'
class_name = 'green'
else
class_name = 'black'
end
end
Also you are passing a hash as p and trying to compare it to a string. You don't need combo at all and should leave interface concerns out of your controller code. I would get rid of #combo. As far as stying things using CSS I try to keep it semantic. So I would name classes like:
span.mer{color:red;}
span.lec{color:green;}
And just leave black as the default.
Edit
Also if you can change your #output to just a simple hash of items with the string as the key and the hash of price and points as the value like this:
#output = {"Mer lec sai ham"=>{:price=>13.1, :points=>53.4}, "Mer rus sai ham"=>{:price=>12.1, :points=>32.2}, "Fer rus sai ham"=>{:price=>13.1, :points=>31.4}, "Mcl rus sai ham"=>{:price=>13.5, :points=>14.9}}
Possible view code(Edited to address request for table):
<table class="table table-condensed">
<thead>
<tr>
<th>Team</th>
<th>Double</th>
<th>Drivers</th>
<th>Price</th>
<th>Points</th>
</tr>
</thead>
<tbody>
<% #output.each do |key,value| %>
<tr>
<% key = key.split(' ') %>
<% key = "<td>#{key[0]}</td><td>#{key[1]}</td><td>#{key[2]} #{key[3]}</td>" %>
<%= raw(key.gsub(/Mer/, '<span class="mer">Mer</span>')) %>
<td><%= value[:price] %></td>
<td><%= value[:points] %></td>
</tr>
<% end %>
</tbody>
</table>
This will wrap any word with a <span> tag that applies the color to that word based on the css defs. If "Lec" could appear as "Lec" or "lec" I would change the regexp in gsub to be case insensitive.
Multiple problems:
you are using asignment (=) instead of comparison (==)
you are comparing (if you change it) a Hash to a string, which will never be true
you are assigning a local variable (class_name) which does not have an effect on the output
it seems you want to highlight each word with the color, not just the full sentence
you have an array of hashes, where the key is a "sentence" (at least multiple words) which you want to highlight seperately and the value is a hash again.
Here is something to get you started (first step is to get code working, next step would be to clean it up. E.g. moving things to helpers or components or decorators).
def whatever_action_this_is
#output_with_colors = #output.map do |item|
item.transform_keys do |key|
words = key.split(' ')
highlighted = words.map do |word|
content_tag(:span, word, class: css_class(word))
end
highlighted.join(' ')
end
end
end
private
def css_class(word)
case word.downcase
when 'mer' then 'red'
when 'lec' then 'green'
else 'black'
end
end
And then in your view, assuming some ERB template
<ul>
<% #output_with_colors.each do |item|
<% item.each do |key, value|
<li>
<%= key.html_safe %>
<br/>
<%= value %>
</li>
<% end %>
<% end %>
</ul>

bar charts in ggplot2 with .csv files

I think my question is very basic, but I have just begun to learn R. Today I have spent a long time trying to import data from .csv files. I have 25 .csv files, which are all structured like the example file. From the data I would like to create graphs like I did with Excel 1, because with ggplot2 I can create much nicer graphs than with Excel.
In my csv files I have data from different organisms, the concentration of formed compounds (molecule1 and molecule2) and the standard deviation (molecule1sd, molecule2sd). In my figures I want to show the data for one compound with standard deviations for all organisms as a bar chart, which is overlaid with a line chart with the concentration without organisms.
However, somehow I can' t get this to work. I have tested this, but somehow it doesn' t work.
I need your help
+ usePackage("data.table")
> usePackage("ggplot2")
>
> # load packages
> library(data.table)
> library(ggplot2)
>
> results <- fread("Results.csv", header=TRUE, sep=";")
>
> str(results)
Classes ‘data.table’ and 'data.frame': 7 obs. of 5 variables:
$ Organism : chr "AC1432" "D3425" "BF3523" "XR2405" ...
$ Molecule1 : num 39.5 418.4 189.2 49.3 4610.9 ...
$ Molecule1sd: num 19.6 70.9 102.8 21.2 275.9 ...
$ Molecule2 : num 276 6511 235 500 11205 ...
$ Molecule2sd: num 21 291.1 109.7 67.1 94.5 ...
- attr(*, ".internal.selfref")=<externalptr>
>
> df <- data.frame(results)
>
> str(df)
'data.frame': 7 obs. of 5 variables:
$ Organism : chr "AC1432" "D3425" "BF3523" "XR2405" ...
$ Molecule1 : num 39.5 418.4 189.2 49.3 4610.9 ...
$ Molecule1sd: num 19.6 70.9 102.8 21.2 275.9 ...
$ Molecule2 : num 276 6511 235 500 11205 ...
$ Molecule2sd: num 21 291.1 109.7 67.1 94.5 ...
>
> ggplot(data=df, aes(x=Organism)+
+ geom_bar(aes(fill=Organism)) + geom_errorbar(data=df, mapping=aes(x="Organism", ymin="Molecule1sd", ymax="Molecule1sd"), width=0.2, size=1, color="blue")
+
Data from results.csv in the following table:
</head>
<body link="#0563C1" vlink="#954F72">
<table border=0 cellpadding=0 cellspacing=0 width=480 style='border-collapse:
collapse;table-layout:fixed;width:360pt'>
<col width=80 span=6 style='width:60pt'>
<tr height=20 style='height:15.0pt'>
<td height=20 width=80 style='height:15.0pt;width:60pt'>Organism</td>
<td width=80 style='width:60pt'>Molecule1</td>
<td width=80 style='width:60pt'>Molecule1sd</td>
<td width=80 style='width:60pt'>Molecule2</td>
<td colspan=2 width=160 style='mso-ignore:colspan;width:120pt'>Molecule2sd</td>
</tr>
<tr height=20 style='height:15.0pt'>
<td height=20 style='height:15.0pt'>AC1432</td>
<td class=xl65 align=right>39.5</td>
<td class=xl65 align=right>19.6</td>
<td class=xl65 align=right>275.9</td>
<td class=xl65 align=right>21.0</td>
<td></td>
</tr>
<tr height=20 style='height:15.0pt'>
<td height=20 style='height:15.0pt'>D3425</td>
<td class=xl65 align=right>418.4</td>
<td class=xl65 align=right>70.9</td>
<td class=xl65 align=right>6511.0</td>
<td class=xl65 align=right>291.1</td>
<td></td>
</tr>
<tr height=20 style='height:15.0pt'>
<td height=20 style='height:15.0pt'>BF3523</td>
<td class=xl65 align=right>189.2</td>
<td class=xl65 align=right>102.8</td>
<td class=xl65 align=right>235.2</td>
<td class=xl65 align=right>109.7</td>
<td></td>
</tr>
<tr height=20 style='height:15.0pt'>
<td height=20 style='height:15.0pt'>XR2405</td>
<td class=xl65 align=right>49.3</td>
<td class=xl65 align=right>21.2</td>
<td class=xl65 align=right>499.9</td>
<td class=xl65 align=right>67.1</td>
<td></td>
</tr>
<tr height=20 style='height:15.0pt'>
<td height=20 style='height:15.0pt'>XR2463</td>
<td class=xl65 align=right>4610.9</td>
<td class=xl65 align=right>275.9</td>
<td class=xl65 align=right>11205.3</td>
<td class=xl65 align=right>94.5</td>
<td></td>
</tr>
<tr height=20 style='height:15.0pt'>
<td height=20 style='height:15.0pt'>ATF259</td>
<td class=xl65 align=right>751.7</td>
<td class=xl65 align=right>71.6</td>
<td class=xl65 align=right>9507.9</td>
<td class=xl65 align=right>707.2</td>
<td></td>
</tr>
<tr height=20 style='height:15.0pt'>
<td height=20 style='height:15.0pt'>without organism</td>
<td align=right>35</td>
<td></td>
<td align=right>250</td>
<td colspan=2 style='mso-ignore:colspan'></td>
</tr>
<![if supportMisalignedColumns]>
<tr height=0 style='display:none'>
<td width=80 style='width:60pt'></td>
<td width=80 style='width:60pt'></td>
<td width=80 style='width:60pt'></td>
<td width=80 style='width:60pt'></td>
<td width=80 style='width:60pt'></td>
<td width=80 style='width:60pt'></td>
</tr>
<![endif]>
</table>
Thank you for your help
Try this as a starting point:
ggplot( df ,aes( Organism, Molecule1, fill=Organism )) +
geom_bar( stat="identity" ) +
geom_errorbar( aes(ymin=Molecule1-Molecule1sd, ymax=Molecule1+Molecule1sd), width=.2 )
Changing the details can be done later easily, once you have a working backbone.
Saving can be done outside of ggplot, simply by calling:
png("yourfile.png")
<plotting stuff>
dev.off()
Result:

re-sequencing a column of numbers based on a set value, R

I have a column of numbers that need to be re sequenced starting from 1, for example:
column with numbers: 226 227 227 227 228 228 229 229 ...... so on
I would like to re sequence so that: 226 changes 1, 227 changes 2, 227 = 2, 227 = 2, 228 = 3, 228 = 3,
229 = 4, 229 = 4 ....... and so on
<html>
<head>
<style>
table {
font-family: arial, sans-serif;
border-collapse: collapse;
width: 30%;
}
td, th {
border: 1px solid #dddddd;
text-align: left;
padding: 8px;
}
tr:nth-child(even) {
background-color: #dddddd;
}
</style>
</head>
<body>
<table>
<tr>
<th>Current</th>
<th>Desired</th>
</tr>
<tr>
<td>226</td>
<td>99</td>
</tr>
<tr>
<td>227</td>
<td>100</td>
</tr>
<tr>
<td>227</td>
<td>100</td>
</tr>
<tr>
<td>227</td>
<td>100</td>
</tr>
<tr>
<td>228</td>
<td>101</td>
</tr>
<tr>
<td>228</td>
<td>101</td>
</tr>
<tr>
<td>229</td>
<td>102</td>
</tr>
<tr>
<td>229</td>
<td>102</td>
</tr>
<tr>
<td>229</td>
<td>102</td>
</tr>
</table>
</body>
</html>
You can try as.numeric + factor like below, i.e.,
r <- as.numeric(factor(v))
such that
> r
[1] 1 2 2 2 3 3 4 4
DATA
v <- c(226, 227, 227, 227, 228, 228, 229, 229)
We can subtract the minimum or the first element of 'Current' and then add 99 to it
df1$Desired <- df1$Current - df1$Current[1] + 99
df1$Desired
#[1] 99 100 100 100 101 101 102 102 102
Or if it needs to be resequenced from 1
df1$Current - df1$Current[1] + 1
Or use match
match(df1$Current, unique(df1$Current))
data
df1 <- data.frame(Current = c(226, 227, 227, 227, 228, 228, 229, 229, 229))

ASP.NET Repeater Group by Data

I'm using a FormView Repeater to display my data in a table. However, I want to group the same data together. I have total 4 categories. Ford, Nissan, Toyota, and Volvo. The data row for each brand of car is different. How to group it and make it show only once?
<asp:Repeater runat="server" DataSource='<%# Container.DataSource %>'>
<ItemTemplate>
<tr>
<td align="right">Ford</td>
<td align="left"><%# Eval("Carcode")%></td>
<td align="left"><%# Eval("PlateNum")%></td>
<td align="left"><%# Eval("StoreName")%></td>
</tr>
</ItemTemplate>
</asp:Repeater>
My results from the Data Table:
Car_Name Car_Code Plate_Number Store_Name
Ford 1234 abc123 storeA
Ford 1121 abc111 storeB
Ford 2311 aaa111 storeC
.....................................
Expected Results:
Car_Name Car_Code Plate_Number Store_Name
Ford 1234 abc123 storeA
1121 abc111 storeB
2311 aaa111 storeC
Nissan 1234 abc123 storeA
1121 abc111 storeB
2311 aaa111 storeC
Change your markup around the company name like:
<td align="right">
<asp:Label id="lblName" runat="server" Text='<%# Eval("CompanyName") %>' />
</td>
Create a private variable
private _lastCompany As String = ""
In the event handler
Public Sub ItemDatabound(e As RepeaterItemEventArgs) ..
Dim data As ClassType = CType(e.Item.DataItem, ClassType)
If (data.CompanyName = _lastCompany) Then
e.Item.FindControl("lblName").Visible = False
End If
_lastCompany = data.CompanyName
End Sub
You can use the ItemDataBound event to get the value of the row, and compare to a variable that has the previous item. Hide the control anytime the previous row matches the current row.

Customize Table in Bootstrap

I am creating an ecommerce website using the current Bootstrap version. On the admin panel I am trying to show all the product infos within a table in such a way that it looks like this :
+______+_____________+_____+_______+_____+
| name | description | qty | price | sku |
+______+_____________+_____+_______+_____+
| P1 | Description | 10 | 200 | 2ds |
+______+_____________+_____+_______+____ +
| Image 1 , Image 2 , Image 3, ...... |
+______+_____________+_____+_______+____ +
| P2 | Description | 14 | 500 | 3ds |
+______+_____________+_____+_______+____ +
| Image 1 , Image 2 , Image 3, ...... |
+______+_____________+_____+_______+____ +
| P3 | Description | 45 | 234 | 4xs |
+______+_____________+_____+_______+____ +
| Image 1 , Image 2 , Image 3, ...... |
+______+_____________+_____+_______+____ +
And so on. Hope I can make you understand what I am trying to do here. After every product row comes the product images.. then again another product row and its corresponding images and so on....
What I have tried :
<table class="table table-striped text-center">
<thead>
<tr>
<th>Name</th>
<th>Category</th>
<th>Description</th>
<th>Stock Qty</th>
<th>Shown Qty</th>
<th>Sale Price</th>
<th>Original Price</th>
<th>SKU</th>
<th>Image</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<tr>
<td>Some Product</td>
<td>T Shirt</td>
<td>Description</td>
<td>24</td>
<td>500</td>
<td>BKSTU56V57</td>
</tr>
<tr><img src=".." /><img src="...."></tr>
</tbody>
</table>
And it's not working. taking the images to the top even before the <th>
You're missing the <td> element. Try this with column span:
<tr><td colspan="6"><img src=".." /><img src="...."></td></tr>

Resources