Skip to content

Commit b1960a0

Browse files
karwaKarthikRIyer
authored andcommitted
bfix: Fix histograms (#58)
* Fix Issue #57. We were failing to clear some state in the histogram, leading to errors the second/third/etc time it was rendered. * Add reference images for the new test
1 parent f309cf4 commit b1960a0

File tree

6 files changed

+120
-0
lines changed

6 files changed

+120
-0
lines changed

Sources/SwiftPlot/Histogram.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,7 @@ extension Histogram: HasGraphLayout {
269269
histogramSeries.scaledBinFrequency.append(frequency*scaleYInv + origin.y)
270270
}
271271
for index in 0..<histogramStackSeries.count {
272+
histogramStackSeries[index].scaledBinFrequency.removeAll()
272273
for j in 0..<histogramStackSeries[index].binFrequency.count {
273274
let frequency = Float(histogramStackSeries[index].binFrequency[j])
274275
histogramStackSeries[index].scaledBinFrequency.append(frequency*scaleYInv + origin.y)
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import SwiftPlot
2+
import SVGRenderer
3+
#if canImport(AGGRenderer)
4+
import AGGRenderer
5+
#endif
6+
#if canImport(QuartzRenderer)
7+
import QuartzRenderer
8+
#endif
9+
10+
extension HistogramTests {
11+
12+
/// Regression test [#57]. We used to fail to clear the histogram state between renders,
13+
/// leading to slight offsets in the subsequent AGG and CG renders.
14+
func testHistogramStackedStepLineJoins() throws {
15+
let fileName = "_reg_57_histogram_stacked_step_line_joins"
16+
17+
let histogram = Histogram<Float>(isNormalized: false, enableGrid: true)
18+
histogram.addSeries(data: [5], bins: 10, label: "Plot 1", color: .blue, histogramType: .step)
19+
histogram.addStackSeries(data: [6], label: "Plot 2", color: .orange)
20+
histogram.plotTitle = PlotTitle("HISTOGRAM STACKED STEP")
21+
histogram.plotLabel = PlotLabel(xLabel: "X", yLabel: "Frequency")
22+
23+
let svg_renderer = SVGRenderer()
24+
try histogram.drawGraphAndOutput(fileName: self.svgOutputDirectory+fileName,
25+
renderer: svg_renderer)
26+
#if canImport(AGGRenderer)
27+
let agg_renderer = AGGRenderer()
28+
try histogram.drawGraphAndOutput(fileName: self.aggOutputDirectory+fileName,
29+
renderer: agg_renderer)
30+
#endif
31+
#if canImport(QuartzRenderer)
32+
let quartz_renderer = QuartzRenderer()
33+
try histogram.drawGraphAndOutput(fileName: self.coreGraphicsOutputDirectory+fileName,
34+
renderer: quartz_renderer)
35+
#endif
36+
}
37+
}
15.2 KB
Loading
28.2 KB
Loading
Lines changed: 81 additions & 0 deletions
Loading

Tests/SwiftPlotTests/XCTestManifests.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ extension HistogramTests {
3838
("testHistogram", testHistogram),
3939
("testHistogramStacked", testHistogramStacked),
4040
("testHistogramStackedStep", testHistogramStackedStep),
41+
("testHistogramStackedStepLineJoins", testHistogramStackedStepLineJoins),
4142
("testHistogramStep", testHistogramStep),
4243
]
4344
}

0 commit comments

Comments
 (0)