Android Question xCharts rotation issue

JdV

Active Member
Licensed User
Longtime User
Hello

In the attached example when the setting 'LineChart1.Rotation = 90' is used the chart is rotated as expected but it doesn't fit the space of the LineChart1 view.

Without rotation:


With rotation:


I don't know if this is a bug or expected behaviour.

Regards

Joe
 

Attachments

  • xChartTest.zip
    34 KB · Views: 229

klaus

Expert
Licensed User
Longtime User
It is the expected behaviour, it simply rotates the base panel of the chart.

Looking at your project I noticed that you are using this code in the Designer Script:
LineChart1.SetLeftAndRight(0,100%x)
LineChart1.SetTopAndBottom(0,100%y)
You can remove these two lines and set the anchors like below:

 
Upvote 0

JdV

Active Member
Licensed User
Longtime User
Hi

Thanks for the reply.

I have now set the view's anchors and it still doesn't fill the entire view with chart.


I have attached the project again with the amendments.

What I'm aiming for is a chart that fills the view regardless of the device's orientation. In the example app I can only achieve that by making the chart view a square.

Joe
 

Attachments

  • xChartTest.zip
    34 KB · Views: 198
Upvote 0

Mahares

Expert
Licensed User
Longtime User
I have attached the project again with the amendments.
I think klaus mentioned to you that is the expected behavior. If you want a chart that fills the screen whether in portrait or when you rotate the device to landscape, comment this line: LineChart1.Rotation = 90
The chart looks great if you do that.
 
Upvote 0

JdV

Active Member
Licensed User
Longtime User
Hi

You're right, it works for my simple example.

However I have a particular use case where I want to show time on the X axis. As such it doesn't make any sense to draw the chart in portrait mode as it will make the chart unreadable.

This is why I wanted to draw the chart rotated by 90 degrees regardless of the device's orientation.

Joe
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
This is why I wanted to draw the chart rotated by 90 degrees regardless of the device's orientation.
Why don't you then set this line : It will be locked to landscape regardless of the device's rotation
#SupportedOrientations: Landscape
 
Upvote 0

JdV

Active Member
Licensed User
Longtime User
For 'ease of use' reasons I don't want to force the whole app to landscape.

I appreciate that this is very much an edge case!
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
Here you are.
In the attached project I memorize the size and position of the chart.
On each call to CreateChartData, in portrait mode, I set the dimensions of the chart horizontal.
Then draw the chart and rotate it.
That's all.

Attached the modified project.

 

Attachments

  • xChartTestNew.zip
    34.1 KB · Views: 204
Upvote 0

Mahares

Expert
Licensed User
Longtime User
n the attached project I memorize the size and position of the chart.
Is the below equivalent to what you are trying to do without having to memorize the chart dimensions and positions.
It yielded for me the same chart effect as your ingenious solution:
B4X:
Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("layout")
    CreateChartData
End Sub

Sub CreateChartData
    If 100%Y > 100%x Then    ' portrait mode
        #SupportedOrientations: Portrait
    Else
        #SupportedOrientations: Landscape
    End If
    LineChart1.ClearData
    LineChart1.AddLine("First line", Colors.Blue)
    LineChart1.AddLine("Second line", Colors.Magenta)
    
    For i = 0 To 100
        LineChart1.AddLineMultiplePoints(i, Array As Double(i, 100-i), i Mod 10 = 0)
    Next

    LineChart1.Title = "Chart Title"
    LineChart1.XAxisName = "X Axis"
    LineChart1.YAxisName = "Y Axis"
    LineChart1.IncludeLegend = "TOP_RIGHT"
    LineChart1.YScaleMinValue = 0
    LineChart1.AutomaticScale = True
    LineChart1.XScaleTextOrientation = "45 DEGREES"

    LineChart1.DrawChart    
End Sub
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
What do you expect with this code:
B4X:
If 100%Y > 100%x Then    ' portrait mode
    #SupportedOrientations: Portrait
Else
    #SupportedOrientations: Landscape
End If

He wants to have both orientations available.
In landscape mode no special action.
In portrait mode, the chart views' height is bigger than its width.
But, to draw the chart, its width must be bigger than its height.
And then it is rotated by 90°.
I need to memorize the original width and height because the rotation doesn't change those.
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
He wants to have both orientations available.
In your case, the chart portrait look shows that is simply an inverted horizontal chart. In portrait mode, the y Axis is on top of the screen and horizontal, and the X axis is vertical. It does not look natural. But, if the OP is seeking that look, that is all it counts.
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
It does not look natural.
I agree with you.
But, making 'vertical' charts would need to double all drawing routines, or rewrite them with a management for both directions.
And I don't want to do it.
 
Upvote 0

JdV

Active Member
Licensed User
Longtime User
Thank you so much for your solution.

It works perfectly for my needs.

Joe
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…