<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Ryan Off on Medium]]></title>
        <description><![CDATA[Stories by Ryan Off on Medium]]></description>
        <link>https://medium.com/@ryanoff?source=rss-303ae0b61b78------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*NS4tN0R876X-uxe8.</url>
            <title>Stories by Ryan Off on Medium</title>
            <link>https://medium.com/@ryanoff?source=rss-303ae0b61b78------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 04 Dec 2026 06:39:18 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@ryanoff/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Create a Sparkline Line Chart in SwiftUI]]></title>
            <link>https://medium.com/@ryanoff/create-a-sparkline-line-chart-in-swiftui-6172f7259316?source=rss-303ae0b61b78------2</link>
            <guid isPermaLink="false">https://medium.com/p/6172f7259316</guid>
            <category><![CDATA[swiftui]]></category>
            <category><![CDATA[swift]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[sparklines]]></category>
            <category><![CDATA[charts]]></category>
            <dc:creator><![CDATA[Ryan Off]]></dc:creator>
            <pubDate>Fri, 05 Jun 2020 23:50:34 GMT</pubDate>
            <atom:updated>2020-06-05T23:50:34.284Z</atom:updated>
            <cc:license>http://creativecommons.org/publicdomain/zero/1.0/</cc:license>
            <content:encoded><![CDATA[<p>For a recent project, I wanted a simple line chart to help show a visual representation of some data. So, I decided to create my own lightweight Sparkline Shape in SwiftUI. Besides a little math, it is relatively easy to do.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*p60UewZPs0hBqlr3" /><figcaption>Photo by <a href="https://unsplash.com/@isaacmsmith?utm_source=medium&amp;utm_medium=referral">Isaac Smith</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>Below, I will outline the steps to create it, but if you just want the code, <a href="https://gist.github.com/ryanoff/ca6b55176e5314dccb94e0924562239a">see this Github Gist</a>. I also embed it at the bottom of this article.</p><p>To create a Sparkline or Line Chart, you basically need a set of data that can be presented on two axis: x and y. In SwiftUI, it is quite easy to draw a path from one point to another, but here, we to be a little creative on where the points of the path reside. To draw this chart, it basically comes down to this one line of code:</p><pre>path.addLine(to: CGPoint(x: rect.minX + (item.x * xScale), y: rect.maxY - (item.y * yScale) ))</pre><p>Here, we are adding a new line to a point on the x and y axis. SwiftUI’s path drawings use a coordinate system that goes down to the right. The top left is 0,0 and the bottom right is 100,100.</p><p>For the X (horizontal) position, we start at the minimum (left) then add the X coordinate multiplying this by a scale factor so chart takes up the entire space provided. For the Y (vertical) position, we start the maximum (bottom) then subtract the Y coordinate and again multiply by a scale factor.</p><p>To get this working we will need some points, a path, a scaling factor based on some maximum values, and a starting point. I’d also suggest putting this path inside a SwiftUI Shape so it can reused across your app.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/369/1*Xu416oK6zNpA0qFlQm6Y-g.png" /></figure><p>To get started, let’s create a Shape struct</p><pre><strong>struct</strong> Sparkline: Shape {</pre><p>Create some default points presented as an array of CGPoints. We could simply initialize an empty points array, but so this articles flows, here are some default points.</p><pre>var points: [CGPoint] = [<br>        CGPoint(x: 1, y: 5),<br>        CGPoint(x: 3, y: 8),<br>        CGPoint(x: 2, y: 6),<br>        CGPoint(x: 4, y: 9)<br>    ]</pre><p>Then, create our path. When making a Shape in SwiftUI, it will try to fill the contents of the frame it is in. The size of this frame is where we get <strong>rect</strong> from.</p><pre>func path(in rect: CGRect) -&gt; Path {<br>        var path = Path()</pre><p>Sort the points array.</p><pre>let sPoints = points.sorted { $0.x &lt; $1.x }</pre><p>Get the largest X and Y values.</p><pre><strong>let m</strong>axYCoord = sPoints.map {$0.y}.max() ?? 1<br>let maxXCoord = sPoints.map {$0.x}.max() ?? 1</pre><p>Create a scale factor by dividing the maximum coordinates by the maximum size of the containing <strong>rect</strong> (frame).</p><pre>let xScale: CGFloat = rect.maxX / CGFloat(maxXCoord)<br>let yScale: CGFloat = rect.maxY / CGFloat(maxYCoord)</pre><p>Now, let’s move to our first point or our sparkline. It starts at the far left, the minimum X value of the rectangle, and for the Y coordinate we will start at the bottom, maximum Y, and subtract back up to the first Y coordinate and multiply by our scale factor.</p><pre>path.move(to: CGPoint(x: rect.minX, y: rect.maxY - (CGFloat(sPoints[0].y) * yScale)) )</pre><p>Then we step through the rest of our points, after removing the first we already used, and draw the connecting lines using the first bit of code I discussed above.</p><pre>for item in sPoints.dropFirst() {<br>  path.addLine(to: CGPoint(x: rect.minX + (item.x * xScale), y: rect.maxY - (item.y * yScale) ))<br>}</pre><p>That’t is. Now, just return our path and close the Shape struct.</p><pre>       return path<br>    }<br>}</pre><p>That created our Sparkline Shape. To us it, just call the Shape with some points, apply a <strong>stroke</strong> to the path and wrap it in a <strong>frame</strong>.</p><pre>Sparkline(points: points)<br>  .stroke(Color.green, style: StrokeStyle(lineWidth: 3, lineCap: .round, lineJoin: .round))<br>  .frame(width: 250, height: 150)</pre><p>That’s it. I hope you found this helpful.</p><p>In the Github Gist below, I also added some sliders and optional bounding box around the line chart.</p><p>I’m sure this code can be improved upon and if you have any suggestions, please don’t hesitate to <a href="https://twitter.com/Squarefish_x">contact me on Twitter</a>.</p><p>Finally, a more robust SwiftUI charting library exists, and I recommend you check it out if you want more options: ChartView on GitHub (<a href="https://github.com/AppPear/ChartView">https://github.com/AppPear/ChartView</a>).</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/73344381b08f945652f2dae2baa757f2/href">https://medium.com/media/73344381b08f945652f2dae2baa757f2/href</a></iframe><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=6172f7259316" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>