|
| 1 | +# Tests for hard difficulty issue fixes |
| 2 | +# Following TDD: these tests are written FIRST before implementing fixes |
| 3 | + |
| 4 | +# Issue #2419: Two NAs per category cause incorrect line connection |
| 5 | +# When exactly 2 NA values exist per category with a hovertemplate, |
| 6 | +# lines incorrectly connect across the NAs instead of creating gaps. |
| 7 | + |
| 8 | +test_that("Issue #2419: exactly 2 NAs per category create gaps, not connected lines", { |
| 9 | + df <- data.frame( |
| 10 | + Category = rep(c("A", "B"), each = 6), |
| 11 | + Date = c(2020, 2021, 2022, 2023, 2024, 2025, 2020, 2021, 2022, 2023, 2024, 2025), |
| 12 | + Value = c(10, 15, NA, NA, 20, 25, 12, 14, NA, 22, NA, 27) |
| 13 | + ) |
| 14 | + df$Date <- factor(df$Date, levels = unique(df$Date), ordered = TRUE) |
| 15 | + |
| 16 | + p <- plot_ly( |
| 17 | + df, |
| 18 | + x = ~Date, |
| 19 | + y = ~Value, |
| 20 | + color = ~Category, |
| 21 | + type = 'scatter', |
| 22 | + mode = 'lines+markers', |
| 23 | + text = ~Category, |
| 24 | + hovertemplate = paste0("Date: %{x}<br>Category: %{text}") |
| 25 | + ) |
| 26 | + |
| 27 | + built <- plotly_build(p) |
| 28 | + |
| 29 | + # There should be 2 traces (one per category) |
| 30 | + expect_equal(length(built$x$data), 2) |
| 31 | + |
| 32 | + # For category A: values are 10, 15, NA, NA, 20, 25 |
| 33 | + # After NA handling, the y values should have NAs inserted to create gaps |
| 34 | + traceA <- built$x$data[[1]] |
| 35 | + |
| 36 | + # The key test: NAs should be present in the y data to create gaps |
| 37 | + |
| 38 | + # If exactly 2 NAs are being connected incorrectly, this would fail |
| 39 | + # We should see NA values in the output that separate the groups |
| 40 | + expect_true(any(is.na(traceA$y))) |
| 41 | + |
| 42 | + # For category B: values are 12, 14, NA, 22, NA, 27 |
| 43 | + traceB <- built$x$data[[2]] |
| 44 | + expect_true(any(is.na(traceB$y))) |
| 45 | +}) |
| 46 | + |
| 47 | +test_that("Issue #2419: single NA per category creates gaps correctly", { |
| 48 | + df <- data.frame( |
| 49 | + Category = rep(c("A", "B"), each = 6), |
| 50 | + Date = c(2020, 2021, 2022, 2023, 2024, 2025, 2020, 2021, 2022, 2023, 2024, 2025), |
| 51 | + Value = c(10, 15, NA, 18, 20, 25, 12, 14, NA, 22, 24, 27) |
| 52 | + ) |
| 53 | + df$Date <- factor(df$Date, levels = unique(df$Date), ordered = TRUE) |
| 54 | + |
| 55 | + p <- plot_ly( |
| 56 | + df, |
| 57 | + x = ~Date, |
| 58 | + y = ~Value, |
| 59 | + color = ~Category, |
| 60 | + type = 'scatter', |
| 61 | + mode = 'lines+markers', |
| 62 | + text = ~Category, |
| 63 | + hovertemplate = paste0("Date: %{x}<br>Category: %{text}") |
| 64 | + ) |
| 65 | + |
| 66 | + built <- plotly_build(p) |
| 67 | + |
| 68 | + # There should be 2 traces (one per category) |
| 69 | + expect_equal(length(built$x$data), 2) |
| 70 | + |
| 71 | + # Both traces should have NA values to create gaps |
| 72 | + traceA <- built$x$data[[1]] |
| 73 | + traceB <- built$x$data[[2]] |
| 74 | + expect_true(any(is.na(traceA$y))) |
| 75 | + expect_true(any(is.na(traceB$y))) |
| 76 | +}) |
| 77 | + |
| 78 | + |
| 79 | +# Issue #2468: Pie chart color mapping doesn't work properly when aggregating data |
| 80 | +# When plotly.js aggregates pie chart data (duplicate labels), the marker.colors |
| 81 | +# don't apply correctly to the first slice. |
| 82 | + |
| 83 | +test_that("Issue #2468: pie chart colors apply correctly with aggregated data", { |
| 84 | + # When there are 3 unique labels but more rows (so plotly aggregates), |
| 85 | + # marker.colors should apply to all slices correctly |
| 86 | + p <- plot_ly( |
| 87 | + mtcars[, c("cyl", "drat")], |
| 88 | + labels = ~cyl, |
| 89 | + values = ~drat, |
| 90 | + type = 'pie', |
| 91 | + marker = list(colors = c("cyan", "magenta", "black")) |
| 92 | + ) |
| 93 | + |
| 94 | + built <- plotly_build(p) |
| 95 | + |
| 96 | + # The colors should be present in the marker (as-is, values preserved) |
| 97 | + colors <- as.character(built$x$data[[1]]$marker$colors) |
| 98 | + expect_equal(length(colors), 3) |
| 99 | + expect_equal(colors, c("cyan", "magenta", "black")) |
| 100 | +}) |
| 101 | + |
| 102 | +test_that("Issue #2468: pie chart colors work without aggregation", { |
| 103 | + # Without aggregation (unique labels), colors should still work |
| 104 | + p <- plot_ly( |
| 105 | + mtcars[c(1, 3, 5), c("cyl", "drat")], |
| 106 | + labels = ~cyl, |
| 107 | + values = ~drat, |
| 108 | + type = 'pie', |
| 109 | + marker = list(colors = c("cyan", "magenta", "black")) |
| 110 | + ) |
| 111 | + |
| 112 | + built <- plotly_build(p) |
| 113 | + |
| 114 | + # The colors should be present in the marker (as-is, values preserved) |
| 115 | + colors <- as.character(built$x$data[[1]]$marker$colors) |
| 116 | + expect_equal(length(colors), 3) |
| 117 | + expect_equal(colors, c("cyan", "magenta", "black")) |
| 118 | +}) |
| 119 | + |
| 120 | + |
| 121 | +# Issue #2437: subplot() with bar and pie chart creates NA layout attribute |
| 122 | +# When combining bar and pie charts in a subplot, an NA attribute is created |
| 123 | +# in the layout, causing a warning. |
| 124 | + |
| 125 | +test_that("Issue #2437: subplot with bar and pie does not create NA layout attribute", { |
| 126 | + bar_info <- data.frame( |
| 127 | + Group = rep(c("first", "second", "third"), 2), |
| 128 | + values_monthly = c(100, 200, 300, 400, 500, 600), |
| 129 | + month = factor(rep(c("April", "May"), each = 3)) |
| 130 | + ) |
| 131 | + pie_info <- aggregate(values_monthly ~ Group, data = bar_info, sum) |
| 132 | + names(pie_info)[2] <- "values_total" |
| 133 | + |
| 134 | + colors <- c("red", "blue", "yellow") |
| 135 | + |
| 136 | + bar_chart <- plot_ly( |
| 137 | + bar_info, |
| 138 | + type = "bar", |
| 139 | + x = ~month, |
| 140 | + y = ~values_monthly, |
| 141 | + color = ~Group, |
| 142 | + colors = colors |
| 143 | + ) |
| 144 | + |
| 145 | + pie_chart <- plot_ly( |
| 146 | + pie_info, |
| 147 | + type = "pie", |
| 148 | + labels = ~Group, |
| 149 | + values = ~values_total, |
| 150 | + marker = list(colors = colors), |
| 151 | + domain = list(x = c(0.9, 1), y = c(0, 1)), |
| 152 | + showlegend = FALSE |
| 153 | + ) |
| 154 | + |
| 155 | + # Should not produce warnings about NA attributes |
| 156 | + expect_no_warning({ |
| 157 | + combined_chart <- subplot(bar_chart, pie_chart, nrows = 1, widths = c(0.9, 0.1)) |
| 158 | + }) |
| 159 | + |
| 160 | + built <- plotly_build(combined_chart) |
| 161 | + |
| 162 | + # Layout should not have any attributes with NA names |
| 163 | + layout_names <- names(built$x$layout) |
| 164 | + expect_false(any(is.na(layout_names))) |
| 165 | + expect_false(any(grepl("^NA", layout_names))) |
| 166 | +}) |
| 167 | + |
| 168 | +test_that("Issue #2437: subplot warnings about discrete/non-discrete data", { |
| 169 | + bar_info <- data.frame( |
| 170 | + Group = rep(c("first", "second", "third"), 2), |
| 171 | + values_monthly = c(100, 200, 300, 400, 500, 600), |
| 172 | + month = factor(rep(c("April", "May"), each = 3)) |
| 173 | + ) |
| 174 | + pie_info <- aggregate(values_monthly ~ Group, data = bar_info, sum) |
| 175 | + names(pie_info)[2] <- "values_total" |
| 176 | + |
| 177 | + colors <- c("red", "blue", "yellow") |
| 178 | + |
| 179 | + bar_chart <- plot_ly( |
| 180 | + bar_info, |
| 181 | + type = "bar", |
| 182 | + x = ~month, |
| 183 | + y = ~values_monthly, |
| 184 | + color = ~Group, |
| 185 | + colors = colors |
| 186 | + ) |
| 187 | + |
| 188 | + pie_chart <- plot_ly( |
| 189 | + pie_info, |
| 190 | + type = "pie", |
| 191 | + labels = ~Group, |
| 192 | + values = ~values_total, |
| 193 | + marker = list(colors = colors), |
| 194 | + domain = list(x = c(0.9, 1), y = c(0, 1)), |
| 195 | + showlegend = FALSE |
| 196 | + ) |
| 197 | + |
| 198 | + # Specifically check that no warning about NA attributes is thrown |
| 199 | + warnings_caught <- character(0) |
| 200 | + withCallingHandlers({ |
| 201 | + combined_chart <- subplot(bar_chart, pie_chart, nrows = 1, widths = c(0.9, 0.1)) |
| 202 | + }, warning = function(w) { |
| 203 | + warnings_caught <<- c(warnings_caught, conditionMessage(w)) |
| 204 | + invokeRestart("muffleWarning") |
| 205 | + }) |
| 206 | + |
| 207 | + # Should not have warning about 'NA' attribute |
| 208 | + expect_false(any(grepl("NA", warnings_caught))) |
| 209 | +}) |
0 commit comments