Skip to content

Commit ee674d9

Browse files
committed
feat: Implement stacked histogram for step type and update examples
1 parent 78fe1fa commit ee674d9

18 files changed

+1384
-883
lines changed

Package.swift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,18 @@ let package = Package(
130130
name: "HistogramExample",
131131
dependencies: ["AGGRenderer", "SVGRenderer", "SwiftPlot"],
132132
path: "examples/Histogram"),
133+
.target(
134+
name: "HistogramStepExample",
135+
dependencies: ["AGGRenderer", "SVGRenderer", "SwiftPlot"],
136+
path: "examples/HistogramStep"),
137+
.target(
138+
name: "HistogramStackedExample",
139+
dependencies: ["AGGRenderer", "SVGRenderer", "SwiftPlot"],
140+
path: "examples/HistogramStacked"),
141+
.target(
142+
name: "HistogramStackedStepExample",
143+
dependencies: ["AGGRenderer", "SVGRenderer", "SwiftPlot"],
144+
path: "examples/HistogramStackedStep"),
133145
//.testTarget(
134146
// name: "swiftplotTests",
135147
// dependencies: ["swiftplot"]),

examples/Histogram/main.swift

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,21 +19,13 @@ for _ in 1...numberOfSamples {
1919
x.append(z1*deviation + mean)
2020
}
2121

22-
for _ in 1...numberOfSamples {
23-
let x1 = Float.random(in: 0.0...1.0)
24-
let x2 = Float.random(in: 0.0...1.0)
25-
let z1 = sqrt(-2 * log(x1))*cos(2*Float.pi*x2)
26-
y.append(z1*deviation + 150)
27-
}
28-
2922
var agg_renderer = AGGRenderer()
3023
var svg_renderer = SVGRenderer()
3124

3225
var histogram: Histogram = Histogram(isNormalized: false)
33-
histogram.addSeries(data: x, bins: 50, label: "Plot 1", color: .blue, histogramType: .step)
34-
histogram.addStackSeries(data: y, label: "Plot 2", color: .orange)
26+
histogram.addSeries(data: x, bins: 50, label: "Plot 1", color: .blue)
3527
histogram.plotTitle = PlotTitle("HISTOGRAM")
36-
histogram.plotLabel = PlotLabel(xLabel: "X-AXIS", yLabel: "Y-AXIS")
28+
histogram.plotLabel = PlotLabel(xLabel: "X", yLabel: "Frequency")
3729

3830
histogram.drawGraphAndOutput(fileName: filePath+"agg/"+fileName, renderer: agg_renderer)
3931
histogram.drawGraphAndOutput(fileName: filePath+"svg/"+fileName, renderer: svg_renderer)

