Can you have a primary and a secondary Yaxis on a graph?

Mahares

Expert
Licensed User
Longtime User
I have two data series such as PRESSURE and TEMPERATURE that need plotted on the same graph versus the same Xaxis. The plot will look a lot more readable and professional looking if I can make 2 Yaxis ; one the left to represent the pressure and the second on the right to represent the temperature. In the charts module, I could not find a way to insert more than one Yaxis. Any bright ideas or workarounds will really enhance the charting feature of B4A.
Also, it would be nice to to be able to insert a legend on the plot to distinguish between the 2 data series.
Thank you
 

klaus

Expert
Licensed User
Longtime User
Attached you find an example program that displays up to 4 curves on a same diagram with different Y scales.
You can adapt it to your requirements.

Best regards.
 

Attachments

  • Graph1.zip
    7.8 KB · Views: 308
  • Graph1.jpg
    Graph1.jpg
    38.6 KB · Views: 349
Upvote 0

Mahares

Expert
Licensed User
Longtime User
Thank you very much Klaus for the sample. That is a tremendous effort. It appears that it is the first of its kind in the forum. I hope it will help others too. I will report back to you as I decipher its components and see how I can adapt it to my situation where my graph data is extracted from a SQLite table. It appears overwhelming, but as I work through it the next few days, I hope I can break it down into manageable segments.
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
Klaus: After trying to accommodate your primary and secondary axis graph sample to my situation, I was getting an error such as : ArrayIndexOutofBoundsException error on line:
Curve(i, n) = Cursor1.GetInt("AVMCFD")
I zipped and uploaded the project along with the SQLite database: Mydb.db. This is the first time I zip and attach files to a forum thread. I hope it gets to you.
Thank you in advance for any assistance.
 

Attachments

  • ChartsWithPrimaryAndSecondaryAxiis.zip
    25.2 KB · Views: 207
Upvote 0

Mahares

Expert
Licensed User
Longtime User
Klaus: I have made some modifications so the the primary Y axis (left axis) has a logarithmic scale. I tried for several hours between a few days to give the first data series the look of a semi-log plot (y axis logarithmic and the X axis as linear), but as you can see from the code, I was not able to achieve it. I would like the Y scale of the left data to be something like: .1, 1, 10, 100 or 1, 10,100, 1000, 10000, or say 10,100 depending of course on the range of data points plotted. I have yet to see a semi-log plot in the forum to get hints from. If it is possible to make a semi-log plot, and you can lead me to that, that is great. If not, I can accept its original linear design.
Thank you very much.
 

Attachments

  • ChartPrimAndSecAxisKlaus.zip
    25.3 KB · Views: 169
Upvote 0

klaus

Expert
Licensed User
Longtime User
Here you are.
I made some other changes in your program.
To get the max and min values I use ExecQuerySingleResult.
I added some more variables to make the program a bit more universal.
CurveField, contains the filed name for the given curve
CurveMax, max value of the given curve
CurveMin, min value of the given curve.

Best regards.
 

Attachments

  • ChartPrimAndSecAxisKlaus1.zip
    25.7 KB · Views: 317
Upvote 0

positrom2

Active Member
Licensed User
Longtime User
Klaus,
concerning your recent ChartPrimAndSecAxisKlaus1.zip:
On the tablet it works fine, thanks, nice work!
But in the emulator, I get
java.io.FileNotFoundException:/mnt/sdcard/Mydb.db (Permission denied)
Since I am receiving this error always in the emulator when trying to write to file, could you advise me on :
How are storage locations to be accessed in the emulator?
Thanks, positrom2
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
Did you copy the DB to File.DirRootExternal in the Emulator ?
Did you set SD Card suport Beginner's Guide chapter 6.2
Try to comment the two lines below once, run the program and uncomment them aftrwards :
B4X:
' If File.Exists(DBFilePath, DBFileName) = False Then
  File.Copy(File.DirAssets, DBFileName, DBFilePath, DBFileName)
' End If
Best regards.
 
Upvote 0

ValDog

Active Member
Licensed User
Longtime User
The problem came that you didn't Dim the Curve() variable correctly.
I made some changes in your program, see the attached version.

Best regards.


Klaus,

How would you modify the sample code to display logarithm values < 1, say 0.1 or even 0.01?
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
You must think in decades.
In the sacle grid, each decade has the same width, but inside each decade the widths are logarithmic.
Then, depending on the numebr of decades you probably want to display a more or less detailed scale grid.
For numbers >1 the logarithm is positive, examples log(2) = 0.301, log(20) = 1.301
For numbers <1 the logarithm is negative, examples log(0.2) = -0.699, log(0.02) = -1.699
You need to take the logarithm of the number and split it into the integer and fractional parts.
For the scaling:
With the integer part you know in what decade the number is and with the fractional part you know where it is in the given decade.
If the logarithm is negative, you need to take the complement to 1 of the fractional part to know where it is in the decade.
In the example: log(0.02) = -1.699, the fracional part is 0.699 and because the logarithm is negative you must take the complement to 1, 1- 0.699 = 0.301.
 
Upvote 0

ValDog

Active Member
Licensed User
Longtime User
You must think in decades.
In the sacle grid, each decade has the same width, but inside each decade the widths are logarithmic.
Then, depending on the numebr of decades you probably want to display a more or less detailed scale grid.
For numbers >1 the logarithm is positive, examples log(2) = 0.301, log(20) = 1.301
For numbers <1 the logarithm is negative, examples log(0.2) = -0.699, log(0.02) = -1.699
You need to take the logarithm of the number and split it into the integer and fractional parts.
For the scaling:
With the integer part you know in what decade the number is and with the fractional part you know where it is in the given decade.
If the logarithm is negative, you need to take the complement to 1 of the fractional part to know where it is in the decade.
In the example: log(0.02) = -1.699, the fracional part is 0.699 and because the logarithm is negative you must take the complement to 1, 1- 0.699 = 0.301.


Klaus,

Having said that, I am still having difficulty implementing this. Could you update the most recent project's source code (ChartPrimAndSecAxisKlaus1.zip) to illustrate this approach? I would happily make a donation for your efforts.
 
Upvote 0

ValDog

Active Member
Licensed User
Longtime User
Here you are.
As you mix linear and logarithmic scales there are no lines between decades in the logarithmic scale.

Klaus,

Could you take a look at this again. It still does not seem to work (logarithmic scale) for positive values < 1 (fractional, say 2E-02, etc.. BTW, I don't really care about the linear scale - mostly just the logarithmic scale graphing.

Also, could you also give me a link for donation?
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
Here you are.
Attached the modified version.
I made a mistake with the Power function.
You shouldn't have changed the Curve variable type from Double to Int.

There still are no additional lines lines in the decades, this can only be done if you use logarythmic scales for all curves displayed at the same time with the same number of decades.

Also, could you also give me a link for donation?
There is no and there will be no link :).
 
Upvote 0
Top