Closed ahmedsafadii closed 1 year ago
This is what i got
This is the example that I'm trying to do
Try this
class ViewController: UIViewController {
// The graph view in interface builder
@IBOutlet weak var lineStandardView: DSFSparklineLineGraphView!
// The line source for the graph. We need to hold on to it as the dataSource on the graph is weak
private var lineDataSource: DSFSparkline.DataSource?
override func viewDidLoad() {
super.viewDidLoad()
// Set the graph style
lineStandardView.lineWidth = 3
lineStandardView.lineShading = true
lineStandardView.graphColor = .systemYellow
lineStandardView.backgroundColor = .clear
lineStandardView.showZeroLine = true
lineStandardView.centeredAtZeroLine = false
// Create a static data source with a window size and range
let dataSource = DSFSparkline.DataSource(
windowSize: 20,
range: 0 ... 1,
zeroLineValue: 0.3
)
// Make sure we hold onto the data source, as the graph only holds the datasource weakly
self.lineDataSource = dataSource
// Set the values in the datasource (20 values to fill the entire window)
dataSource.set(values: [
0.72, 0.84, 0.15, 0.16, 0.30, 0.58, 0.87, 0.44, 0.02, 0.27,
0.48, 0.16, 0.15, 0.14, 0.81, 0.53, 0.67, 0.52, 0.07, 0.50
])
// And attach the data source to the graph view
lineStandardView.dataSource = dataSource
}
}
You can assign a static set of values to a datasource using the set methods on the datasource object.
Even easier, just create the datasource with an array of values to display
// Create a static data source with an initial array of values
let dataSource = DSFSparkline.DataSource(
values: [
0.72, 0.84, 0.15, 0.16, 0.30, 0.58, 0.87, 0.44, 0.02, 0.27,
0.48, 0.16, 0.15, 0.14, 0.81, 0.53, 0.67, 0.52, 0.07, 0.50
],
range: 0 ... 1
)
The graph only animates if the datasource content changes, so if you just need a static graph this will be fine.
@dagronf Thank you for the example but it didn't fill the width that I'm using for example
I have this
100px x 36px
the result it
Note that I only use 6 values I need, so if they're at least equal width, to fill the width that I set in the constraint
0.72, 0.84, 0.15, 0.16, 0.30, 0.58
It looks like youre seting the window size in the data source to 30, but you’re only providing 6 values. Try setting the window size to 6?
Or, create the datasource with the static data as I mentioned in an earlier comment
let dataSource = DSFSparkline.DataSource(
values: [
0.72, 0.84, 0.15, 0.16, 0.30, 0.58
],
range: 0 ... 1
)
Using the code I wrote earlier with the data you've just provided I see the following for a 100x38 line graph
import UIKit
import DSFSparkline
class ViewController: UIViewController {
// The graph view in interface builder
@IBOutlet weak var lineStandardView: DSFSparklineLineGraphView!
// The line source for the graph. We need to hold on to it as the dataSource on the graph is weak
private var lineDataSource: DSFSparkline.DataSource?
override func viewDidLoad() {
super.viewDidLoad()
// Set the graph style
lineStandardView.lineWidth = 1
lineStandardView.lineShading = true
lineStandardView.graphColor = .systemRed
lineStandardView.backgroundColor = .clear
lineStandardView.showZeroLine = false
lineStandardView.centeredAtZeroLine = false
// Create a static data source with an initial array of values
let dataSource = DSFSparkline.DataSource(
values: [
0.72, 0.84, 0.15, 0.16, 0.30, 0.58
],
range: 0 ... 1
)
dataSource.zeroLineValue = 0.3
// Make sure we hold onto the data source, as the graph only holds the datasource weakly
self.lineDataSource = dataSource
// And attach the data source to the graph view
lineStandardView.dataSource = dataSource
}
}
My interface builder layout looks like this...
Big hug and thanks for you <3 <3.
I would say that this library it's not the only one that supports swift UI and swift, it's actually more cool and stable than the charts which are huge.
Thank you it was my issue
I had to remove 30 from here.
Thank you again
My pleasure mate. I'm glad we were able to sort it out!
I really need the code for this chart exactly static without animation, I try everything but not getting close ;(
@dagronf
I want it to fill the view