ios-charts: Can Selecting A Node Trigger A Labels Text To Change? - swift

I am using ios-charts in swift language. I'm wanting to show more data about each node when they are selected. A marker wouldn't have enough space for all the info I am wanting to display so I am wanting to display it all in a dedicated label on the ViewController itself. Is there any way to do this?
I got the chartValueSelected method working but am not sure how to utilize it for this.

Included ChartViewDelegate
&
chtChart.delegate = self
I have an array called textNotes thats corresponds to each data entry in the array and made a label called infoLabelChart
func chartValueSelected(_ chartView: ChartViewBase, entry:
ChartDataEntry, highlight: Highlight) {
let pos = NSInteger(entry.x)
infoLabelChart.text = "\(textNotes[pos])"
}

Related

Handle paragraphs in swift

Is there a recommended way of handling paragraphs in swift? I am very new to swift so I'm not sure what the recommended solution or any solution for that matter is.
I want to be able to open a .txt file and be able to select a paragraph, selecting the paragraph needs to print the selected paragraph to a label.
I haven't got any code for this yet, other than opening and viewing the text file by doing the following:
let file = "/Users/wade/Desktop/ht.txt"
let path=URL(fileURLWithPath: file)
let text=try! String(contentsOf: path)
textView.stringValue = text
Once the .txt file is displayed I want to be able to click on a paragraph and have the paragraph display in a separate label
I am not fixed on using .txt files if there is a better format for achieving this
I'm guessing that printing to the label should be as easy as
let selectedParagraph = //however we identify the paragraph stringvalue
let thelabel = selectedParagraph.stringValue
But I need to know how to identify and get the text from the paragraph
Create a subsclass of NSTextView and use it to display the whole text. This will always select text by paragraph:
class ParagraphTextView: NSTextView {
override func selectionRange(forProposedRange proposedCharRange: NSRange,
granularity: NSSelectionGranularity) -> NSRange {
return super.selectionRange(forProposedRange: proposedCharRange,
granularity: .selectByParagraph)
}
}
Then set a delegate (NSTextViewDelegate) and track selection changes of the text view to update your secondary label with the current selection.

select an option with image o buttons

I have 3 buttons (can be images too) I need to select only one of them and the one that is selected changes color.
Some reference guide or help to implement.
see image of how it would be
Similar to Waze report: https://i.stack.imgur.com/dOiLe.jpg
The simplest way to do this is to use a UISegmentedControl. It consists of multiple buttons only one of which is selected at a time. It can be customized very heavily, so you can have the button images be one color when selected and another color when not selected.
This is a fairly standard UISegmentedControl:
This is a heavily customized UISegmentedControl:
As you can see, you can make the segments look however you like, and "the one that is selected changes color".
To implement it, example assuming you have 3 options in the segment, to receive the selected data you can use the following code in an IB:
#IBOutlet weak var optionSegment: UISegmentedControl!
var optionSelect = String()
#IBAction func indexChanged(_ sender: Any) {
switch optionSegment.selectedSegmentIndex
{
case 0:
optionSelect = "Select option 1"
print(optionSelect)
case 1:
optionSelect = "Select option 2"
print(optionSelect)
default:
optionSelect = "Select option 3"
print(optionSelect)
}
}
To use it outside of the function, you can use
let dataSelect = optionSegment.titleForSegment(at: optionSegment.selectedSegmentIndex)
print(dataSelect)

Touch events on pie graph in swift 3

i am currently working on an app that involves a pie chart. And i am struggling to figure out how to make the segments of the pie graph behave as UIButtons. I have found how to do what i am looking for on bar graphs, but it doesnt seem to be the same with pie graphs.
I am using IOSCharts, I have added ChartViewDelegate to the class header, pieView.delegate = self is in the viewDidLoad().
This is the current function i am using to try and use the touchevents
func chartValueSelected(chartView: ChartViewBase, entry: ChartDataEntry, dataSetIndex: Int, highlight: Highlight) {
print("Triangle")
}
I replicated your scene of the problem with the new function:
func chartValueSelected(_ chartView: ChartViewBase, entry: ChartDataEntry, highlight: Highlight) {
print("Triangle")
}
And it is working as expected, so I'm guessing your problem is not in the code.
Be sure to check for typos or that your pieView is linked correctly.
Apologies for the late answer.