examples/HistogramStacked/main.swift

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import SwiftPlot
2+
import AGGRenderer
3+
import SVGRenderer
4+
import Foundation
5+
6+
var filePath = "examples/Reference/"
7+
let fileName = "_22_histogram_stacked"
8+
9+
var x = [Float]()
10+
var y = [Float]()
11+
var mean1: Float = 100
12+
var mean2: Float = 150
13+
var deviation: Float = 15
14+
let numberOfSamples = 10000
15+
16+
for _ in 1...numberOfSamples {
17+
let x1 = Float.random(in: 0.0...1.0)
18+
let x2 = Float.random(in: 0.0...1.0)
19+
let z1 = sqrt(-2 * log(x1))*cos(2*Float.pi*x2)
20+
x.append(z1*deviation + mean1)
21+
}
22+
23+
for _ in 1...numberOfSamples {
24+
let x1 = Float.random(in: 0.0...1.0)
25+
let x2 = Float.random(in: 0.0...1.0)
26+
let z1 = sqrt(-2 * log(x1))*cos(2*Float.pi*x2)
27+
y.append(z1*deviation + mean2)
28+
}
29+
30+
var agg_renderer = AGGRenderer()
31+
var svg_renderer = SVGRenderer()
32+
33+
var histogram: Histogram = Histogram(isNormalized: false)
34+
histogram.addSeries(data: x, bins: 50, label: "Plot 1", color: .blue)
35+
histogram.addStackSeries(data: y, label: "Plot 2", color: .orange)
36+
histogram.plotTitle = PlotTitle("HISTOGRAM STACKED")
37+
histogram.plotLabel = PlotLabel(xLabel: "X", yLabel: "Frequency")
38+
39+
histogram.drawGraphAndOutput(fileName: filePath+"agg/"+fileName, renderer: agg_renderer)
40+
histogram.drawGraphAndOutput(fileName: filePath+"svg/"+fileName, renderer: svg_renderer)
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import SwiftPlot
2+
import AGGRenderer
3+
import SVGRenderer
4+
import Foundation
5+
6+
var filePath = "examples/Reference/"
7+
let fileName = "_23_histogram_stacked_step"
8+
9+
var x = [Float]()
10+
var y = [Float]()
11+
var mean1: Float = 100
12+
var mean2: Float = 150
13+
var deviation: Float = 15
14+
let numberOfSamples = 10000
15+
16+
for _ in 1...numberOfSamples {
17+
let x1 = Float.random(in: 0.0...1.0)
18+
let x2 = Float.random(in: 0.0...1.0)
19+
let z1 = sqrt(-2 * log(x1))*cos(2*Float.pi*x2)
20+
x.append(z1*deviation + mean1)
21+
}
22+
23+
for _ in 1...numberOfSamples {
24+
let x1 = Float.random(in: 0.0...1.0)
25+
let x2 = Float.random(in: 0.0...1.0)
26+
let z1 = sqrt(-2 * log(x1))*cos(2*Float.pi*x2)
27+
y.append(z1*deviation + mean2)
28+
}
29+
30+
var agg_renderer = AGGRenderer()
31+
var svg_renderer = SVGRenderer()
32+
33+
var histogram: Histogram = Histogram(isNormalized: false)
34+
histogram.addSeries(data: x, bins: 50, label: "Plot 1", color: .blue, histogramType: .step)
35+
histogram.addStackSeries(data: y, label: "Plot 2", color: .orange)
36+
histogram.plotTitle = PlotTitle("HISTOGRAM STACKED STEP")
37+
histogram.plotLabel = PlotLabel(xLabel: "X", yLabel: "Frequency")
38+
39+
histogram.drawGraphAndOutput(fileName: filePath+"agg/"+fileName, renderer: agg_renderer)
40+
histogram.drawGraphAndOutput(fileName: filePath+"svg/"+fileName, renderer: svg_renderer)

examples/HistogramStep/main.swift

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import SwiftPlot
2+
import AGGRenderer
3+
import SVGRenderer
4+
import Foundation
5+
6+
var filePath = "examples/Reference/"
7+
let fileName = "_21_histogram_step"
8+
9+
var x = [Float]()
10+
var y = [Float]()
11+
var mean: Float = 100
12+
var deviation: Float = 15
13+
let numberOfSamples = 10000
14+
15+
for _ in 1...numberOfSamples {
16+
let x1 = Float.random(in: 0.0...1.0)
17+
let x2 = Float.random(in: 0.0...1.0)
18+
let z1 = sqrt(-2 * log(x1))*cos(2*Float.pi*x2)
19+
x.append(z1*deviation + mean)
20+
}
21+
22+
var agg_renderer = AGGRenderer()
23+
var svg_renderer = SVGRenderer()
24+
25+
var histogram: Histogram = Histogram(isNormalized: false)
26+
histogram.addSeries(data: x, bins: 50, label: "Plot 1", color: .blue, histogramType: .step)
27+
histogram.plotTitle = PlotTitle("HISTOGRAM STEP")
28+
histogram.plotLabel = PlotLabel(xLabel: "X", yLabel: "Frequency")
29+
30+
histogram.drawGraphAndOutput(fileName: filePath+"agg/"+fileName, renderer: agg_renderer)
31+
histogram.drawGraphAndOutput(fileName: filePath+"svg/"+fileName, renderer: svg_renderer)
99 Bytes
Loading
-5.76 KB
Loading
24.1 KB
Loading
21.5 KB
Loading
Loading

0 commit comments

Comments
 (0)