Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 15 additions & 19 deletions Sources/AGGRenderer/AGGRenderer/AGGRenderer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import SwiftPlot

public class AGGRenderer: Renderer{

public var offset = zeroPoint
public var offset: Point = .zero
public var imageSize: Size {
willSet {
delete_plot(agg_object);
Expand Down Expand Up @@ -156,19 +156,17 @@ public class AGGRenderer: Renderer{
agg_object);
}

public func drawSolidPolygon(points: [Point],
public func drawSolidPolygon(_ polygon: SwiftPlot.Polygon,
fillColor: Color) {
precondition(points.count > 2, "drawSolidPolygon: Cannot draw a polygon with \(points.count) points.")

var x = [Float]()
var y = [Float]()
for index in 0..<points.count {
x.append(points[index].x + xOffset)
y.append(points[index].y + yOffset)
for point in polygon.points {
x.append(point.x + xOffset)
y.append(point.y + yOffset)
}
draw_solid_polygon(x,
y,
Int32(points.count),
Int32(x.count),
fillColor.r,
fillColor.g,
fillColor.b,
Expand Down Expand Up @@ -200,23 +198,21 @@ public class AGGRenderer: Renderer{
agg_object)
}

public func drawPlotLines(points p: [Point],
strokeWidth thickness: Float,
strokeColor: Color,
isDashed: Bool) {
precondition(p.count > 1, "drawPlotLines: Cannot draw lines with \(p.count) points.")

public func drawPolyline(_ polyline: Polyline,
strokeWidth thickness: Float,
strokeColor: Color,
isDashed: Bool) {
var x = [Float]()
var y = [Float]()

for index in 0..<p.count {
x.append(p[index].x + xOffset)
y.append(p[index].y + yOffset)
for point in polyline.points {
x.append(point.x + xOffset)
y.append(point.y + yOffset)
}

draw_plot_lines(x,
y,
Int32(p.count),
Int32(x.count),
thickness,
strokeColor.r,
strokeColor.g,
Expand Down
34 changes: 17 additions & 17 deletions Sources/QuartzRenderer/QuartzRenderer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public class QuartzRenderer: Renderer {
/// Whether or not this context was given to us. If `true`, we should never re-make `context`
let isExternalContext: Bool
var fontPath = ""
public var offset = zeroPoint
public var offset: Point = .zero

public var fontSmoothing: Bool = false {
didSet { context.setShouldSmoothFonts(fontSmoothing) }
Expand Down Expand Up @@ -381,15 +381,11 @@ public class QuartzRenderer: Renderer {
context.fillPath()
}

public func drawSolidPolygon(points: [Point],
public func drawSolidPolygon(_ polygon: SwiftPlot.Polygon,
fillColor: Color) {
precondition(points.count > 2, "drawSolidPolygon: Cannot draw a polygon with \(points.count) points.")

let polygonPath = CGMutablePath()
polygonPath.move(to: CGPoint(x: Double(points[0].x + xOffset), y: Double(points[0].y + yOffset)))
for index in 1..<points.count {
polygonPath.addLine(to: CGPoint(x: Double(points[index].x + xOffset), y: Double(points[index].y + yOffset)))
}
polygonPath.addLines(between: polygon.points.map { CGPoint(x: CGFloat($0.x), y: CGFloat($0.y)) },
transform: CGAffineTransform(translationX: CGFloat(xOffset), y: CGFloat(yOffset)))
polygonPath.closeSubpath()
context.setFillColor(fillColor.cgColor)
context.addPath(polygonPath)
Expand All @@ -415,19 +411,23 @@ public class QuartzRenderer: Renderer {
context.setLineDash(phase: 1, lengths: [])
}

public func drawPlotLines(points p: [Point],
public func drawPolyline(_ polyline: Polyline,
strokeWidth thickness: Float,
strokeColor: Color,
isDashed: Bool) {
precondition(p.count > 1, "drawPlotLines: Cannot draw lines with \(p.count) points.")

for i in 0..<p.count-1 {
drawLine(startPoint: p[i],
endPoint: p[i+1],
strokeWidth: thickness,
strokeColor: strokeColor,
isDashed: isDashed)

let linePath = CGMutablePath()
linePath.addLines(between: polyline.points.map { CGPoint(x: CGFloat($0.x), y: CGFloat($0.y)) },
transform: CGAffineTransform(translationX: CGFloat(xOffset), y: CGFloat(yOffset)))
context.setStrokeColor(strokeColor.cgColor)
context.setLineWidth(CGFloat(thickness))
context.addPath(linePath)
if(isDashed) {
let dashes: [ CGFloat ] = [ CGFloat(thickness + 1), CGFloat(thickness + 1) ]
context.setLineDash(phase: 1, lengths: dashes)
}
context.strokePath()
context.setLineDash(phase: 1, lengths: [])
}

public func drawText(text s: String,
Expand Down
26 changes: 11 additions & 15 deletions Sources/SVGRenderer/SVGRenderer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ public class SVGRenderer: Renderer{
static let horizontalHatch: String = #"<defs><pattern id="horizontalHatch" width="10" height="10" patternUnits="userSpaceOnUse"><line x1="0" y1="5" x2="10" y2="5" style="stroke:black; stroke-width:1" /></pattern></defs>"#
static let gridHatch: String = #"<defs><pattern id="gridHatch" width="10" height="10" patternUnits="userSpaceOnUse"><line x1="0" y1="5" x2="10" y2="5" style="stroke:black; stroke-width:1" /><line x1="5" y1="0" x2="5" y2="10" style="stroke:black; stroke-width:1" /></pattern></defs>"#
static let crossHatch: String = #"<defs><pattern id="crossHatch" width="10" height="10" patternUnits="userSpaceOnUse"><line x1="0" y1="0" x2="10" y2="10" style="stroke:black; stroke-width:1" /><line x1="0" y1="10" x2="10" y2="0" style="stroke:black; stroke-width:1" /></pattern></defs>"#

public var offset = zeroPoint
public var offset: Point = .zero
public var imageSize: Size

var hatchingIncluded = Array(repeating: false,
Expand Down Expand Up @@ -165,18 +164,17 @@ public class SVGRenderer: Renderer{
let triangle = #"<polygon points="\#(p1.x),\#(p1.y) \#(p2.x),\#(p2.y) \#(p3.x),\#(p3.y)" style="fill:\#(fillColor.svgColorString);opacity:\#(fillColor.a)" />"#
lines.append(triangle)
}

public func drawSolidPolygon(points: [Point],
public func drawSolidPolygon(_ polygon: SwiftPlot.Polygon,
fillColor: Color) {
precondition(points.count > 2, "drawSolidPolygon: Cannot draw a polygon with \(points.count) points.")

let pts = points.map { convertToSVGCoordinates($0) }
var pointsString = ""
for index in 0..<pts.count {
pointsString = pointsString + "\(pts[index].x),\(pts[index].y) "
for point in polygon.points {
let convertedPoint = convertToSVGCoordinates(point)
pointsString.append("\(convertedPoint.x),\(convertedPoint.y) ")
}
let polygon = #"<polygon points="\#(pointsString)" style="fill:\#(fillColor.svgColorString);opacity:\#(fillColor.a)" />"#
lines.append(polygon)

let polygonString = #"<polygon points="\#(pointsString)" style="fill:\#(fillColor.svgColorString);opacity:\#(fillColor.a)" />"#
lines.append(polygonString)
}

public func drawLine(startPoint p1: Point,
Expand All @@ -196,13 +194,11 @@ public class SVGRenderer: Renderer{
lines.append(line)
}

public func drawPlotLines(points p: [Point],
public func drawPolyline(_ polyline: Polyline,
strokeWidth thickness: Float,
strokeColor: Color,
isDashed: Bool) {
precondition(p.count > 1, "drawPlotLines: Cannot draw lines with \(p.count) points.")

let pointsString = p.lazy.map { point in
let pointsString = polyline.points.lazy.map { point in
let convertedPoint = self.convertToSVGCoordinates(point)
return "\(convertedPoint.x),\(convertedPoint.y)"
}.joined(separator: " ")
Expand Down
8 changes: 4 additions & 4 deletions Sources/SwiftPlot/BarChart.swift
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,8 @@ extension BarGraph: HasGraphLayout {
var stackSeries_scaledValues = [[Pair<Float,Float>]]()
var scaleY: Float = 1
var scaleX: Float = 1
var barWidth : Int = 0
var origin = zeroPoint
var barWidth: Int = 0
var origin: Point = .zero
}

// functions implementing plotting logic
Expand Down Expand Up @@ -136,7 +136,7 @@ extension BarGraph: HasGraphLayout {
}

if (minimumY >= U(0)) {
results.origin = zeroPoint
results.origin = .zero
minimumY = U(0)
}
else{
Expand Down Expand Up @@ -226,7 +226,7 @@ extension BarGraph: HasGraphLayout {
}

if minimumX >= U(0) {
results.origin = zeroPoint
results.origin = .zero
minimumX = U(0)
}
else{
Expand Down
4 changes: 2 additions & 2 deletions Sources/SwiftPlot/GraphLayout.swift
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ public struct GraphLayout {
/// Calculates the region of the plot which is used for displaying the plot's data (inside all of the chrome).
private func calcBorder(totalSize: Size, labelSizes: Results.LabelSizes, renderer: Renderer) -> Rect {
var borderRect = Rect(
origin: zeroPoint,
origin: .zero,
size: totalSize
)
if let xLabel = labelSizes.xLabelSize {
Expand Down Expand Up @@ -211,7 +211,7 @@ public struct GraphLayout {
// Drawing.

func drawBackground(results: Results, renderer: Renderer) {
renderer.drawSolidRect(Rect(origin: zeroPoint, size: results.totalSize),
renderer.drawSolidRect(Rect(origin: .zero, size: results.totalSize),
fillColor: backgroundColor, hatchPattern: .none)
if let plotBackgroundColor = plotBackgroundColor {
renderer.drawSolidRect(results.plotBorderRect, fillColor: plotBackgroundColor, hatchPattern: .none)
Expand Down
10 changes: 6 additions & 4 deletions Sources/SwiftPlot/Histogram.swift
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ extension Histogram: HasGraphLayout {

var barWidth: Float = 0
let xMargin: Float = 5
var origin = zeroPoint
var origin: Point = .zero
}

// functions implementing plotting logic
Expand Down Expand Up @@ -274,9 +274,11 @@ extension Histogram: HasGraphLayout {
var frontLeftBinHeight = frontHeightsSlice.removeFirst()
for ((backRightBinHeight, frontRightBinHeight), x) in zip(zip(backHeightsSlice, frontHeightsSlice), xValues) {
func endLine() {
renderer.drawPlotLines(points: line, strokeWidth: strokeWidth,
strokeColor: allSeriesInfo[seriesIdx].color,
isDashed: false)
// This algorithm should never produce lines with less than 2 points
guard let polyline = Polyline(line) else { fatalError("Histogram.drawData: Expecting 2 or more points, got \(line.count) instead.") }
renderer.drawPolyline(polyline, strokeWidth: strokeWidth,
strokeColor: allSeriesInfo[seriesIdx].color,
isDashed: false)
line.removeAll(keepingCapacity: true)
}

Expand Down
24 changes: 15 additions & 9 deletions Sources/SwiftPlot/LineChart.swift
Original file line number Diff line number Diff line change
Expand Up @@ -154,19 +154,25 @@ extension LineGraph: HasGraphLayout {
if let axisInfo = data.primaryAxisInfo {
for dataset in primaryAxis.series {
let points = dataset.values.map { axisInfo.convertCoordinate(fromData: $0) }
renderer.drawPlotLines(points: points,
strokeWidth: plotLineThickness,
strokeColor: dataset.color,
isDashed: false)
guard let polyline = Polyline(points) else {
fatalError("LineChart.drawData: Expecting 2 or more points, got \(points.count) instead")
}
renderer.drawPolyline(polyline,
strokeWidth: plotLineThickness,
strokeColor: dataset.color,
isDashed: false)
}
}
if let secondaryAxis = secondaryAxis, let axisInfo = data.secondaryAxisInfo {
for dataset in secondaryAxis.series {
let points = dataset.values.map { axisInfo.convertCoordinate(fromData: $0) }
renderer.drawPlotLines(points: points,
strokeWidth: plotLineThickness,
strokeColor: dataset.color,
isDashed: true)
guard let polyline = Polyline(points) else {
fatalError("LineChart.drawData: Expecting 2 or more points, got \(points.count) instead")
}
renderer.drawPolyline(polyline,
strokeWidth: plotLineThickness,
strokeColor: dataset.color,
isDashed: true)
}
}
}
Expand All @@ -184,7 +190,7 @@ extension LineGraph {
var scaleY: Float = 1
// The "origin" is just a known value at a known location,
// used for calculating where other points are located.
var origin: Point = zeroPoint
var origin: Point = .zero
var originValue = Pair(T(0), U(0))

init(series: [Series<T, U>], size: Size) {
Expand Down
7 changes: 5 additions & 2 deletions Sources/SwiftPlot/Pair.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ public struct Pair<T,U> {
}

public typealias Point = Pair<Float,Float>
public let zeroPoint = Point(0.0, 0.0)

extension Point {
public static let zero = Point(0.0, 0.0)
}

public func + (lhs: Point, rhs: Point) -> Point {
return Point(lhs.x + rhs.x, lhs.y + rhs.y)
Expand Down Expand Up @@ -42,7 +45,7 @@ public struct Rect {
}
extension Rect {

public static let empty = Rect(origin: zeroPoint, size: .zero)
public static let empty = Rect(origin: .zero, size: .zero)

public var normalized: Rect {
let normalizedOrigin = Point(origin.x + (size.width < 0 ? size.width : 0),
Expand Down
26 changes: 15 additions & 11 deletions Sources/SwiftPlot/PlotStyleHelpers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -184,13 +184,14 @@ struct Arrow : Annotation {
let wedgeRotateAngle = -atan2(end.x - start.x, end.y - start.y)
p1 = rotatePoint(point: p1, center: start, angleRadians: wedgeRotateAngle + 0.5 * Float.pi)
p2 = rotatePoint(point: p2, center: start, angleRadians: wedgeRotateAngle + 0.5 * Float.pi)
renderer.drawSolidPolygon(points: [p1, p2, end],
let head = Polygon(p1, p2, end)!
renderer.drawSolidPolygon(head,
fillColor: color)
default:
renderer.drawPlotLines(points: [start, end],
strokeWidth: strokeWidth,
strokeColor: color,
isDashed: isDashed)
renderer.drawPolyline(Polyline(start, end)!,
strokeWidth: strokeWidth,
strokeColor: color,
isDashed: isDashed)
}
}
public func drawHead(renderer: Renderer, a: Point, b: Point) {
Expand All @@ -204,17 +205,20 @@ struct Arrow : Annotation {
// Draws arrow head points.
switch headStyle {
case .skeletal:
renderer.drawPlotLines(points: [p1, b, p2],
strokeWidth: strokeWidth,
strokeColor: color,
isDashed: isDashed)
let head = Polyline(p1, b, p2)!
renderer.drawPolyline(head,
strokeWidth: strokeWidth,
strokeColor: color,
isDashed: isDashed)
case .filled:
renderer.drawSolidPolygon(points: [p1, b, p2],
let head = Polygon(p1, b, p2)!
renderer.drawSolidPolygon(head,
fillColor: color)
case .dart:
var p3 = end + Point(-headLength/2, 0.0)
p3 = rotatePoint(point: p3, center: b, angleRadians: rotateAngle + 0.5 * Float.pi)
renderer.drawSolidPolygon(points: [p1, p3, p2, b],
let head = Polygon(p1, p3, p2, b)!
renderer.drawSolidPolygon(head,
fillColor: color)
default:
break
Expand Down
Loading