Fixed range for y-axis in Swift Charts - swiftui

I want that my y-axis always shows a range from 0-60 even though there are data sets that do not represent the full range.
E.g. if I'm showing a data set with values of just 10 and 40 the y-axis range is only from 0-40 instead of 0-60.
Any ideas how to configure the y-axis for Swift Charts (iOS 16) with a fixed range?
Chart {
ForEach(model.series.entries, id: \.weekday) { element in
BarMark(
x: .value("Day", element.weekday, unit: .day),
y: .value("Feeling", element.value)
)
}
}
.chartXAxis {
let unit: Calendar.Component = model.resolution == .yearly ? .month : .day
AxisMarks(values: .stride(by: unit, count: 1)) { _ in
AxisGridLine()
AxisValueLabel(format: .dateTime.weekday(.narrow), centered: true)
}
}

try adding this to your Chart, ...to configure the y-axis for Swift Charts (iOS 16) with a fixed range:
.chartYScale(domain: 0...60)

Related

Any way to make the y-axis border longer in chart.js?

Any way to make the y-axis border longer on either side in chart.js?
HAVE
WANT
While looking how to add padding between the tick marks and x-axis, I found a solution which allows me to do just that plus the above.
Which is by setting the y offset to true and hiding the x border:
Copied from here:
x: {
grid: {
drawBorder: false, // hide the x axis
},
},
y: {
offset: true, // create a sensation of space with the x axis hidden
},
In my case, it's enough.

how to use Element.status(); in Raphael.js

the documentation is too breif.
can anyone knowa about how to use Element.status() in Raphael.js?
very appreciate if you can give a demo.
Element.status shows the current animation applied to an element OR you can set the status on a particular animation.
Think of an animation interpolating between 0 and 1. So half way through an animation, it would have a value of 0.5. This is applied to whatever attribute is being animated. (So if animating x from 0 to 100 half way through, status will be 0.5 and x will be 50, not accounting for any easing applied like bounce).
So lets suppose instead of creating a nice smooth interpolation between attributes, we want to make an element move in 10 discrete steps. We can set the status each time of an animation manually. Example.
var raphAnimation = Raphael.animation( { x: 100, y: 100 }, 1000);
rect.status( raphAnimation, 0.5 )
example jsfiddle
var rect = paper.rect(10, 20, 300, 200);
var raphAnimation = Raphael.animation( { x: 100, y: 100 }, 1000);
for( var c = 1; c <= 10; c++ ) {
(function() {
var step = c
setTimeout( function() {
rect.status( raphAnimation, 0.1 * step )
}, step * 200)
})();
}
Note: If you repeat using the status set command, I think it will take the animation as fresh. So suppose the animation is from 0->100 and you set status to be 0.5 it will go half way. Now suppose you do the same and call it again with 0.5, it will now be 0.5 of the 'remainder' as it now only has half the distance to traverse.

Google chart how to have two y axis

I want one chart showing temperature with one curve and humidity in percent with a second curve.
I did it.
However the curve of the temperature is small since max is around 22 while humidity Max is around 90.
Does Google chart support the possibility to have 2 scale with y axis?
Thanks
Rod
You need to add a second y-axis. You can do this by setting the vAxes option (which takes an object whose properties are objects with vAxis options), and use the series option to target each series to a specific axis. Here's an example:
vAxes: {
0: {
// options for left y-axis
title: 'Temperature'
},
1: {
// options for right y-axis
title: 'Humidity'
}
},
series: {
0: {
// options for first data series (I'm assuming this is temperature)
targetAxisIndex: 0 // target left axis
},
1: {
// options for second data series (I'm assuming this is humidity)
targetAxisIndex: 1 // target right axis
}
}

Horizontal line in Google scatter chart

I'm using a scatter chart to display data with the following range: x = [-1..1] y = [-1..1]. Is it possible to draw a horizontal line on e.g. y = 0.5?
I'm using the JavaScript charts (i.e. not the image charts).
We had the same problem at work. Unfortunately, for the moment Google Charts does not provide an easy way to display a line in the scatter chart, like in the bar chart.
Finally we found a "small trick" that works perfectly for us, as you can see here:
http://csgid.org/csgid/statistics/structures
The trick consist in creating a "Line chart" but setting the linewidth property to 0 and pointsize to 5 in the series of the points, and linewidth 1 and pointsize 0 in the serie of the line.
It looks like:
interpolateNulls: true,
series: {
0: { lineWidth: 0, pointSize: 5 },
1: { lineWidth: 0, pointSize: 5 },
2: { lineWidth: 0, pointSize: 5 },
3: { lineWidth: 0, pointSize: 5 },
4: { lineWidth: 1, pointSize: 0 }
}
Why did I set interpolateNulls to true? Because then, I had to change the way I was setting the data in the array before convert it to JSON and pass it to Google Charts. In every row I had to set the values of every serie in the X axis for each value of the Y axis. So I had to set to null the X value when a serie didn't have a Y value for that X value (I mean, when a serie didn't have any point for that X value). So, the same for the serie of the line.
This would be one point of the first serie (in JSON):
[2.6,0.184,null,null,null,null]
And this one "point" of the line serie (the last serie):
[4,null,null,null,null,0.254]
Maybe it is not the most efficient way, but it works :)
I hope I have explained it clear, let me know if you have more questions.

