I have variables populated throughout my program and on the last form I'm trying to display a pie chart that shows the portions of each expense in respect to the total expenses. The code I have right now is as follows:
Sub Chart()
Chart1.Series("Expenses").ChartType = SeriesChartType.Pie
Chart1.Series("Expenses").Points.Add(GlobalVariables.cellphone / totalexpenses * 100)
Chart1.Series("Expenses").Points.Add(GlobalVariables.carinsurance / totalexpenses * 100)
Chart1.Series("Expenses").Points.Add(GlobalVariables.drappointments / totalexpenses * 100)
Chart1.Series("Expenses").Points.Add(GlobalVariables.grocery / totalexpenses * 100)
Chart1.Series("Expenses").Points.Add(GlobalVariables.healthinsurance / totalexpenses * 100)
Chart1.Series("Expenses").Points.Add(GlobalVariables.medications / totalexpenses * 100)
Chart1.Series("Expenses").Points.Add(GlobalVariables.misc / totalexpenses * 100)
Chart1.Series("Expenses").Points.Add(GlobalVariables.rent / totalexpenses * 100)
Chart1.Series("Expenses").Points.Add(GlobalVariables.therapysessions / totalexpenses * 100)
Chart1.Series("Expenses").Points.Add(GlobalVariables.travel / totalexpenses * 100)
'Chart1.Width = 700
Chart1.Legends.Add("Expenses")
Chart1.Legends("Expenses").LegendStyle = LegendStyle.Table
End Sub
When I run the program and include the call to the sub the chart does not show at all and the legend still only shows "Series1." What am I doing wrong? How can I make this work?
By default the 1st series is 'Series1' and the pie chart is taking the blank data of this series. You need to delete it if you would use your own series. Add this code as the 1st line :
Chart1.Series.clear
Related
I'm looking for an SQL query to let me retrieve Zipcodes within a specific radius.
I have a table called tblZip with columns: Zipcode, Lat(latitude), Long(Longitude).
tblZip
Zipcode | Lat | Long
Short Text | Number | Number
I have checked few other answers that suggest use of Great-circle Distance but I don't seem to understand how it works. I get a headache just seeing all the conversions from / to radians and degrees and I really tried to understand it, but I'm so bad with mathematics.
I appreciate your help and guidance.
PS: I'm using a Microsoft Access Database.
Consider using the haversine distance formula in a VBA function (borrowed here). Then have it called in your SQL query. And even have your query filtered by distance with WHERE clause per your needs. Do note: this solution will only work inside the MSAccess.exe program. You will not be able to call it externally via ODBC/OLEDB.
And since you need comparison geocodes, SQL query below uses a cross join where each record is compared with every other record in table. Additionally, query avoids reverse duplicates and same-matched pairings reducing the size. But be careful if table is large as cross joins on self returns N2 records, that is before the filtering of duplicates.
SQL (Zipcode pairings with distance of 5 km or less)
SELECT z1.Zipcode, z2.Zipcode,
GetDistance(z1.Lat, z1.Lon, z2.Lat, z2.Lon) As km_distance
FROM tblZip z1, tblZip z2
WHERE z1.Zipcode > z2.Zipcode
AND GetDistance(s1.lat, s1.lon, s1.lat, s2.lon) <= 5;
VBA (save in a standalone module)
Function GetDistance(lat1Degrees As Double, lon1Degrees As Double, lat2Degrees As Double, lon2Degrees As Double) As Double
Dim earthSphereRadiusKilometers As Double
Dim kilometerConversionToMilesFactor As Double
Dim lat1Radians As Double
Dim lon1Radians As Double
Dim lat2Radians As Double
Dim lon2Radians As Double
Dim AsinBase As Double
Dim DerivedAsin As Double
'Mean radius of the earth (replace with 3443.89849 to get nautical miles)
earthSphereRadiusKilometers = 6371
'Convert kilometers into miles (replace 0.621371 with 1 to keep in kilometers)
kilometerConversionToMilesFactor = 0.621371
'Convert each decimal degree to radians
lat1Radians = (lat1Degrees / 180) * (4 * ATN(1))
lon1Radians = (lon1Degrees / 180) * (4 * ATN(1))
lat2Radians = (lat2Degrees / 180) * (4 * ATN(1))
lon2Radians = (lon2Degrees / 180) * (4 * ATN(1))
AsinBase = Sin(Sqr(Sin((lat1Radians - lat2Radians) / 2) ^ 2 + Cos(lat1Radians) * Cos(lat2Radians) * Sin((lon1Radians - lon2Radians) / 2) ^ 2))
DerivedAsin = (AsinBase / Sqr(-AsinBase * AsinBase + 1))
'Get distance from [lat1,lon1] to [lat2,lon2]
GetMiles = Round(2 * DerivedAsin * (earthSphereRadiusKilometers * kilometerConversionToMilesFactor), 2)
End Function
I have two bits of code in VBA for Excel. One calculates the A-squared statistic for the Anderson-Darling test, this bit of code calculates the P value of the A-squared statistic. I am curious if there is a more concise way or more efficient way to calculate this value in VBA:
Function AndDarP(AndDar, Elements)
'Calculates P value for level of significance for the
'Anderson-Darling Test for Normality
'AndDar is the Anderson-Darling Test Statistic
'Elements is the count of elements used in the
'Anderson-Darling test statistic.
'based on calculations at
'http://www.kevinotto.com/RSS/Software/Anderson-Darling%20Normality%20Test%20Calculator.xls
'accessed 21 May 2010
'www.kevinotto.com
'kevin_n_otto#yahoo.com
'Version 6.0
'Permission to freely distribute and modify when properly
'referenced and contact information maintained.
'
'"Keep in mind the test assumes normality, and is looking for sufficient evidence to reject normality.
'That is, a large p-value (often p > alpha = 0.05) would indicate normality.
' * * *
'Test Hypotheses:
'Ho: Data is sampled from a population that is normally distributed
'(no difference between the data and normal data).
'Ha: Data is sampled from a population that is not normally distributed"
Dim M As Double
M = AndDar * (1 + 0.75 / Elements + 2.25 / Elements ^ 2)
Select Case M
Case Is < 0.2
AndDarP = 1 - Exp(-13.436 + 101.14 * M - 223.73 * M ^ 2)
Case Is < 0.34
AndDarP = 1 - Exp(-8.318 + 42.796 * M - 59.938 * M ^ 2)
Case Is < 0.6
AndDarP = Exp(0.9177 - 4.279 * M - 1.38 * M ^ 2)
Case Is < 13
AndDarP = Exp(1.2937 - 5.709 * M + 0.0186 * M ^ 2)
Case Else
AndDarP = 0
End Select
End Function
I am in the process of converting Mathcad code into VBA and am trying to figure out how to replicate the While loop, which asks the program to run the loop while TGuess < 0. At the end of the loop is an if statement to break the loop if sGuess>1/1.4 (I would attach a picture, but my reputation does not allow me to).
I have written this code in VBA, but am wondering if including the sGuess variable in the original While statement is correct, or if it could influence the output of the loop:
While TGuess < 0 And sGuess <= 1 / 1.4
kterm = (kj ^ (1 / 6)) / 26 'k term in the numerator of depth equation
epw = 3 / 5
FDepth = ((kterm * RainInt * L * CF) / sGuess ^ 0.5) ^ epw
tflow = UW_Wat * g * sGuess * FDepth 'Calc Flow Shear Stress
pflow = 7.853 * UW_Wat * (tflow / UW_Wat) ^ (3 / 2)
TGuess = pflow - pcrit 'Recalc TGuess as E-P
sGuess = sGuess + SlopeInc 'Calc new stable slope
Wend
Any input would be appreciated.
To mitigate your concern, it might be better to replace the while...wend loop with a Do While ... Loop block. You can then put your break condition where you'd have it in the corresponding Mathcad code by using something along the lines of
If sGuess > 1/1.4 Then
Exit Do
End If
I am just starting to learn jython, and just have a question which I cannot seem to get right.
From my text, I am to create a picture that is 640 x 480 pixels, and then, using a loop, pixel by pixel set the color to a calculation for r, g, b which we have already been given.
I can create a picture, I can set variables, however I cannot seem to go any further in creating a loop to set each pixel colour.
I know its only simple, but just wandering if anyone can help me out here.
xrange() will create a generator which yields integers in a range. for will loop once per element of an iterable.
for row in xrange(480):
for col in xrange(640):
...
This may help you to iterate through the pixels.
picture = makeEmptyPicture(400,200)
pixels = getPixels(picture)
#make an empty picture and get the pixels
for px in getPixels(picture):
x=getX(px)
y=getY(px)
r = (sin(x * radian * id[1]) * cos(y * radian * id[4]) + 1) * ord(StringID[0]) * 2.5
g = (sin(x * radian * id[2]) * cos(y * radian * id[5]) + 1) * ord(StringID[0]) * 2.5
b = (sin(x * radian * id[3]) * cos(y * radian * id[6]) + 1) * ord(StringID[0]) * 2.5
newColor=makeColor(255 - r, 255 - g, 255 - b)
setColor(px, newColor)
show(picture)
repaint(picture)
I have the following code written by VBA in MS Access 2010. This code prints in the Label1.caption the number of this equation.
the_average = CDbl(TextBox1.Text) * 2 / 3 + CDbl(TextBox2.Text) / 3
the_average = Format(the_average, "#.###")
Label1.caption= the_average
Some of the operations have more than three decimal places. This code rounds the numbers when printing them in the caption of the label. For example if I have 1.66666666, it shows 1.667 and I want it to show 1.666 without rounding.
How can I do that?
Thanks in advance
the_average = CDbl(TextBox1.Text) * 2 / 3 + CDbl(TextBox2.Text) / 3
the_average = Format(the_average, "#.####")
Label1.caption= Left(the_average,5)
If your final string can vary in length, you will have to use the InStr() function (I believe that's it).
You can take advantage of Int()'s truncation;
the_average = formatnumber(int(the_average * 1000) / 1000, 3)
You can omit formatnumber if you don't want trailing zeros (.99 -> .990)