dagronf / DSFSparkline

A lightweight sparkline component for macOS, iOS and tvOS
MIT License
141 stars 11 forks source link

UIKIT for this chart #11

Closed ahmedsafadii closed 1 year ago

ahmedsafadii commented 1 year ago

image

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

ahmedsafadii commented 1 year ago
Screenshot 2022-11-09 at 12 33 02 AM

This is what i got

ahmedsafadii commented 1 year ago
Screenshot 2022-11-09 at 12 33 19 AM

This is the example that I'm trying to do

dagronf commented 1 year ago

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
   }
}
dagronf commented 1 year ago
image
dagronf commented 1 year ago

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.

ahmedsafadii commented 1 year ago

@dagronf Thank you for the example but it didn't fill the width that I'm using for example

I have this

Screenshot 2022-11-09 at 9 07 00 AM

100px x 36px

the result it

Screenshot 2022-11-09 at 9 07 19 AM

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

dagronf commented 1 year ago

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
   }
}

Simulator Screen Shot - iPhone Xʀ - 2022-11-09 at 18 11 23

dagronf commented 1 year ago

My interface builder layout looks like this...

image

ahmedsafadii commented 1 year ago

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

Screenshot 2022-11-09 at 10 17 49 AM

I had to remove 30 from here.

Thank you again

dagronf commented 1 year ago

My pleasure mate. I'm glad we were able to sort it out!