I need to access the chart (echarts 5.0.0) on which the tooltip ist shown from inside the tooltip formatter function. How can I access it?
let chartOption = {
//... all the other stuff
tooltip: {
//...
formatter: function (params, ticket, callback) {
//how to access chart when formatter function is called?
}
},
//... more stuff
};
let chart = echarts.init(document.getElementById("myChart"));
chart.setOption(chartOption);
I've solved this by getting the charts id from mouse position:
let elements = document.querySelectorAll( ":hover" );
let chartId = elements.item(elements.length -1).parentElement.parentElement.id;
let chart = charts[chartId];
where "charts" holds all the chart instances on the page with the id of the div as key.
let chartOption = {
//... all the other stuff
tooltip: {
//...
formatter: function (params, ticket, callback) {
chart.xxx
}
},
//... more stuff
};
let chart = echarts.init(document.getElementById("myChart"));
chart.setOption(chartOption);
But I'm not sure what you want to do with the chart instance because in most cases you don't have to.
Related
Is this the place to ask for clarification on previously written lines of code? I apologize if not. I'm trying to understand some Swift code for using LineCharts. Code was written for line charts using iOS16's Swift Charts. My understanding is this code will create a list of dates and numbers to plot. But how do I call it and print out the values? Having no luck printing out data with either playground or within SwiftUI.
I'd like to somehow print out the array of data (days and numbers) that were generated before attempting to plot them.
struct HeartRate: Hashable {
var day: String
var value: Int = .random(in: 60..<150)
}
extension HeartRate {
static var data: [HeartRate] {
let calendar = Calendar(identifier: .gregorian)
let days = calendar.shortWeekdaySymbols
return days.map { day in
HeartRate(day: day)
}
}
}
struct NewLineChartView: View {
var dataPoints: [HeartRate]
var body: some View {
Chart(dataPoints, id: \.self) { rate in
LineMark(x: .value("Day", rate.day),
y: .value("Heart rate", rate.value))
.foregroundStyle(.red)
.symbol(Circle().strokeBorder(lineWidth: 1.5))
}
}
}
Do you want to print to the console? If so, you could capture and print the values(result) before charting the same values:
extension HeartRate {
static var data: [HeartRate] {
let calendar = Calendar(identifier: .gregorian)
let days = calendar.shortWeekdaySymbols
let result = days.map { day in
HeartRate(day: day)
}
print(result)
return result
}
}
I am trying to get a cell in my row/column grid to display as an empty cell or blank content given a certain case and as a CellRenderer type in another case.
Currently I am doing:
cellRendererSelector: function (params) {
const exampleCheckBox = {
component: 'exampleCheckboxCellRenderer',
params: {values: ['true', 'false']}
};
if (params.data.exampleField) {
return exampleCheckBox;
} else {
return null; // HERE: null sets the string value of that field as a default cell
}
},
In my else case above I want to return or show an empty cell or a cell with an empty string. Returning null only returns a default basic cell with the field value as a string.
The cellRendererSelector is a function that returns the name of the component to be used as a renderer, or null.
You could try either of these two approaches:
Creating a Cell Renderer that returns an empty string, e.g. exampleEmptyRenderer:
if (params.data.exampleField) {
return exampleCheckBox;
} else {
return exampleEmptyRenderer;
}
Inside the exampleCheckBox renderer, add logic to do the checking inside the init method to conditionally return what you want, as you have access to the node data:
ExampleCheckBoxCellRenderer.prototype.init = function (params) {
this.eGui = document.createElement('div');
if (params.data.exampleField) {
// here return your renderer
this.eGui.innerHTML = params.value;
} else {
// here return empty string
this.eGui.innerHTML = ''
}
}
i want to update data after editing. for that i need the previous data.
but in my case after editing, the variable that hold the previous value are replaced by the present value.
how can i hold the previous value??
if itemToEdit == nil{
item = TestDataModel()
else{
item = itemToEdit
selectedItemToEdit = itemToEdit
// print(selectedItemToEdit?.title)
print(item.title)
// print(itemToEdit?.title)
}
if let title = titleField.text {
item.title = title
print(item.title)
}
// print(selectedItemToEdit.title!)
print(itemToEdit?.title!)
if let price = pricefield.text {
item.price = (price as NSString).doubleValue
print(item.price)
}
if let details = detailsField.text {
item.details = details
print(item.details)
}
itemToEdit, is that which i want to edit/ update. that's why i stored it in selectedItemToEdit. in item, i have stored the data after editing.
i need both the previous value and the present value that i entered in textfield.
how can i get that one.please any one help....
What you're doing when you try to save previous information is simply creating a new variable that points to the same object. When the object changes, it doesn't matter what variable you use to examine it, you will see the changes in that one object.
Here's a playground sample that should give you an idea of the difference between having two objects vs. having two pointers to the same object.
class MyData {
var title = "Default"
init() {
}
init(source: MyData)
{
title = source.title
}
}
var d1 = MyData()
var d2 = d1
var d3 = MyData(source: d1)
d1.title = "changed"
print(d2.title)
print(d3.title)
yeah i solved the problem:
i just declared a variable of string type as bellow:
var selectedItemToEdit: string!
selectedItemToEdit = itemToEdit.title
i passed that string type variable and the edited variable to the save function.
Here's my doozy.
I've got this lovely little function in a file called functions.swift
//functions.swift
func latestActiveGoal() -> Object {
let realm = try! Realm()
let currentGoal = realm.objects(Goal).filter("Active == 1").sorted("CreatedOn").last
return currentGoal!
}
which returns a Goal object. (A Goal might be wanting to lose weight, or stop being so inept at Swift).
In a different view controller, I want to access this object. Here's what I'm trying:
//viewController.swift
#IBOutlet weak var aimText: UILabel!
let funky = functions()
func getGoals(){
var currentGoal = funky.latestActiveGoal()
print(currentGoal)
aimText.text = currentGoal.Title
}
The print(CurrentGoal) output shows this:
Goal {
id = 276;
Title = Goal Title;
Aim = Aim;
Action = Nothing;
Active = 1;
CreatedOn = 2016-02-12 00:14:45 +0000;
}
aimText.text = currentGoal.Title and aimText = currentGoal.Title both throw the error:
Value of 'Object' has no member 'Title'
By printing the contents of the object, I can see the data, but can't figure out how. Any help greatly appreciated.
As the error message said, currentGoal is a value of Object type which doesn't have member Title.
This is because function latestActiveGoal returns Object instead of Goal. You just need to make it return Goal by change the return type:
func latestActiveGoal() -> Goal {
Just replace your functions with below code.
It will works perfect.
This fuction will check if goal available, then only it will return.
func latestActiveGoal() -> Object? {
let realm = try! Realm()
let currentGoals = realm.objects(Goal).filter("Active == 1").sorted("CreatedOn")
if currentGoals.count > 0 {
return currentGoals.last;
}
return nil;
}
Your getGoals method will be as follow.
func getGoals(){
if let currentGoalObject = funky.latestActiveGoal() {
print(currentGoalObject)
let goal = currentGoalObject as! Goal
print(goal.Title)
aimText.text = goal.Title
}
}
I found this piece of code regarding sections in a table view in order to make an index in a table view:
class User: NSObject {
let namex: String
var section: Int?
init(name: String) {
self.namex = name
}
}
// custom type to represent table sections
class Section {
var users: [User] = []
func addUser(user: User) {
self.users.append(user)
}
}
// raw user data
let names = [
"Clementine",
"Bessie",
"Annis",
"Charlena"
]
// `UIKit` convenience class for sectioning a table
let collation = UILocalizedIndexedCollation.currentCollation() as UILocalizedIndexedCollation
// table sections
var sections: [Section] {
// return if already initialized
if self._sections != nil {
return self._sections!
}
// create users from the name list
var users: [User] = names.map { namess in
var user = User(name: namess)
user.section = self.collation.sectionForObject(user, collationStringSelector: "namex")
return user
}
// create empty sections
var sections = [Section]()
for i in 0..<self.collation.sectionIndexTitles.count {
sections.append(Section())
}
// put each user in a section
for user in users {
sections[user.section!].addUser(user)
}
// sort each section
for section in sections {
section.users = self.collation.sortedArrayFromArray(section.users, collationStringSelector: "namex") as [User]
}
self._sections = sections
return self._sections!
}
var _sections: [Section]?
The part that I didn't understand is this:
// table sections
var sections: [Section] {
// return if already initialized
if self._sections != nil {
return self._sections!
}
// etc...
return self._sections!
}
var _sections: [Section]?
My questions are:
What does that mean var sections: [Section] { }? I guess it's not a function as there is no func keyword in front.
What is this var _sections: [Section]? What's the reason to place an _ in front?
It is very similar to a function despite the absence of keywords – it’s a computed property.
These look like variables, but act like functions. They can be read-only (get but not set), or can have both a get and a set version. Here’s a simpler example:
var y = 3
var x: Int {
get { return 2*y }
}
println(x) // prints 6
y += 1
println(x) // prints 8
var z: Int {
get { return y }
set(newVal) { y = newVal }
}
println(z) // prints 4
z = 10 // sets y to 10
println(x) // prints 20 (2*10)
When it’s only a get, you can omit the keyword, which is how it’s being done in the version in your question. The declaration of x above could have been written without it:
var x: Int {
return 2*y
}
The var _sections in your example is playing a similar role to the y variable in the above code – it’s the underlying data from which the computed property result is derived. The reason for the _ is just to indicate that it is a internal implementation detail. The underscore’s not meaningful to Swift itself, that’s just a naming convention people use.
It's a read-only computed property. They act like properties, but are computed each time. You can read more about it in the docs. Check out the section on "Computed Properties" as well as the "Read-Only Computed Properties" section to understand the shorthand.