Show and hide line chart highlight on touch

I want to only highlight a data point when the finger is on the chart, as soon as it lifts off the screen I want to call, or simple deselect the highlight.
func chartValueNothingSelected(chartView: ChartViewBase) {
print("Nothing Selected")
markerView.hidden = true
}
I've tried to override the touch ended but haven't gotten it to work.
You can turn off highlighting any bars/data all together using the highlightEnabled property.
Example of this is:
barChartView.data?.highlightEnabled = false
If you still want to be able to highlight values, but want them to automatically deselect after the touch has ended, I also found another function highlightValues(highs: [ChartHighlight]?) which says in the documentation..
Provide null or an empty array to undo all highlighting.
Call this when you want to deselect all the values and I believe this will work. Example of this could be:
let emptyVals = [ChartHighlight]()
barChartView.highlightValues(emptyVals)
Ref:
Charts Docs: highlightValues documentation
If you don't have to do anything with the tapped data you can use:
barChartView.data?.highlightEnabled = false
If you want to use the tapped data point without displaying the highlight lines, you can use the selection delegate (don't forget to add ChartViewDelegate to your class):
yourChartView.delegate = self // setup the delegate
Add delegate function:
func chartValueSelected(_ chartView: ChartViewBase, entry: ChartDataEntry, highlight: Highlight) {
// do something with the selected data entry here
yourChartView.highlightValue(nil) // deselect selected data point
}

How to setup printing in cocoa, swift?

I have made printing functionality for custom NSView of NSPopover by the assigning the following action to button for this NSView in mainController:
#IBOutlet var plasmidMapIBOutlet: PlasmidMapView!
#IBAction func actionPrintfMap(sender: AnyObject)
{
plasmidMapIBOutlet.print(sender)
}
It is working, but the print window has no option for Paper Size and Orientation, see screenshot below.
What should I do to get these options in the print window?
And, how to make the NSView fitting to the printable area? Now it is not fitting.
I have figured out some moments, but not completely. So, I can setup the printing by the following code
#IBAction func actionPrintMap(sender: AnyObject)
{
let printInfo = NSPrintInfo.sharedPrintInfo()
let operation: NSPrintOperation = NSPrintOperation(view: plasmidMapIBOutlet, printInfo: printInfo)
operation.printPanel.options = NSPrintPanelOptions.ShowsPaperSize
operation.printPanel.options = NSPrintPanelOptions.ShowsOrientation
operation.runOperation()
//plasmidMapIBOutlet.print(sender)
}
But, I still have problem. From the code above I can get only orientation (the last, ShowsOrientation), but not both PaperSize and Orientation. How can I manage both ShowsPaperSize and ShowsOrientation?
Finally I have found the answer which is simple to write but it is not really obvious from apple documentation.
operation.printPanel.options.insert(NSPrintPanelOptions.showsPaperSize)
operation.printPanel.options.insert(NSPrintPanelOptions.showsOrientation)
The problem in the code originally posted is that options is being assigned twice, so the first value assigned, ShowsPaperSize is overwritten by the value ShowsOrientation. That's why you only see the ShowsOrientation option in the dialog.
By using multiple insert operations, you are adding options rather than overwriting each time. You can also do it this way which I think reads better:
operation.printPanel.options.insert([.showsPaperSize, .showsOrientation])
And finally, it also works to "set" the options, and by supplying the existing options as the first array value, you achieve the affect of appending:
operation.printPanel.options = [
operation.printPanel.options,
.showsPaperSize,
.showsOrientation
]
(The first array element operation.printPanel.options means that the old options are supplied in the list of new options.)