How to set the title of a SwiftUi Chart? - swiftui

How does one set the title for a SwiftUi chart?
Any suggestions for how to dig through the docs.
I'm having a hard time learning the ins and out of this framework
Nothing obvious in Xcode docs.
And the web interface is also hard to navigate.
I've tried searching it but it's not a great strategy.
https://developer.apple.com/documentation/charts
maybe someone could suggest a strategy for finding relevant info in an Apple framework?
here's the essence of the chart in question
Chart {
ForEach(vm.wtrChrtData, id: \.self) { wtrDatum in
BarMark(x: .value("hr", wtrDatum.timestamp),
y: .value("cups", wtrDatum.cups))
.foregroundStyle(Color.blue1)
}
}
i could put a title above it use a Text("some title") in a VStack.

Related

SwiftUI present sheet in a TabItem of TabView using presentationDetents

I am trying to create the same experience where a sheet is presented in one of the tab items of a TabView - not covering the tabs.
I am using .presentationDetents() and SwiftUI4.0 for that.
See example of Apple's own app doing that
The sheet is covering the child view inside TabView.
However, instead in my own code I am getting
this - the tabs of the parent view are covered by the modal sheet
The link https://developer.apple.com/forums/thread/711702 suggests that it is not possible to achieve the same behaviour you see in Apple's Maps and Find My apps with the current SwiftUI API.

What is the SwiftUI sheet view equivalent in UIKit?

SwiftUI can easily present a sheet view on top of the existing review when a condition is met (see screen record below). What is the sheet component equivalent in UIKit?
Tried to Google a result and it looks like there is a sheet related view called UIActionSheet, but that does not seem like the right UI control.
REFERENCE
Example of how to achieve the same behavior with UIKit.
iOS 14 navigation bar title is not displayed in UINavigationController created programmatically
This is just another UIViewController presented modally with a Done UIBarButtonItem on the right side.

How to persist the selection of a favourite from a SwiftUI List?

I am following along with this SwiftUI Tutorial: https://developer.apple.com/tutorials/swiftui/handling-user-input
The concepts here are perfect for an app I want to create. I do however have one question.
Whilst this tutorial is great for showing the basics of using Environment Objects to update SwiftUI views (in this case, to add a star image to elements that have been favourited.), how might I go about saving these changes to last beyond the app's runtime.
Right now, I can favourite a bunch of Landmarks but if I quit and re-launch the app, those selections are lost. Is there a "SwiftUI friendly" way to store those favourite selections and have the main Landmark List view automatically draw the star images to mark previously chosen favourites when the app is launched cold? I'm guessing UserDefaults would be the way, but I don't know how well that will adapt to these funky Environment Objects.
Thank you.
EDIT:
I am now using UserDefaults to persist an array of "TeamProfile" objects which gets appended to whenever a team is marked as Favourite.
In my SwiftUI code, I am trying to add Star images to favourites with the following code. These TeamRow items are built into a List by the parent view.:
struct TeamRow: View {
var team: TeamProfile
var body: some View {
HStack {
Text(team.TeamName)
Spacer()
if team.isFavourite || teamsSelected.contains(team) {
Image(systemName: "star.fill")
.imageScale(.medium)
.foregroundColor(.yellow)
}
}
}
}
teamsSelected is the array that is being maintained by UserDefaults. It should add a star to TeamRow items from my persisted array (which is loaded on app launch and I can verify it's contents). However, the star is only drawn on the row if I make new favourite selections during the app session.
I can verify the contents of the data I am persisting with UserDefaults, so I don't know why SwiftUI is not responding by starring the correct elements when the app is launched.

How to add mouseover tooltip in google charts sankey diagram

and have been using Google Charts to visualize my data. I have tried to search for the answer to this question but couldn't not find anyone having the same problem as mine, or maybe my problem is really basic. Appreciate if someone can give me a hand.
I built a sankey diagram by following the steps listed in Google Charts - Sankey Diagram.
Here is my chart:
http://kuangkeng.github.io/keng-data-journalism/procurement%20project/sankey/index.html
However I would like to add tooltip to each line/path/link so when users mouseover, they can see the value and other details of the line.
According to Google Charts (the link above), I can fire a mouseover event using 'onmouseover' and 'onmouseout', but Google Charts does not show how to do it for Sankey diagram.
I then came over an example of using 'onmouseover' and 'onmouseout' in a Google Bar Chart:
https://developers.google.com/chart/interactive/docs/examples#mouseovertooltip
So I copied the code, modified and pasted into my code.
google.visualization.events.addListener(chart, 'onmouseover', barMouseOver);
google.visualization.events.addListener(chart, 'onmouseout', barMouseOut);
function barMouseOver(e) {
chart.setSelection([e]);
}
function barMouseOut(e) {
chart.setSelection([{'row': null, 'column': null}]);
}
When I mouseover, I got the error message "undefined is not a function". You can see the error message appears at the top of the chart when you mouseover the lines.
I tried to use another alternative showed by Google Charts documentation by adding a another column to my data and set its role as 'tooltip' but it didn't work for my chart because Google Sankey Diagram can only accept 3 columns.
Appreciate if someone can have a look or refer me to any solution available. Thanks.

Google Charts (JS) - is there a way of using a transparent background on a chart?

I'm using the Google Charts API to include various graphs on a webapp I'm working on. I'm using the javascript chart tools (not the image chart tools), and am wondering if it's possible to use a transparent background on a chart (e.g. line graph, pie chart, etc.)?
In the Configuration Options of the chart, specify
backgroundColor: { fill:'transparent' }
This worked for me in Chrome and Firefox.
It took me some time to find out. The doc page says you can only put in HTML color strings and I assumed 'transparent' was not one of them.
Setting a transparent background for Google Charts:
// Set chart options
var options = {'title':'Chart Title',
'width':600,
'height':300,
'backgroundColor': 'transparent',
'is3D':true
};
JSFIDDLE DEMO
backgroundColor: "00000000" worked for me.
If nothing works for you try locating the background rectangle at the end of your drawChart() function and add the fill-opacity attribute.
fill-opacity="0.0"
Example:
$('#mychart').find('svg rect:eq( 1 )').attr('fill-opacity','0.0');
Use the eq:() selector to select the rectangle you want to be transparent.
On the left of the cart there is a dropdown arrow - click that, and go to "cop chart".
When you paste the chart, you can still choose to link it, and it will paste with the background transparent.