Get size of excel cell in pixels

I am trying to programatically (C++ but VBA explanations are OK) get the size of an excel cell in pixels. The excel application gui shows the size of the cell as:
Width: 8.28 (160 pixels) Height: 24.6 (41 pixels), Font is Arial 20 pt.
Using an excel range I can get the:
ColumnWidth: 8.3, RowHeight: 24.6
Range Width: 96, Range Height 24.6
I tried using PointsToScreenPixelsX and PointsToScreenPixelsY for all of the above values but they returned values which didn't match up with what the excel gui said (396 for row/cell height, 136 for column width and 224 for column width).
Any ideas?
The conversion from points to pixels depends on your DPI setting. There are 72 points to an inch, so if you have 96 points that's 4/3 of an inch. If your DPI (in Display Properties) is 120 then that works out to 160 pixels.
In other words, pixels = points * DPI / 72.
However, this doesn't take zoom into account. ActiveWindow.Zoom in Excel is a percentage, so for instance 200 is twice normal size. Note that the UI still shows unzoomed pixels.
The OP stated:
The excel application gui shows the size of the cell as:
Width: 8.28 (160 pixels) Height: 24.6 (41 pixels), Font is Arial 20 pt.
First let me clarify: the application gui shows column widths and height in a decimal measurement and a pixel measurement, regardless of font size, screen size, zoom, etc. For any of these factors, if the Excel column width is 8.43, it will always be defined as 64 pixels. Second, I am a little confused, because my version of Excel (2010 currently) and every prior version I can remember had the standard column width of 8.43 equal 64 pixels; likewise, the standard row height of 15 equals 20 pixels, which does not seem to match the OP's example.
Having established that, one poster asked "Why?" One reason: if you're adjusting column widths or row heights, Excel allows that in discrete units that, unfortunately, they decided to name pixels. Maybe they related to pixels in some early version, but they seem just as random as the units used - 8.43 is what, inches, picas, ??? Not twips, that's for sure! Here, I'll call it a decimal unit.
Anyway, for all column widths over 1.00, that discrete pixel unit is 1/7th of a decimal unit. Bizarrely, column widths under 1.00 are divided into 12 units. Therefore, the discrete widths up to the 2.00 decimal unit are as follows:
0.08, 0.17, 0.25, 0.33, 0.42, 0.5, 0.58, 0.67, 0.75, 0.83, 0.92, 1.00,
1.14, 1.29, 1.43, 1.57, 1.71, 1.86, 2.00
with 2.00 equaling 19 pixels. Yes, I'll pause while you shake your head in disbelief, but that's how they made it.
Fortunately, row heights appear to be more uniform, with 1 pixel equaling 0.75 decimal units; 10 pixels equaling 7.50; standard row height of 20 pixels equaling 15.00; and so on. Just in case you should ever need to convert between these randomly discrete units, here are a couple of VBA functions to do so:
Function ColumnWidthToPixels(ByVal ColWidth As Single) As Integer
Select Case Round(ColWidth, 4) ' Adjust for floating point errors
Case Is < 0:
ColumnWidthToPixels = ColumnWidthToPixels(ActiveSheet.StandardWidth)
Case Is < 1:
ColumnWidthToPixels = Round(ColWidth * 12, 0)
Case Is <= 255:
ColumnWidthToPixels = Round(12 + ((Int(ColWidth) - 1) * 7) _
+ Round((ColWidth - Int(ColWidth)) * 7, 0), 0)
Case Else:
ColumnWidthToPixels = ColumnWidthToPixels(ActiveSheet.StandardWidth)
End Select
End Function
Function PixelsToColumnWidth(ByVal Pixels As Integer) As Single
Select Case Pixels
Case Is < 0:
PixelsToColumnWidth = ActiveSheet.StandardWidth
Case Is < 12:
PixelsToColumnWidth = Round(Pixels / 12, 2)
Case Is <= 1790:
PixelsToColumnWidth = Round(1 + ((Pixels - 12) / 7), 2)
Case Else:
PixelsToColumnWidth = ActiveSheet.StandardWidth
End Select
End Function
Example
This example determines the height and width (in pixels) of the selected cells in the active window and returns the values in the lWinWidth and lWinHeight variables.
With ActiveWindow
lWinWidth = PointsToScreenPixelsX(.Selection.Width)
lWinHeight = PointsToScreenPixelsY(.Selection.Height)
End With